This is a complete, standalone example of exposing your RustAPI endpoints as MCP tools for LLMs and AI agents (Claude, Cursor, Continue, custom agents, etc.).
RustApi::auto()for zero-boilerplate routing + OpenAPI- Tagging routes with
#[rustapi_rs::tag("agent")]to selectively expose them McpServer::from_rustapi(...)withInvocationMode::InProcess(zero network overhead, full pipeline respect)- Side-by-side HTTP API (port 8080) + MCP server (port 9090)
- Graceful shutdown via
run_rustapi_and_mcp_with_shutdown - Security: untagged routes (e.g.
/admin/secret) are never visible to agents
# From the rustapi-rs-examples root
cargo run -p mcp-serverOutput:
🚀 HTTP API: http://127.0.0.1:8080
🧠 MCP tools: http://127.0.0.1:9090
GET http://127.0.0.1:8080/weather/IstanbulPOST http://127.0.0.1:8080/calc/sumwith{"a": 40, "b": 2}
Point any MCP client at http://127.0.0.1:9090 (HTTP + SSE transport).
Manual test with curl (JSON-RPC):
# initialize
curl -X POST http://127.0.0.1:9090 \
-H 'content-type: application/json' \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize"}'
# list tools (only the ones with "agent" tag)
curl -X POST http://127.0.0.1:9090 \
-H 'content-type: application/json' \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'
# call a tool
curl -X POST http://127.0.0.1:9090 \
-H 'content-type: application/json' \
-d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_weather","arguments":{"city":"Istanbul"}}}'Routes annotated with #[rustapi_rs::tag("agent")] are automatically turned into MCP tools using the OpenAPI schema RustAPI already generates.
InvocationMode::InProcess means:
- Tool calls bypass the network entirely
- Still go through validation, extractors, middleware, error handlers, etc.
- Extremely fast (~microseconds)
See source in src/main.rs.
- Main RustAPI repo examples:
crates/rustapi-rs/examples/mcp_tools.rs(quick in-tree demo) - Cookbook: MCP Integration
- Other MCP recipes in the cookbook (in-process, OpenAPI CLI, stdio)
- The full rustapi-rs-examples repository
rustapi-rs = { version = "0.1.507", features = ["protocol-mcp", "swagger-ui"] }Same as the main RustAPI project (MIT OR Apache-2.0).