rmcp 3.6.0

Rust SDK for Model Context Protocol
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
use rmcp::model::{JsonRpcResponse, ServerJsonRpcMessage, ServerResult};
#[test]
fn test_tool_list_result() {
    let json = std::fs::read("tests/test_deserialization/tool_list_result.json").unwrap();
    let result: ServerJsonRpcMessage = serde_json::from_slice(&json).unwrap();
    println!("{result:#?}");

    assert!(matches!(
        result,
        ServerJsonRpcMessage::Response(JsonRpcResponse {
            result: ServerResult::ListToolsResult(_),
            ..
        })
    ));
}

/// Regression tests for `#[serde(untagged)]` deserialization of `ServerResult`.
///
/// `ServerResult` is an untagged enum, so serde tries each variant in declaration
/// order, with `CustomResult(Value)` acting as the catch-all. If variant ordering
/// changes, these tests will catch the regression.
mod untagged_server_result {
    use rmcp::model::{CallToolResult, JsonRpcResponse, ServerJsonRpcMessage, ServerResult};
    use serde_json::json;

    /// Helper: wrap a result value in a JSON-RPC response envelope.
    fn wrap_response(result: serde_json::Value) -> serde_json::Value {
        json!({
            "jsonrpc": "2.0",
            "id": 1,
            "result": result
        })
    }

    /// Parse a JSON-RPC response and return the inner `ServerResult`.
    fn parse_result(json: serde_json::Value) -> ServerResult {
        let msg: ServerJsonRpcMessage = serde_json::from_value(json).unwrap();
        match msg {
            ServerJsonRpcMessage::Response(JsonRpcResponse { result, .. }) => result,
            other => panic!("expected Response, got {other:?}"),
        }
    }

    #[test]
    fn initialize_result_deserializes_to_correct_variant() {
        let result = parse_result(wrap_response(json!({
            "protocolVersion": "2025-03-26",
            "capabilities": {},
            "serverInfo": {
                "name": "test-server",
                "version": "1.0.0"
            }
        })));
        assert!(
            matches!(result, ServerResult::InitializeResult(_)),
            "expected InitializeResult, got {result:?}"
        );
    }

    #[test]
    fn call_tool_result_deserializes_to_correct_variant() {
        let result = parse_result(wrap_response(json!({
            "content": [
                { "type": "text", "text": "hello" }
            ]
        })));
        assert!(
            matches!(result, ServerResult::CallToolResult(_)),
            "expected CallToolResult, got {result:?}"
        );
    }

    #[test]
    fn call_tool_result_with_null_structured_content_deserializes_to_correct_variant() {
        for payload in [
            json!({
                "resultType": "complete",
                "content": [],
                "structuredContent": null
            }),
            json!({
                "resultType": "complete",
                "structuredContent": null
            }),
            json!({ "structuredContent": null }),
        ] {
            let result = parse_result(wrap_response(payload.clone()));
            let ServerResult::CallToolResult(result) = result else {
                panic!("{payload} should deserialize as CallToolResult, got {result:?}");
            };
            assert_eq!(result.structured_content, Some(serde_json::Value::Null));
        }
    }

    #[test]
    fn null_structured_content_does_not_shadow_custom_result() {
        // Counting a present `structuredContent: null` as a known field must not
        // make CallToolResult swallow other objects carrying only null values.
        let result = parse_result(wrap_response(json!({
            "somethingElse": null
        })));
        assert!(
            matches!(result, ServerResult::CustomResult(_)),
            "expected CustomResult, got {result:?}"
        );
    }

    #[test]
    fn input_required_result_with_meta_deserializes_to_correct_variant() {
        let result = parse_result(wrap_response(json!({
            "resultType": "input_required",
            "inputRequests": {
                "username": {
                    "method": "elicitation/create",
                    "params": {
                        "message": "Please provide your username",
                        "requestedSchema": {
                            "type": "object",
                            "properties": {
                                "username": { "type": "string" }
                            },
                            "required": ["username"]
                        }
                    }
                }
            },
            "requestState": "opaque-state",
            "_meta": {
                "io.modelcontextprotocol/serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            }
        })));

        let ServerResult::InputRequiredResult(result) = result else {
            panic!("expected InputRequiredResult, got {result:?}");
        };
        assert!(
            result.input_requests.is_some_and(|requests| {
                requests.len() == 1 && requests.contains_key("username")
            })
        );
        assert_eq!(result.request_state.as_deref(), Some("opaque-state"));
        assert_eq!(
            result
                .meta
                .as_ref()
                .and_then(|meta| meta.get("io.modelcontextprotocol/serverInfo")),
            Some(&json!({
                "name": "test-server",
                "version": "1.0.0"
            }))
        );
    }

    #[test]
    fn call_tool_result_rejects_input_required_discriminator() {
        assert!(
            serde_json::from_value::<CallToolResult>(json!({
                "resultType": "input_required",
                "requestState": "opaque-state",
                "_meta": {}
            }))
            .is_err()
        );
    }

    #[test]
    fn invalid_input_required_result_falls_through_to_custom_result() {
        let payload = json!({
            "resultType": "input_required",
            "_meta": {}
        });
        let result = parse_result(wrap_response(payload.clone()));

        let ServerResult::CustomResult(result) = result else {
            panic!("expected CustomResult, got {result:?}");
        };
        assert_eq!(result.0, payload);
    }

    #[test]
    fn empty_object_deserializes_to_empty_result() {
        let result = parse_result(wrap_response(json!({})));
        assert!(
            matches!(result, ServerResult::EmptyResult(_)),
            "expected EmptyResult, got {result:?}"
        );
    }

    #[test]
    fn unknown_shape_falls_through_to_custom_result() {
        // A value that doesn't match any known result type should land in
        // CustomResult.
        let result = parse_result(wrap_response(json!({
            "some_unknown_field": "some_value",
            "number": 42
        })));
        assert!(
            matches!(result, ServerResult::CustomResult(_)),
            "expected CustomResult, got {result:?}"
        );
    }

    #[test]
    fn result_type_bearing_objects_do_not_match_task_ack() {
        // TaskAckResult carries only `resultType` (+ optional `_meta`), so it
        // must not greedily swallow arbitrary results that happen to include
        // a `resultType` key inside the untagged ServerResult union.
        let result = parse_result(wrap_response(json!({
            "resultType": "weird-custom",
            "payload": { "a": 1 }
        })));
        assert!(
            matches!(result, ServerResult::CustomResult(_)),
            "expected CustomResult, got {result:?}"
        );

        let result = parse_result(wrap_response(json!({
            "resultType": "complete",
            "customField": 42
        })));
        assert!(
            matches!(result, ServerResult::CustomResult(_)),
            "expected CustomResult, got {result:?}"
        );

        // A bare complete ack (the actual tasks/update / tasks/cancel ack
        // shape) still parses as TaskAckResult.
        let result = parse_result(wrap_response(json!({ "resultType": "complete" })));
        assert!(
            matches!(result, ServerResult::TaskAckResult(_)),
            "expected TaskAckResult, got {result:?}"
        );
    }

    #[test]
    fn arbitrary_json_value_falls_through_to_custom_result() {
        // Any bare JSON value must fall through to CustomResult.
        for value in [json!(42), json!("hello"), json!(null), json!([1, 2, 3])] {
            let result = parse_result(wrap_response(value.clone()));
            assert!(
                matches!(result, ServerResult::CustomResult(_)),
                "value {value} should deserialize as CustomResult, got {result:?}"
            );
        }
    }

    #[test]
    fn round_trip_initialize_result_preserves_variant() {
        let json = json!({
            "protocolVersion": "2025-03-26",
            "capabilities": {},
            "serverInfo": { "name": "test", "version": "1.0" }
        });
        // Parse as ServerResult, serialize back, parse again — must stay InitializeResult.
        let result = parse_result(wrap_response(json.clone()));
        assert!(matches!(&result, ServerResult::InitializeResult(_)));
        let reserialized = serde_json::to_value(&result).unwrap();
        let result2 = parse_result(wrap_response(reserialized));
        assert!(matches!(result2, ServerResult::InitializeResult(_)));
    }

    #[test]
    fn round_trip_call_tool_result_preserves_variant() {
        let original =
            CallToolResult::success(vec![rmcp::model::ContentBlock::text("hello world")]);
        let json = serde_json::to_value(&original).unwrap();
        let result = parse_result(wrap_response(json));
        assert!(matches!(result, ServerResult::CallToolResult(_)));
    }
}

/// Regression tests for decimal float fields under serde_json's
/// `arbitrary_precision` feature, which rmcp's dev-dependencies enable.
///
/// With the feature, serde replays a decimal it buffered for an untagged enum
/// as serde_json's private number map. A plain `f32`/`f64` field rejected that
/// map, so each of these messages fell through to a `Custom*` variant. They are
/// decoded from text, which is where the map comes from.
mod arbitrary_precision {
    use rmcp::model::{
        ClientJsonRpcMessage, ClientNotification, ElicitRequestParams, JsonRpcMessage,
        JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, PrimitiveSchemaDefinition,
        ServerJsonRpcMessage, ServerNotification, ServerRequest, ServerResult,
    };

    /// Decodes a server message the way a client does (`RxJsonRpcMessage<RoleClient>`).
    fn from_server(text: &str) -> ServerJsonRpcMessage {
        serde_json::from_str(text).unwrap()
    }

    #[test]
    fn feature_is_enabled() {
        // Only the feature keeps the number as written; without it this reads `0.6`.
        let value: serde_json::Value = serde_json::from_str("0.60").unwrap();
        assert_eq!(value.to_string(), "0.60");
    }

    #[test]
    fn call_tool_result_with_fractional_priority() {
        let message = from_server(
            r#"{"jsonrpc":"2.0","id":1,"result":{"content":[
                {"type":"text","text":"ok","annotations":{"priority":0.6}}]}}"#,
        );
        let JsonRpcMessage::Response(JsonRpcResponse {
            result: ServerResult::CallToolResult(result),
            ..
        }) = message
        else {
            panic!("expected CallToolResult, got {message:?}");
        };
        let annotations = result.content[0].as_text().unwrap().annotations.as_ref();
        assert_eq!(annotations.unwrap().priority, Some(0.6));
    }

    #[test]
    fn progress_notification_with_fractional_progress() {
        let text = r#"{"jsonrpc":"2.0","method":"notifications/progress",
            "params":{"progressToken":"t","progress":0.5,"total":2.5}}"#;

        let message = from_server(text);
        let JsonRpcMessage::Notification(JsonRpcNotification {
            notification: ServerNotification::ProgressNotification(notification),
            ..
        }) = message
        else {
            panic!("expected ProgressNotification, got {message:?}");
        };
        assert_eq!(notification.params.progress, 0.5);
        assert_eq!(notification.params.total, Some(2.5));

        let message: ClientJsonRpcMessage = serde_json::from_str(text).unwrap();
        assert!(
            matches!(
                message,
                JsonRpcMessage::Notification(JsonRpcNotification {
                    notification: ClientNotification::ProgressNotification(_),
                    ..
                })
            ),
            "expected ProgressNotification, got {message:?}"
        );
    }

    #[test]
    #[expect(deprecated, reason = "sampling is deprecated by SEP-2577")]
    fn create_message_request_with_fractional_parameters() {
        let message = from_server(
            r#"{"jsonrpc":"2.0","id":2,"method":"sampling/createMessage","params":{
                "messages":[],"maxTokens":10,"temperature":0.7,"modelPreferences":{
                "costPriority":0.2,"speedPriority":0.5,"intelligencePriority":0.9}}}"#,
        );
        let JsonRpcMessage::Request(JsonRpcRequest {
            request: ServerRequest::CreateMessageRequest(request),
            ..
        }) = message
        else {
            panic!("expected CreateMessageRequest, got {message:?}");
        };
        assert_eq!(request.params.temperature, Some(0.7));
        let preferences = request.params.model_preferences.unwrap();
        assert_eq!(preferences.cost_priority, Some(0.2));
        assert_eq!(preferences.speed_priority, Some(0.5));
        assert_eq!(preferences.intelligence_priority, Some(0.9));
    }

    #[test]
    fn elicit_request_with_fractional_number_schema() {
        let message = from_server(
            r#"{"jsonrpc":"2.0","id":3,"method":"elicitation/create","params":{
                "mode":"form","message":"How much?","requestedSchema":{"type":"object",
                "properties":{"amount":{"type":"number",
                "minimum":0.5,"maximum":9.5,"default":1.5}}}}}"#,
        );
        let JsonRpcMessage::Request(JsonRpcRequest {
            request: ServerRequest::ElicitRequest(request),
            ..
        }) = message
        else {
            panic!("expected ElicitRequest, got {message:?}");
        };
        let ElicitRequestParams::FormElicitationParams {
            requested_schema, ..
        } = request.params
        else {
            panic!("expected a form elicitation");
        };
        let Some(PrimitiveSchemaDefinition::Number(amount)) =
            requested_schema.properties.get("amount")
        else {
            panic!("expected a number schema");
        };
        assert_eq!(amount.minimum, Some(0.5));
        assert_eq!(amount.maximum, Some(9.5));
        assert_eq!(amount.default, Some(1.5));
    }
}

/// A message that carries `id` and `method` is a request. When the `id` is not
/// one the SDK can represent, the message is invalid; it must not be read as a
/// notification, which would leave the peer waiting for a response.
mod request_id {
    use rmcp::model::{ClientJsonRpcMessage, JsonRpcMessage, NumberOrString};

    fn parse(id: &str, method: &str) -> Result<ClientJsonRpcMessage, serde_json::Error> {
        serde_json::from_str(&format!(
            r#"{{"jsonrpc":"2.0","id":{id},"method":"{method}"}}"#
        ))
    }

    #[test]
    fn request_with_unusable_id_is_rejected() {
        for id in [
            "9223372036854775808",
            "1.5",
            "9007199254740993.0",
            "true",
            "[1]",
            "{}",
        ] {
            for method in ["ping", "tools/list", "notifications/initialized"] {
                let result = parse(id, method);
                assert!(
                    result.is_err(),
                    "id {id} with method {method} parsed as {result:?}"
                );
            }
        }
    }

    #[test]
    fn integral_id_in_float_form_is_that_integer() {
        for (id, expected) in [("1.0", 1), ("1e2", 100), ("-3.0", -3)] {
            let JsonRpcMessage::Request(request) = parse(id, "ping").unwrap() else {
                panic!("id {id} did not parse as a request");
            };
            assert_eq!(request.id, NumberOrString::Number(expected), "id {id}");
        }
    }

    #[test]
    fn largest_i64_id_is_kept() {
        let JsonRpcMessage::Request(request) = parse("9223372036854775807", "ping").unwrap() else {
            panic!("expected a request");
        };
        assert_eq!(request.id, NumberOrString::Number(i64::MAX));
    }

    #[test]
    fn notification_still_parses() {
        let message: ClientJsonRpcMessage =
            serde_json::from_str(r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#)
                .unwrap();
        assert!(matches!(message, JsonRpcMessage::Notification(_)));
    }
}