Expand description
Rocket integration for the Rust MCP SDK.
This crate provides integration between the MCP SDK and the Rocket web framework, making it easy to expose MCP servers over HTTP.
§Features
- HTTP POST endpoint for JSON-RPC messages
- Server-Sent Events (SSE) streaming for notifications
- Session management with automatic cleanup
- Protocol version validation
- CORS support via Rocket fairings
§HTTP Protocol Requirements
Clients must include the Mcp-Protocol-Version header in all requests:
POST /mcp HTTP/1.1
Content-Type: application/json
Mcp-Protocol-Version: 2025-11-25
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{...}}Supported protocol versions: 2024-11-05, 2025-03-26, 2025-06-18, 2025-11-25
§Quick Start
ⓘ
use mcpkit::prelude::*;
use mcpkit_rocket::McpRouter;
// Your MCP server handler (use #[mcp_server] macro)
#[mcp_server(name = "my-server", version = "1.0.0")]
impl MyServer {
#[tool(description = "Say hello")]
async fn hello(&self, name: String) -> ToolOutput {
ToolOutput::text(format!("Hello, {name}!"))
}
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
McpRouter::new(MyServer::new())
.launch()
.await?;
Ok(())
}§Advanced Usage
For more control, use into_rocket() to integrate with an existing app:
ⓘ
use mcpkit_rocket::McpRouter;
use rocket::{Build, Rocket};
fn build_app() -> Rocket<Build> {
let mcp = McpRouter::new(MyServer::new())
.with_cors();
rocket::build()
.attach(mcp.into_fairing())
.mount("/health", routes![health_check])
}
#[rocket::get("/")]
fn health_check() -> &'static str {
"OK"
}§Client Example (curl)
# Initialize the connection
curl -X POST http://localhost:8000/mcp \
-H "Content-Type: application/json" \
-H "Mcp-Protocol-Version: 2025-11-25" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","clientInfo":{"name":"test","version":"1.0"},"capabilities":{}}}'
# List available tools
curl -X POST http://localhost:8000/mcp \
-H "Content-Type: application/json" \
-H "Mcp-Protocol-Version: 2025-11-25" \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'Re-exports§
pub use handler::LastEventIdHeader;pub use handler::McpResponse;pub use handler::ProtocolVersionHeader;pub use handler::SessionIdHeader;pub use handler::handle_mcp_post;pub use handler::handle_sse;
Modules§
- handler
- Handler module for MCP request processing. HTTP handlers for MCP requests using Rocket.
- prelude
- Prelude module for convenient imports.
Macros§
- create_
mcp_ routes - Create MCP route handlers for a specific handler type.
Structs§
- Cors
- CORS fairing for permissive cross-origin requests.
- McpRouter
- Builder for MCP Rocket routers.
- McpState
- Shared state for MCP request handling.
- Session
Store - Session manager for tracking MCP client sessions.
Enums§
- Rocket
Error - Errors that can occur during MCP request handling.
Constants§
- SUPPORTED_
VERSIONS - Protocol versions supported by this extension.
Traits§
- Session
Manager - Session manager trait for managing MCP sessions.
Functions§
- is_
supported_ version - Check if a protocol version is supported.