pmcp 2.2.0

High-quality Rust SDK for Model Context Protocol (MCP) with full TypeScript SDK compatibility
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
//! MCP-based foundation client implementation.
//!
//! This module provides `McpFoundationClient`, which uses the existing MCP
//! client infrastructure to connect to foundation servers over HTTP.

use super::{
    CompositionError, FoundationClient, FoundationConfig, FoundationEndpoint, PromptContent,
    PromptMessage, PromptResult, ResourceContent,
};
use async_trait::async_trait;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;
use url::Url;

use crate::shared::streamable_http::{StreamableHttpTransport, StreamableHttpTransportConfig};
use crate::types::ClientCapabilities;
use crate::Client;

/// Connection state for a foundation server.
struct FoundationConnection {
    /// The MCP client for this connection.
    client: Arc<tokio::sync::RwLock<Client<StreamableHttpTransport>>>,
    /// Whether the connection is initialized.
    initialized: bool,
}

/// MCP-based foundation client that connects to servers over HTTP.
///
/// This client maintains persistent connections to foundation servers,
/// initializing them once and reusing them for subsequent calls.
///
/// # Example
///
/// ```rust,ignore
/// use pmcp::composition::{McpFoundationClient, FoundationConfig};
///
/// // Create configuration
/// let config = FoundationConfig::with_foundation("calculator", "http://localhost:8080");
///
/// // Create client
/// let client = McpFoundationClient::new(config).await?;
///
/// // Call a tool
/// let result = client.call_tool(
///     "calculator",
///     "add",
///     &serde_json::json!({"a": 5, "b": 3})
/// ).await?;
/// ```
pub struct McpFoundationClient {
    /// Configuration for foundation servers.
    config: FoundationConfig,
    /// Active connections to foundation servers.
    connections: RwLock<HashMap<String, Arc<FoundationConnection>>>,
}

impl std::fmt::Debug for McpFoundationClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("McpFoundationClient")
            .field("config", &self.config)
            .field(
                "connected_servers",
                &self.connections.read().keys().cloned().collect::<Vec<_>>(),
            )
            .finish()
    }
}

impl McpFoundationClient {
    /// Create a new MCP foundation client with the given configuration.
    ///
    /// This does not immediately connect to any servers. Connections are
    /// established lazily when first used.
    pub fn new(config: FoundationConfig) -> Self {
        Self {
            config,
            connections: RwLock::new(HashMap::new()),
        }
    }

    /// Create a new client from a TOML configuration file.
    pub fn from_file(path: impl AsRef<std::path::Path>) -> Result<Self, CompositionError> {
        let config = FoundationConfig::from_file(path)?;
        Ok(Self::new(config))
    }

    /// Create a new client from environment variables.
    pub fn from_env() -> Self {
        let config = FoundationConfig::from_env();
        Self::new(config)
    }

    /// Create a client for a single foundation server.
    ///
    /// This is a convenience method for simple setups.
    pub fn for_server(server_id: impl Into<String>, url: impl Into<String>) -> Self {
        let config = FoundationConfig::with_foundation(server_id, url);
        Self::new(config)
    }

    /// Get or create a connection to a foundation server.
    async fn get_connection(
        &self,
        server_id: &str,
    ) -> Result<Arc<FoundationConnection>, CompositionError> {
        // Fast path: check if we already have a connection
        {
            let connections = self.connections.read();
            if let Some(conn) = connections.get(server_id) {
                if conn.initialized {
                    return Ok(conn.clone());
                }
            }
        }

        // Slow path: create a new connection
        let endpoint = self
            .config
            .get_endpoint(server_id)
            .ok_or_else(|| CompositionError::ServerNotFound(server_id.to_string()))?
            .clone();

        let conn = self.create_connection(server_id, &endpoint).await?;
        let conn = Arc::new(conn);

        // Store the connection
        {
            let mut connections = self.connections.write();
            connections.insert(server_id.to_string(), conn.clone());
        }

        Ok(conn)
    }

    /// Create a new connection to a foundation server.
    async fn create_connection(
        &self,
        server_id: &str,
        endpoint: &FoundationEndpoint,
    ) -> Result<FoundationConnection, CompositionError> {
        // Parse the URL
        let url = Url::parse(&endpoint.url).map_err(|e| {
            CompositionError::Configuration(format!("Invalid URL for {}: {}", server_id, e))
        })?;

        // Build transport configuration
        let mut transport_config = StreamableHttpTransportConfig {
            url,
            extra_headers: endpoint
                .headers
                .iter()
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect(),
            auth_provider: None,
            session_id: None,
            enable_json_response: endpoint.enable_json_response,
            on_resumption_token: None,
            http_middleware_chain: None,
        };

        // Add auth header if configured
        if let Some(token) = &endpoint.auth_token {
            transport_config
                .extra_headers
                .push(("Authorization".to_string(), format!("Bearer {}", token)));
        }

        // Create transport
        let transport = StreamableHttpTransport::new(transport_config);

        // Create MCP client
        let mut client = Client::new(transport);

        // Initialize the connection
        let capabilities = ClientCapabilities::minimal();
        client.initialize(capabilities).await.map_err(|e| {
            CompositionError::ConnectionFailed(format!(
                "Failed to initialize connection to {}: {}",
                server_id, e
            ))
        })?;

        Ok(FoundationConnection {
            client: Arc::new(tokio::sync::RwLock::new(client)),
            initialized: true,
        })
    }

    /// Extract text from tool result content.
    fn extract_tool_result_text(
        result: &crate::types::CallToolResult,
    ) -> Result<String, CompositionError> {
        // If the result is an error, return it as an error
        if result.is_error {
            let error_text = result
                .content
                .first()
                .and_then(|c| match c {
                    crate::types::Content::Text { text } => Some(text.clone()),
                    _ => None,
                })
                .unwrap_or_else(|| "Unknown error".to_string());
            return Err(CompositionError::ServerError {
                code: -1,
                message: error_text,
            });
        }

        // Extract text content
        for content in &result.content {
            if let crate::types::Content::Text { text } = content {
                return Ok(text.clone());
            }
        }

        // If no text content, try to serialize the whole result
        serde_json::to_string(&result.content)
            .map_err(|e| CompositionError::Serialization(e.to_string()))
    }
}

#[async_trait]
impl FoundationClient for McpFoundationClient {
    async fn call_tool(
        &self,
        server_id: &str,
        tool_name: &str,
        arguments: &serde_json::Value,
    ) -> Result<String, CompositionError> {
        let conn = self.get_connection(server_id).await?;
        let client = conn.client.read().await;

        let result = client
            .call_tool(tool_name.to_string(), arguments.clone())
            .await
            .map_err(|e| CompositionError::ToolCallFailed {
                server_id: server_id.to_string(),
                tool_name: tool_name.to_string(),
                message: e.to_string(),
            })?;

        Self::extract_tool_result_text(&result)
    }

    async fn read_resource(
        &self,
        server_id: &str,
        uri: &str,
    ) -> Result<ResourceContent, CompositionError> {
        let conn = self.get_connection(server_id).await?;
        let client = conn.client.read().await;

        let result = client.read_resource(uri.to_string()).await.map_err(|e| {
            CompositionError::ResourceReadFailed {
                server_id: server_id.to_string(),
                uri: uri.to_string(),
                message: e.to_string(),
            }
        })?;

        // Convert MCP resource content to our ResourceContent type
        if let Some(content) = result.contents.first() {
            match content {
                crate::types::Content::Text { text } => Ok(ResourceContent {
                    uri: uri.to_string(),
                    mime_type: None,
                    text: Some(text.clone()),
                    blob: None,
                }),
                crate::types::Content::Resource {
                    uri,
                    text,
                    mime_type,
                    ..
                } => Ok(ResourceContent {
                    uri: uri.clone(),
                    mime_type: mime_type.clone(),
                    text: text.clone(),
                    blob: None,
                }),
                crate::types::Content::Image { data, mime_type } => Ok(ResourceContent {
                    uri: uri.to_string(),
                    mime_type: Some(mime_type.clone()),
                    text: None,
                    blob: Some(data.clone()),
                }),
                crate::types::Content::Audio { .. } | crate::types::Content::ResourceLink(_) => {
                    Ok(ResourceContent {
                        uri: uri.to_string(),
                        mime_type: None,
                        text: None,
                        blob: None,
                    })
                },
            }
        } else {
            Err(CompositionError::InvalidResponse(
                "Empty resource content".to_string(),
            ))
        }
    }

    async fn get_prompt(
        &self,
        server_id: &str,
        prompt_name: &str,
        arguments: &serde_json::Value,
    ) -> Result<PromptResult, CompositionError> {
        let conn = self.get_connection(server_id).await?;
        let client = conn.client.read().await;

        // Convert JSON arguments to HashMap<String, String>
        let args: HashMap<String, String> = if arguments.is_object() {
            arguments
                .as_object()
                .unwrap()
                .iter()
                .map(|(k, v)| {
                    let value = if v.is_string() {
                        v.as_str().unwrap().to_string()
                    } else {
                        v.to_string()
                    };
                    (k.clone(), value)
                })
                .collect()
        } else {
            HashMap::new()
        };

        let result = client
            .get_prompt(prompt_name.to_string(), args)
            .await
            .map_err(|e| CompositionError::PromptFailed {
                server_id: server_id.to_string(),
                prompt_name: prompt_name.to_string(),
                message: e.to_string(),
            })?;

        // Convert MCP prompt result to our PromptResult type
        let messages = result
            .messages
            .into_iter()
            .map(|msg| {
                let role = match msg.role {
                    crate::types::Role::User => "user",
                    crate::types::Role::Assistant => "assistant",
                    crate::types::Role::System => "system",
                }
                .to_string();

                let content = match msg.content {
                    crate::types::Content::Text { text } => PromptContent::Text { text },
                    crate::types::Content::Image { data, mime_type } => {
                        PromptContent::Image { data, mime_type }
                    },
                    crate::types::Content::Resource {
                        uri,
                        text,
                        mime_type,
                        ..
                    } => PromptContent::Resource {
                        resource: super::types::EmbeddedResource {
                            uri,
                            mime_type,
                            text,
                            blob: None,
                        },
                    },
                    crate::types::Content::Audio { .. }
                    | crate::types::Content::ResourceLink(_) => PromptContent::Text {
                        text: String::new(),
                    },
                };

                PromptMessage { role, content }
            })
            .collect();

        Ok(PromptResult {
            description: result.description,
            messages,
        })
    }

    async fn is_available(&self, server_id: &str) -> bool {
        // Check if we have a configuration for this server
        if self.config.get_endpoint(server_id).is_none() {
            return false;
        }

        // Try to get or create a connection
        match self.get_connection(server_id).await {
            Ok(conn) => conn.initialized,
            Err(_) => false,
        }
    }

    fn foundation_ids(&self) -> Vec<String> {
        self.config.foundations.keys().cloned().collect()
    }
}

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

    #[test]
    fn test_for_server() {
        let client = McpFoundationClient::for_server("calculator", "http://localhost:8080");
        assert_eq!(client.foundation_ids(), vec!["calculator".to_string()]);
    }

    #[test]
    fn test_from_config() {
        let mut config = FoundationConfig::default();
        config.add_foundation("server1", FoundationEndpoint::new("http://localhost:8080"));
        config.add_foundation("server2", FoundationEndpoint::new("http://localhost:8081"));

        let client = McpFoundationClient::new(config);
        let ids = client.foundation_ids();
        assert!(ids.contains(&"server1".to_string()));
        assert!(ids.contains(&"server2".to_string()));
    }
}