claude-agent 0.2.25

Rust SDK for building AI agents with Anthropic's Claude - Direct API, no CLI dependency
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
//! MCP Client implementation using rmcp
//!
//! This module provides a high-level MCP client that wraps rmcp functionality.

#[cfg(feature = "mcp")]
use serde_json::Value;
#[cfg(feature = "mcp")]
use std::collections::HashMap;
#[cfg(feature = "mcp")]
use std::sync::Arc;
#[cfg(feature = "mcp")]
use tokio::sync::RwLock;

use super::{
    McpConnectionStatus, McpError, McpResourceDefinition, McpResult, McpServerConfig,
    McpServerState, McpToolDefinition, McpToolResult,
};
#[cfg(feature = "mcp")]
use super::{McpContent, McpServerInfo};

#[cfg(feature = "mcp")]
use rmcp::{
    RoleClient,
    model::{CallToolRequestParam, ReadResourceRequestParam},
    service::{RunningService, ServiceError, ServiceExt},
    transport::{ConfigureCommandExt, TokioChildProcess},
};
#[cfg(feature = "mcp")]
use tokio::process::Command;

#[cfg(feature = "mcp")]
type McpRunningService = RunningService<RoleClient, ()>;

/// Convert rmcp ServiceError into our McpError, preserving JSON-RPC error codes.
#[cfg(feature = "mcp")]
fn map_service_error(e: ServiceError, context: &str) -> McpError {
    match e {
        ServiceError::McpError(err_data) => McpError::JsonRpc {
            code: err_data.code.0,
            message: err_data.message.to_string(),
        },
        _ => McpError::Protocol {
            message: format!("{}: {}", context, e),
        },
    }
}

pub struct McpClient {
    name: String,
    state: McpServerState,
    #[cfg(feature = "mcp")]
    service: Option<Arc<RwLock<McpRunningService>>>,
    #[cfg(not(feature = "mcp"))]
    _phantom: std::marker::PhantomData<()>,
}

impl McpClient {
    pub fn new(name: impl Into<String>, config: McpServerConfig) -> Self {
        let name = name.into();
        Self {
            name: name.clone(),
            state: McpServerState::new(name, config),
            #[cfg(feature = "mcp")]
            service: None,
            #[cfg(not(feature = "mcp"))]
            _phantom: std::marker::PhantomData,
        }
    }

    #[cfg(feature = "mcp")]
    pub async fn connect(&mut self) -> McpResult<()> {
        match &self.state.config {
            McpServerConfig::Stdio {
                command, args, env, ..
            } => {
                self.connect_stdio(command.clone(), args.clone(), env.clone())
                    .await
            }
            McpServerConfig::Sse { url, headers } => {
                self.connect_sse(url.clone(), headers.clone()).await
            }
        }
    }

    #[cfg(not(feature = "mcp"))]
    pub async fn connect(&mut self) -> McpResult<()> {
        Err(McpError::Protocol {
            message: "MCP feature not enabled".to_string(),
        })
    }

    #[cfg(feature = "mcp")]
    async fn connect_stdio(
        &mut self,
        command: String,
        args: Vec<String>,
        env: HashMap<String, String>,
    ) -> McpResult<()> {
        use tokio::time::timeout;

        let transport = TokioChildProcess::new(Command::new(&command).configure(|cmd| {
            cmd.args(&args);
            for (key, value) in &env {
                cmd.env(key, value);
            }
        }))
        .map_err(|e| McpError::ConnectionFailed {
            message: format!("Failed to create transport: {}", e),
        })?;

        let service: McpRunningService = timeout(super::MCP_CONNECT_TIMEOUT, ().serve(transport))
            .await
            .map_err(|_| McpError::ConnectionFailed {
                message: format!(
                    "Connection timed out after {:?}",
                    super::MCP_CONNECT_TIMEOUT
                ),
            })?
            .map_err(|e| McpError::ConnectionFailed {
                message: format!("Failed to connect: {}", e),
            })?;

        if let Some(info) = service.peer_info() {
            let protocol_version = info.protocol_version.to_string();

            if !super::SUPPORTED_PROTOCOL_VERSIONS.contains(&protocol_version.as_str()) {
                tracing::warn!(
                    server = %self.name,
                    server_version = %protocol_version,
                    supported = ?super::SUPPORTED_PROTOCOL_VERSIONS,
                    "MCP protocol version mismatch"
                );
            }

            self.state.server_info = Some(McpServerInfo {
                name: info.server_info.name.to_string(),
                version: info.server_info.version.to_string(),
                protocol_version,
            });
        }
        self.state.status = McpConnectionStatus::Connected;

        let tools_result = service
            .list_tools(Default::default())
            .await
            .map_err(|e| map_service_error(e, "Failed to list tools"))?;

        self.state.tools = tools_result
            .tools
            .into_iter()
            .map(|t| McpToolDefinition {
                name: t.name.to_string(),
                description: t.description.map(|d| d.to_string()).unwrap_or_default(),
                input_schema: serde_json::Value::Object((*t.input_schema).clone()),
            })
            .collect();

        if let Ok(resources_result) = service.list_resources(Default::default()).await {
            self.state.resources = resources_result
                .resources
                .into_iter()
                .map(|r| McpResourceDefinition {
                    uri: r.raw.uri,
                    name: r.raw.name,
                    description: r.raw.description,
                    mime_type: r.raw.mime_type,
                })
                .collect();
        }

        self.service = Some(Arc::new(RwLock::new(service)));

        Ok(())
    }

    /// Connect via SSE transport.
    ///
    /// SSE transport is not yet supported by the rmcp crate.
    /// The MCP specification has moved to Streamable HTTP as the preferred
    /// remote transport. Use stdio transport for local servers.
    #[cfg(feature = "mcp")]
    async fn connect_sse(
        &mut self,
        url: String,
        _headers: HashMap<String, String>,
    ) -> McpResult<()> {
        Err(McpError::Protocol {
            message: format!(
                "SSE transport is not supported (url: {}). \
                 Use stdio transport for local MCP servers, or wait for \
                 Streamable HTTP transport support.",
                url
            ),
        })
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn state(&self) -> &McpServerState {
        &self.state
    }

    pub fn is_connected(&self) -> bool {
        self.state.is_connected()
    }

    pub fn tools(&self) -> &[McpToolDefinition] {
        &self.state.tools
    }

    pub fn resources(&self) -> &[McpResourceDefinition] {
        &self.state.resources
    }

    #[cfg(feature = "mcp")]
    pub async fn call_tool(&self, name: &str, arguments: Value) -> McpResult<McpToolResult> {
        use tokio::time::timeout;

        let service = self
            .service
            .as_ref()
            .ok_or_else(|| McpError::ConnectionFailed {
                message: "Not connected".to_string(),
            })?;

        let service = service.read().await;
        let result = timeout(
            super::MCP_CALL_TIMEOUT,
            service.call_tool(CallToolRequestParam {
                name: name.to_string().into(),
                arguments: arguments.as_object().cloned(),
            }),
        )
        .await
        .map_err(|_| McpError::ToolError {
            message: format!("Tool call timed out after {:?}", super::MCP_CALL_TIMEOUT),
        })?
        .map_err(|e| map_service_error(e, "Tool call failed"))?;

        let content = result
            .content
            .into_iter()
            .map(|c| {
                // Content in rmcp 0.12 is Annotated<RawContent>
                // Access the raw content through the raw field
                match &c.raw {
                    rmcp::model::RawContent::Text(t) => McpContent::Text {
                        text: t.text.clone(),
                    },
                    rmcp::model::RawContent::Image(i) => McpContent::Image {
                        data: i.data.clone(),
                        mime_type: i.mime_type.clone(),
                    },
                    rmcp::model::RawContent::Resource(r) => {
                        // ResourceContents is an enum with TextResourceContents and BlobResourceContents
                        match &r.resource {
                            rmcp::model::ResourceContents::TextResourceContents {
                                uri,
                                mime_type,
                                text,
                                ..
                            } => McpContent::Resource {
                                uri: uri.clone(),
                                text: Some(text.clone()),
                                blob: None,
                                mime_type: mime_type.clone(),
                            },
                            rmcp::model::ResourceContents::BlobResourceContents {
                                uri,
                                mime_type,
                                blob,
                                ..
                            } => McpContent::Resource {
                                uri: uri.clone(),
                                text: None,
                                blob: Some(blob.clone()),
                                mime_type: mime_type.clone(),
                            },
                        }
                    }
                    rmcp::model::RawContent::Audio(_) => {
                        // Audio content not yet fully supported, return as text placeholder
                        McpContent::Text {
                            text: "[Audio content]".to_string(),
                        }
                    }
                    rmcp::model::RawContent::ResourceLink(r) => McpContent::Resource {
                        uri: r.uri.clone(),
                        text: None,
                        blob: None,
                        mime_type: r.mime_type.clone(),
                    },
                }
            })
            .collect();

        Ok(McpToolResult {
            content,
            is_error: result.is_error.unwrap_or(false),
        })
    }

    #[cfg(not(feature = "mcp"))]
    pub async fn call_tool(
        &self,
        _name: &str,
        _arguments: serde_json::Value,
    ) -> McpResult<McpToolResult> {
        Err(McpError::Protocol {
            message: "MCP feature not enabled".to_string(),
        })
    }

    #[cfg(feature = "mcp")]
    pub async fn read_resource(&self, uri: &str) -> McpResult<Vec<McpContent>> {
        use tokio::time::timeout;

        let service = self
            .service
            .as_ref()
            .ok_or_else(|| McpError::ConnectionFailed {
                message: "Not connected".to_string(),
            })?;

        let service = service.read().await;
        let result = timeout(
            super::MCP_RESOURCE_TIMEOUT,
            service.read_resource(ReadResourceRequestParam { uri: uri.into() }),
        )
        .await
        .map_err(|_| McpError::ResourceNotFound {
            uri: format!("{}: timed out after {:?}", uri, super::MCP_RESOURCE_TIMEOUT),
        })?
        .map_err(|e| map_service_error(e, &format!("Resource read failed for {}", uri)))?;

        Ok(result
            .contents
            .into_iter()
            .map(|c| match c {
                rmcp::model::ResourceContents::TextResourceContents {
                    uri,
                    text,
                    mime_type,
                    ..
                } => McpContent::Resource {
                    uri,
                    text: Some(text),
                    blob: None,
                    mime_type,
                },
                rmcp::model::ResourceContents::BlobResourceContents {
                    uri,
                    blob,
                    mime_type,
                    ..
                } => McpContent::Resource {
                    uri,
                    text: None,
                    blob: Some(blob),
                    mime_type,
                },
            })
            .collect())
    }

    #[cfg(not(feature = "mcp"))]
    pub async fn read_resource(&self, _uri: &str) -> McpResult<Vec<super::McpContent>> {
        Err(McpError::Protocol {
            message: "MCP feature not enabled".to_string(),
        })
    }

    #[cfg(feature = "mcp")]
    pub async fn close(&mut self) -> McpResult<()> {
        if let Some(service_arc) = self.service.take() {
            match Arc::try_unwrap(service_arc) {
                Ok(service_rwlock) => {
                    let service = service_rwlock.into_inner();
                    service.cancel().await.map_err(|e| McpError::Protocol {
                        message: format!("Failed to cancel: {}", e),
                    })?;
                    self.state.status = McpConnectionStatus::Disconnected;
                }
                Err(arc) => {
                    tracing::debug!(
                        server = %self.name,
                        refs = Arc::strong_count(&arc),
                        "MCP service has active references, deferring cleanup"
                    );
                    self.service = Some(arc);
                    return Err(McpError::Protocol {
                        message: "Cannot close: service has active references".to_string(),
                    });
                }
            }
        } else {
            self.state.status = McpConnectionStatus::Disconnected;
        }
        Ok(())
    }

    #[cfg(not(feature = "mcp"))]
    pub async fn close(&mut self) -> McpResult<()> {
        self.state.status = McpConnectionStatus::Disconnected;
        Ok(())
    }
}

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

    #[test]
    fn test_mcp_client_new() {
        let client = McpClient::new(
            "test",
            McpServerConfig::Stdio {
                command: "echo".to_string(),
                args: vec![],
                env: std::collections::HashMap::new(),
                cwd: None,
            },
        );

        assert_eq!(client.name(), "test");
        assert!(!client.is_connected());
    }
}