| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | /** |
| 4 | * MCP Ping Method |
| 5 | * |
| 6 | * The ping method is a core part of the Model Context Protocol (MCP), |
| 7 | * allowing connection health checks between client and server. |
| 8 | * |
| 9 | * Standard method in the MCP specification: |
| 10 | * |
| 11 | * 1. ping - Simple connection health check |
| 12 | * - Takes no parameters (empty params object) |
| 13 | * - The receiver must respond promptly with an empty result object |
| 14 | * - Either client or server can initiate a ping |
| 15 | * - If no response is received within a reasonable timeout, the connection may be considered stale |
| 16 | * |
| 17 | * According to the MCP specification: |
| 18 | * - The ping method is mandatory for all MCP implementations |
| 19 | * - It serves as a basic mechanism to verify the connection is still active |
| 20 | * - Implementations should handle ping requests promptly to ensure accurate health checks |
| 21 | * |
| 22 | * This implementation provides a simple handler for ping requests that responds with an |
| 23 | * empty result object, as required by the specification. |
| 24 | */ |
| 25 | |
| 26 | #include "mcp-ping.h" |
| 27 | |
| 28 | /** |
| 29 | * Handle a ping request from a client or server |
| 30 | * |
| 31 | * @param mcpc The MCP client context |
| 32 | * @param params The JSON params object (should be empty for ping) |
| 33 | * @param id The request ID |
| 34 | * @return MCP_RETURN_CODE - MCP_RC_OK if successful |
| 35 | */ |
| 36 | MCP_RETURN_CODE mcp_method_ping(MCP_CLIENT *mcpc, struct json_object *params __maybe_unused, MCP_REQUEST_ID id) { |
| 37 | if (!mcpc) { |
| 38 | return MCP_RC_ERROR; |
| 39 | } |
| 40 | |
| 41 | // Initialize success response with empty result object |
| 42 | mcp_init_success_result(mcpc, id); |
| 43 | buffer_json_finalize(mcpc->result); |
| 44 | |
| 45 | // Log the ping for debugging |
| 46 | netdata_log_debug(D_MCP, "Received ping request (ID: %zu), responded", id); |
| 47 | |
| 48 | return MCP_RC_OK; |
| 49 | } |