mcpserver
A Rust library for building Model Context Protocol (MCP) servers, implementing the 2025-03-26 specification with Streamable HTTP transport.
Define your tools and resources in JSON, register async handlers, and serve over HTTP with Axum — or call Server::handle() directly for Lambda / custom integrations.
Installation
[]
= "0.1"
Or via the CLI:
Quick start
use Arc;
use ;
use Value;
async
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 transport
http_router() returns an axum::Router with a single route: POST /mcp (the MCP JSON-RPC endpoint). Session management via mcp-session-id headers is handled automatically.
Merge it into your own router to add health checks, landing pages, or middleware:
use ;
use ;
let server = builder.build;
let app = new
.route
.merge;
Custom integration (Lambda, etc.)
For environments where you control the HTTP layer (e.g., AWS Lambda), use Server::handle() directly:
use ;
async
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 and session ID |
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 (HTTP 202) |
notifications/cancelled |
Client notification (HTTP 202) |
License
MIT — see LICENSE.