main
hkr04 2025-03-09 17:24:46 +08:00
parent b62d328780
commit 76ac796464
4 changed files with 38 additions and 1 deletions

View File

@ -52,6 +52,12 @@ public:
* @return True if initialization was successful
*/
bool initialize(const std::string& client_name, const std::string& client_version);
/**
* @brief Ping request
* @return True if the server is alive
*/
bool ping();
/**
* @brief Set authentication token

View File

@ -60,6 +60,26 @@ bool client::initialize(const std::string& client_name, const std::string& clien
}
}
bool client::ping() {
// Create ping request
request req = request::create("ping", {});
try {
// Send the request
json result = send_jsonrpc(req);
// The receiver MUST respond promptly with an empty response
if (result.empty()) {
return true;
} else {
return false;
}
} catch (const std::exception& e) {
// Ping failed
return false;
}
}
void client::set_auth_token(const std::string& token) {
std::lock_guard<std::mutex> lock(mutex_);
auth_token_ = token;

View File

@ -287,6 +287,9 @@ json server::process_request(const request& req) {
// Special case for initialize
if (req.method == "initialize") {
return handle_initialize(req);
} else if (req.method == "ping") {
// The receiver MUST respond promptly with an empty response
return response::create_success(req.id, {}).to_json();
}
// Look for registered method handler

View File

@ -239,4 +239,12 @@ TEST_F(ClientTest, CancellationTest) {
EXPECT_EQ(result["content"][0]["type"], "text");
EXPECT_EQ(result["content"][0]["text"], "This call should be cancelled");
}
}
TEST_F(ClientTest, PingTest) {
// Initialize client
client->initialize("TestClient", mcp::MCP_VERSION);
// Send ping
EXPECT_TRUE(client->ping());
}