Get a server and client talking in five minutes: register a tool, list and call it, then read the result back. Both ends run in one process over the in-memory transport, so no external host is required.
- CMake 3.28+ and Ninja configured as described in Installation and Integration.
- A checkout of this repository (example programs are not built by default; the switch must be turned on).
cmake --preset debug -DMCP_BUILD_EXAMPLES=ON
cmake --build --preset debug --target SimpleClient
./build/debug/examples/SimpleClient/SimpleClient # SimpleClient.exe on WindowsExpected output (measured):
Server: EchoServer v1.0.0
Protocol: 2026-07-28
Available tools:
- echo: Echo input
Calling echo tool with 'Hello, MCP!'...
Response: Hello, MCP!
Done!
The second line matters: the in-process InMemoryTransport pair lets the client probe server/discover in the default Auto mode, and a successful probe negotiates the modern era (2026-07-28). A handshake through initialize can only land in the legacy era (at most 2025-11-25) — see Protocol Versions.
#include <mcp/Content.hpp>
#include <mcp/McpTypes.hpp>
#include <mcp/server/McpServer.hpp>
#include <mcp/server/RequestContext.hpp>
#include <mcp/server/ServerOptions.hpp>
#include <mcp/transport/StdioServerTransport.hpp>
using namespace mcp;
int main() {
ServerOptions options;
options.server_info = Implementation{"my-server", "1.0.0"};
auto server = McpServer::Create(std::make_shared<StdioServerTransport>(), options);
server->RegisterTool(
"greet",
ToolOptions{}.Description("Say hello"),
[](const RequestContext<CallToolRequestParams>& ctx) -> CallToolResult {
CallToolResult result;
result.content.push_back(TextContent{"text", "hello"});
return result;
});
server->Run();
return 0;
}Key points:
McpServer::Createreturnsstd::unique_ptr; the transport is passed asstd::shared_ptr<ITransport>.ToolOptions{}.Description(...)is a chainable setter; assign the remaining fields directly.Run()blocks in the server loop and only returns afterClose(); never callClose()from a callback running on the IO thread — it would join itself.- Full registration shapes (resources, templates, prompts, skills) live in Register a Tool and Server Reference.
- stdio: point any MCP host (Claude Desktop, IDE plugins) at your executable, including the
.exesuffix on Windows. - End-to-end self test:
examples/SimpleClient/main.cppstarts both ends in one process throughInMemoryTransport::CreatePair()— the fastest way to debug. - Out-of-process check: build
EchoServer(cmake --build --preset debug --target EchoServer) and connect the official MCP Inspector over stdio.
cmake --build --preset debug
ctest --preset debug --output-on-failureExpect a clean build and the full suite passing (currently 18 test targets, 1021 registered cases on Windows). For an example-only change, build just that target: cmake --build --preset debug --target SimpleClient.
| Symptom | Cause | Fix |
|---|---|---|
No rule to make target 'SimpleClient' |
-DMCP_BUILD_EXAMPLES=ON was not passed |
Reconfigure, then build that target |
cannot open file '.../SimpleClient' |
Missing .exe on Windows, or the preset output directory is not build/debug |
Add the suffix per platform |
| Host connects but nothing happens | Server writes business logs to stdout and corrupts stdio framing | Internal logging is off by default; debug with MCP_LOG_LEVEL=4 and never log to stdout |
Tool call returns is_error with invalid arguments: ... |
Arguments did not match input_schema; the SDK rejects them before the handler runs |
See Validate Tool Input |
- Server capabilities in depth: Register a Tool, Serve a Resource, Collect User Input
- Transport choice: Transports
- Signatures and fields: Server Reference, Client Reference
examples/SimpleClient/main.cpp,examples/EchoServer/main.cpp,include/mcp/server/McpServer.hpp,include/mcp/transport/InMemoryTransport.hpp,CMakePresets.json