# mcp_cpp **Repository Path**: zhengtying/mcp_cpp ## Basic Information - **Project Name**: mcp_cpp - **Description**: 构建CPP c++ - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-09-22 - **Last Updated**: 2025-09-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # MCP C++ SDK A modern C++ implementation of the Model Context Protocol (MCP) SDK with high-performance features and comprehensive transport support. ## Features - **Modern C++20/23**: Utilizes latest C++ features including coroutines, concepts, and modules - **High Performance**: Zero-overhead abstractions and optimized JSON handling - **Comprehensive Transport Support**: stdio, HTTP, WebSocket, SSE - **Robust Authentication**: OAuth 2.1, Basic Auth, Bearer Tokens, API Keys - **Type Safety**: Strong typing with templates and compile-time validation - **Asynchronous Operations**: Native coroutine support for async/await patterns - **Memory Safety**: RAII principles and smart pointers throughout - **Cross-Platform**: Windows, Linux, macOS support ## Quick Start ### Prerequisites - C++20 compatible compiler (GCC 10+, Clang 10+, MSVC 19.28+) - CMake 3.20 or higher - Dependencies: fmt, nlohmann_json, OpenSSL, libcurl, zlib ### Building ```bash # Clone the repository git clone cd MCP_CPP # Create build directory mkdir build && cd build # Configure with CMake cmake .. # Build cmake --build . # Run tests ctest --output-on-failure # Install (optional) cmake --install . ``` ### Basic Usage #### Server Example ```cpp #include #include using namespace mcp; using namespace mcp::server; // Define a simple tool Result add_tool(const json::Value& args, Context<>& context) { auto a = args["a"].as_double(); auto b = args["b"].as_double(); auto result = a + b; context.info(std::format("Adding {} and {} = {}", a, b, result)); return json::Value::object({ {"result", result} }); } int main() { // Create server configuration ServerConfig config{ .name = "My MCP Server", .version = "1.0.0", .description = "Example MCP server", .host = "localhost", .port = 8080 }; // Create server auto server = std::make_shared(config); // Register tool server->tool("add", add_tool, ToolOptions{ .description = "Add two numbers", .input_schema = json::object_schema() .property("a", json::number_schema().description("First number")) .property("b", json::number_schema().description("Second number")) .required({"a", "b"}) }); // Start server std::cout << "Starting server...\n"; server->run(); return 0; } ``` #### Client Example ```cpp #include #include using namespace mcp; using namespace mcp::client; int main() { // Create client ClientConfig config{ .name = "My MCP Client", .version = "1.0.0", .timeout_seconds = 10 }; auto client = create_simple_client("stdio://", config); // Connect to server if (!client->connect()) { std::cerr << "Failed to connect\n"; return 1; } // List tools auto tools = client->list_tools(); if (tools) { std::cout << "Available tools:\n"; for (const auto& tool : *tools) { std::cout << " - " << tool.name << "\n"; } } // Call a tool auto result = client->call_tool("add", json::Value::object({ {"a", 10}, {"b", 20} })); if (result) { std::cout << "Result: " << result->serialize() << "\n"; } client->disconnect(); return 0; } ``` ## Architecture ### Core Components - **Types System**: Type-safe MCP protocol types and JSON-RPC bindings - **JSON Library**: High-performance JSON with copy-on-write semantics - **Error Handling**: Comprehensive error system with `std::expected` - **Server Framework**: FastMCP server with template-based registration - **Client Framework**: Async client with multiple transport support - **Authentication**: OAuth 2.1 and various authentication methods - **Transport Layer**: Pluggable transport system ### Transport Types - **Stdio Transport**: Standard input/output for local communication - **HTTP Transport**: RESTful HTTP endpoints - **WebSocket Transport**: Full-duplex WebSocket communication - **SSE Transport**: Server-Sent Events for real-time updates ### Authentication Methods - **OAuth 2.1**: Complete OAuth 2.1 implementation with PKCE - **Basic Authentication**: Username/password authentication - **Bearer Tokens**: JWT and opaque token support - **API Keys**: Custom API key authentication ## Examples The `examples/` directory contains several complete examples: - `fastmcp_server/`: Basic FastMCP server example - `simple_client/`: Simple client example - `echo_server/`: Echo server demonstrating basic functionality - `math_server/`: Mathematical operations server - `auth_server/`: Authentication server example ## Building Examples ```bash # Build all examples cmake --build . --target all # Build specific example cmake --build . --target fastmcp_server # Run example ./examples/fastmcp_server/fastmcp_server ``` ## API Reference ### Server Classes - `FastMCPServer`: Main server class - `ToolManager`: Tool registration and management - `ResourceManager`: Resource management - `PromptManager`: Prompt management ### Client Classes - `Client`: Async client interface - `SimpleClient`: Simplified synchronous client ### Transport Classes - `StdioTransport`: stdio transport - `HttpTransport`: HTTP transport - `WebSocketTransport`: WebSocket transport - `SseTransport`: SSE transport ### Authentication Classes - `Authenticator`: Base authenticator interface - `OAuth2Authenticator`: OAuth 2.1 authenticator - `BasicAuthenticator`: Basic authentication - `TokenManager`: Token management ## Configuration ### Server Configuration ```cpp ServerConfig config{ .name = "My Server", .version = "1.0.0", .description = "Server description", .host = "localhost", .port = 8080, .debug = false }; ``` ### Client Configuration ```cpp ClientConfig config{ .name = "My Client", .version = "1.0.0", .timeout_seconds = 30, .auto_reconnect = true, .max_reconnect_attempts = 10 }; ``` ## Testing The SDK includes comprehensive tests: ```bash # Run all tests ctest --output-on-failure # Run specific test ctest --output-on-failure -R "test_name" # Run tests with verbose output ctest --output-on-failure --verbose ``` ## Contributing 1. Fork the repository 2. Create a feature branch 3. Make your changes 4. Add tests for new functionality 5. Run the test suite 6. Submit a pull request ## License This project is licensed under the MIT License - see the LICENSE file for details. ## Support For support and questions: - Create an issue on GitHub - Check the documentation - Review the examples ## Performance The MCP C++ SDK is designed for high performance: - **Zero-copy JSON parsing** where possible - **Memory pooling** for frequently allocated objects - **Async I/O** with coroutine support - **Minimal allocations** in hot paths - **Cache-friendly** data structures Benchmark results coming soon! ## Changelog ### Version 1.0.0 - Initial release - Complete MCP protocol implementation - All major transport types - OAuth 2.1 authentication - Comprehensive test suite - Multiple examples