feat: add Rust binary calculator example · modelcontextprotocol/mcpb@cc4aa75 · GitHub
Skip to content

Commit cc4aa75

Browse files
feat: add Rust binary calculator example
First example of the binary server type — a compiled Rust MCP server packaged as an MCPB. Based on the official Rust SDK calculator example (rmcp 1.2.0, 2 tools: sum/sub, ~80 LOC). Includes Cargo.toml, Dockerfile for Docker-based builds, .mcpbignore to exclude source from the bundle, and README with build/pack/test instructions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1dca91e commit cc4aa75

8 files changed

Lines changed: 248 additions & 5 deletions

File tree

‎examples/README.md‎

Lines changed: 6 additions & 5 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target/
2+
Cargo.lock
3+
server/mcp-calculator
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Cargo.toml
2+
Cargo.lock
3+
src/
4+
target/
5+
Dockerfile
6+
README.md
7+
.gitignore
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "mcp-calculator"
3+
version = "1.0.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
rmcp = { version = "1.2.0", features = ["server", "macros", "transport-io", "schemars"] }
8+
tokio = { version = "1", features = ["macros", "rt-multi-thread", "io-std"] }
9+
serde = { version = "1.0", features = ["derive"] }
10+
serde_json = "1.0"
11+
anyhow = "1.0"
12+
tracing = "0.1"
13+
tracing-subscriber = { version = "0.3", features = ["env-filter", "std", "fmt"] }
14+
schemars = "1.0"
15+
16+
[profile.release]
17+
strip = true
18+
lto = true
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM rust:1.94-bookworm AS builder
2+
3+
WORKDIR /app
4+
COPY Cargo.toml ./
5+
COPY src/ ./src/
6+
7+
RUN cargo build --release
8+
9+
FROM debian:bookworm-slim
10+
COPY --from=builder /app/target/release/mcp-calculator /mcp-calculator
11+
ENTRYPOINT ["/mcp-calculator"]

‎examples/calculator-rust/README.md‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"$schema": "../../dist/mcpb-manifest.schema.json",
3+
"manifest_version": "0.3",
4+
"name": "calculator-rust",
5+
"display_name": "Calculator (Rust Binary)",
6+
"version": "1.0.0",
7+
"description": "A simple calculator MCP server compiled from Rust",
8+
"long_description": "Demonstrates packaging a compiled Rust binary as an MCP Bundle using the binary server type. The server provides sum and sub tools for basic arithmetic. Based on the official Rust MCP SDK calculator example.",
9+
"author": {
10+
"name": "Model Context Protocol"
11+
},
12+
"server": {
13+
"type": "binary",
14+
"entry_point": "server/mcp-calculator",
15+
"mcp_config": {
16+
"command": "${__dirname}/server/mcp-calculator",
17+
"args": [],
18+
"env": {}
19+
}
20+
},
21+
"tools": [
22+
{
23+
"name": "sum",
24+
"description": "Calculate the sum of two numbers"
25+
},
26+
{
27+
"name": "sub",
28+
"description": "Calculate the difference of two numbers"
29+
}
30+
],
31+
"keywords": ["calculator", "rust", "binary", "example"],
32+
"license": "Apache-2.0",
33+
"compatibility": {
34+
"platforms": ["darwin", "linux"]
35+
}
36+
}
Lines changed: 80 additions & 0 deletions

0 commit comments

Comments
 (0)