cpp-mcp/README.md

196 lines
5.8 KiB
Markdown
Raw Permalink Normal View History

2025-03-13 21:46:49 +08:00
# MCP Protocol Framework
2025-03-17 16:29:33 +08:00
[Model Context Protocol (MCP)](https://spec.modelcontextprotocol.io/specification/2024-11-05/architecture/) is an open protocol that provides a standardized way for AI models and agents to interact with various resources, tools, and services. This framework implements the core functionality of the MCP protocol, conforming to the 2024-11-05 basic protocol specification.
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
## Core Features
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
- **JSON-RPC 2.0 Communication**: Request/response communication based on JSON-RPC 2.0 standard
- **Resource Abstraction**: Standard interfaces for resources such as files, APIs, etc.
- **Tool Registration**: Register and call tools with structured parameters
- **Extensible Architecture**: Easy to extend with new resource types and tools
- **Multi-Transport Support**: Supports HTTP and standard input/output (stdio) communication methods
2025-03-13 21:46:49 +08:00
2025-04-01 14:47:30 +08:00
## How to Build
Example of building with CMake:
```bash
cmake -B build
cmake --build build --config Release
```
Build with tests:
```
git submodule --init --recursive # Get GoogleTest
cmake -B build -DMCP_BUILD_TESTS=ON
cmake --build build --config Release
```
2025-03-17 16:29:33 +08:00
## Components
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
The MCP C++ library includes the following main components:
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
### Core Components
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
#### Client Interface (`mcp_client.h`)
Defines the abstract interface for MCP clients, which all concrete client implementations inherit from.
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
#### SSE Client (`mcp_sse_client.h`, `mcp_sse_client.cpp`)
Client implementation that communicates with MCP servers using HTTP and Server-Sent Events (SSE).
2025-03-13 21:50:35 +08:00
2025-03-17 16:29:33 +08:00
#### Stdio Client (`mcp_stdio_client.h`, `mcp_stdio_client.cpp`)
Client implementation that communicates with MCP servers using standard input/output, capable of launching subprocesses and communicating with them.
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
#### Message Processing (`mcp_message.h`, `mcp_message.cpp`)
Handles serialization and deserialization of JSON-RPC messages.
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
#### Tool Management (`mcp_tool.h`, `mcp_tool.cpp`)
Manages and invokes MCP tools.
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
#### Resource Management (`mcp_resource.h`, `mcp_resource.cpp`)
Manages MCP resources.
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
#### Server (`mcp_server.h`, `mcp_server.cpp`)
Implements MCP server functionality.
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
## Examples
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
### HTTP Server Example (`examples/server_example.cpp`)
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
Example MCP server implementation with custom tools:
- Time tool: Get the current time
- Calculator tool: Perform mathematical operations
- Echo tool: Process and analyze text
2025-04-01 14:47:30 +08:00
- Greeting tool: Returns `Hello, `+ input name + `!`, defaults to `Hello, World!`
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
### HTTP Client Example (`examples/client_example.cpp`)
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
Example MCP client connecting to a server:
- Get server information
- List available tools
- Call tools with parameters
- Access resources
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
### Stdio Client Example (`examples/stdio_client_example.cpp`)
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
Demonstrates how to use the stdio client to communicate with a local server:
- Launch a local server process
- Access filesystem resources
- Call server tools
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
## How to Use
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
### Setting up an HTTP Server
2025-03-13 21:46:49 +08:00
```cpp
2025-03-17 16:29:33 +08:00
// Create and configure the server
2025-03-13 21:46:49 +08:00
mcp::server server("localhost", 8080);
server.set_server_info("MCP Example Server", "2024-11-05");
2025-03-17 16:29:33 +08:00
// Register tools
2025-03-13 21:46:49 +08:00
mcp::json hello_handler(const mcp::json& params) {
std::string name = params.contains("name") ? params["name"].get<std::string>() : "World";
return {
{
{"type", "text"},
{"text", "Hello, " + name + "!"}
}
};
}
mcp::tool hello_tool = mcp::tool_builder("hello")
.with_description("Say hello")
.with_string_param("name", "Name to say hello to", "World")
.build();
server.register_tool(hello_tool, hello_handler);
2025-03-17 16:29:33 +08:00
// Register resources
2025-03-13 21:46:49 +08:00
auto file_resource = std::make_shared<mcp::file_resource>("<file_path>");
server.register_resource("file://<file_path>", file_resource);
2025-03-17 16:29:33 +08:00
// Start the server
server.start(true); // Blocking mode
2025-03-13 21:46:49 +08:00
```
2025-03-17 16:29:33 +08:00
### Creating an HTTP Client
2025-03-13 21:46:49 +08:00
```cpp
2025-03-17 16:29:33 +08:00
// Connect to the server
2025-03-16 01:14:05 +08:00
mcp::sse_client client("localhost", 8080);
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
// Initialize the connection
2025-03-13 21:46:49 +08:00
client.initialize("My Client", "1.0.0");
2025-03-17 16:29:33 +08:00
// Call a tool
2025-03-13 21:46:49 +08:00
mcp::json params = {
{"name", "Client"}
};
mcp::json result = client.call_tool("hello", params);
```
2025-03-17 16:29:33 +08:00
### Using the SSE Client
2025-03-16 01:14:05 +08:00
2025-03-17 16:29:33 +08:00
The SSE client uses HTTP and Server-Sent Events (SSE) to communicate with MCP servers. This is a communication method based on Web standards, suitable for communicating with servers that support HTTP/SSE.
2025-03-16 01:14:05 +08:00
```cpp
#include "mcp_sse_client.h"
2025-03-17 16:29:33 +08:00
// Create a client, specifying the server address and port
2025-03-16 01:14:05 +08:00
mcp::sse_client client("localhost", 8080);
2025-03-17 16:29:33 +08:00
// Or use a base URL
2025-03-16 01:14:05 +08:00
// mcp::sse_client client("http://localhost:8080");
2025-03-17 16:29:33 +08:00
// Set an authentication token (if needed)
2025-03-16 01:14:05 +08:00
client.set_auth_token("your_auth_token");
2025-03-17 16:29:33 +08:00
// Set custom request headers (if needed)
2025-03-16 01:14:05 +08:00
client.set_header("X-Custom-Header", "value");
2025-03-17 16:29:33 +08:00
// Initialize the client
2025-03-16 01:14:05 +08:00
if (!client.initialize("My Client", "1.0.0")) {
2025-03-17 16:29:33 +08:00
// Handle initialization failure
2025-03-16 01:14:05 +08:00
}
2025-03-17 16:29:33 +08:00
// Call a tool
2025-03-16 01:14:05 +08:00
json result = client.call_tool("tool_name", {
{"param1", "value1"},
{"param2", 42}
});
```
2025-03-17 16:29:33 +08:00
### Using the Stdio Client
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
The Stdio client can communicate with any MCP server that supports stdio transport, such as:
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
- @modelcontextprotocol/server-everything - Example server
- @modelcontextprotocol/server-filesystem - Filesystem server
- Other [MCP servers](https://www.pulsemcp.com/servers) that support stdio transport
2025-03-13 21:46:49 +08:00
```cpp
#include "mcp_stdio_client.h"
2025-03-17 16:29:33 +08:00
// Create a client, specifying the server command
2025-03-13 21:46:49 +08:00
mcp::stdio_client client("npx -y @modelcontextprotocol/server-everything");
// mcp::stdio_client client("npx -y @modelcontextprotocol/server-filesystem /path/to/directory");
2025-03-17 16:29:33 +08:00
// Initialize the client
2025-03-13 21:46:49 +08:00
if (!client.initialize("My Client", "1.0.0")) {
2025-03-17 16:29:33 +08:00
// Handle initialization failure
2025-03-13 21:46:49 +08:00
}
2025-03-17 16:29:33 +08:00
// Access resources
2025-03-13 21:46:49 +08:00
json resources = client.list_resources();
json content = client.read_resource("resource://uri");
2025-03-17 16:29:33 +08:00
// Call a tool
2025-03-13 21:46:49 +08:00
json result = client.call_tool("tool_name", {
{"param1", "value1"},
{"param2", "value2"}
});
```
2025-03-17 16:29:33 +08:00
## License
2025-03-13 21:46:49 +08:00
2025-03-17 16:29:33 +08:00
This framework is provided under the MIT license. For details, please see the LICENSE file.