/** * @file mcp_client.h * @brief MCP Client implementation * * This file implements the client-side functionality for the Model Context Protocol. * Follows the 2024-11-05 protocol specification. */ #ifndef MCP_CLIENT_H #define MCP_CLIENT_H #include "mcp_message.h" #include "mcp_tool.h" // Include the HTTP library #include "httplib.h" #include #include #include #include #include #include #include namespace mcp { /** * @class client * @brief Client for connecting to MCP servers * * The client class provides functionality to connect to MCP servers, * initialize the connection, and send/receive JSON-RPC messages. */ class client { public: /** * @brief Constructor * @param host The server host (e.g., "localhost", "example.com") * @param port The server port */ client(const std::string& host, int port = 8080, const json& capabilities = json::object(), const std::string& sse_endpoint = "/sse"); /** * @brief Constructor * @param base_url The base URL of the server (e.g., "http://localhost:8080") * @param capabilities The capabilities of the client */ client(const std::string& base_url, const json& capabilities = json::object(), const std::string& sse_endpoint = "/sse"); /** * @brief Destructor */ ~client(); /** * @brief Initialize the connection with the server * @param client_name The name of the client * @param client_version The version of the client * @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 * @param token The authentication token */ void set_auth_token(const std::string& token); /** * @brief Set a request header that will be sent with all requests * @param key Header name * @param value Header value */ void set_header(const std::string& key, const std::string& value); /** * @brief Set timeout for requests * @param timeout_seconds Timeout in seconds */ void set_timeout(int timeout_seconds); /** * @brief Set client capabilities * @param capabilities The capabilities of the client */ void set_capabilities(const json& capabilities); /** * @brief Send a request and wait for a response * @param method The method to call * @param params The parameters to pass * @return The response * @throws mcp_exception on error */ response send_request(const std::string& method, const json& params = json::object()); /** * @brief Send a notification (no response expected) * @param method The method to call * @param params The parameters to pass * @throws mcp_exception on error */ void send_notification(const std::string& method, const json& params = json::object()); /** * @brief Get server capabilities * @return The server capabilities * @throws mcp_exception on error */ json get_server_capabilities(); /** * @brief Call a tool * @param tool_name The name of the tool to call * @param arguments The arguments to pass to the tool * @return The result of the tool call * @throws mcp_exception on error */ json call_tool(const std::string& tool_name, const json& arguments = json::object()); /** * @brief Get available tools * @return List of available tools * @throws mcp_exception on error */ std::vector get_tools(); /** * @brief Get client capabilities * @return The client capabilities */ json get_capabilities(); /** * @brief List available resources * @param cursor Optional cursor for pagination * @return List of resources */ json list_resources(const std::string& cursor = ""); /** * @brief Read a resource * @param resource_uri The URI of the resource * @return The resource content */ json read_resource(const std::string& resource_uri); /** * @brief Subscribe to resource changes * @param resource_uri The URI of the resource * @return Subscription result */ json subscribe_to_resource(const std::string& resource_uri); /** * @brief List resource templates * @return List of resource templates */ json list_resource_templates(); private: std::string base_url_; std::string host_; int port_; std::string sse_endpoint_; std::string msg_endpoint_; std::string auth_token_; int timeout_seconds_ = 30; json capabilities_; std::map default_headers_; json server_capabilities_; // HTTP client std::unique_ptr http_client_; // Mutex for thread safety mutable std::mutex mutex_; // SSE connection std::unique_ptr sse_thread_; // SSE连接状态 std::atomic sse_running_{false}; // Initialize the client void init_client(const std::string& host, int port); void init_client(const std::string& base_url); void open_sse_connection(); void close_sse_connection(); bool parse_sse_data(const char* data, size_t length); // Send a JSON-RPC request and get the response json send_jsonrpc(const request& req); }; } // namespace mcp #endif // MCP_CLIENT_H