mcpway 0.2.0

Run MCP stdio servers over SSE, WebSocket, Streamable HTTP, and gRPC transports.
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
use std::collections::HashMap;
use std::sync::Arc;

use serde_json::{json, Value};
use tokio::sync::Mutex;

use crate::tool_api::ergonomic::ErgonomicToolsFacade;
use crate::tool_api::error::ToolCallError;
use crate::tool_api::schema::{apply_defaults, validate_required};
use crate::tool_api::transport::TransportClient;

#[derive(Debug, Clone)]
pub struct ToolMetadata {
    pub name: String,
    pub description: Option<String>,
    pub input_schema: Value,
}

#[derive(Debug, Clone)]
pub struct ToolCatalogMetadata {
    pub name: String,
    pub description: Option<String>,
    pub required_keys: usize,
    pub defaulted_keys: usize,
}

#[derive(Clone)]
pub struct ToolClient {
    state: Arc<Mutex<ClientState>>,
}

pub struct ToolsFacade {
    client: ToolClient,
}

#[derive(Clone)]
pub struct ToolHandle {
    client: ToolClient,
    metadata: ToolMetadata,
}

pub(crate) struct ClientState {
    transport: TransportClient,
    protocol_version: String,
    request_seq: u64,
    initialized: bool,
    tools: Vec<ToolMetadata>,
    tools_by_name: HashMap<String, ToolMetadata>,
}

impl ToolClient {
    pub(crate) fn from_parts(transport: TransportClient, protocol_version: String) -> Self {
        Self {
            state: Arc::new(Mutex::new(ClientState {
                transport,
                protocol_version,
                request_seq: 0,
                initialized: false,
                tools: Vec::new(),
                tools_by_name: HashMap::new(),
            })),
        }
    }

    pub fn tools(&self) -> ToolsFacade {
        ToolsFacade {
            client: self.clone(),
        }
    }

    pub fn ergonomic(&self) -> ErgonomicToolsFacade {
        ErgonomicToolsFacade::new(self.clone())
    }

    pub async fn refresh_tools(&self) -> Result<(), ToolCallError> {
        let mut state = self.state.lock().await;
        state.ensure_initialized().await?;
        let response = state.send_jsonrpc_request("tools/list", json!({})).await?;
        let tools = parse_tools_list_response(&response)?;
        state.replace_tools(tools);
        Ok(())
    }

    async fn tool_by_name(&self, name: &str) -> Result<ToolMetadata, ToolCallError> {
        let state = self.state.lock().await;
        state.resolve_tool(name)
    }

    async fn resolve_tool_with_refresh(&self, name: &str) -> Result<ToolMetadata, ToolCallError> {
        match self.tool_by_name(name).await {
            Ok(metadata) => Ok(metadata),
            Err(ToolCallError::ToolNotFound { .. }) => {
                self.refresh_tools().await?;
                self.tool_by_name(name).await
            }
            Err(err) => Err(err),
        }
    }

    async fn list_tools(&self) -> Vec<ToolMetadata> {
        let state = self.state.lock().await;
        state.tools.clone()
    }

    pub async fn list_with_metadata(&self) -> Result<Vec<ToolCatalogMetadata>, ToolCallError> {
        self.refresh_tools().await?;
        let state = self.state.lock().await;
        Ok(state.list_with_metadata())
    }

    pub async fn prepare_args(&self, name: &str, args: Value) -> Result<Value, ToolCallError> {
        let metadata = self.resolve_tool_with_refresh(name).await?;
        normalize_args_for_tool(&metadata, args)
    }

    pub async fn call_by_name(&self, name: &str, args: Value) -> Result<Value, ToolCallError> {
        let metadata = self.resolve_tool_with_refresh(name).await?;
        self.call_tool(&metadata, args).await
    }

    pub async fn request(&self, method: &str, params: Value) -> Result<Value, ToolCallError> {
        let mut state = self.state.lock().await;
        state.ensure_initialized().await?;
        state.send_jsonrpc_request(method, params).await
    }

    pub async fn notify(&self, method: &str, params: Value) -> Result<(), ToolCallError> {
        let mut state = self.state.lock().await;
        state.ensure_initialized().await?;
        state.send_jsonrpc_notification(method, params).await
    }

    async fn call_tool(
        &self,
        metadata: &ToolMetadata,
        args: Value,
    ) -> Result<Value, ToolCallError> {
        let args_object = normalize_args_for_tool(metadata, args)?;

        let mut state = self.state.lock().await;
        state.ensure_initialized().await?;
        state
            .send_jsonrpc_request(
                "tools/call",
                json!({
                    "name": metadata.name,
                    "arguments": args_object,
                }),
            )
            .await
    }
}

impl ToolsFacade {
    pub async fn by_name(&self, name: &str) -> Result<ToolHandle, ToolCallError> {
        let metadata = self.client.resolve_tool_with_refresh(name).await?;
        Ok(ToolHandle {
            client: self.client.clone(),
            metadata,
        })
    }

    pub async fn list(&self) -> Vec<ToolMetadata> {
        self.client.list_tools().await
    }
}

impl ToolHandle {
    pub fn metadata(&self) -> &ToolMetadata {
        &self.metadata
    }

    pub async fn call(&self, args: Value) -> Result<Value, ToolCallError> {
        self.client.call_tool(&self.metadata, args).await
    }
}

impl ClientState {
    async fn ensure_initialized(&mut self) -> Result<(), ToolCallError> {
        if self.initialized {
            return Ok(());
        }

        let request_id = self.next_request_id();
        let request = json!({
            "jsonrpc": "2.0",
            "id": request_id,
            "method": "initialize",
            "params": {
                "protocolVersion": self.protocol_version,
                "capabilities": {
                    "roots": { "listChanged": true },
                    "sampling": {}
                },
                "clientInfo": {
                    "name": "mcpway-tool-api",
                    "version": env!("CARGO_PKG_VERSION")
                }
            }
        });

        let response = self.transport.send_request(&request).await?;
        ensure_no_rpc_error("initialize", &response)?;

        let initialized_notification = json!({
            "jsonrpc": "2.0",
            "method": "notifications/initialized"
        });
        self.transport
            .send_notification(&initialized_notification)
            .await?;

        self.initialized = true;
        Ok(())
    }

    async fn send_jsonrpc_request(
        &mut self,
        method: &str,
        params: Value,
    ) -> Result<Value, ToolCallError> {
        let request = json!({
            "jsonrpc": "2.0",
            "id": self.next_request_id(),
            "method": method,
            "params": params,
        });

        let response = self.transport.send_request(&request).await?;
        ensure_no_rpc_error(method, &response)?;
        Ok(response)
    }

    async fn send_jsonrpc_notification(
        &mut self,
        method: &str,
        params: Value,
    ) -> Result<(), ToolCallError> {
        let notification = json!({
            "jsonrpc": "2.0",
            "method": method,
            "params": params,
        });
        self.transport.send_notification(&notification).await
    }

    fn resolve_tool(&self, name: &str) -> Result<ToolMetadata, ToolCallError> {
        self.tools_by_name
            .get(name)
            .cloned()
            .ok_or_else(|| ToolCallError::ToolNotFound {
                name: name.to_string(),
            })
    }

    fn replace_tools(&mut self, tools: Vec<ToolMetadata>) {
        let mut tools_by_name = HashMap::new();
        for tool in &tools {
            tools_by_name.insert(tool.name.clone(), tool.clone());
        }

        self.tools = tools;
        self.tools_by_name = tools_by_name;
    }

    fn list_with_metadata(&self) -> Vec<ToolCatalogMetadata> {
        self.tools
            .iter()
            .map(|tool| ToolCatalogMetadata {
                name: tool.name.clone(),
                description: tool.description.clone(),
                required_keys: count_required_keys(&tool.input_schema),
                defaulted_keys: count_defaulted_keys(&tool.input_schema),
            })
            .collect()
    }

    fn next_request_id(&mut self) -> String {
        self.request_seq = self.request_seq.saturating_add(1);
        format!("tool-api-{}", self.request_seq)
    }
}

fn normalize_args_for_tool(metadata: &ToolMetadata, args: Value) -> Result<Value, ToolCallError> {
    let mut args_object = args;
    if !args_object.is_object() {
        return Err(ToolCallError::InvalidArguments(format!(
            "Tool '{}' expects JSON object arguments",
            metadata.name
        )));
    }

    apply_defaults(&metadata.input_schema, &mut args_object);
    validate_required(&metadata.name, &metadata.input_schema, &args_object)?;
    Ok(args_object)
}

fn parse_tools_list_response(response: &Value) -> Result<Vec<ToolMetadata>, ToolCallError> {
    let result = response
        .get("result")
        .ok_or_else(|| ToolCallError::Protocol("tools/list response missing result".to_string()))?;
    let tools = result
        .get("tools")
        .and_then(Value::as_array)
        .ok_or_else(|| {
            ToolCallError::Protocol("tools/list result missing tools array".to_string())
        })?;

    let mut parsed = Vec::with_capacity(tools.len());
    for tool in tools {
        let Some(obj) = tool.as_object() else {
            return Err(ToolCallError::Protocol(
                "tools/list item was not an object".to_string(),
            ));
        };

        let name = obj
            .get("name")
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .ok_or_else(|| {
                ToolCallError::Protocol("tool entry missing non-empty name".to_string())
            })?
            .to_string();

        let description = obj
            .get("description")
            .and_then(Value::as_str)
            .map(str::to_string);

        let input_schema = obj
            .get("inputSchema")
            .cloned()
            .unwrap_or_else(|| json!({"type": "object"}));

        parsed.push(ToolMetadata {
            name,
            description,
            input_schema,
        });
    }

    Ok(parsed)
}

fn ensure_no_rpc_error(method: &str, response: &Value) -> Result<(), ToolCallError> {
    if response.get("error").is_none() {
        return Ok(());
    }

    Err(ToolCallError::Protocol(format!(
        "RPC method '{method}' returned error: {}",
        response.get("error").unwrap_or(&Value::Null)
    )))
}

fn count_required_keys(schema: &Value) -> usize {
    let mut total = schema
        .get("required")
        .and_then(Value::as_array)
        .map(|values| values.iter().filter(|value| value.is_string()).count())
        .unwrap_or(0);

    let Some(properties) = schema.get("properties").and_then(Value::as_object) else {
        return total;
    };

    for property in properties.values() {
        total += count_required_keys(property);
    }

    total
}

fn count_defaulted_keys(schema: &Value) -> usize {
    let mut total = 0usize;
    let Some(properties) = schema.get("properties").and_then(Value::as_object) else {
        return total;
    };

    for property in properties.values() {
        if property.get("default").is_some() {
            total = total.saturating_add(1);
        }
        total = total.saturating_add(count_defaulted_keys(property));
    }

    total
}

pub use crate::tool_api::transport::Transport;

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

    #[test]
    fn schema_counts_include_nested_required_and_defaults() {
        let schema = json!({
            "type": "object",
            "required": ["city"],
            "properties": {
                "city": {"type": "string"},
                "units": {"type": "string", "default": "metric"},
                "filters": {
                    "type": "object",
                    "required": ["region"],
                    "properties": {
                        "region": {"type": "string"},
                        "lang": {"type": "string", "default": "en"}
                    }
                }
            }
        });

        assert_eq!(count_required_keys(&schema), 2);
        assert_eq!(count_defaulted_keys(&schema), 2);
    }

    #[test]
    fn list_with_metadata_reports_required_and_default_counts() {
        let mut state = ClientState {
            transport: TransportClient::new(
                Transport::StreamableHttp,
                crate::tool_api::transport::TransportOptions {
                    endpoint: "http://127.0.0.1:1".to_string(),
                    headers: HashMap::new(),
                    connect_timeout: std::time::Duration::from_secs(1),
                    request_timeout: Some(std::time::Duration::from_secs(1)),
                    ..Default::default()
                },
            )
            .expect("transport init"),
            protocol_version: "2024-11-05".to_string(),
            request_seq: 0,
            initialized: false,
            tools: Vec::new(),
            tools_by_name: HashMap::new(),
        };

        state.replace_tools(vec![ToolMetadata {
            name: "weather-report".to_string(),
            description: Some("Weather lookup".to_string()),
            input_schema: json!({
                "type": "object",
                "required": ["city"],
                "properties": {
                    "city": {"type": "string"},
                    "units": {"type": "string", "default": "metric"}
                }
            }),
        }]);

        let list = state.list_with_metadata();
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].name, "weather-report");
        assert_eq!(list[0].required_keys, 1);
        assert_eq!(list[0].defaulted_keys, 1);
    }

    #[test]
    fn id_key_from_envelope_handles_numeric_and_string_ids() {
        assert_eq!(
            crate::tool_api::transport::id_key_from_envelope(&json!({"id": "abc"})),
            Some("s:abc".to_string())
        );
        assert_eq!(
            crate::tool_api::transport::id_key_from_envelope(&json!({"id": 42})),
            Some("n:42".to_string())
        );
    }
}