/** * @file mcp_protocol.h * @brief Core definitions for the Model Context Protocol (MCP) framework * * This file contains the core structures, enums, and definitions for the MCP protocol. * MCP connects AI models with data sources and tools in a standardized way. */ #ifndef MCP_PROTOCOL_H #define MCP_PROTOCOL_H #include #include #include #include #include #include // Include the JSON library for parsing and generating JSON #include "json.hpp" namespace mcp { // Use the nlohmann json library using json = nlohmann::ordered_json; // MCP version constexpr const char* MCP_VERSION = "1.0.0"; // MCP error codes enum class error_code { none = 0, invalid_request = 400, unauthorized = 401, forbidden = 403, not_found = 404, method_not_allowed = 405, conflict = 409, payload_too_large = 413, unsupported_media_type = 415, internal_server_error = 500, service_unavailable = 503 }; // MCP exception class class mcp_exception : public std::runtime_error { public: mcp_exception(error_code code, const std::string& message) : std::runtime_error(message), code_(code) {} error_code code() const { return code_; } private: error_code code_; }; // Resource Types enum class resource_type { file, // File resource directory, // Directory/folder resource database, // Database resource api, // API endpoints document, // Document resource image, // Image resource custom // Custom resource type }; // Content Types struct content_type { static constexpr const char* json = "application/json"; static constexpr const char* text = "text/plain"; static constexpr const char* octet_stream = "application/octet-stream"; static constexpr const char* form = "application/x-www-form-urlencoded"; static constexpr const char* multipart = "multipart/form-data"; static constexpr const char* image_jpeg = "image/jpeg"; static constexpr const char* image_png = "image/png"; }; // HTTP Methods enum class http_method { get, post, put, delete_, patch, options, head }; // Convert HTTP method to string inline std::string http_method_to_string(http_method method) { switch (method) { case http_method::get: return "GET"; case http_method::post: return "POST"; case http_method::put: return "PUT"; case http_method::delete_: return "DELETE"; case http_method::patch: return "PATCH"; case http_method::options: return "OPTIONS"; case http_method::head: return "HEAD"; default: return "UNKNOWN"; } } // Convert string to HTTP method inline http_method string_to_http_method(const std::string& method_str) { if (method_str == "GET") return http_method::get; if (method_str == "POST") return http_method::post; if (method_str == "PUT") return http_method::put; if (method_str == "DELETE") return http_method::delete_; if (method_str == "PATCH") return http_method::patch; if (method_str == "OPTIONS") return http_method::options; if (method_str == "HEAD") return http_method::head; throw mcp_exception(error_code::method_not_allowed, "Unsupported HTTP method: " + method_str); } // MCP Request structure struct request { http_method method; std::string path; std::map headers; std::map query_params; std::string body; // Parse JSON body json json_body() const { if (body.empty()) { return json::object(); } try { return json::parse(body); } catch (const json::exception& e) { throw mcp_exception(error_code::invalid_request, "Invalid JSON in request body: " + std::string(e.what())); } } }; // MCP Response structure struct response { int status_code = 200; std::map headers; std::string body; // Set JSON body void set_json_body(const json& data) { body = data.dump(); headers["Content-Type"] = content_type::json; } // Set error response void set_error(error_code code, const std::string& message) { status_code = static_cast(code); json error = { {"error", { {"code", status_code}, {"message", message} }} }; set_json_body(error); } }; // Base class for MCP resources class resource { public: virtual ~resource() = default; // Get resource type virtual resource_type type() const = 0; // Get resource metadata virtual json metadata() const = 0; // Handle request virtual response handle_request(const request& req) = 0; // Get resource schema virtual json schema() const { return json::object(); // Default empty schema } }; // MCP Tool definition struct tool { std::string name; std::string description; json parameters_schema; // Convert to JSON for API documentation json to_json() const { return { {"name", name}, {"description", description}, {"parameters", parameters_schema} }; } }; // Tool handler function type using tool_handler = std::function; } // namespace mcp #endif // MCP_PROTOCOL_H