A modern C++20 client library for the Model Context Protocol (MCP).
- Full MCP Protocol Support - Tools, resources, prompts, completions, subscriptions
- Multiple Transports - Process (stdio), HTTP with SSE
- Sync & Async APIs - Blocking
McpClientand coroutine-basedAsyncMcpClient - Production Ready - Circuit breaker, URL validation, session management, retry logic
- Modern C++ - C++20 coroutines, concepts,
std::expected-style error handling - Comprehensive Tests - 495+ tests covering unit, integration, and real server scenarios
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Your Application β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββββ β
β β McpClient β β AsyncMcpClient β β
β β (synchronous) β β (coroutines) β β
β ββββββββββββββββ¬βββββββββββββββ ββββββββββββββββ¬βββββββββββββββ β
β β β β
β ββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββ΄βββββββββββββββ β
β β Transport Layer β β
β β βββββββββββββββββββββββ βββββββββββββββββββββββ β β
β β β ProcessTransport β β HttpTransport β β β
β β β (stdio to subprocess) β (HTTP + SSE) β β β
β β βββββββββββββββββββββββ βββββββββββββββββββββββ β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Resilience & Security β β
β β βββββββββββββββββ βββββββββββββββββ βββββββββββββββββββββ β β
β β βCircuit Breakerβ β URL Validator β β Session Manager β β β
β β βββββββββββββββββ βββββββββββββββββ βββββββββββββββββββββ β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MCP Server β
β (filesystem, GitHub, Slack, custom AI agents, etc.) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- C++20 compiler (GCC 11+, Clang 14+, MSVC 2022+)
- CMake 3.20+
git clone https://github.com/your-org/mcpp.git
cd mcpp
mkdir build && cd build
cmake ..
make -j$(nproc)#include <mcpp/transport/process_transport.hpp>
#include <mcpp/client/mcp_client.hpp>
using namespace mcpp;
int main() {
// Connect to an MCP server via stdio
ProcessTransportConfig config;
config.command = "npx";
config.args = {"-y", "@modelcontextprotocol/server-filesystem", "/tmp"};
auto transport = std::make_unique<ProcessTransport>(config);
transport->start();
McpClient client(std::move(transport));
client.connect();
client.initialize();
// List available tools
auto tools = client.list_tools();
if (tools) {
for (const auto& tool : tools->tools) {
std::cout << "Tool: " << tool.name << "\n";
}
}
// Call a tool
auto result = client.call_tool("read_file", {{"path", "/tmp/test.txt"}});
if (result) {
for (const auto& content : result->content) {
if (content.type == "text") {
std::cout << content.text.value_or("") << "\n";
}
}
}
client.disconnect();
return 0;
}ββββββββββββββ ββββββββββββββ
β Client β β Server β
βββββββ¬βββββββ βββββββ¬βββββββ
β β
β ββββββββββββ initialize βββββββββββββββββββΊ β
β ββββββββββββ InitializeResult ββββββββββββ β
β β
β ββββββββββββ notifications/initialized ββββΊ β
β β
β ββββββββββββ tools/list βββββββββββββββββββΊ β
β ββββββββββββ ListToolsResult βββββββββββββ β
β β
β ββββββββββββ tools/call βββββββββββββββββββΊ β
β ββββββββββββ CallToolResult ββββββββββββββ β
β β
β ββββββββββββ notifications/progress ββββββ β (optional)
β β
β ββββββββββββ ping βββββββββββββββββββββββββΊ β
β ββββββββββββ pong ββββββββββββββββββββββββ β
β β
βΌ βΌ
See the examples/ directory for complete working examples:
| Example | Description |
|---|---|
| 01_basic_sync | Synchronous client basics |
| 02_basic_async | Async/coroutine client |
| 03_filesystem_server | File operations |
| 04_http_transport | Remote HTTP servers |
| 05_custom_handlers | Elicitation, sampling, roots |
| 06_circuit_breaker | Resilience patterns |
| 07_arcade_toolkit | Arcade AI integration |
| Feature | Status | Description |
|---|---|---|
| Initialize/Shutdown | β | Session lifecycle |
| Tools (list, call) | β | Execute server-provided tools |
| Resources (list, read, subscribe) | β | Access server resources |
| Resource Templates | β | URI template discovery |
| Prompts (list, get) | β | Prompt templates |
| Completions | β | Autocompletion API |
| Ping | β | Health checks |
| Cancellation | β | Cancel in-progress operations |
| Progress Notifications | β | Track long-running ops |
| Logging Control | β | Server log level |
| Elicitation Handler | β | User input requests |
| Sampling Handler | β | LLM completion requests |
| Roots Handler | β | Filesystem root exposure |
ββββββββββββββββ stdin ββββββββββββββββ
β Client βββββββββββββββββΊβ Server β
β ββββββββββββββββββ (process) β
ββββββββββββββββ stdout ββββββββββββββββ
ProcessTransportConfig config;
config.command = "python";
config.args = {"-m", "my_mcp_server"};
config.use_content_length_framing = false; // or true for framed mode
config.working_directory = "/path/to/server";
config.environment = {{"API_KEY", "secret"}};ββββββββββββββββ POST /message ββββββββββββββββ
β Client ββββββββββββββββββββΊβ Server β
β βββββββββββββββββββββ (HTTP) β
ββββββββββββββββ SSE /sse ββββββββββββββββ
HttpClientConfig config;
config.base_url = "https://api.example.com/mcp/";
config.headers = {{"Authorization", "Bearer token"}};
config.timeout = std::chrono::seconds(30);Uses tl::expected for explicit error handling without exceptions:
auto result = client.call_tool("my_tool", args);
if (!result) {
switch (result.error().code) {
case ClientErrorCode::NotConnected:
// Handle connection error
break;
case ClientErrorCode::TransportError:
// Handle transport failure
break;
case ClientErrorCode::ProtocolError:
// Handle protocol error
break;
}
return;
}
// Use result.value() CLOSED βββββββΊ OPEN βββββββΊ HALF-OPEN
β² β β
β β success β
ββββββββββββββ΄ββββββββββββββββ
failure: back to OPEN
McpClientConfig config;
config.enable_circuit_breaker = true;
config.circuit_breaker.failure_threshold = 5;
config.circuit_breaker.recovery_timeout = std::chrono::seconds(30);
McpClient client(std::move(transport), config);
if (client.is_circuit_open()) {
// Fast-fail, don't wait for timeout
}# List tools
./mcpp-cli -c 'npx' -a '-y' -a '@modelcontextprotocol/server-filesystem' -a '/tmp' --list-tools
# Call a tool
./mcpp-cli -c 'python' -a 'server.py' --call-tool read_file --tool-args '{"path":"/tmp/test.txt"}'
# Interactive mode
./mcpp-cli -c 'node' -a 'server.js' --interactive
# JSON output
./mcpp-cli -c 'python' -a 'server.py' --list-tools --jsonmcpp/
βββ include/mcpp/
β βββ protocol/ # MCP types and JSON serialization
β βββ transport/ # Transport layer (Process, HTTP/SSE)
β βββ client/ # Synchronous McpClient
β βββ async/ # Asynchronous AsyncMcpClient
β βββ resilience/ # Circuit breaker
β βββ security/ # URL validation
β βββ log/ # Logging interfaces
βββ src/ # Implementation files
βββ tests/ # Test suite (495+ tests)
βββ tools/mcpp-cli/ # Command-line interface
βββ examples/ # Usage examples
All fetched automatically via CMake FetchContent:
- nlohmann/json - JSON parsing
- ASIO - Async I/O and coroutines
- tl::expected - Error handling
- ada-url - URL parsing
- spdlog - Logging
- cpr - HTTP client
- Catch2 - Testing
- cxxopts - CLI parsing
cd build
ctest --output-on-failure
# Or directly
./mcpp_tests # All tests
./mcpp_tests "[mcp_client]" # Client tests
./mcpp_tests "[real-server]" # Integration testsMIT License - see LICENSE file.