pub trait JsonRpcHandler:
Send
+ Sync
+ 'static {
// Required method
fn handle_request<'life0, 'async_trait>(
&'life0 self,
request: Value,
) -> Pin<Box<dyn Future<Output = Value> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn server_info(&self) -> ServerInfo { ... }
fn capabilities(&self) -> Value { ... }
}Expand description
Handler for JSON-RPC requests - Core abstraction for MCP protocol implementation
This trait provides a transport-agnostic interface for handling MCP JSON-RPC requests. Implementations of this trait can work seamlessly across all transport layers (HTTP, STDIO, WebSocket, etc.) without transport-specific code.
§Architecture
The JsonRpcHandler trait serves as the bridge between:
- Protocol Logic: Tools, resources, prompts dispatch (typically macro-generated)
- Transport Layer: HTTP, STDIO, WebSocket protocol details
This separation enables:
- Clean, testable handler implementations
- Transport-agnostic server code
- Full MCP 2025-06-18 compliance in transport layer
- Compile-time dispatch optimizations in handlers
§Example: Macro-Generated Implementation
use turbomcp_protocol::JsonRpcHandler;
use async_trait::async_trait;
use serde_json::Value;
#[derive(Clone)]
struct WeatherServer;
#[async_trait]
impl JsonRpcHandler for WeatherServer {
async fn handle_request(&self, req: Value) -> Value {
// Parse method and dispatch
let method = req["method"].as_str().unwrap_or("");
match method {
"initialize" => { /* ... */ },
"tools/call" => { /* dispatch to tools */ },
"resources/read" => { /* dispatch to resources */ },
_ => serde_json::json!({"error": "method not found"}),
}
}
fn server_info(&self) -> ServerInfo {
ServerInfo {
name: "Weather Server".to_string(),
version: "1.0.0".to_string(),
}
}
}§Usage with Transports
// HTTP Transport
use turbomcp_transport::streamable_http_v2::StreamableHttpTransport;
let handler = Arc::new(WeatherServer);
let transport = StreamableHttpTransport::new(config, handler);
transport.run().await?;
// STDIO Transport
use turbomcp_transport::stdio::StdioTransport;
let handler = Arc::new(WeatherServer);
let transport = StdioTransport::new(handler);
transport.run().await?;Required Methods§
Sourcefn handle_request<'life0, 'async_trait>(
&'life0 self,
request: Value,
) -> Pin<Box<dyn Future<Output = Value> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle_request<'life0, 'async_trait>(
&'life0 self,
request: Value,
) -> Pin<Box<dyn Future<Output = Value> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Handle a JSON-RPC request and return a response
This method receives a JSON-RPC request as a serde_json::Value and must return
a valid JSON-RPC response. The implementation should:
- Route the request based on the
methodfield - Validate parameters
- Execute the appropriate handler logic
- Return a success response with results or an error response
§Arguments
request- The JSON-RPC request as a JSON value
§Returns
A JSON-RPC response as a JSON value containing either:
result: For successful operationserror: For failed operations with error details
§Note
The request and response are serde_json::Value to avoid tight coupling with
protocol types. Transport layers handle conversion to/from typed structs.
Provided Methods§
Sourcefn server_info(&self) -> ServerInfo
fn server_info(&self) -> ServerInfo
Get server metadata
Returns information about the server including name and version. This is used during the MCP initialization handshake.
§Returns
Server information including name and version
Sourcefn capabilities(&self) -> Value
fn capabilities(&self) -> Value
Get server capabilities
Returns the capabilities supported by this server. Override this to advertise custom capabilities to clients.
§Returns
JSON value describing server capabilities