add ping
parent
b62d328780
commit
76ac796464
|
@ -53,6 +53,12 @@ public:
|
|||
*/
|
||||
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
|
||||
* @param token The authentication token
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -240,3 +240,11 @@ 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());
|
||||
}
|
Loading…
Reference in New Issue