cpp-mcp/include/mcp_client.h

179 lines
4.9 KiB
C
Raw Normal View History

2025-03-08 01:50:39 +08:00
/**
* @file mcp_client.h
* @brief MCP Client implementation using cpp-httplib
*
* This file implements the client-side functionality for the Model Context Protocol.
*/
#ifndef MCP_CLIENT_H
#define MCP_CLIENT_H
#include "mcp_protocol.h"
// Include the HTTP library
#include "httplib.h"
#include <string>
#include <map>
#include <vector>
#include <memory>
#include <mutex>
#include <functional>
namespace mcp {
/**
* @class client
* @brief Client for connecting to MCP servers
*
* The client class provides functionality to connect to MCP servers,
* discover available resources and tools, and make requests to them.
*/
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);
/**
* @brief Destructor
*/
~client();
/**
* @brief Set authentication credentials
* @param username The username
* @param password The password
*/
void set_auth(const std::string& username, const std::string& password);
/**
* @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 Get server information
* @return Server information as a JSON object
* @throws mcp_exception on error
*/
json get_server_info();
/**
* @brief Get available tools
* @return List of available tools as a JSON array
* @throws mcp_exception on error
*/
json get_tools();
/**
* @brief Call a tool
* @param tool_name The name of the tool to call
* @param parameters The parameters to pass to the tool
* @return The result of the tool call as a JSON object
* @throws mcp_exception on error
*/
json call_tool(const std::string& tool_name, const json& parameters = json::object());
/**
* @brief Make a GET request
* @param path The path to request
* @param query_params Query parameters
* @return The response
* @throws mcp_exception on error
*/
response get(const std::string& path,
const std::map<std::string, std::string>& query_params = {});
/**
* @brief Make a POST request
* @param path The path to request
* @param body The request body
* @param content_type The content type
* @return The response
* @throws mcp_exception on error
*/
response post(const std::string& path,
const std::string& body = "",
const std::string& content_type = content_type::json);
/**
* @brief Make a POST request with JSON body
* @param path The path to request
* @param json_body The JSON body
* @return The response
* @throws mcp_exception on error
*/
response post_json(const std::string& path, const json& json_body);
/**
* @brief Make a PUT request
* @param path The path to request
* @param body The request body
* @param content_type The content type
* @return The response
* @throws mcp_exception on error
*/
response put(const std::string& path,
const std::string& body = "",
const std::string& content_type = content_type::json);
/**
* @brief Make a DELETE request
* @param path The path to request
* @return The response
* @throws mcp_exception on error
*/
response delete_request(const std::string& path);
/**
* @brief Make a generic request
* @param method The HTTP method
* @param path The path
* @param body The request body (for POST, PUT, etc.)
* @param headers Additional headers
* @param query_params Query parameters
* @return The response
* @throws mcp_exception on error
*/
response make_request(http_method method,
const std::string& path,
const std::string& body = "",
const std::map<std::string, std::string>& headers = {},
const std::map<std::string, std::string>& query_params = {});
private:
std::string host_;
int port_;
std::string auth_header_;
int timeout_seconds_ = 30;
std::map<std::string, std::string> default_headers_;
// HTTP client
std::unique_ptr<httplib::Client> http_client_;
// Mutex for thread safety
mutable std::mutex mutex_;
// Initialize the client
void init_client();
// Process response
response process_response(const httplib::Result& result);
};
} // namespace mcp
#endif // MCP_CLIENT_H