cc-agent-sdk 0.1.7

claude agent sdk
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
//! Example 8: MCP Server Integration
//!
//! This example demonstrates how to create and use custom MCP (Model Context Protocol)
//! servers with the Claude Agent SDK. MCP servers allow you to extend Claude's
//! capabilities with custom tools that run in-process.
//!
//! What it demonstrates:
//! - Creating custom tools with the `tool!` macro
//! - Building an SDK MCP server with multiple tools
//! - Integrating MCP servers with ClaudeClient
//! - Configuring allowed_tools to enable custom tools
//! - Using custom tools in a bidirectional conversation
//!
//! The example creates a "math-tools" MCP server with calculator, statistics, and
//! random number generator tools.
//!
//! Run with:
//! ```bash
//! cargo run --example 08_mcp_server_integration
//! ```

use claude_agent_sdk::{
    ClaudeAgentOptions, ClaudeClient, ContentBlock, McpServers, McpToolResultContent, Message,
    ToolResult, create_sdk_mcp_server, tool,
};
use futures::StreamExt;
use serde_json::json;
use std::collections::HashMap;

/// Handler for the calculator tool
async fn calculator_handler(args: serde_json::Value) -> anyhow::Result<ToolResult> {
    let operation = args["operation"]
        .as_str()
        .ok_or_else(|| anyhow::anyhow!("Missing operation"))?;
    let a = args["a"]
        .as_f64()
        .ok_or_else(|| anyhow::anyhow!("Missing or invalid parameter 'a'"))?;
    let b = args["b"]
        .as_f64()
        .ok_or_else(|| anyhow::anyhow!("Missing or invalid parameter 'b'"))?;

    let result = match operation {
        "add" => a + b,
        "subtract" => a - b,
        "multiply" => a * b,
        "divide" => {
            if b == 0.0 {
                return Ok(ToolResult {
                    content: vec![McpToolResultContent::Text {
                        text: "Error: Division by zero".to_string(),
                    }],
                    is_error: true,
                });
            }
            a / b
        },
        _ => {
            return Ok(ToolResult {
                content: vec![McpToolResultContent::Text {
                    text: format!("Error: Unknown operation '{}'", operation),
                }],
                is_error: true,
            });
        },
    };

    Ok(ToolResult {
        content: vec![McpToolResultContent::Text {
            text: format!("Result: {} {} {} = {}", a, operation, b, result),
        }],
        is_error: false,
    })
}

/// Handler for the statistics tool
async fn statistics_handler(args: serde_json::Value) -> anyhow::Result<ToolResult> {
    let numbers = args["numbers"]
        .as_array()
        .ok_or_else(|| anyhow::anyhow!("Missing or invalid 'numbers' array"))?;

    let nums: Vec<f64> = numbers.iter().filter_map(|v| v.as_f64()).collect();

    if nums.is_empty() {
        return Ok(ToolResult {
            content: vec![McpToolResultContent::Text {
                text: "Error: No valid numbers provided".to_string(),
            }],
            is_error: true,
        });
    }

    let sum: f64 = nums.iter().sum();
    let mean = sum / nums.len() as f64;
    let max = nums.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
    let min = nums.iter().cloned().fold(f64::INFINITY, f64::min);

    // Calculate variance and standard deviation
    let variance = nums.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / nums.len() as f64;
    let std_dev = variance.sqrt();

    let stats = json!({
        "count": nums.len(),
        "sum": sum,
        "mean": mean,
        "min": min,
        "max": max,
        "variance": variance,
        "std_dev": std_dev
    });

    Ok(ToolResult {
        content: vec![McpToolResultContent::Text {
            text: format!("Statistics: {}", serde_json::to_string_pretty(&stats)?),
        }],
        is_error: false,
    })
}

/// Handler for the random number generator tool
async fn random_handler(args: serde_json::Value) -> anyhow::Result<ToolResult> {
    let min = args["min"].as_i64().unwrap_or(0);
    let max = args["max"].as_i64().unwrap_or(100);

    if min >= max {
        return Ok(ToolResult {
            content: vec![McpToolResultContent::Text {
                text: "Error: min must be less than max".to_string(),
            }],
            is_error: true,
        });
    }

    // Simple random number generation (not cryptographically secure)
    use std::time::{SystemTime, UNIX_EPOCH};
    let seed = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    let random = (seed % (max - min) as u128) as i64 + min;

    Ok(ToolResult {
        content: vec![McpToolResultContent::Text {
            text: format!("Random number between {} and {}: {}", min, max, random),
        }],
        is_error: false,
    })
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== MCP Server Integration Example ===\n");

    // Step 1: Create custom tools using the tool! macro
    println!("Step 1: Creating custom MCP tools...\n");

    let calculator_tool = tool!(
        "calculator",
        "Perform basic arithmetic operations (add, subtract, multiply, divide)",
        json!({
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "enum": ["add", "subtract", "multiply", "divide"],
                    "description": "The arithmetic operation to perform"
                },
                "a": {
                    "type": "number",
                    "description": "First operand"
                },
                "b": {
                    "type": "number",
                    "description": "Second operand"
                }
            },
            "required": ["operation", "a", "b"]
        }),
        calculator_handler
    );

    let statistics_tool = tool!(
        "statistics",
        "Calculate statistical measures (mean, median, std dev, etc.) for a list of numbers",
        json!({
            "type": "object",
            "properties": {
                "numbers": {
                    "type": "array",
                    "items": { "type": "number" },
                    "description": "Array of numbers to analyze"
                }
            },
            "required": ["numbers"]
        }),
        statistics_handler
    );

    let random_tool = tool!(
        "random_number",
        "Generate a random number within a specified range",
        json!({
            "type": "object",
            "properties": {
                "min": {
                    "type": "integer",
                    "description": "Minimum value (inclusive)"
                },
                "max": {
                    "type": "integer",
                    "description": "Maximum value (exclusive)"
                }
            },
            "required": ["min", "max"]
        }),
        random_handler
    );

    println!("✓ Created 3 tools: calculator, statistics, random_number\n");

    // Step 2: Create an SDK MCP server with the tools
    println!("Step 2: Creating SDK MCP server...\n");

    let math_server = create_sdk_mcp_server(
        "math-tools",
        "1.0.0",
        vec![calculator_tool, statistics_tool, random_tool],
    );

    println!("✓ Created 'math-tools' MCP server v1.0.0\n");

    // Step 3: Configure ClaudeClient with the MCP server
    println!("Step 3: Configuring Claude client with MCP server...\n");

    let mut mcp_servers = HashMap::new();
    mcp_servers.insert(
        "math-tools".to_string(),
        claude_agent_sdk::McpServerConfig::Sdk(math_server),
    );

    let options = ClaudeAgentOptions {
        mcp_servers: McpServers::Dict(mcp_servers),
        // IMPORTANT: Tools must be explicitly allowed with format mcp__{server_name}__{tool_name}
        allowed_tools: vec![
            "mcp__math-tools__calculator".to_string(),
            "mcp__math-tools__statistics".to_string(),
            "mcp__math-tools__random_number".to_string(),
        ],
        max_turns: Some(10),
        permission_mode: Some(claude_agent_sdk::PermissionMode::AcceptEdits),
        ..Default::default()
    };

    let mut client = ClaudeClient::new(options);

    // Step 4: Connect and interact with Claude using the custom tools
    println!("Step 4: Connecting to Claude...\n");
    client.connect().await?;
    println!("✓ Connected!\n");

    // Query 1: Use the calculator tool
    println!("=== Query 1: Calculator Tool ===");
    println!("User: Calculate 42 multiplied by 7\n");
    client.query("Calculate 42 multiplied by 7").await?;

    let mut stream = client.receive_response();
    while let Some(message) = stream.next().await {
        match message? {
            Message::Assistant(msg) => {
                for block in msg.message.content {
                    match block {
                        ContentBlock::Text(text) => {
                            println!("Claude: {}", text.text);
                        },
                        ContentBlock::ToolUse(tool) => {
                            println!("🔧 Using tool: {} ({})", tool.name, tool.id);
                            println!("   Input: {}", serde_json::to_string_pretty(&tool.input)?);
                        },
                        _ => {},
                    }
                }
            },
            Message::User(user_msg) => {
                // Tool results come as User messages
                if let Some(content_blocks) = &user_msg.content {
                    for content in content_blocks {
                        if let ContentBlock::ToolResult(result) = content
                            && let Some(claude_agent_sdk::ToolResultContent::Text(text)) =
                                &result.content
                        {
                            println!("📊 Tool result: {}", text);
                        }
                    }
                }
            },
            Message::Result(result) => {
                println!(
                    "\n[Result] Turns: {}, Duration: {}ms\n",
                    result.num_turns, result.duration_ms
                );
            },
            _ => {},
        }
    }
    drop(stream);

    // Query 2: Use the statistics tool
    println!("=== Query 2: Statistics Tool ===");
    println!("User: Calculate statistics for the numbers: 10, 20, 30, 40, 50\n");
    client
        .query("Calculate statistics for the numbers: 10, 20, 30, 40, 50")
        .await?;

    let mut stream = client.receive_response();
    while let Some(message) = stream.next().await {
        match message? {
            Message::Assistant(msg) => {
                for block in msg.message.content {
                    match block {
                        ContentBlock::Text(text) => {
                            println!("Claude: {}", text.text);
                        },
                        ContentBlock::ToolUse(tool) => {
                            println!("🔧 Using tool: {} ({})", tool.name, tool.id);
                            println!("   Input: {}", serde_json::to_string_pretty(&tool.input)?);
                        },
                        _ => {},
                    }
                }
            },
            Message::User(user_msg) => {
                // Tool results come as User messages
                if let Some(content_blocks) = &user_msg.content {
                    for content in content_blocks {
                        if let ContentBlock::ToolResult(result) = content
                            && let Some(claude_agent_sdk::ToolResultContent::Text(text)) =
                                &result.content
                        {
                            println!("📊 Tool result: {}", text);
                        }
                    }
                }
            },
            Message::Result(result) => {
                println!(
                    "\n[Result] Turns: {}, Duration: {}ms\n",
                    result.num_turns, result.duration_ms
                );
            },
            _ => {},
        }
    }
    drop(stream);

    // Query 3: Use the random number generator
    println!("=== Query 3: Random Number Tool ===");
    println!("User: Generate a random number between 1 and 100\n");
    client
        .query("Generate a random number between 1 and 100")
        .await?;

    let mut stream = client.receive_response();
    while let Some(message) = stream.next().await {
        match message? {
            Message::Assistant(msg) => {
                for block in msg.message.content {
                    match block {
                        ContentBlock::Text(text) => {
                            println!("Claude: {}", text.text);
                        },
                        ContentBlock::ToolUse(tool) => {
                            println!("🔧 Using tool: {} ({})", tool.name, tool.id);
                            println!("   Input: {}", serde_json::to_string_pretty(&tool.input)?);
                        },
                        _ => {},
                    }
                }
            },
            Message::User(user_msg) => {
                // Tool results come as User messages
                if let Some(content_blocks) = &user_msg.content {
                    for content in content_blocks {
                        if let ContentBlock::ToolResult(result) = content
                            && let Some(claude_agent_sdk::ToolResultContent::Text(text)) =
                                &result.content
                        {
                            println!("📊 Tool result: {}", text);
                        }
                    }
                }
            },
            Message::Result(result) => {
                println!(
                    "\n[Result] Turns: {}, Duration: {}ms\n",
                    result.num_turns, result.duration_ms
                );
            },
            _ => {},
        }
    }
    drop(stream);

    // Query 4: Complex task using multiple tools
    println!("=== Query 4: Multiple Tools ===");
    println!("User: Calculate the average of 15, 25, and 35, then multiply it by 3\n");
    client
        .query("Calculate the average of 15, 25, and 35, then multiply the result by 3")
        .await?;

    let mut stream = client.receive_response();
    while let Some(message) = stream.next().await {
        match message? {
            Message::Assistant(msg) => {
                for block in msg.message.content {
                    match block {
                        ContentBlock::Text(text) => {
                            println!("Claude: {}", text.text);
                        },
                        ContentBlock::ToolUse(tool) => {
                            println!("🔧 Using tool: {} ({})", tool.name, tool.id);
                            println!("   Input: {}", serde_json::to_string_pretty(&tool.input)?);
                        },
                        _ => {},
                    }
                }
            },
            Message::User(user_msg) => {
                // Tool results come as User messages
                if let Some(content_blocks) = &user_msg.content {
                    for content in content_blocks {
                        if let ContentBlock::ToolResult(result) = content
                            && let Some(claude_agent_sdk::ToolResultContent::Text(text)) =
                                &result.content
                        {
                            println!("📊 Tool result: {}", text);
                        }
                    }
                }
            },
            Message::Result(result) => {
                println!(
                    "\n[Result] Turns: {}, Duration: {}ms\n",
                    result.num_turns, result.duration_ms
                );
            },
            _ => {},
        }
    }
    drop(stream);

    // Disconnect
    println!("Disconnecting...");
    client.disconnect().await?;
    println!("✓ Disconnected!\n");

    println!("=== Example Complete ===");
    println!("\nKey Takeaways:");
    println!("• Created custom tools with the tool! macro");
    println!("• Built an SDK MCP server with multiple tools");
    println!("• Integrated MCP server with ClaudeClient");
    println!("• Claude automatically selected and used appropriate tools");
    println!("• Tools can be composed to solve complex tasks");

    Ok(())
}