daimon 0.16.0

A Rust-native AI agent framework
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 server: expose Daimon tools over the Model Context Protocol.
//!
//! [`McpServer`] reads JSON-RPC 2.0 requests from stdin, dispatches
//! `initialize`, `tools/list`, and `tools/call` to a [`ToolRegistry`],
//! and writes responses to stdout.
//!
//! ```ignore
//! use daimon::mcp::server::McpServer;
//! use daimon::tool::ToolRegistry;
//!
//! let mut registry = ToolRegistry::new();
//! registry.register(my_tool)?;
//!
//! McpServer::new(registry).serve_stdio().await?;
//! ```

use serde::{Deserialize, Serialize};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt};

use crate::error::{DaimonError, Result};
use crate::mcp::protocol::McpToolInfo;
use crate::tool::ToolRegistry;

/// An MCP-compliant tool server.
///
/// Exposes a [`ToolRegistry`] over JSON-RPC 2.0 using Content-Length framed
/// messages on stdin/stdout (the standard MCP stdio transport).
pub struct McpServer {
    tools: ToolRegistry,
    server_name: String,
    server_version: String,
}

#[derive(Debug, Deserialize)]
struct IncomingRequest {
    #[allow(dead_code)]
    jsonrpc: String,
    id: Option<serde_json::Value>,
    method: String,
    #[serde(default)]
    params: Option<serde_json::Value>,
}

#[derive(Debug, Serialize)]
struct OutgoingResponse {
    jsonrpc: String,
    id: serde_json::Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    result: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<RpcError>,
}

#[derive(Debug, Serialize)]
struct RpcError {
    code: i64,
    message: String,
}

impl McpServer {
    /// Creates a new MCP server wrapping the given tool registry.
    pub fn new(tools: ToolRegistry) -> Self {
        Self {
            tools,
            server_name: "daimon".to_string(),
            server_version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }

    /// Sets the server name reported during initialization.
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.server_name = name.into();
        self
    }

    /// Sets the server version reported during initialization.
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.server_version = version.into();
        self
    }

    /// Runs the server, reading from stdin and writing to stdout using
    /// Content-Length framed JSON-RPC messages.
    pub async fn serve_stdio(self) -> Result<()> {
        let stdin = tokio::io::stdin();
        let stdout = tokio::io::stdout();
        let mut reader = tokio::io::BufReader::new(stdin);
        let mut writer = tokio::io::BufWriter::new(stdout);

        loop {
            let body = match read_message(&mut reader).await {
                Ok(Some(body)) => body,
                Ok(None) => break,
                Err(e) => {
                    tracing::warn!("failed to read message: {e}");
                    continue;
                }
            };

            let request: IncomingRequest = match serde_json::from_str(&body) {
                Ok(r) => r,
                Err(e) => {
                    tracing::warn!("invalid JSON-RPC: {e}");
                    continue;
                }
            };

            if request.id.is_none() {
                tracing::debug!(method = %request.method, "notification (ignored)");
                continue;
            }

            let id = request.id.unwrap();
            let response = self.handle_request(&request.method, request.params).await;

            let out = match response {
                Ok(result) => OutgoingResponse {
                    jsonrpc: "2.0".into(),
                    id,
                    result: Some(result),
                    error: None,
                },
                Err((code, msg)) => OutgoingResponse {
                    jsonrpc: "2.0".into(),
                    id,
                    result: None,
                    error: Some(RpcError {
                        code,
                        message: msg,
                    }),
                },
            };

            let body = serde_json::to_string(&out)
                .map_err(|e| DaimonError::Mcp(format!("serialize response: {e}")))?;

            write_message(&mut writer, &body)
                .await
                .map_err(|e| DaimonError::Mcp(format!("write response: {e}")))?;
        }

        Ok(())
    }

    /// Process a synchronous request from an in-memory buffer (for testing or embedding).
    pub async fn handle_request_raw(
        &self,
        body: &str,
    ) -> std::result::Result<String, String> {
        let request: IncomingRequest =
            serde_json::from_str(body).map_err(|e| format!("parse error: {e}"))?;

        let id = request.id.clone().unwrap_or(serde_json::Value::Null);

        let out = match self.handle_request(&request.method, request.params).await {
            Ok(result) => OutgoingResponse {
                jsonrpc: "2.0".into(),
                id,
                result: Some(result),
                error: None,
            },
            Err((code, msg)) => OutgoingResponse {
                jsonrpc: "2.0".into(),
                id,
                result: None,
                error: Some(RpcError {
                    code,
                    message: msg,
                }),
            },
        };

        serde_json::to_string(&out).map_err(|e| format!("serialize: {e}"))
    }

    async fn handle_request(
        &self,
        method: &str,
        params: Option<serde_json::Value>,
    ) -> std::result::Result<serde_json::Value, (i64, String)> {
        match method {
            "initialize" => self.handle_initialize(),
            "tools/list" => self.handle_tools_list(),
            "tools/call" => self.handle_tools_call(params).await,
            _ => Err((-32601, format!("method '{method}' not found"))),
        }
    }

    fn handle_initialize(&self) -> std::result::Result<serde_json::Value, (i64, String)> {
        Ok(serde_json::json!({
            "protocolVersion": "2024-11-05",
            "capabilities": {
                "tools": {}
            },
            "serverInfo": {
                "name": self.server_name,
                "version": self.server_version
            }
        }))
    }

    fn handle_tools_list(&self) -> std::result::Result<serde_json::Value, (i64, String)> {
        let tools: Vec<McpToolInfo> = self
            .tools
            .tool_specs()
            .iter()
            .map(|spec| McpToolInfo {
                name: spec.name.clone(),
                description: Some(spec.description.clone()),
                input_schema: spec.parameters.clone(),
            })
            .collect();

        Ok(serde_json::json!({ "tools": tools }))
    }

    async fn handle_tools_call(
        &self,
        params: Option<serde_json::Value>,
    ) -> std::result::Result<serde_json::Value, (i64, String)> {
        let params = params.ok_or((-32602, "missing params".to_string()))?;

        let name = params
            .get("name")
            .and_then(|v| v.as_str())
            .ok_or((-32602, "missing 'name' in params".to_string()))?;

        let arguments = params
            .get("arguments")
            .cloned()
            .unwrap_or(serde_json::Value::Object(serde_json::Map::new()));

        let tool = self
            .tools
            .get(name)
            .ok_or((-32602, format!("tool '{name}' not found")))?;

        match tool.execute_erased(&arguments).await {
            Ok(output) => Ok(serde_json::json!({
                "content": [{
                    "type": "text",
                    "text": output.content
                }],
                "isError": output.is_error
            })),
            Err(e) => Ok(serde_json::json!({
                "content": [{
                    "type": "text",
                    "text": e.to_string()
                }],
                "isError": true
            })),
        }
    }
}

async fn read_message<R: tokio::io::AsyncBufRead + Unpin>(
    reader: &mut R,
) -> std::result::Result<Option<String>, std::io::Error> {
    let mut header_line = String::new();
    let n = reader.read_line(&mut header_line).await?;
    if n == 0 {
        return Ok(None);
    }

    let content_length: usize = if header_line
        .trim()
        .to_lowercase()
        .starts_with("content-length:")
    {
        header_line
            .trim()
            .split(':')
            .nth(1)
            .and_then(|s| s.trim().parse().ok())
            .unwrap_or(0)
    } else {
        let content = header_line.trim().to_string();
        if content.is_empty() {
            return Ok(None);
        }
        return Ok(Some(content));
    };

    let mut separator = String::new();
    reader.read_line(&mut separator).await?;

    let mut body = vec![0u8; content_length];
    tokio::io::AsyncReadExt::read_exact(reader, &mut body).await?;

    Ok(Some(String::from_utf8_lossy(&body).into_owned()))
}

async fn write_message<W: tokio::io::AsyncWrite + Unpin>(
    writer: &mut W,
    body: &str,
) -> std::result::Result<(), std::io::Error> {
    let header = format!("Content-Length: {}\r\n\r\n", body.len());
    writer.write_all(header.as_bytes()).await?;
    writer.write_all(body.as_bytes()).await?;
    writer.flush().await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tool::{Tool, ToolOutput};

    struct GreetTool;

    impl Tool for GreetTool {
        fn name(&self) -> &str {
            "greet"
        }
        fn description(&self) -> &str {
            "Greets a person"
        }
        fn parameters_schema(&self) -> serde_json::Value {
            serde_json::json!({
                "type": "object",
                "properties": {
                    "name": {"type": "string"}
                },
                "required": ["name"]
            })
        }
        async fn execute(&self, input: &serde_json::Value) -> crate::error::Result<ToolOutput> {
            let name = input
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or("World");
            Ok(ToolOutput::text(format!("Hello, {name}!")))
        }
    }

    fn make_server() -> McpServer {
        let mut registry = ToolRegistry::new();
        registry.register(GreetTool).unwrap();
        McpServer::new(registry)
    }

    #[tokio::test]
    async fn test_initialize() {
        let server = make_server();
        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {}
        })
        .to_string();

        let resp = server.handle_request_raw(&body).await.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&resp).unwrap();
        assert!(parsed["result"]["capabilities"]["tools"].is_object());
        assert_eq!(parsed["result"]["serverInfo"]["name"], "daimon");
    }

    #[tokio::test]
    async fn test_tools_list() {
        let server = make_server();
        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list"
        })
        .to_string();

        let resp = server.handle_request_raw(&body).await.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&resp).unwrap();
        let tools = parsed["result"]["tools"].as_array().unwrap();
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0]["name"], "greet");
    }

    #[tokio::test]
    async fn test_tools_call() {
        let server = make_server();
        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 3,
            "method": "tools/call",
            "params": {
                "name": "greet",
                "arguments": {"name": "Daimon"}
            }
        })
        .to_string();

        let resp = server.handle_request_raw(&body).await.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&resp).unwrap();
        assert_eq!(parsed["result"]["content"][0]["text"], "Hello, Daimon!");
        assert_eq!(parsed["result"]["isError"], false);
    }

    #[tokio::test]
    async fn test_tools_call_unknown_tool() {
        let server = make_server();
        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 4,
            "method": "tools/call",
            "params": {
                "name": "nonexistent",
                "arguments": {}
            }
        })
        .to_string();

        let resp = server.handle_request_raw(&body).await.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&resp).unwrap();
        assert!(parsed["error"].is_object());
    }

    #[tokio::test]
    async fn test_unknown_method() {
        let server = make_server();
        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 5,
            "method": "unknown/method"
        })
        .to_string();

        let resp = server.handle_request_raw(&body).await.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&resp).unwrap();
        assert_eq!(parsed["error"]["code"], -32601);
    }
}