turboclaudeagent 0.3.0

Interactive Agent SDK for TurboClaude - Use Claude agents in your Rust applications
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
//! SDK MCP Server implementation for in-process tool execution.
//!
//! This module provides a builder API for creating MCP servers that run within
//! the same process as your application, eliminating subprocess overhead.
//!
//! # Example
//!
//! ```rust
//! use turboclaudeagent::mcp::sdk::*;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize)]
//! struct CalcInput { a: i32, b: i32 }
//!
//! #[derive(Serialize)]
//! struct CalcOutput { result: i32 }
//!
//! # async fn example() -> Result<(), SdkToolError> {
//! let server = SdkMcpServerBuilder::new("calculator")
//!     .tool("add", "Add two numbers", |input: CalcInput| async move {
//!         Ok(CalcOutput { result: input.a + input.b })
//!     })
//!     .build();
//!
//! // Execute a tool
//! let result = server.execute_tool(
//!     "add",
//!     serde_json::json!({"a": 5, "b": 3})
//! ).await?;
//! # Ok(())
//! # }
//! ```

use async_trait::async_trait;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::collections::HashMap;
use std::future::Future;
use std::marker::PhantomData;
use std::sync::Arc;
use thiserror::Error;

/// Errors that can occur during SDK tool execution.
#[derive(Debug, Error)]
pub enum SdkToolError {
    /// Input JSON doesn't match expected schema
    #[error("Invalid input: {0}")]
    InvalidInput(String),

    /// Tool execution failed
    #[error("Execution failed: {0}")]
    ExecutionFailed(String),

    /// JSON serialization/deserialization error
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
}

/// An in-process MCP tool that can be executed synchronously.
///
/// Implement this trait to create custom tools, or use the builder API
/// with closures for simpler use cases.
#[async_trait]
pub trait SdkTool: Send + Sync {
    /// Unique identifier for this tool.
    fn name(&self) -> &str;

    /// Human-readable description of what this tool does.
    fn description(&self) -> &str;

    /// JSON Schema describing the expected input format.
    ///
    /// For MVP, this can return a basic schema. Future versions may
    /// support automatic schema generation from Rust types.
    fn input_schema(&self) -> Value;

    /// Execute the tool with the given input.
    ///
    /// # Arguments
    ///
    /// * `input` - JSON value matching the `input_schema()`
    ///
    /// # Returns
    ///
    /// JSON value representing the tool's output, or an error if execution failed.
    async fn execute(&self, input: Value) -> Result<Value, SdkToolError>;
}

/// Type-safe wrapper for function-based tools.
///
/// This struct implements `SdkTool` for closures that accept a typed input
/// and return a typed output. It handles JSON serialization/deserialization
/// automatically.
pub struct FunctionTool<F, Fut, I, O> {
    name: String,
    description: String,
    handler: F,
    _phantom: PhantomData<(Fut, I, O)>,
}

impl<F, Fut, I, O> FunctionTool<F, Fut, I, O> {
    /// Create a new function-based tool.
    ///
    /// # Arguments
    ///
    /// * `name` - Unique identifier for the tool
    /// * `description` - Human-readable description
    /// * `handler` - Async function that executes the tool logic
    pub fn new(name: String, description: String, handler: F) -> Self {
        Self {
            name,
            description,
            handler,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<F, Fut, I, O> SdkTool for FunctionTool<F, Fut, I, O>
where
    F: Fn(I) -> Fut + Send + Sync,
    Fut: Future<Output = Result<O, SdkToolError>> + Send + Sync,
    I: DeserializeOwned + Send + Sync,
    O: Serialize + Send + Sync,
{
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn input_schema(&self) -> Value {
        // MVP: Return a permissive schema that accepts any object.
        // Future enhancement: Use `schemars` crate for type-based schema generation.
        serde_json::json!({
            "type": "object",
            "properties": {},
            "additionalProperties": true
        })
    }

    async fn execute(&self, input: Value) -> Result<Value, SdkToolError> {
        // Deserialize input to typed struct
        let typed_input: I = serde_json::from_value(input).map_err(|e| {
            SdkToolError::InvalidInput(format!("Failed to deserialize input: {}", e))
        })?;

        // Call the handler function
        let output = (self.handler)(typed_input).await?;

        // Serialize output to JSON
        let json_output = serde_json::to_value(output)?;

        Ok(json_output)
    }
}

/// Builder for creating SDK MCP servers with a fluent API.
///
/// # Example
///
/// ```rust
/// use turboclaudeagent::mcp::sdk::*;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize)]
/// struct Input { value: String }
///
/// #[derive(Serialize)]
/// struct Output { processed: String }
///
/// let server = SdkMcpServerBuilder::new("my-tools")
///     .tool("process", "Process a value", |input: Input| async move {
///         Ok(Output { processed: input.value.to_uppercase() })
///     })
///     .build();
/// ```
pub struct SdkMcpServerBuilder {
    name: String,
    tools: HashMap<String, Arc<dyn SdkTool>>,
}

impl SdkMcpServerBuilder {
    /// Create a new builder with the given server name.
    ///
    /// # Arguments
    ///
    /// * `name` - Unique identifier for this MCP server
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            tools: HashMap::new(),
        }
    }

    /// Add a function-based tool with type-safe input/output.
    ///
    /// # Type Parameters
    ///
    /// * `F` - The closure type (inferred)
    /// * `Fut` - The future returned by the closure (inferred)
    /// * `I` - Input type that implements `DeserializeOwned`
    /// * `O` - Output type that implements `Serialize`
    ///
    /// # Arguments
    ///
    /// * `name` - Unique identifier for the tool
    /// * `description` - Human-readable description
    /// * `handler` - Async closure that implements the tool logic
    ///
    /// # Example
    ///
    /// ```rust
    /// # use turboclaudeagent::mcp::sdk::*;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Deserialize)]
    /// # struct Input { x: i32 }
    /// # #[derive(Serialize)]
    /// # struct Output { result: i32 }
    /// let builder = SdkMcpServerBuilder::new("math")
    ///     .tool("double", "Double a number", |input: Input| async move {
    ///         Ok(Output { result: input.x * 2 })
    ///     });
    /// ```
    pub fn tool<F, Fut, I, O>(mut self, name: &str, description: &str, handler: F) -> Self
    where
        F: Fn(I) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, SdkToolError>> + Send + Sync + 'static,
        I: DeserializeOwned + Send + Sync + 'static,
        O: Serialize + Send + Sync + 'static,
    {
        let tool = FunctionTool::new(name.to_string(), description.to_string(), handler);
        self.tools.insert(name.to_string(), Arc::new(tool));
        self
    }

    /// Add a custom tool implementation.
    ///
    /// Use this method if you've implemented the `SdkTool` trait yourself
    /// and want more control than the closure-based API provides.
    ///
    /// # Arguments
    ///
    /// * `tool` - An implementation of the `SdkTool` trait
    ///
    /// # Example
    ///
    /// ```rust
    /// # use turboclaudeagent::mcp::sdk::*;
    /// # use async_trait::async_trait;
    /// # use serde_json::Value;
    /// # use std::sync::Arc;
    /// struct MyTool;
    ///
    /// #[async_trait]
    /// impl SdkTool for MyTool {
    ///     fn name(&self) -> &str { "my_tool" }
    ///     fn description(&self) -> &str { "Custom tool" }
    ///     fn input_schema(&self) -> Value { serde_json::json!({}) }
    ///     async fn execute(&self, _input: Value) -> Result<Value, SdkToolError> {
    ///         Ok(serde_json::json!({"status": "ok"}))
    ///     }
    /// }
    ///
    /// let builder = SdkMcpServerBuilder::new("custom")
    ///     .add_tool(Arc::new(MyTool));
    /// ```
    pub fn add_tool(mut self, tool: Arc<dyn SdkTool>) -> Self {
        let name = tool.name().to_string();
        self.tools.insert(name, tool);
        self
    }

    /// Build the SDK MCP server.
    ///
    /// Consumes the builder and returns a ready-to-use `SdkMcpServer`.
    pub fn build(self) -> SdkMcpServer {
        SdkMcpServer {
            name: self.name,
            tools: self.tools,
        }
    }
}

/// An in-process MCP server that executes tools without subprocess overhead.
///
/// This server runs within the same process as your application, providing:
/// - Zero IPC overhead
/// - Direct access to application state via closures
/// - Simpler deployment (single process)
/// - Easier debugging
///
/// # Thread Safety
///
/// `SdkMcpServer` is immutable after construction and can be safely shared
/// across threads using `Arc` or cloned directly (implements `Clone`).
#[derive(Clone)]
pub struct SdkMcpServer {
    name: String,
    tools: HashMap<String, Arc<dyn SdkTool>>,
}

impl std::fmt::Debug for SdkMcpServer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SdkMcpServer")
            .field("name", &self.name)
            .field("tool_count", &self.tools.len())
            .finish()
    }
}

impl SdkMcpServer {
    /// Get the server's name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get a tool by name.
    ///
    /// Returns `None` if no tool with the given name exists.
    pub fn get_tool(&self, name: &str) -> Option<&Arc<dyn SdkTool>> {
        self.tools.get(name)
    }

    /// List all available tools.
    ///
    /// Returns a vector of references to the tools registered with this server.
    pub fn list_tools(&self) -> Vec<&Arc<dyn SdkTool>> {
        self.tools.values().collect()
    }

    /// Execute a tool by name with the given input.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the tool to execute
    /// * `input` - JSON value matching the tool's input schema
    ///
    /// # Returns
    ///
    /// The tool's output as a JSON value, or an error if:
    /// - The tool doesn't exist
    /// - The input doesn't match the schema
    /// - The tool execution fails
    ///
    /// # Example
    ///
    /// ```rust
    /// # use turboclaudeagent::mcp::sdk::*;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Deserialize)]
    /// # struct Input { value: i32 }
    /// # #[derive(Serialize)]
    /// # struct Output { result: i32 }
    /// # async fn example() -> Result<(), SdkToolError> {
    /// # let server = SdkMcpServerBuilder::new("test")
    /// #     .tool("double", "Double a number", |input: Input| async move {
    /// #         Ok(Output { result: input.value * 2 })
    /// #     })
    /// #     .build();
    /// let result = server.execute_tool(
    ///     "double",
    ///     serde_json::json!({"value": 21})
    /// ).await?;
    /// assert_eq!(result, serde_json::json!({"result": 42}));
    /// # Ok(())
    /// # }
    /// ```
    pub async fn execute_tool(&self, name: &str, input: Value) -> Result<Value, SdkToolError> {
        match self.get_tool(name) {
            Some(tool) => tool.execute(input).await,
            None => Err(SdkToolError::InvalidInput(format!(
                "Tool '{}' not found in server '{}'",
                name, self.name
            ))),
        }
    }

    /// Check if a tool exists in this server.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the tool to check
    ///
    /// # Returns
    ///
    /// `true` if a tool with this name exists, `false` otherwise.
    pub fn has_tool(&self, name: &str) -> bool {
        self.tools.contains_key(name)
    }

    /// Get the number of tools in this server.
    pub fn tool_count(&self) -> usize {
        self.tools.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    #[derive(Deserialize)]
    struct TestInput {
        value: i32,
    }

    #[derive(Serialize, PartialEq, Debug)]
    struct TestOutput {
        result: i32,
    }

    #[tokio::test]
    async fn test_function_tool_execution() {
        let tool = FunctionTool::new(
            "double".to_string(),
            "Double a number".to_string(),
            |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value * 2,
                })
            },
        );

        let input = serde_json::json!({"value": 21});
        let output = tool.execute(input).await.expect("execution failed");

        assert_eq!(output, serde_json::json!({"result": 42}));
    }

    #[tokio::test]
    async fn test_sdk_server_builder() {
        let server = SdkMcpServerBuilder::new("test-server")
            .tool("add", "Add two numbers", |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value + 10,
                })
            })
            .tool(
                "multiply",
                "Multiply by two",
                |input: TestInput| async move {
                    Ok(TestOutput {
                        result: input.value * 2,
                    })
                },
            )
            .build();

        assert_eq!(server.name(), "test-server");
        assert_eq!(server.tool_count(), 2);
        assert!(server.has_tool("add"));
        assert!(server.has_tool("multiply"));
        assert!(!server.has_tool("nonexistent"));
    }

    #[tokio::test]
    async fn test_server_execute_tool() {
        let server = SdkMcpServerBuilder::new("calculator")
            .tool("double", "Double a number", |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value * 2,
                })
            })
            .build();

        let result = server
            .execute_tool("double", serde_json::json!({"value": 5}))
            .await
            .expect("execution failed");

        assert_eq!(result, serde_json::json!({"result": 10}));
    }

    #[tokio::test]
    async fn test_tool_not_found() {
        let server = SdkMcpServerBuilder::new("empty").build();

        let result = server.execute_tool("missing", serde_json::json!({})).await;

        assert!(result.is_err());
        match result {
            Err(SdkToolError::InvalidInput(msg)) => {
                assert!(msg.contains("not found"));
            }
            _ => panic!("Expected InvalidInput error"),
        }
    }

    #[tokio::test]
    async fn test_invalid_input_deserialization() {
        let server = SdkMcpServerBuilder::new("test")
            .tool("strict", "Strict input", |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value,
                })
            })
            .build();

        // Missing required field
        let result = server.execute_tool("strict", serde_json::json!({})).await;

        assert!(result.is_err());
        match result {
            Err(SdkToolError::InvalidInput(_)) => {}
            _ => panic!("Expected InvalidInput error"),
        }
    }

    #[tokio::test]
    async fn test_tool_execution_error() {
        let server = SdkMcpServerBuilder::new("test")
            .tool("failing", "Always fails", |_input: TestInput| async move {
                Err::<TestOutput, _>(SdkToolError::ExecutionFailed(
                    "intentional failure".to_string(),
                ))
            })
            .build();

        let result = server
            .execute_tool("failing", serde_json::json!({"value": 1}))
            .await;

        assert!(result.is_err());
        match result {
            Err(SdkToolError::ExecutionFailed(msg)) => {
                assert_eq!(msg, "intentional failure");
            }
            _ => panic!("Expected ExecutionFailed error"),
        }
    }

    #[tokio::test]
    async fn test_server_clone() {
        let server = SdkMcpServerBuilder::new("original")
            .tool("tool1", "First tool", |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value,
                })
            })
            .build();

        let cloned = server.clone();

        assert_eq!(server.name(), cloned.name());
        assert_eq!(server.tool_count(), cloned.tool_count());
        assert!(cloned.has_tool("tool1"));
    }

    #[tokio::test]
    async fn test_list_tools() {
        let server = SdkMcpServerBuilder::new("test")
            .tool("tool1", "First", |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value,
                })
            })
            .tool("tool2", "Second", |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value,
                })
            })
            .build();

        let tools = server.list_tools();
        assert_eq!(tools.len(), 2);

        let names: Vec<&str> = tools.iter().map(|t| t.name()).collect();
        assert!(names.contains(&"tool1"));
        assert!(names.contains(&"tool2"));
    }
}