agent-sdk 0.9.2

Rust Agent SDK for building LLM agents
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! MCP JSON-RPC protocol types.

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// JSON-RPC version string.
pub const JSONRPC_VERSION: &str = "2.0";

/// The MCP protocol revision this client prefers to negotiate.
///
/// MCP revisions are date-stamped. `2025-06-18` is the current stable
/// revision and supersedes the original `2024-11-05` this client was
/// previously pinned to. During the `initialize` handshake we advertise
/// this version; the server replies with the revision it actually selected
/// (which may be older), and we honour that for subsequent requests
/// (notably the `MCP-Protocol-Version` HTTP header).
pub const PREFERRED_PROTOCOL_VERSION: &str = "2025-06-18";

/// The oldest MCP revision this client interoperates with.
///
/// Servers that only speak `2024-11-05` are still supported: the client
/// adapts to whatever revision the server selects during initialization.
pub const MIN_PROTOCOL_VERSION: &str = "2024-11-05";

/// MCP protocol revisions this client knows about, newest first.
///
/// Used to decide whether a server-selected revision is one we recognise.
/// An unknown revision is not fatal — the client still proceeds — but it is
/// logged so operators can tell when a server negotiated something outside
/// this set.
pub const SUPPORTED_PROTOCOL_VERSIONS: &[&str] = &["2025-06-18", "2025-03-26", "2024-11-05"];

/// Returns `true` if `version` is a revision this client explicitly knows.
#[must_use]
pub fn is_known_protocol_version(version: &str) -> bool {
    SUPPORTED_PROTOCOL_VERSIONS.contains(&version)
}

/// JSON-RPC request.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JsonRpcRequest {
    /// JSON-RPC version (always "2.0").
    pub jsonrpc: String,
    /// Request method name.
    pub method: String,
    /// Request parameters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
    /// Request ID.
    pub id: RequestId,
}

impl JsonRpcRequest {
    /// Create a new JSON-RPC request.
    #[must_use]
    pub fn new(method: impl Into<String>, params: Option<Value>, id: u64) -> Self {
        Self {
            jsonrpc: JSONRPC_VERSION.to_string(),
            method: method.into(),
            params,
            id: RequestId::Number(id),
        }
    }
}

/// JSON-RPC request ID.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum RequestId {
    /// Numeric ID.
    Number(u64),
    /// String ID.
    String(String),
}

/// JSON-RPC response.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JsonRpcResponse {
    /// JSON-RPC version (always "2.0").
    pub jsonrpc: String,
    /// Response result (success case).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,
    /// Response error (error case).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<JsonRpcError>,
    /// Request ID this response corresponds to.
    pub id: RequestId,
}

impl JsonRpcResponse {
    /// Check if this response is an error.
    #[must_use]
    pub const fn is_error(&self) -> bool {
        self.error.is_some()
    }

    /// Get the result value, if present.
    #[must_use]
    pub const fn result(&self) -> Option<&Value> {
        self.result.as_ref()
    }
}

/// JSON-RPC error object.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JsonRpcError {
    /// Error code.
    pub code: i32,
    /// Error message.
    pub message: String,
    /// Additional error data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

/// Standard JSON-RPC error codes.
pub mod error_codes {
    /// Parse error - Invalid JSON.
    pub const PARSE_ERROR: i32 = -32700;
    /// Invalid Request - JSON is not a valid Request object.
    pub const INVALID_REQUEST: i32 = -32600;
    /// Method not found.
    pub const METHOD_NOT_FOUND: i32 = -32601;
    /// Invalid params.
    pub const INVALID_PARAMS: i32 = -32602;
    /// Internal error.
    pub const INTERNAL_ERROR: i32 = -32603;
}

/// MCP tool definition from server.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct McpToolDefinition {
    /// Tool name.
    pub name: String,
    /// Tool description.
    #[serde(default)]
    pub description: Option<String>,
    /// Input schema (JSON Schema).
    #[serde(rename = "inputSchema")]
    pub input_schema: Value,
}

/// MCP tool call result.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct McpToolCallResult {
    /// Content items returned by the tool.
    pub content: Vec<McpContent>,
    /// Whether this is an error result.
    #[serde(default, rename = "isError")]
    pub is_error: bool,
}

/// MCP content item.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum McpContent {
    /// Text content.
    #[serde(rename = "text")]
    Text {
        /// The text content.
        text: String,
    },
    /// Image content (base64 encoded).
    #[serde(rename = "image")]
    Image {
        /// Base64 encoded image data.
        data: String,
        /// MIME type of the image.
        #[serde(rename = "mimeType")]
        mime_type: String,
    },
    /// Resource reference.
    #[serde(rename = "resource")]
    Resource {
        /// Resource URI.
        uri: String,
        /// Resource MIME type.
        #[serde(rename = "mimeType")]
        mime_type: Option<String>,
        /// Optional text content.
        text: Option<String>,
    },
}

/// MCP server capabilities.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct McpServerCapabilities {
    /// Tool capabilities.
    #[serde(default)]
    pub tools: Option<McpToolsCapability>,
    /// Resource capabilities.
    #[serde(default)]
    pub resources: Option<McpResourcesCapability>,
    /// Prompt capabilities.
    #[serde(default)]
    pub prompts: Option<McpPromptsCapability>,
}

/// Tool capabilities.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct McpToolsCapability {
    /// Whether tools list can change.
    #[serde(default, rename = "listChanged")]
    pub list_changed: bool,
}

/// Resource capabilities.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct McpResourcesCapability {
    /// Whether subscriptions are supported.
    #[serde(default)]
    pub subscribe: bool,
    /// Whether resource list can change.
    #[serde(default, rename = "listChanged")]
    pub list_changed: bool,
}

/// Prompt capabilities.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct McpPromptsCapability {
    /// Whether prompts list can change.
    #[serde(default, rename = "listChanged")]
    pub list_changed: bool,
}

/// Initialize request params.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InitializeParams {
    /// Protocol version.
    #[serde(rename = "protocolVersion")]
    pub protocol_version: String,
    /// Client capabilities.
    pub capabilities: ClientCapabilities,
    /// Client info.
    #[serde(rename = "clientInfo")]
    pub client_info: ClientInfo,
}

/// Client capabilities.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ClientCapabilities {
    /// Roots capability.
    #[serde(default)]
    pub roots: Option<RootsCapability>,
    /// Sampling capability.
    #[serde(default)]
    pub sampling: Option<SamplingCapability>,
}

/// Roots capability.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct RootsCapability {
    /// Whether list can change.
    #[serde(default, rename = "listChanged")]
    pub list_changed: bool,
}

/// Sampling capability.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SamplingCapability {}

/// Client info.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ClientInfo {
    /// Client name.
    pub name: String,
    /// Client version.
    pub version: String,
}

/// Initialize response result.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InitializeResult {
    /// Protocol version.
    #[serde(rename = "protocolVersion")]
    pub protocol_version: String,
    /// Server capabilities.
    pub capabilities: McpServerCapabilities,
    /// Server info.
    #[serde(rename = "serverInfo")]
    pub server_info: ServerInfo,
}

/// Server info.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ServerInfo {
    /// Server name.
    pub name: String,
    /// Server version.
    #[serde(default)]
    pub version: Option<String>,
}

/// Tools list response.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ToolsListResult {
    /// List of available tools.
    pub tools: Vec<McpToolDefinition>,
}

/// Tool call params.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ToolCallParams {
    /// Tool name.
    pub name: String,
    /// Tool arguments.
    #[serde(default)]
    pub arguments: Option<Value>,
}

// ── Resources ──────────────────────────────────────────────────────────

/// An MCP resource descriptor as returned by `resources/list`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct McpResource {
    /// Resource URI (used to read the resource via `resources/read`).
    pub uri: String,
    /// Human-readable resource name.
    #[serde(default)]
    pub name: Option<String>,
    /// Optional resource description.
    #[serde(default)]
    pub description: Option<String>,
    /// MIME type of the resource contents, if known.
    #[serde(default, rename = "mimeType")]
    pub mime_type: Option<String>,
}

/// `resources/list` response.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ResourcesListResult {
    /// Available resources.
    pub resources: Vec<McpResource>,
    /// Opaque cursor for pagination, if the server returned more pages.
    #[serde(default, rename = "nextCursor")]
    pub next_cursor: Option<String>,
}

/// `resources/read` params.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ResourceReadParams {
    /// URI of the resource to read.
    pub uri: String,
}

/// The contents of a single resource returned by `resources/read`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct McpResourceContents {
    /// Resource URI.
    pub uri: String,
    /// MIME type of the contents, if known.
    #[serde(default, rename = "mimeType")]
    pub mime_type: Option<String>,
    /// Text contents (mutually exclusive with `blob`).
    #[serde(default)]
    pub text: Option<String>,
    /// Base64-encoded binary contents (mutually exclusive with `text`).
    #[serde(default)]
    pub blob: Option<String>,
}

/// `resources/read` response.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ResourceReadResult {
    /// One or more content blocks for the requested resource.
    pub contents: Vec<McpResourceContents>,
}

// ── Prompts ────────────────────────────────────────────────────────────

/// A prompt argument descriptor from `prompts/list`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct McpPromptArgument {
    /// Argument name.
    pub name: String,
    /// Optional argument description.
    #[serde(default)]
    pub description: Option<String>,
    /// Whether the argument is required.
    #[serde(default)]
    pub required: bool,
}

/// An MCP prompt descriptor as returned by `prompts/list`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct McpPrompt {
    /// Prompt name (used to fetch the prompt via `prompts/get`).
    pub name: String,
    /// Optional prompt description.
    #[serde(default)]
    pub description: Option<String>,
    /// Declared prompt arguments.
    #[serde(default)]
    pub arguments: Vec<McpPromptArgument>,
}

/// `prompts/list` response.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PromptsListResult {
    /// Available prompts.
    pub prompts: Vec<McpPrompt>,
    /// Opaque cursor for pagination, if the server returned more pages.
    #[serde(default, rename = "nextCursor")]
    pub next_cursor: Option<String>,
}

/// `prompts/get` params.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PromptGetParams {
    /// Name of the prompt to fetch.
    pub name: String,
    /// Arguments to interpolate into the prompt template.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub arguments: Option<Value>,
}

/// The role of a prompt message.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum McpRole {
    /// A user-authored message.
    User,
    /// An assistant-authored message.
    Assistant,
}

/// A single message in a rendered prompt.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct McpPromptMessage {
    /// Message role.
    pub role: McpRole,
    /// Message content block.
    pub content: McpContent,
}

/// `prompts/get` response.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PromptGetResult {
    /// Optional prompt description.
    #[serde(default)]
    pub description: Option<String>,
    /// The rendered prompt messages.
    pub messages: Vec<McpPromptMessage>,
}

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

    #[test]
    fn test_json_rpc_request_serialization() {
        let request =
            JsonRpcRequest::new("test_method", Some(serde_json::json!({"key": "value"})), 1);

        let json = serde_json::to_string(&request).expect("serialize");
        assert!(json.contains("test_method"));
        assert!(json.contains("2.0"));
    }

    #[test]
    fn test_json_rpc_response_success() {
        let response = JsonRpcResponse {
            jsonrpc: JSONRPC_VERSION.to_string(),
            result: Some(serde_json::json!({"success": true})),
            error: None,
            id: RequestId::Number(1),
        };

        assert!(!response.is_error());
        assert!(response.result().is_some());
    }

    #[test]
    fn test_json_rpc_response_error() {
        let response = JsonRpcResponse {
            jsonrpc: JSONRPC_VERSION.to_string(),
            result: None,
            error: Some(JsonRpcError {
                code: error_codes::METHOD_NOT_FOUND,
                message: "Method not found".to_string(),
                data: None,
            }),
            id: RequestId::Number(1),
        };

        assert!(response.is_error());
        assert!(response.result().is_none());
    }

    #[test]
    fn test_mcp_tool_definition_deserialization() {
        let json = r#"{
            "name": "test_tool",
            "description": "A test tool",
            "inputSchema": {
                "type": "object",
                "properties": {}
            }
        }"#;

        let tool: McpToolDefinition = serde_json::from_str(json).expect("deserialize");
        assert_eq!(tool.name, "test_tool");
        assert_eq!(tool.description.as_deref(), Some("A test tool"));
    }

    #[test]
    fn test_mcp_content_text() {
        let content = McpContent::Text {
            text: "Hello".to_string(),
        };

        let json = serde_json::to_string(&content).expect("serialize");
        assert!(json.contains("text"));
        assert!(json.contains("Hello"));
    }

    #[test]
    fn test_request_id_variants() {
        let num_id = RequestId::Number(42);
        let str_id = RequestId::String("req-1".to_string());

        let json_num = serde_json::to_string(&num_id).expect("serialize");
        let json_str = serde_json::to_string(&str_id).expect("serialize");

        assert_eq!(json_num, "42");
        assert_eq!(json_str, "\"req-1\"");
    }

    #[test]
    fn preferred_protocol_version_is_newer_than_floor() {
        // The preferred revision must be recognised and must not be the
        // legacy floor — otherwise we never moved off `2024-11-05`.
        assert_ne!(PREFERRED_PROTOCOL_VERSION, MIN_PROTOCOL_VERSION);
        assert!(is_known_protocol_version(PREFERRED_PROTOCOL_VERSION));
        assert!(is_known_protocol_version(MIN_PROTOCOL_VERSION));
        assert!(!is_known_protocol_version("1999-01-01"));
        // Newest-first ordering: the preferred revision leads the list.
        assert_eq!(SUPPORTED_PROTOCOL_VERSIONS[0], PREFERRED_PROTOCOL_VERSION);
    }

    #[test]
    fn test_resources_list_deserialization() {
        let json = r#"{
            "resources": [
                {"uri": "file:///a.txt", "name": "A", "mimeType": "text/plain"},
                {"uri": "mem://b"}
            ],
            "nextCursor": "page2"
        }"#;
        let parsed: ResourcesListResult = serde_json::from_str(json).expect("deserialize");
        assert_eq!(parsed.resources.len(), 2);
        assert_eq!(parsed.resources[0].uri, "file:///a.txt");
        assert_eq!(parsed.resources[0].mime_type.as_deref(), Some("text/plain"));
        assert_eq!(parsed.resources[1].name, None);
        assert_eq!(parsed.next_cursor.as_deref(), Some("page2"));
    }

    #[test]
    fn test_resource_read_text_and_blob() {
        let json = r#"{
            "contents": [
                {"uri": "file:///a.txt", "mimeType": "text/plain", "text": "hello"},
                {"uri": "file:///b.bin", "blob": "AAAA"}
            ]
        }"#;
        let parsed: ResourceReadResult = serde_json::from_str(json).expect("deserialize");
        assert_eq!(parsed.contents.len(), 2);
        assert_eq!(parsed.contents[0].text.as_deref(), Some("hello"));
        assert_eq!(parsed.contents[1].blob.as_deref(), Some("AAAA"));
        assert_eq!(parsed.contents[1].text, None);
    }

    #[test]
    fn test_prompts_list_deserialization() {
        let json = r#"{
            "prompts": [
                {
                    "name": "summarize",
                    "description": "Summarize text",
                    "arguments": [
                        {"name": "text", "required": true},
                        {"name": "tone", "description": "voice", "required": false}
                    ]
                }
            ]
        }"#;
        let parsed: PromptsListResult = serde_json::from_str(json).expect("deserialize");
        assert_eq!(parsed.prompts.len(), 1);
        assert_eq!(parsed.prompts[0].name, "summarize");
        assert_eq!(parsed.prompts[0].arguments.len(), 2);
        assert!(parsed.prompts[0].arguments[0].required);
        assert!(!parsed.prompts[0].arguments[1].required);
    }

    #[test]
    fn test_prompt_get_messages() {
        let json = r#"{
            "description": "rendered",
            "messages": [
                {"role": "user", "content": {"type": "text", "text": "hi"}},
                {"role": "assistant", "content": {"type": "text", "text": "hello"}}
            ]
        }"#;
        let parsed: PromptGetResult = serde_json::from_str(json).expect("deserialize");
        assert_eq!(parsed.messages.len(), 2);
        assert_eq!(parsed.messages[0].role, McpRole::User);
        assert_eq!(parsed.messages[1].role, McpRole::Assistant);
        match &parsed.messages[0].content {
            McpContent::Text { text } => assert_eq!(text, "hi"),
            _ => panic!("expected text content"),
        }
    }
}