Skip to main content

objectiveai_sdk/http/
mcp_handler.rs

1//! Handler trait for inbound objectiveai-mcp `server_request` frames.
2//!
3//! The API hosts `/objectiveai-mcp/{session_id}` as a pure HTTP→WS
4//! bridge: every HTTP request the proxy makes is forwarded over the
5//! reverse-attach WS as a [`server_request::Request`], and the
6//! handler's returned [`server_response::Response`] is translated
7//! back into the HTTP response the proxy receives.
8//!
9//! Clients that host objectiveai-mcp (e.g. the CLI) implement
10//! [`McpHandler`] to spawn / forward / reply. Clients that don't
11//! use [`RejectHandler`], which 501s every request — the API's
12//! list-tools verification probe then short-circuits and any agent
13//! that declares `client_objectiveai_mcp` falls through to the next
14//! fallback server-side.
15
16use crate::client_objectiveai_mcp::{server_request, server_response};
17use std::future::Future;
18
19/// Handler for inbound `server_request` frames on a streaming WS.
20///
21/// One handler instance is bound at `create_streaming` time and
22/// stays live for the lifetime of the WS session. Implementations
23/// must be `Send + Sync + 'static` since the demux task that
24/// invokes them is spawned.
25pub trait McpHandler: Send + Sync + 'static {
26    /// Dispatch a single request. The returned `Response`'s `id`
27    /// must echo `request.id` so the API can correlate the reply
28    /// to the in-flight proxy request waiting on it.
29    fn handle(
30        &self,
31        request: server_request::Request,
32    ) -> impl Future<Output = server_response::Response> + Send;
33}
34
35/// Default handler that 501s every `server_request`. Used when the
36/// calling client doesn't host objectiveai-mcp — agents that
37/// declare `client_objectiveai_mcp` will see this and fall through
38/// to the next fallback agent on the server side.
39#[derive(Debug, Clone, Copy, Default)]
40pub struct RejectHandler;
41
42/// JSON-RPC error code for the reject-handler's stock reply. -32601 is
43/// "method not found"; pairs cleanly with the proxy seeing the API
44/// fall through to a fallback agent.
45const REJECT_CODE: i64 = -32601;
46const REJECT_MESSAGE: &str = "this client does not host objectiveai-mcp";
47
48fn reject_err<R>() -> server_response::JsonRpcResult<R> {
49    server_response::JsonRpcResult::Err {
50        code: REJECT_CODE,
51        message: REJECT_MESSAGE.into(),
52        data: None,
53    }
54}
55
56impl McpHandler for RejectHandler {
57    async fn handle(
58        &self,
59        request: server_request::Request,
60    ) -> server_response::Response {
61        // Reply with a typed `JsonRpcResult::Err` in the variant
62        // that pairs with the inbound payload — the API's
63        // `variant_mismatch` check is satisfied AND the error
64        // surfaces as a method-not-found on the proxy side.
65        use server_response::Payload;
66        let payload = match request.payload {
67            server_request::Payload::Initialize { mcp_kind, .. } => {
68                Payload::Initialize { mcp_kind, result: reject_err() }
69            }
70            server_request::Payload::ToolsList { mcp_kind, .. } => {
71                Payload::ToolsList { mcp_kind, result: reject_err() }
72            }
73            server_request::Payload::ToolsCall { mcp_kind, .. } => {
74                Payload::ToolsCall { mcp_kind, result: reject_err() }
75            }
76            server_request::Payload::ResourcesList { mcp_kind, .. } => {
77                Payload::ResourcesList { mcp_kind, result: reject_err() }
78            }
79            server_request::Payload::ResourcesRead { mcp_kind, .. } => {
80                Payload::ResourcesRead { mcp_kind, result: reject_err() }
81            }
82            server_request::Payload::SessionTerminate { mcp_kind } => {
83                Payload::SessionTerminate { mcp_kind, result: reject_err() }
84            }
85            server_request::Payload::ReadMessageQueue(_) => {
86                Payload::ReadMessageQueue(reject_err())
87            }
88            server_request::Payload::Retrieve(_) => {
89                Payload::Retrieve(reject_err())
90            }
91        };
92        server_response::Response {
93            id: request.id,
94            payload,
95        }
96    }
97}