mcpserver
A Rust library for building Model Context Protocol (MCP) servers, implementing the 2025-03-26 specification.
mcpserver is a pure protocol handler — it parses JSON-RPC, routes MCP methods, validates tool arguments, and dispatches to your handlers. It has zero HTTP or transport opinion: you bring your own framework (Axum, Lambda, Warp, etc.) and own the routing, middleware, and status codes.
Installation
[]
= "0.2"
= "1"
The library has no runtime or HTTP dependencies. Add axum, tokio, etc. only if your application needs them.
Quick start
use ;
use Value;
// Build the server and register handlers.
let mut server = builder
.tools_file
.resources_file
.server_info
.build;
server.handle_tool;
// Deserialize from any source, call handle(), serialize the response.
let req: JsonRpcRequest = from_str.unwrap;
let resp = server.handle.await;
// resp.is_notification() → true for fire-and-forget methods (return 202, no body)
let json = to_string.unwrap;
Defining tools (tools.json)
Tools are defined as a JSON array. Each tool has a name, description, and an inputSchema (JSON Schema) that drives automatic argument validation.
Supported schema features
| Feature | Description | Example |
|---|---|---|
required |
Fields that must be present | "required": ["name"] |
oneOf |
At least one set of required fields must match | "oneOf": [{"required": ["phone"]}, {"required": ["email"]}] |
dependencies |
If field A is present, field B must also be present | "dependencies": {"lat": ["lon"]} |
See examples/tools.json for a full example with all three features.
Defining resources (resources.json)
Handler patterns
Struct-based handler
use async_trait;
use ;
use Value;
;
Closure-based handler
use ;
use Value;
let handler = new;
Resource handler
use async_trait;
use ;
;
HTTP integration (Axum example)
Since the library is transport-agnostic, you wire up HTTP yourself. Here's the pattern with Axum:
use Arc;
use ;
use ;
async
let server = new;
let app = new
.route
.with_state;
This makes it trivial to mount multiple MCP endpoints with different middleware:
let app = new
.route
.route
.with_state;
See examples/basic_server.rs for a complete working example with session management, health checks, and tool handlers.
Running the example
Then in another terminal:
# Initialize
# List tools
# Call a tool
Nginx deployment
An example Nginx config for TLS termination is provided in nginx/mcp.conf. Key settings:
proxy_buffering off— required for streamingproxy_http_version 1.1— keep-alive to upstreamproxy_read_timeout 300s— long timeout for streaming
MCP methods supported
| Method | Description |
|---|---|
initialize |
Handshake, returns server capabilities |
ping |
Keepalive |
tools/list |
List available tools |
tools/call |
Execute a tool |
resources/list |
List available resources |
resources/read |
Read a resource by name or URI |
notifications/initialized |
Client notification (no response body) |
notifications/cancelled |
Client notification (no response body) |
License
MIT — see LICENSE.