turbomcp_server/routing/
traits.rs

1//! Router traits and type definitions
2
3use turbomcp_protocol::RequestContext;
4use turbomcp_protocol::{
5    jsonrpc::{JsonRpcRequest, JsonRpcResponse},
6    types::{
7        CreateMessageRequest, ElicitRequest, ElicitResult, ListRootsResult, PingRequest, PingResult,
8    },
9};
10
11use crate::ServerResult;
12
13/// Server request dispatcher trait for server-initiated requests
14#[async_trait::async_trait]
15pub trait ServerRequestDispatcher: Send + Sync {
16    /// Send an elicitation request to the client
17    async fn send_elicitation(
18        &self,
19        request: ElicitRequest,
20        ctx: RequestContext,
21    ) -> ServerResult<ElicitResult>;
22
23    /// Send a ping request to the client
24    async fn send_ping(
25        &self,
26        request: PingRequest,
27        ctx: RequestContext,
28    ) -> ServerResult<PingResult>;
29
30    /// Send a sampling create message request to the client
31    async fn send_create_message(
32        &self,
33        request: CreateMessageRequest,
34        ctx: RequestContext,
35    ) -> ServerResult<turbomcp_protocol::types::CreateMessageResult>;
36
37    /// Send a roots list request to the client
38    async fn send_list_roots(
39        &self,
40        request: turbomcp_protocol::types::ListRootsRequest,
41        ctx: RequestContext,
42    ) -> ServerResult<ListRootsResult>;
43
44    /// Check if client supports bidirectional communication
45    fn supports_bidirectional(&self) -> bool;
46
47    /// Get client capabilities
48    async fn get_client_capabilities(&self) -> ServerResult<Option<serde_json::Value>>;
49}
50
51/// Route handler trait for custom routes
52#[async_trait::async_trait]
53pub trait RouteHandler: Send + Sync {
54    /// Handle the request
55    async fn handle(
56        &self,
57        request: JsonRpcRequest,
58        ctx: RequestContext,
59    ) -> ServerResult<JsonRpcResponse>;
60
61    /// Check if this handler can handle the request
62    fn can_handle(&self, method: &str) -> bool;
63
64    /// Get handler metadata
65    fn metadata(&self) -> RouteMetadata {
66        RouteMetadata::default()
67    }
68}
69
70/// Route metadata
71#[derive(Debug, Clone)]
72pub struct RouteMetadata {
73    /// Route name
74    pub name: String,
75    /// Route description
76    pub description: Option<String>,
77    /// Route version
78    pub version: String,
79    /// Supported methods
80    pub methods: Vec<String>,
81    /// Route tags
82    pub tags: Vec<String>,
83}
84
85impl Default for RouteMetadata {
86    fn default() -> Self {
87        Self {
88            name: "unknown".to_string(),
89            description: None,
90            version: "1.0.0".to_string(),
91            methods: Vec::new(),
92            tags: Vec::new(),
93        }
94    }
95}
96
97/// Route definition for custom routing
98#[derive(Clone)]
99pub struct Route {
100    /// Route method pattern
101    pub method: String,
102    /// Route handler
103    pub handler: std::sync::Arc<dyn RouteHandler>,
104    /// Route metadata
105    pub metadata: RouteMetadata,
106}
107
108impl std::fmt::Debug for Route {
109    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110        f.debug_struct("Route")
111            .field("method", &self.method)
112            .field("metadata", &self.metadata)
113            .finish()
114    }
115}