objectiveai-sdk 2.2.16

ObjectiveAI SDK, definitions, and utilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! The JSON-RPC request frame — typed, method-tagged, derive-only.
//!
//! Every variant carries the FULL frame (`jsonrpc`, `id`, `method`,
//! `params`) — there is no envelope type. The `method` field of each
//! typed variant is a single-variant enum that only (de)serializes its
//! one exact method string, so `#[serde(untagged)]` trying variants in
//! order IS the method dispatch: a frame with a different method fails
//! that variant's `method` field and falls through.
//!
//! The LAST variant, [`JsonRpcRequest::Fallback`], is total over
//! well-formed id-bearing frames: `method` is [`ClientRequestMethod`]
//! (the union of every known method marker plus a `String` catch-all)
//! and `params` is raw JSON. A frame lands there in exactly two cases,
//! distinguishable by matching the fallback's `method`:
//!
//! - a KNOWN method marker → the method exists but its params were
//!   missing/malformed (receivers answer `-32602` WITH the id);
//! - [`ClientRequestMethod::Other`] → unknown method (`-32601`).
//!
//! Either way the id survives, so no valid frame ever loses its id to
//! a parse failure.

use schemars::JsonSchema;

use super::RequestId;

macro_rules! method_marker {
    ($(#[$doc:meta])* $name:ident, $variant:ident, $method:literal) => {
        $(#[$doc])*
        #[derive(
            Debug,
            Clone,
            Copy,
            PartialEq,
            Eq,
            serde::Serialize,
            serde::Deserialize,
            JsonSchema,
        )]
        pub enum $name {
            #[serde(rename = $method)]
            $variant,
        }

        impl $name {
            pub const METHOD: &'static str = $method;
        }
    };
}
pub(super) use method_marker;

method_marker!(
    /// The literal `"initialize"`.
    InitializeMethod, Initialize, "initialize"
);
method_marker!(
    /// The literal `"ping"`.
    PingMethod, Ping, "ping"
);
method_marker!(
    /// The literal `"tools/list"`.
    ListToolsMethod, ListTools, "tools/list"
);
method_marker!(
    /// The literal `"tools/call"`.
    CallToolMethod, CallTool, "tools/call"
);
method_marker!(
    /// The literal `"resources/list"`.
    ListResourcesMethod, ListResources, "resources/list"
);
method_marker!(
    /// The literal `"resources/read"`.
    ReadResourceMethod, ReadResource, "resources/read"
);

/// The method of a [`JsonRpcRequest::Fallback`] frame: every known
/// request method marker, then a `String` catch-all. Matching a known
/// marker here means "known method, unusable params" (→ `-32602`);
/// [`ClientRequestMethod::Other`] means "unknown method" (→ `-32601`).
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "mcp.ClientRequestMethod")]
pub enum ClientRequestMethod {
    Initialize(InitializeMethod),
    Ping(PingMethod),
    ListTools(ListToolsMethod),
    CallTool(CallToolMethod),
    ListResources(ListResourcesMethod),
    ReadResource(ReadResourceMethod),
    Other(String),
}

impl ClientRequestMethod {
    /// The wire method string.
    pub fn as_str(&self) -> &str {
        match self {
            ClientRequestMethod::Initialize(_) => InitializeMethod::METHOD,
            ClientRequestMethod::Ping(_) => PingMethod::METHOD,
            ClientRequestMethod::ListTools(_) => ListToolsMethod::METHOD,
            ClientRequestMethod::CallTool(_) => CallToolMethod::METHOD,
            ClientRequestMethod::ListResources(_) => {
                ListResourcesMethod::METHOD
            }
            ClientRequestMethod::ReadResource(_) => {
                ReadResourceMethod::METHOD
            }
            ClientRequestMethod::Other(method) => method,
        }
    }
}

/// A typed client → server JSON-RPC request frame. See the module docs
/// for the dispatch rules; use the constructor methods to build frames
/// without spelling out the marker fields.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "mcp.JsonRpcRequest")]
pub enum JsonRpcRequest {
    Initialize {
        jsonrpc: String,
        id: RequestId,
        method: InitializeMethod,
        params: InitializeRequestParams,
    },
    /// Params are ignored (and tolerated) per spec.
    Ping {
        jsonrpc: String,
        id: RequestId,
        method: PingMethod,
    },
    ListTools {
        jsonrpc: String,
        id: RequestId,
        method: ListToolsMethod,
        /// Absent params are legal — an absent cursor.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        #[schemars(extend("omitempty" = true))]
        params: Option<crate::mcp::tool::ListToolsRequest>,
    },
    CallTool {
        jsonrpc: String,
        id: RequestId,
        method: CallToolMethod,
        params: crate::mcp::tool::CallToolRequestParams,
    },
    ListResources {
        jsonrpc: String,
        id: RequestId,
        method: ListResourcesMethod,
        /// Absent params are legal — an absent cursor.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        #[schemars(extend("omitempty" = true))]
        params: Option<crate::mcp::resource::ListResourcesRequest>,
    },
    ReadResource {
        jsonrpc: String,
        id: RequestId,
        method: ReadResourceMethod,
        params: crate::mcp::resource::ReadResourceRequestParams,
    },
    /// Total catch-all — known method with unusable params, or unknown
    /// method (see [`ClientRequestMethod`]). `params` is raw JSON here
    /// by definition: a frame only lands on this variant because its
    /// params could NOT be given a type.
    Fallback {
        jsonrpc: String,
        id: RequestId,
        method: ClientRequestMethod,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        #[schemars(extend("omitempty" = true))]
        params: Option<serde_json::Value>,
    },
}

impl JsonRpcRequest {
    pub fn initialize(id: RequestId, params: InitializeRequestParams) -> Self {
        JsonRpcRequest::Initialize {
            jsonrpc: "2.0".to_string(),
            id,
            method: InitializeMethod::Initialize,
            params,
        }
    }

    pub fn ping(id: RequestId) -> Self {
        JsonRpcRequest::Ping {
            jsonrpc: "2.0".to_string(),
            id,
            method: PingMethod::Ping,
        }
    }

    pub fn list_tools(
        id: RequestId,
        params: crate::mcp::tool::ListToolsRequest,
    ) -> Self {
        JsonRpcRequest::ListTools {
            jsonrpc: "2.0".to_string(),
            id,
            method: ListToolsMethod::ListTools,
            params: Some(params),
        }
    }

    pub fn call_tool(
        id: RequestId,
        params: crate::mcp::tool::CallToolRequestParams,
    ) -> Self {
        JsonRpcRequest::CallTool {
            jsonrpc: "2.0".to_string(),
            id,
            method: CallToolMethod::CallTool,
            params,
        }
    }

    pub fn list_resources(
        id: RequestId,
        params: crate::mcp::resource::ListResourcesRequest,
    ) -> Self {
        JsonRpcRequest::ListResources {
            jsonrpc: "2.0".to_string(),
            id,
            method: ListResourcesMethod::ListResources,
            params: Some(params),
        }
    }

    pub fn read_resource(
        id: RequestId,
        params: crate::mcp::resource::ReadResourceRequestParams,
    ) -> Self {
        JsonRpcRequest::ReadResource {
            jsonrpc: "2.0".to_string(),
            id,
            method: ReadResourceMethod::ReadResource,
            params,
        }
    }

    /// The frame's request id.
    pub fn id(&self) -> &RequestId {
        match self {
            JsonRpcRequest::Initialize { id, .. }
            | JsonRpcRequest::Ping { id, .. }
            | JsonRpcRequest::ListTools { id, .. }
            | JsonRpcRequest::CallTool { id, .. }
            | JsonRpcRequest::ListResources { id, .. }
            | JsonRpcRequest::ReadResource { id, .. }
            | JsonRpcRequest::Fallback { id, .. } => id,
        }
    }
}

/// Params of an `initialize` request.
///
/// Lenient on the receive side: only `protocolVersion` is required;
/// `capabilities` defaults and `clientInfo` may be absent.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[schemars(rename = "mcp.InitializeRequestParams")]
pub struct InitializeRequestParams {
    pub protocol_version: String,
    #[serde(default)]
    pub capabilities: ClientCapabilities,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[schemars(extend("omitempty" = true))]
    pub client_info: Option<crate::mcp::initialize_result::Implementation>,
}

/// Capabilities a client declares at `initialize`. We declare none —
/// this serializes as `{}` — and the struct exists so the field is
/// typed and extensible rather than raw JSON.
#[derive(
    Debug, Clone, Default, serde::Serialize, serde::Deserialize, JsonSchema,
)]
#[schemars(rename = "mcp.ClientCapabilities")]
pub struct ClientCapabilities {}

#[cfg(test)]
mod tests {
    use super::*;

    fn number_id(n: u64) -> RequestId {
        RequestId::Number(n.into())
    }

    /// The typed initialize frame serializes to the exact legacy
    /// `json!` literal `client.rs` used to build by hand.
    #[test]
    fn initialize_matches_legacy_frame() {
        let request = JsonRpcRequest::initialize(
            number_id(1),
            InitializeRequestParams {
                protocol_version: "2025-06-18".to_string(),
                capabilities: ClientCapabilities::default(),
                client_info: Some(
                    crate::mcp::initialize_result::Implementation {
                        name: "objectiveai".to_string(),
                        title: None,
                        version: "1.2.3".to_string(),
                        website_url: None,
                        description: None,
                        icons: None,
                    },
                ),
            },
        );
        assert_eq!(
            serde_json::to_value(&request).unwrap(),
            serde_json::json!({
                "jsonrpc": "2.0",
                "id": 1,
                "method": "initialize",
                "params": {
                    "protocolVersion": "2025-06-18",
                    "capabilities": {},
                    "clientInfo": {
                        "name": "objectiveai",
                        "version": "1.2.3",
                    },
                },
            }),
        );
    }

    /// Round-trip every constructor through the wire.
    #[test]
    fn round_trip() {
        let requests = [
            JsonRpcRequest::ping(number_id(2)),
            JsonRpcRequest::list_tools(
                RequestId::String("abc".to_string()),
                crate::mcp::tool::ListToolsRequest {
                    cursor: Some("c1".to_string()),
                },
            ),
            JsonRpcRequest::call_tool(
                number_id(3),
                crate::mcp::tool::CallToolRequestParams {
                    name: "echo".to_string(),
                    arguments: None,
                    _meta: None,
                    task: None,
                },
            ),
            JsonRpcRequest::read_resource(
                number_id(4),
                crate::mcp::resource::ReadResourceRequestParams {
                    uri: "file://x".to_string(),
                },
            ),
        ];
        for request in requests {
            let value = serde_json::to_value(&request).unwrap();
            let back: JsonRpcRequest =
                serde_json::from_str(&value.to_string()).unwrap();
            assert_eq!(back.id(), request.id());
            assert_eq!(serde_json::to_value(&back).unwrap(), value);
            assert!(
                !matches!(back, JsonRpcRequest::Fallback { .. }),
                "constructor round-trip must land on its typed variant",
            );
        }
    }

    /// JSON objects are unordered — params before method must parse
    /// identically.
    #[test]
    fn field_order_independent() {
        let frame = serde_json::json!({
            "params": {"uri": "file://x"},
            "method": "resources/read",
            "id": 9,
            "jsonrpc": "2.0",
        });
        let parsed: JsonRpcRequest =
            serde_json::from_str(&frame.to_string()).unwrap();
        assert!(matches!(
            parsed,
            JsonRpcRequest::ReadResource { params, .. }
                if params.uri == "file://x",
        ));
    }

    /// Unknown methods land on Fallback with `Other` — the receiver
    /// answers -32601 with the id.
    #[test]
    fn unknown_method_is_fallback_other() {
        let frame = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 5,
            "method": "prompts/list",
            "params": {"cursor": null},
        });
        let parsed: JsonRpcRequest =
            serde_json::from_str(&frame.to_string()).unwrap();
        assert!(matches!(
            parsed,
            JsonRpcRequest::Fallback {
                method: ClientRequestMethod::Other(method),
                ..
            } if method == "prompts/list",
        ));
    }

    /// Known method + malformed params → Fallback with the KNOWN
    /// method marker and the id preserved — the receiver answers
    /// -32602 with the id.
    #[test]
    fn bad_params_is_fallback_with_known_method() {
        // tools/call with no params at all — missing required `name`.
        let frame = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 6,
            "method": "tools/call",
        });
        let parsed: JsonRpcRequest =
            serde_json::from_str(&frame.to_string()).unwrap();
        assert_eq!(parsed.id(), &number_id(6));
        assert!(matches!(
            parsed,
            JsonRpcRequest::Fallback {
                method: ClientRequestMethod::CallTool(_),
                ..
            },
        ));
    }

    /// tools/list with absent params parses onto its typed variant.
    #[test]
    fn list_tools_with_absent_params_parses() {
        let frame = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 7,
            "method": "tools/list",
        });
        let parsed: JsonRpcRequest =
            serde_json::from_str(&frame.to_string()).unwrap();
        assert!(matches!(
            parsed,
            JsonRpcRequest::ListTools { params: None, .. },
        ));
    }

    /// initialize params are lenient: only protocolVersion required.
    #[test]
    fn initialize_with_only_protocol_version_parses() {
        let frame = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 8,
            "method": "initialize",
            "params": {"protocolVersion": "2025-06-18"},
        });
        let parsed: JsonRpcRequest =
            serde_json::from_str(&frame.to_string()).unwrap();
        assert!(matches!(
            parsed,
            JsonRpcRequest::Initialize { params, .. }
                if params.protocol_version == "2025-06-18"
                    && params.client_info.is_none(),
        ));
    }

    /// Ping tolerates (and ignores) params, per spec.
    #[test]
    fn ping_ignores_params() {
        let frame = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 10,
            "method": "ping",
            "params": {"_meta": {"x": 1}},
        });
        let parsed: JsonRpcRequest =
            serde_json::from_str(&frame.to_string()).unwrap();
        assert!(matches!(parsed, JsonRpcRequest::Ping { .. }));
    }
}