mcp_server_cpp

xpc123/mcp_server_cpp

3.3

If you are the rightful owner of mcp_server_cpp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

The MCP C++ Minimal Core Library is a lightweight, embeddable framework for creating JSON-RPC 2.0 servers with essential features like a request loop, tool registry, and thread pool.

Tools
1
Resources
0
Prompts
0

MCP Server C++

A robust C++ implementation of the Model Context Protocol (MCP) server with StreamableHTTP transport. This server provides a comprehensive foundation for building MCP-compatible services with excellent performance and reliability.

๐ŸŽฏ Key Features

  • โœ… Full MCP Protocol 2025-06-18 compliance with complete handshake support
  • ๐Ÿš€ StreamableHTTP Transport for real-time bidirectional communication
  • ๐Ÿ”ง 6 Built-in Sample Tools demonstrating diverse functionality patterns
  • โšก Thread-pool Architecture for high-performance concurrent execution
  • ๐Ÿ”„ JSON-RPC 2.0 with proper MCP protocol layering
  • ๐Ÿ Official MCP SDK Integration - tested with Python SDK client
  • ๐Ÿงช Comprehensive Test Suite with both demo and interactive modes
  • ๐Ÿ“ฆ Zero External Dependencies - all libraries bundled

Current Version: 0.1.0
Protocol Version: 2025-06-18
Build Status: โœ… Production Ready

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  MCP Clients (Python SDK, Claude Desktop, etc.)           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  ๐Ÿ“ก StreamableHTTP Transport (JSON-RPC 2.0)                โ”‚
โ”‚    โ”œโ”€ POST / (main MCP endpoint)                           โ”‚
โ”‚    โ”œโ”€ POST /rpc (legacy compatibility)                     โ”‚
โ”‚    โ””โ”€ Session management & streaming responses             โ”‚  
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  ๐ŸŽฏ MCP Protocol Layer (2025-06-18)                        โ”‚
โ”‚    โ”œโ”€ initialize/initialized handshake                     โ”‚
โ”‚    โ”œโ”€ tools/list & tools/call methods                      โ”‚
โ”‚    โ””โ”€ Proper error handling & validation                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  ๐Ÿ”ง mcp::McpServer (Core Engine)                           โ”‚
โ”‚    โ”œโ”€ Tool registry with dynamic discovery                 โ”‚
โ”‚    โ”œโ”€ Thread pool for concurrent execution                 โ”‚
โ”‚    โ””โ”€ Extensible plugin architecture                       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  ๐Ÿ› ๏ธ  Built-in Tools (6 Sample Implementations)            โ”‚
โ”‚    โ”œโ”€ time, echo, upper, sum (utility tools)              โ”‚
โ”‚    โ”œโ”€ getWeather (mock API simulation)                     โ”‚
โ”‚    โ””โ”€ slow (progress simulation)                           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Core Components:

  • Server Core: mcp::McpServer with tool registry and thread pool management
  • Transport Layer: StreamableHTTP on port 8080 (configurable) with session support
  • Protocol Implementation: Full MCP 2025-06-18 over JSON-RPC 2.0 messaging
  • Tool System: Extensible framework with 6 comprehensive sample tools
  • Client Integration: Official MCP SDK Python client with demo and interactive modes

๐Ÿ“‹ Requirements

Build Environment

  • CMake โ‰ฅ 3.16 for modern build configuration
  • C++14+ compiler (GCC 9.3+, Clang 10+, or MSVC 2019+)
  • Make or Ninja build system
  • Git for repository cloning

Bundled Dependencies (Zero Installation Required)

  • โœ… nlohmann/json (v3.11+) - bundled in include/nlohmann/
  • โœ… cpp-httplib (v0.14+) - bundled in include/httplib.h
  • โœ… Boost detection and integration (system boost preferred)

Runtime Environment

  • Platform: Linux, macOS, Windows (WSL supported)
  • Shell: csh/tcsh for test scripts (bash compatibility available)
  • Network: Available port for HTTP server (default: 8080)

Python Client Dependencies

  • Python 3.8+ with pip package manager
  • MCP SDK: Official Model Context Protocol Python library
  • Virtual Environment: Automatically managed by test scripts

๐Ÿš€ Quick Start

1. Automated Build and Test (Recommended)

# Clone the repository
git clone https://github.com/xpc123/mcp_server_cpp.git
cd mcp_server_cpp

# Run comprehensive test suite (builds everything automatically)
./examples/scripts/test_mcp_streamable_http_server.csh

What this script does:

  • ๐Ÿ—๏ธ Builds the project with optimal CMake configuration
  • ๐Ÿš€ Starts MCP server on port 8080 with full logging
  • ๐Ÿงช Tests Demo Mode: Automatically tests all 6 tools with realistic parameters
  • ๐ŸŽฎ Tests Interactive Mode: Simulates user interaction with comprehensive commands
  • ๐Ÿ“Š Reports Results: Beautiful formatted output with success/failure indicators
  • ๐Ÿ›‘ Cleans Up: Properly stops server and manages all processes

Expected Output:

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘                        Test Summary                               โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Demo Mode:        โœ… PASSED
Interactive Mode: โœ… PASSED  
Overall Result:   โœ… PASSED

2. Manual Build

mkdir -p build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DMCP_WITH_HTTP=ON -DMCP_BUILD_EXAMPLE=ON
cmake --build . -j4

Build Artifacts:

  • build/libmcp.a - Static library
  • build/mcp_streamable_http_server - StreamableHTTP server

CMake Options:

  • -DMCP_WITH_HTTP=ON/OFF - Enable StreamableHTTP transport (default: ON)
  • -DMCP_BUILD_EXAMPLE=ON/OFF - Build example servers (default: ON)
  • -DMCP_ENABLE_ASAN=ON - Enable AddressSanitizer for debugging

๐Ÿ› ๏ธ Available Tools

The server includes 6 comprehensive sample tools demonstrating different MCP patterns:

ToolDescriptionParametersUse Case
timeCurrent UTC timestamp (ISO8601)NoneSystem info, logging
echoEcho back provided texttext (string)Input validation, debugging
upperConvert text to uppercasetext (string)Text processing
sumSum array of numbersnumbers (array)Mathematical operations
getWeatherMock weather datacity (string)API simulation, external services
slowLong-running task simulationNoneProgress tracking, async operations

Tool Examples

# Time tool - no parameters
{"jsonrpc":"2.0","method":"tools/call","id":"1","params":{"name":"time","arguments":{}}}
โ†’ {"utc":"2025-09-24T01:04:20Z"}

# Echo tool - simple text processing  
{"jsonrpc":"2.0","method":"tools/call","id":"2","params":{"name":"echo","arguments":{"text":"Hello MCP!"}}}
โ†’ {"echo":"Hello MCP!"}

# Sum tool - array processing
{"jsonrpc":"2.0","method":"tools/call","id":"3","params":{"name":"sum","arguments":{"numbers":[1,2,3,4,5]}}}
โ†’ {"sum":15.0,"count":5,"avg":3.0}

# Weather tool - mock API with realistic data
{"jsonrpc":"2.0","method":"tools/call","id":"4","params":{"name":"getWeather","arguments":{"city":"Beijing"}}}
โ†’ {"city":"Beijing","temperature":26,"condition":"Sunny","humidity":50,"note":"mock data"}

Tool Schema Validation

Each tool includes complete JSON Schema definitions for parameter validation:

  • Input validation with detailed error messages
  • Type checking for all parameters
  • Required vs optional parameter handling
  • Enum constraints where applicable

Manual Server Usage

Start the StreamableHTTP Server

# Build first (if not done already)
mkdir -p build && cd build
cmake .. -DMCP_WITH_HTTP=ON -DMCP_BUILD_EXAMPLE=ON
cmake --build . -j4

# Start server (runs on port 8080 by default)
./mcp_streamable_http_server

Environment Variables:

  • MCP_PORT - Server port (default: 8080)
  • MCP_HOST - Server host (default: 127.0.0.1)

Test with curl

# List available tools
curl -X POST http://127.0.0.1:8080/ \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"tools/list","id":"1"}'

# Call a tool (uppercase example)
curl -X POST http://127.0.0.1:8080/ \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"tools/call","id":"2","params":{"name":"upper","arguments":{"text":"hello world"}}}'

๐Ÿ Python Client Integration

The project includes a production-ready MCP SDK-based Python client demonstrating best practices:

Automatic Client Setup

The test script automatically handles Python environment setup:

# Virtual environment is created and managed automatically
./examples/scripts/test_mcp_streamable_http_server.csh

Manual Client Usage

If you need to run the client manually:

cd examples/client

# Setup virtual environment (if not exists)
python3 -m venv venv
source venv/bin/activate    # bash/zsh
# OR: source venv/bin/activate.csh  # csh/tcsh

# Install dependencies (auto-detected by test script)
pip install -r requirements.txt

# Run client modes
python3 mcp_sdk_client.py --demo          # Automated testing
python3 mcp_sdk_client.py --interactive   # Manual exploration
python3 mcp_sdk_client.py --verbose       # Debug output

Client Features

๐ŸŽฏ Demo Mode: Comprehensive automated testing

  • Tests all 6 server tools with realistic parameters
  • Validates response formats and error handling
  • Measures performance and connection reliability
  • Provides detailed success/failure reporting

๐ŸŽฎ Interactive Mode: Manual exploration interface

  • Lists all available tools with descriptions
  • Provides usage examples for each tool
  • Supports real-time tool execution with JSON parameters
  • Interactive help system with command completion

๐Ÿ”ง Advanced Options:

  • --host / --port - Custom server connection
  • --verbose - Detailed HTTP request/response logging
  • --debug - Full MCP protocol message tracing
  • --timeout - Custom request timeout settings

MCP Protocol Implementation

Supported Methods

  • initialize - MCP handshake with protocol version negotiation
  • notifications/initialized - Complete the handshake
  • tools/list - List all available tools with descriptions and schemas
  • tools/call - Execute a tool with given arguments

Transport Details

  • Protocol: StreamableHTTP over HTTP/1.1
  • Format: JSON-RPC 2.0 messages
  • Endpoint: Single POST / endpoint
  • Real-time: Streaming responses for long-running operations

JSON-RPC Examples

MCP Handshake

// 1. Initialize
{"jsonrpc":"2.0","method":"initialize","id":"1","params":{"protocolVersion":"2025-06-18","capabilities":{"roots":{"listChanged":false},"sampling":{}},"clientInfo":{"name":"mcp-client","version":"1.0.0"}}}

// 2. Initialized notification
{"jsonrpc":"2.0","method":"notifications/initialized"}

Tool Operations

// List tools
{"jsonrpc":"2.0","method":"tools/list","id":"2"}

// Call uppercase tool
{"jsonrpc":"2.0","method":"tools/call","id":"3","params":{"name":"upper","arguments":{"text":"hello world"}}}

// Call sum tool
{"jsonrpc":"2.0","method":"tools/call","id":"4","params":{"name":"sum","arguments":{"numbers":[1,2,3,4,5]}}}

Example Response

{"jsonrpc":"2.0","id":"3","result":{"upper":"HELLO WORLD"}}

๐Ÿ“ Project Structure

mcp_server_cpp/
โ”œโ”€โ”€ ๐Ÿ“ include/                          # Header files and bundled dependencies
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ mcp/                          # Core MCP server headers
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ server.hpp                # Main server class and API
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ tool.hpp                  # Tool interface and registry
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ transport_http.hpp        # StreamableHTTP transport
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ messages.hpp              # MCP protocol messages  
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ registry.hpp              # Tool registration system
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“„ *.hpp                     # Additional core headers
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ nlohmann/                     # JSON library (bundled v3.11+)
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“„ json.hpp                  # Complete JSON processing
โ”‚   โ””โ”€โ”€ ๐Ÿ“„ httplib.h                     # HTTP server library (bundled v0.14+)
โ”œโ”€โ”€ ๐Ÿ“ src/                              # Implementation files
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ server.cpp                    # Core server implementation
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ transport_http.cpp            # HTTP transport layer
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ messages.cpp                  # Protocol message handling
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ registry.cpp                  # Tool registry management
โ”‚   โ””โ”€โ”€ ๐Ÿ“„ *.cpp                         # Additional source files
โ”œโ”€โ”€ ๐Ÿ“ examples/                         # Complete examples and demos
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ streamable_http_server.cpp    # Main server executable
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ client/                       # Official MCP SDK integration
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ mcp_sdk_client.py         # Production-ready Python client
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ requirements.txt          # Python dependencies
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ venv/                     # Virtual environment (auto-created)
โ”‚   โ””โ”€โ”€ ๐Ÿ“ scripts/                      # Automated testing and utilities
โ”‚       โ””โ”€โ”€ ๐Ÿ“„ test_mcp_streamable_http_server.csh  # Comprehensive test suite
โ”œโ”€โ”€ ๐Ÿ“ build/                            # Build artifacts (generated by CMake)
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ libmcp.a                      # Static library
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ mcp_streamable_http_server    # Main server executable
โ”‚   โ””โ”€โ”€ ๐Ÿ“„ *                             # Other build artifacts
โ”œโ”€โ”€ ๐Ÿ“„ CMakeLists.txt                    # Build configuration
โ””โ”€โ”€ ๐Ÿ“„ README.md                         # This documentation

Key Files Description

  • mcp_streamable_http_server - Production server executable with all 6 tools
  • mcp_sdk_client.py - Official MCP SDK client with demo and interactive modes
  • test_mcp_streamable_http_server.csh - Complete test automation with both client modes
  • libmcp.a - Reusable static library for integration into other projects

Development Guide

Creating Custom Tools

#include "mcp/server.hpp"
#include "mcp/sample_tools.hpp"

int main() {
    mcp::McpServer server;
    
    // Add custom tool
    auto custom_tool = mcp::Tool{
        "my_tool",
        "Custom tool description",
        nlohmann::json::object(),  // Input schema
        [](const nlohmann::json& args, mcp::ToolContext& ctx) {
            // Tool implementation
            nlohmann::json result;
            result["message"] = "Custom tool executed!";
            return result;
        }
    };
    
    server.add_tool(custom_tool);
    
    // Start StreamableHTTP server
    int port = 8080;
    HttpTransportConfig cfg;
    cfg.port = port;
    cfg.host = "127.0.0.1";
    HttpTransportServer http(server, cfg);
    
    http.run();
    return 0;
}

Integration with MCP Clients

This server is compatible with:

  • Official MCP SDK (Python, TypeScript) - โœ… Tested
  • Claude Desktop - Should work with proper configuration
  • Other MCP clients - Any client supporting StreamableHTTP transport

๐Ÿ”ง Troubleshooting

Common Issues and Solutions

IssueSymptomsSolution
Port ConflictAddress already in usekill $(lsof -ti:8080) or set MCP_PORT=8081
Build FailureCMake configuration errorsEnsure CMake โ‰ฅ 3.16 and C++14+ compiler
Client ConnectionConnection refusedVerify server running: curl http://127.0.0.1:8080/
Test Script HangingInteractive mode timeoutServer logs show connection issues
Python DependenciesImport errorsVirtual environment auto-setup by test script

Debug and Diagnostic Tools

๐Ÿ” Server Debug Mode:

# Start server with verbose logging
MCP_PORT=8080 ./build/mcp_streamable_http_server

# Expected output:
# StreamableHTTP MCP server running on http://127.0.0.1:8080
# Endpoints: POST/GET / (main MCP endpoint), POST /rpc (legacy)

๐Ÿ Client Debug Mode:

# Full protocol tracing
python3 mcp_sdk_client.py --demo --verbose --debug

# Test specific functionality
python3 mcp_sdk_client.py --interactive --port 8080

๐Ÿงช Connection Testing:

# Test server health
curl -X POST -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"tools/list","id":"1"}' \
  http://127.0.0.1:8080/

# Expected response: {"jsonrpc":"2.0","id":"1","result":{"tools":[...]}}

Performance Monitoring

Resource Usage:

  • Memory: ~10MB base footprint with thread pool
  • CPU: Low idle usage, scales with concurrent requests
  • Network: HTTP/1.1 with keep-alive support
  • Concurrency: Thread-pool handles multiple simultaneous tool calls

Load Testing:

# Simple load test with curl
for i in {1..100}; do
  curl -X POST -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"tools/call","id":"'$i'","params":{"name":"time","arguments":{}}}' \
    http://127.0.0.1:8080/ &
done
wait

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test with the provided test script
  5. Submit a pull request

License

This project is provided as example code. Please respect third-party licenses:

  • nlohmann/json: MIT License
  • cpp-httplib: MIT License

Links