- Notifications
You must be signed in to change notification settings - Fork0
MCP C++ SDK - Model Context Protocol implementation in CPP with enterprise-grade security, visibility and connectivity.
License
GopherSecurity/gopher-mcp
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
MCP C++ / MCP CPP - Enterprise-grade C++ implementation of the Model Context Protocol (MCP) for AI model integration. The most comprehensive MCP C++ SDK with production-ready features.
⭐Please star if you find this useful!
This MCP C++ SDK provides a complete implementation of the Model Context Protocol in modern C++, designed for high-performance AI applications and enterprise deployments.
- Production-Ready: Battle-tested MCP C++ code with enterprise design patterns
- High Performance: Zero-copy buffers, lock-free operations, and optimized for MCP protocol
- Cross-Language: C API for FFI bindings to Python, TypeScript, Go, Rust, and more
- Full MCP Spec: Complete Model Context Protocol implementation in C++
- Modern C++: Supports C++14/17/20 with best practices
Our MCP CPP SDK follows a layered architecture designed for high performance, scalability, and extensibility:
┌─────────────────────────────────────────────────────────────┐│ Application Layer ││ ┌──────────────────────────────────────────────────────┐ ││ │ MCP Server / Client / Custom Applications │ ││ └──────────────────────────────────────────────────────┘ │├─────────────────────────────────────────────────────────────┤│ Cross-Language Binding Layer ││ ┌──────────────────────────────────────────────────────┐ ││ │ Python │ TypeScript │ Go │ Rust │ Java │ C# │ Ruby │ ││ └──────────────────────────────────────────────────────┘ │├─────────────────────────────────────────────────────────────┤│ C API (FFI Layer) ││ ┌──────────────────────────────────────────────────────┐ ││ │ libgopher_mcp_c: Opaque Handles │ Memory Safety │ ││ │ RAII Guards │ Type Safety │ Error Handling │ ││ └──────────────────────────────────────────────────────┘ │├─────────────────────────────────────────────────────────────┤│ Protocol Layer ││ ┌──────────────────────────────────────────────────────┐ ││ │ MCP JSON-RPC Protocol Implementation │ ││ │ Request/Response/Notification Handling │ ││ └──────────────────────────────────────────────────────┘ │├─────────────────────────────────────────────────────────────┤│ Filter Chain Layer ││ ┌──────────────────────────────────────────────────────┐ ││ │ HTTP Codec │ SSE Codec │ Routing │ Rate Limiting │ ││ │ Circuit Breaker │ Metrics │ Backpressure │ Auth │ ││ └──────────────────────────────────────────────────────┘ │├─────────────────────────────────────────────────────────────┤│ Transport Layer ││ ┌──────────────────────────────────────────────────────┐ ││ │ Stdio │ HTTP(s)+SSE │ WebSocket │ TCP │ Redis │ P2P │ ││ └──────────────────────────────────────────────────────┘ │├─────────────────────────────────────────────────────────────┤│ Network Layer ││ ┌──────────────────────────────────────────────────────┐ ││ │ Connection Management │ Listener │ Socket Interface │ ││ └──────────────────────────────────────────────────────┘ │├─────────────────────────────────────────────────────────────┤│ Event Loop & Dispatcher ││ ┌──────────────────────────────────────────────────────┐ ││ │ Libevent Integration │ Timer Management │ I/O Events │ ││ └──────────────────────────────────────────────────────┘ │└─────────────────────────────────────────────────────────────┘The MCP C++ SDK is designed with cross-language support as a first-class feature:
- Application Layer: MCP server/client implementations using your preferred languages
- Language Bindings: Native bindings for each language that wrap the C API
- C API Layer: Stable FFI interface (
libgopher_mcp_c) that bridges C++ and other languages
The C API provides a complete FFI-safe interface:
- Opaque handles: Hide C++ implementation details
- Memory safety: RAII guards and automatic resource management
- Type safety: Strong typing with comprehensive error handling
- Thread safety: All operations are thread-safe through the dispatcher model
- Zero-copy buffers: Efficient data sharing where possible
Native bindings are available for all mainstream programming languages:
- Python: ctypes/cffi integration with async support
- TypeScript/Node.js: N-API bindings for high performance
- Go: CGO integration with goroutine-safe wrappers
- Rust: Safe FFI wrappers with ownership guarantees
- Java: JNI bindings with automatic resource management
- C#/.NET: P/Invoke with async/await support
- Ruby: Native extension with GC integration
- Swift: Direct C interop for iOS/macOS
- Kotlin: JNI or Kotlin/Native for Android
Each binding maintains language-specific idioms while providing full access to MCP functionality.
All I/O operations and state transitions occur within dispatcher threads, eliminating the need for complex synchronization:
- Each worker thread has its own dispatcher
- Callbacks are invoked in dispatcher thread context
- Thread-local storage for connection state
Modular processing pipeline for extensible functionality:
- Stateless filters for HTTP/2 concurrent stream support
- In-place buffer modification for zero-copy performance
- Dynamic filter composition based on transport and requirements
Following enterprise design patterns:
- Connection pooling with O(1) insertion/removal
- Circuit breaker for failure isolation
- Watermark-based flow control
- Graceful shutdown with timeout
Base framework providing:
- Worker thread management
- Filter chain construction
- Connection pooling
- Metrics and observability
- Graceful lifecycle management
Production-ready server with:
- Multi-transport support (stdio, HTTP+SSE, WebSocket)
- Session management with timeout
- Resource subscriptions
- Tool registration and execution
- Prompt management
Enterprise client featuring:
- Transport negotiation
- Connection pooling
- Circuit breaker pattern
- Retry with exponential backoff
- Future-based async API
Core networking infrastructure:
- Connection: Manages socket lifecycle and filter chain
- Listener: Accepts incoming connections
- Filter: Modular processing units
- Socket Interface: Platform abstraction
Processing pipeline components:
- HTTP Codec: HTTP/1.1 and HTTP/2 protocol handling
- SSE Codec: Server-Sent Events processing
- Routing: Request routing based on method and path
- Rate Limiting: Token bucket rate control
- Circuit Breaker: Failure isolation
- Metrics: Performance monitoring
- Backpressure: Flow control
Multiple transport implementations:
- Stdio: Standard I/O pipes
- TCP: Raw TCP sockets
- SSL/TLS: Secure sockets
- HTTP+SSE: HTTP with Server-Sent Events
- HTTPS+SSE: Secure HTTP+SSE
- C++14 or later compiler (MCP CPP supports C++14/17/20)
- GCC 8.0+ (Linux)
- CMake 3.10+ for building MCP C++ SDK
- libevent 2.1+ (event loop for MCP protocol)
- OpenSSL 1.1+ (for MCP SSL/TLS transport)
- nghttp2 (optional, for MCP HTTP/2 support)
# Quick build and install (default: /usr/local, auto-prompts for sudo)makemake install# Will prompt for password if needed# User-local installation (no sudo required)make build CMAKE_INSTALL_PREFIX=~/.localmake install# Custom installationcmake -B build -DCMAKE_INSTALL_PREFIX=/opt/gopher-mcpmake -C buildmake install# Will use sudo if needed# Uninstall (auto-detects if sudo is needed)make uninstall
# Build only C++ libraries (no C API)cmake -B build -DBUILD_C_API=OFF# Build only static librariescmake -B build -DBUILD_SHARED_LIBS=OFF# Build for productionmake releasesudo make install
maketest# Run tests with minimal outputmake test-verbose# Run tests with detailed outputmake test-parallel# Run tests in parallel
The C API library (libgopher_mcp_c) is built by default and provides a stable ABI for FFI bindings:
# The C API is included in default installmakemake install# Auto-prompts for sudo if needed# Headers: /usr/local/include/gopher-mcp/mcp/c_api/# Library: /usr/local/lib/libgopher_mcp_c.{so,dylib}
// MCP C++ Server Example#include"mcp/server/mcp_server.h"intmain() { mcp::server::McpServerConfig config; config.server_name ="my-mcp-server"; config.worker_threads =4;auto server =mcp::server::createMcpServer(config);// Register a tool mcp::Tool tool; tool.name ="calculator"; tool.description ="Performs calculations"; server->registerTool(tool, [](const std::string& name,const mcp::optional<mcp::Metadata>& args, mcp::server::SessionContext& session) {// Tool implementation mcp::CallToolResult result;// ... perform calculation ...return result; });// Start listening server->listen("tcp://0.0.0.0:8080"); server->run();return0;}
// MCP C++ Client Example#include"mcp/client/mcp_client.h"intmain() { mcp::client::McpClientConfig config; config.client_name ="my-mcp-client"; config.max_retries =3;auto client =mcp::client::createMcpClient(config);// Connect to server client->connect("tcp://localhost:8080");// Initialize protocolauto init_future = client->initializeProtocol();auto init_result = init_future.get();// Call a tool mcp::Metadata args; args["expression"] ="2 + 2";auto tool_future = client->callTool("calculator", args);auto tool_result = tool_future.get();return0;}
- MCP Protocol in C++ - Model Context Protocol implementation details
- Filter Chain - Processing pipeline architecture
- Transport Layer - Transport implementations
- Network Layer - Connection management and socket abstraction
- Event Loop Design - Event-driven architecture and dispatcher design
- Filter Usage Guide - Comprehensive guide for using and creating filters
- CTAD Alternatives - Class Template Argument Deduction alternatives for C++14
- MCP Serialization Coverage - JSON serialization implementation details
MCP C++,MCP CPP,Model Context Protocol C++,MCP SDK,C++ MCP,CPP MCP,Model Context Protocol CPP,MCP implementation,AI model integration C++,LLM integration C++,MCP server C++,MCP client C++,Model Context Protocol SDK,C++ AI SDK,Enterprise MCP,Production MCP C++
Contributions to this MCP C++ SDK are welcome! Please read our contributing guidelines before submitting pull requests.
This MCP C++ project is licensed under the Apache License - see the LICENSE file for details.
About
MCP C++ SDK - Model Context Protocol implementation in CPP with enterprise-grade security, visibility and connectivity.
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Contributors4
Uh oh!
There was an error while loading.Please reload this page.