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.
HTTP routing is handled by Axum via the workers-rs SDK. MCP protocol logic is handled by the mcpserver crate.
| Method | Path | Description |
|---|---|---|
| GET | / |
Server name and version |
| GET | /healthz |
Health check — returns OK |
| POST | /mcp |
MCP JSON-RPC endpoint |
initializerequests generate a newmcp-session-id(UUID v4) returned in the response header- All subsequent requests should include the
mcp-session-idheader
Includes one example tool:
- echo — echoes the input
messageback to the caller
- Rust with the
wasm32-unknown-unknowntarget:rustup target add wasm32-unknown-unknown
- Wrangler CLI:
npm install -g wrangler
wrangler devwrangler deployPushing to main also deploys automatically via GitHub Actions (requires CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID secrets).
- 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.