Skip to main content

objectiveai_sdk/client_objectiveai_mcp/server_response/
payload.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4/// Tagged union of every reply the CLI can send back to the API in
5/// answer to a [`super::super::server_request::Payload`]. Variant
6/// names pair 1:1 with the request side; the typed `result` /
7/// `error` shape per JSON-RPC method is captured in
8/// [`JsonRpcResult`].
9///
10/// MCP-routed variants echo `mcp_kind` on the variant itself
11/// (lets the API sanity-check routing). Non-MCP variants
12/// (`ReadMessageQueue` / `ClearMessageQueue`) don't carry
13/// `mcp_kind` — they never had one to echo. Use
14/// [`Payload::mcp_kind`] to retrieve it generically.
15#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
16#[schemars(rename = "client_objectiveai_mcp.server_response.Payload")]
17#[serde(tag = "type", rename_all = "snake_case")]
18pub enum Payload {
19    /// Reply to
20    /// [`super::super::server_request::Payload::Initialize`]. On
21    /// success carries the upstream MCP session id the API stamps
22    /// onto its outbound `Mcp-Session-Id` response header so the
23    /// proxy adopts it. On failure (dial or aggregate-build error)
24    /// carries a JSON-RPC error envelope the API translates into its
25    /// own outbound error.
26    #[schemars(title = "Initialize")]
27    Initialize {
28        mcp_kind: super::super::McpKind,
29        #[serde(flatten)]
30        result: JsonRpcResult<InitializeReply>,
31    },
32
33    /// Reply to [`super::super::server_request::Payload::ToolsList`].
34    #[schemars(title = "ToolsList")]
35    ToolsList {
36        mcp_kind: super::super::McpKind,
37        #[serde(flatten)]
38        result: JsonRpcResult<crate::mcp::tool::ListToolsResult>,
39    },
40
41    /// Reply to [`super::super::server_request::Payload::ToolsCall`].
42    #[schemars(title = "ToolsCall")]
43    ToolsCall {
44        mcp_kind: super::super::McpKind,
45        #[serde(flatten)]
46        result: JsonRpcResult<crate::mcp::tool::CallToolResult>,
47    },
48
49    /// Reply to
50    /// [`super::super::server_request::Payload::ResourcesList`].
51    #[schemars(title = "ResourcesList")]
52    ResourcesList {
53        mcp_kind: super::super::McpKind,
54        #[serde(flatten)]
55        result: JsonRpcResult<crate::mcp::resource::ListResourcesResult>,
56    },
57
58    /// Reply to
59    /// [`super::super::server_request::Payload::ResourcesRead`].
60    #[schemars(title = "ResourcesRead")]
61    ResourcesRead {
62        mcp_kind: super::super::McpKind,
63        #[serde(flatten)]
64        result: JsonRpcResult<crate::mcp::resource::ReadResourceResult>,
65    },
66
67    /// Acknowledges
68    /// [`super::super::server_request::Payload::SessionTerminate`].
69    /// On success carries the unit value (no body); on failure
70    /// carries the upstream-delete error so the proxy sees a
71    /// non-2xx and can retry.
72    #[schemars(title = "SessionTerminate")]
73    SessionTerminate {
74        mcp_kind: super::super::McpKind,
75        #[serde(flatten)]
76        result: JsonRpcResult<()>,
77    },
78
79    /// Reply to
80    /// [`super::super::server_request::Payload::ReadMessageQueue`].
81    /// On success carries every matching queue row's id + body in
82    /// oldest-first order; on failure surfaces the local storage
83    /// error so the API can decide whether to retry. Non-MCP — no
84    /// `mcp_kind` to echo.
85    #[schemars(title = "ReadMessageQueue")]
86    ReadMessageQueue(JsonRpcResult<ReadMessageQueueResult>),
87}
88
89impl Payload {
90    /// Which CLI-hosted MCP server produced this reply. `Some` for
91    /// MCP-routed variants (echoes the request's `mcp_kind`); `None`
92    /// for `ReadMessageQueue`.
93    pub fn mcp_kind(&self) -> Option<super::super::McpKind> {
94        match self {
95            Payload::Initialize { mcp_kind, .. }
96            | Payload::ToolsList { mcp_kind, .. }
97            | Payload::ToolsCall { mcp_kind, .. }
98            | Payload::ResourcesList { mcp_kind, .. }
99            | Payload::ResourcesRead { mcp_kind, .. }
100            | Payload::SessionTerminate { mcp_kind, .. } => Some(mcp_kind.clone()),
101            Payload::ReadMessageQueue(_) => None,
102        }
103    }
104}
105
106/// Successful payload for [`Payload::ReadMessageQueue`].
107///
108/// One [`ReadMessageQueueRow`] per consumed `message_queue` row,
109/// in oldest-first id order. Each row's `content_ids` are the
110/// `message_queue_contents.id`s for that row's slots; the row's
111/// `rich_content` is the CLI's reconstructed payload for that
112/// row alone (no cross-row separator splicing — callers join if
113/// they want a unified User message).
114///
115/// Two consumers:
116/// - **Startup snapshot** (`run_agent_loop`): joins every row's
117///   parts with `"\n\n"` separators, flattens `content_ids`, and
118///   stamps the result onto the first
119///   `AssistantResponseChunk.request_message_ids`.
120/// - **`ApiQueueDelegate`** (`agents logs read subscribe`-style
121///   per-tool-response delivery): keeps rows separate so each
122///   gets converted to its own `Vec<ContentBlock>` and surfaces
123///   row-by-row on tool responses.
124///
125/// The downstream LogWriter resolves each id's kind at write
126/// time (SQL CASE against `message_queue_contents.kind`) to
127/// dispatch the right `logs.message_table` variant — kinds don't
128/// need to ride on the wire.
129#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
130#[schemars(rename = "client_objectiveai_mcp.server_response.ReadMessageQueueResult")]
131pub struct ReadMessageQueueResult {
132    pub rows: Vec<ReadMessageQueueRow>,
133}
134
135/// One queued row's payload + its content-slot ids.
136#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
137#[schemars(rename = "client_objectiveai_mcp.server_response.ReadMessageQueueRow")]
138pub struct ReadMessageQueueRow {
139    /// `message_queue_contents.id` of every slot in this row, in
140    /// part order. Matches `rich_content`'s part count 1:1.
141    pub content_ids: Vec<i64>,
142    /// The row's content as the CLI reconstructed it.
143    pub rich_content: crate::agent::completions::message::RichContent,
144}
145
146/// The successful `Initialize` payload — the upstream's verbatim
147/// `InitializeResult` plus the native `Mcp-Session-Id` the CLI got
148/// back from dialing the actual MCP server. The API forwards both
149/// to the proxy: the result as the JSON-RPC body, the session id as
150/// the `Mcp-Session-Id` response header. The CLI is a pure medium —
151/// it doesn't synthesize capabilities, doesn't name itself, doesn't
152/// pin a protocol version. Whatever the upstream MCP advertised is
153/// what the proxy sees.
154#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
155#[schemars(rename = "client_objectiveai_mcp.server_response.InitializeReply")]
156pub struct InitializeReply {
157    /// Upstream's native `Mcp-Session-Id`. One per CLI-hosted MCP
158    /// server — no aggregation, no encoding.
159    pub mcp_session_id: String,
160    /// The upstream's verbatim `InitializeResult` (capabilities,
161    /// server info, protocol version). Returned as-is to the proxy.
162    pub result: crate::mcp::initialize_result::InitializeResult,
163}
164
165/// JSON-RPC result/error shape for every typed method. Mirrors
166/// the wire shape upstream MCP servers return (`{result: …}` on
167/// success, `{error: {code, message, data?}}` on failure) but typed
168/// at the SDK level instead of buried inside a `serde_json::Value`.
169#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
170#[schemars(rename = "client_objectiveai_mcp.server_response.JsonRpcResult.{R}", bound = "R: JsonSchema")]
171#[serde(tag = "kind", rename_all = "snake_case")]
172pub enum JsonRpcResult<R> {
173    /// Method returned a typed result.
174    #[schemars(title = "Ok")]
175    Ok { result: R },
176    /// Method returned a JSON-RPC error envelope.
177    #[schemars(title = "Err")]
178    Err {
179        code: i64,
180        message: String,
181        #[serde(default, skip_serializing_if = "Option::is_none")]
182        #[schemars(extend("omitempty" = true))]
183        data: Option<serde_json::Value>,
184    },
185}