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
42impl McpHandler for RejectHandler {
43    async fn handle(&self, request: server_request::Request) -> server_response::Response {
44        server_response::Response {
45            id: request.id,
46            status: 501,
47            headers: indexmap::IndexMap::new(),
48            body: Some(serde_json::json!({
49                "jsonrpc": "2.0",
50                "id": serde_json::Value::Null,
51                "error": {
52                    "code": -32601,
53                    "message": "this client does not host objectiveai-mcp",
54                },
55            })),
56        }
57    }
58}