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
42impl McpHandler for RejectHandler {
43 async fn handle(
44 &self,
45 request: server_request::Request,
46 ) -> server_response::Response {
47 // Reply with a typed `JsonRpcResult::Err` in the variant
48 // that pairs with the inbound payload — the API's
49 // `variant_mismatch` check is satisfied AND the error
50 // surfaces as a method-not-found on the proxy side.
51 use server_response::{JsonRpcResult, Payload};
52 const CODE: i64 = -32601;
53 const MESSAGE: &str = "this client does not host objectiveai-mcp";
54 let payload = match request.payload {
55 server_request::Payload::Initialize(_) => Payload::Initialize(JsonRpcResult::Err {
56 code: CODE,
57 message: MESSAGE.into(),
58 data: None,
59 }),
60 server_request::Payload::ToolsList(_) => Payload::ToolsList(JsonRpcResult::Err {
61 code: CODE,
62 message: MESSAGE.into(),
63 data: None,
64 }),
65 server_request::Payload::ToolsCall(_) => Payload::ToolsCall(JsonRpcResult::Err {
66 code: CODE,
67 message: MESSAGE.into(),
68 data: None,
69 }),
70 server_request::Payload::ResourcesList(_) => Payload::ResourcesList(JsonRpcResult::Err {
71 code: CODE,
72 message: MESSAGE.into(),
73 data: None,
74 }),
75 server_request::Payload::ResourcesRead(_) => Payload::ResourcesRead(JsonRpcResult::Err {
76 code: CODE,
77 message: MESSAGE.into(),
78 data: None,
79 }),
80 server_request::Payload::SessionTerminate => Payload::SessionTerminate(JsonRpcResult::Err {
81 code: CODE,
82 message: MESSAGE.into(),
83 data: None,
84 }),
85 };
86 server_response::Response {
87 id: request.id,
88 mcp_kind: request.mcp_kind,
89 payload,
90 }
91 }
92}