workers-rs-mcp
An example MCP (Model Context Protocol) server written in Rust, running on Cloudflare Workers.
The server compiles to WebAssembly and runs on Cloudflare's edge infrastructure, handling MCP JSON-RPC requests over HTTP.
How it works
HTTP routing is handled by Axum via the workers-rs SDK. MCP protocol logic is handled by the mcpserver crate.
Endpoints
Session management
initializerequests generate a newmcp-session-id(UUID v4) returned in the response header- All subsequent requests should include the
mcp-session-idheader
Tools
Includes one example tool:
- echo — echoes the input
messageback to the caller
Getting started
Prerequisites
- Rust with the
wasm32-unknown-unknowntarget:rustup target add wasm32-unknown-unknown - Wrangler CLI:
npm install -g wrangler
Local development
wrangler dev
Deploy to Cloudflare Workers
wrangler deploy
Pushing to main also deploys automatically via GitHub Actions (requires CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID secrets).
Adding tools
- Implement the
ToolHandlertrait insrc/lib.rs:
struct MyToolHandler;
#[async_trait]
impl ToolHandler for MyToolHandler {
async fn call(&self, args: Value, _context: Value) -> Result<ToolResult, McpError> {
// your logic here
Ok(text_result("result".to_string()))
}
}
- Register the tool with the MCP server:
mcp_router.handle_tool("my-tool", Arc::new(MyToolHandler));
- Add the tool schema to
mcp/tools.json.