|
| 1 | +# Calculator Rust Binary Example |
| 2 | + |
| 3 | +Demonstrates packaging a compiled Rust binary as an MCP Bundle using the `binary` server type. Based on the [official Rust MCP SDK calculator example](https://github.com/modelcontextprotocol/rust-sdk/tree/main/examples/servers). |
| 4 | + |
| 5 | +## What is Binary Server Type? |
| 6 | + |
| 7 | +Unlike `node` or `uv` server types where the source code is bundled and executed by a runtime, the `binary` server type packages a pre-compiled native executable. This is useful for: |
| 8 | + |
| 9 | +- Rust, Go, C/C++ MCP servers |
| 10 | +- Performance-sensitive workloads |
| 11 | +- Servers with no runtime dependencies |
| 12 | + |
| 13 | +The tradeoff is that binaries are platform-specific — you need a separate build for each target OS/architecture. |
| 14 | + |
| 15 | +## Structure |
| 16 | + |
| 17 | +``` |
| 18 | +calculator-rust/ |
| 19 | +├── manifest.json # server.type = "binary" |
| 20 | +├── Cargo.toml # Rust project |
| 21 | +├── Dockerfile # Build via Docker |
| 22 | +├── .mcpbignore # Exclude source from bundle |
| 23 | +├── .gitignore # Exclude build artifacts |
| 24 | +└── src/ |
| 25 | + └── main.rs # Calculator MCP server (~80 LOC) |
| 26 | +``` |
| 27 | + |
| 28 | +After building, the `server/` directory is created with the compiled binary. |
| 29 | + |
| 30 | +## Building |
| 31 | + |
| 32 | +### Native (requires Rust 1.85+) |
| 33 | + |
| 34 | +```bash |
| 35 | +cd examples/calculator-rust |
| 36 | +cargo build --release |
| 37 | +mkdir -p server |
| 38 | +cp target/release/mcp-calculator server/ |
| 39 | +``` |
| 40 | + |
| 41 | +### Docker (no local Rust required) |
| 42 | + |
| 43 | +```bash |
| 44 | +cd examples/calculator-rust |
| 45 | +docker build -t mcp-calculator . |
| 46 | +# Extract the binary (linux only) |
| 47 | +docker create --name calc mcp-calculator |
| 48 | +docker cp calc:/mcp-calculator server/mcp-calculator |
| 49 | +docker rm calc |
| 50 | +chmod +x server/mcp-calculator |
| 51 | +``` |
| 52 | + |
| 53 | +## Packing |
| 54 | + |
| 55 | +```bash |
| 56 | +mcpb pack examples/calculator-rust |
| 57 | +``` |
| 58 | + |
| 59 | +The `.mcpbignore` file excludes source code and build artifacts — only `manifest.json` and `server/mcp-calculator` end up in the bundle. |
| 60 | + |
| 61 | +## Testing |
| 62 | + |
| 63 | +```bash |
| 64 | +# MCP protocol handshake |
| 65 | +echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' \ |
| 66 | + | ./server/mcp-calculator |
| 67 | + |
| 68 | +# Tool call |
| 69 | +(printf '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}\n' |
| 70 | +printf '{"jsonrpc":"2.0","method":"notifications/initialized"}\n' |
| 71 | +printf '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"sum","arguments":{"a":3,"b":4}}}\n' |
| 72 | +sleep 1) | ./server/mcp-calculator |
| 73 | +``` |
| 74 | + |
| 75 | +Expected: `sum(3, 4)` returns `{"text":"7"}`. |
| 76 | + |
| 77 | +## Tools |
| 78 | + |
| 79 | +- **sum** — Calculate the sum of two numbers |
| 80 | +- **sub** — Calculate the difference of two numbers |
| 81 | + |
| 82 | +## Notes |
| 83 | + |
| 84 | +- Binary size: ~2.5 MB (stripped, LTO enabled) |
| 85 | +- The `Cargo.toml` uses `edition = "2024"` (Rust 1.85+) |
| 86 | +- Logs are written to stderr via `tracing`, so they don't interfere with MCP's stdio transport |
| 87 | +- Uses the [rmcp](https://crates.io/crates/rmcp) crate (official Rust MCP SDK) |
0 commit comments