fastmcp-rust 0.2.1

Fast, cancel-correct MCP framework for Rust
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
//! FastMCP Performance Benchmarks
//!
//! Simple benchmarks using std::time::Instant to measure performance characteristics.
//! Run with: `cargo run --example bench --release`
//!
//! Measurements:
//! - Server creation time
//! - JSON serialization/deserialization throughput
//! - Protocol message encoding/decoding
//! - Handler invocation overhead
//!
//! Note: For accurate results, always run with --release flag.

use std::time::{Duration, Instant};

use fastmcp_rust::prelude::*;

// ============================================================================
// Benchmark Utilities
// ============================================================================

/// Run a benchmark function multiple times and report statistics.
fn benchmark<F: FnMut()>(name: &str, iterations: u64, mut f: F) {
    // Warm-up runs
    for _ in 0..10 {
        f();
    }

    // Timed runs
    let start = Instant::now();
    for _ in 0..iterations {
        f();
    }
    let elapsed = start.elapsed();

    let per_op = elapsed.as_nanos() as f64 / iterations as f64;
    let ops_per_sec = if per_op > 0.0 {
        1_000_000_000.0 / per_op
    } else {
        f64::INFINITY
    };

    println!(
        "{name:40} {:>10.2} ns/op  {:>12.0} ops/sec",
        per_op, ops_per_sec
    );
}

/// Format a duration nicely.
fn format_duration(d: Duration) -> String {
    if d.as_secs() > 0 {
        format!("{:.3} s", d.as_secs_f64())
    } else if d.as_millis() > 0 {
        format!("{:.3} ms", d.as_secs_f64() * 1000.0)
    } else if d.as_micros() > 0 {
        format!("{:.3} µs", d.as_secs_f64() * 1_000_000.0)
    } else {
        format!("{} ns", d.as_nanos())
    }
}

// ============================================================================
// Sample Handlers for Benchmarks
// ============================================================================

#[tool(description = "A no-op tool for benchmarking")]
fn noop(_ctx: &McpContext) -> String {
    String::new()
}

#[tool(description = "Echo back the input")]
#[allow(clippy::needless_pass_by_value)] // Macro requires String
fn bench_echo(_ctx: &McpContext, message: String) -> String {
    message
}

#[tool(description = "Parse and return JSON")]
#[allow(clippy::needless_pass_by_value)] // Macro requires String
fn json_roundtrip(_ctx: &McpContext, data: String) -> String {
    // Parse then re-serialize
    match serde_json::from_str::<serde_json::Value>(&data) {
        Ok(value) => serde_json::to_string(&value).unwrap_or_default(),
        Err(_) => String::new(),
    }
}

#[resource(uri = "bench://data")]
fn bench_data(_ctx: &McpContext) -> String {
    r#"{"benchmark": "data"}"#.to_string()
}

#[prompt(description = "Benchmark prompt")]
#[allow(clippy::needless_pass_by_value)] // Macro requires String
fn bench_prompt(_ctx: &McpContext, name: String) -> Vec<PromptMessage> {
    vec![PromptMessage {
        role: Role::User,
        content: Content::Text {
            text: format!("Hello, {name}"),
        },
    }]
}

// ============================================================================
// Benchmarks
// ============================================================================

fn bench_server_creation() {
    println!("\n=== Server Creation ===");

    // Minimal server
    let start = Instant::now();
    let _server = Server::new("bench", "1.0.0").build();
    let minimal = start.elapsed();
    println!(
        "Minimal server (no handlers):      {}",
        format_duration(minimal)
    );

    // Server with one tool
    let start = Instant::now();
    let _server = Server::new("bench", "1.0.0").tool(Noop).build();
    let one_tool = start.elapsed();
    println!(
        "Server with 1 tool:                {}",
        format_duration(one_tool)
    );

    // Server with multiple handlers
    let start = Instant::now();
    let _server = Server::new("bench", "1.0.0")
        .tool(Noop)
        .tool(BenchEcho)
        .tool(JsonRoundtrip)
        .resource(BenchDataResource)
        .prompt(BenchPromptPrompt)
        .build();
    let full = start.elapsed();
    println!(
        "Server with 3 tools+1 res+1 prompt:{}",
        format_duration(full)
    );

    // Stress test: many tools (simulated by repeated registration)
    let start = Instant::now();
    let mut builder = Server::new("bench", "1.0.0");
    for _ in 0..100 {
        builder = builder.tool(Noop);
    }
    let _server = builder.build();
    let many = start.elapsed();
    println!(
        "Server with 100 tools:             {}",
        format_duration(many)
    );
}

fn bench_json_serialization() {
    println!("\n=== JSON Serialization ===");

    // Small message
    let small_msg = serde_json::json!({
        "jsonrpc": "2.0",
        "method": "tools/call",
        "params": {"name": "echo", "arguments": {"message": "hello"}},
        "id": 1
    });

    benchmark("Small JSON serialize", 100_000, || {
        let _ = serde_json::to_string(&small_msg);
    });

    benchmark("Small JSON deserialize", 100_000, || {
        let _ = serde_json::from_str::<serde_json::Value>(
            r#"{"jsonrpc":"2.0","method":"tools/call","params":{"name":"echo"},"id":1}"#,
        );
    });

    // Medium message (tool list response)
    let medium_msg = serde_json::json!({
        "jsonrpc": "2.0",
        "result": {
            "tools": [
                {"name": "tool1", "description": "Description 1", "inputSchema": {"type": "object"}},
                {"name": "tool2", "description": "Description 2", "inputSchema": {"type": "object"}},
                {"name": "tool3", "description": "Description 3", "inputSchema": {"type": "object"}},
                {"name": "tool4", "description": "Description 4", "inputSchema": {"type": "object"}},
                {"name": "tool5", "description": "Description 5", "inputSchema": {"type": "object"}},
            ]
        },
        "id": 1
    });

    benchmark("Medium JSON serialize", 50_000, || {
        let _ = serde_json::to_string(&medium_msg);
    });

    let medium_str = serde_json::to_string(&medium_msg).unwrap();
    benchmark("Medium JSON deserialize", 50_000, || {
        let _ = serde_json::from_str::<serde_json::Value>(&medium_str);
    });

    // Large message (tool call result with content)
    let large_content = "x".repeat(10_000);
    let large_msg = serde_json::json!({
        "jsonrpc": "2.0",
        "result": {
            "content": [{"type": "text", "text": large_content}],
            "isError": false
        },
        "id": 1
    });

    benchmark("Large JSON serialize (10KB)", 10_000, || {
        let _ = serde_json::to_string(&large_msg);
    });

    let large_str = serde_json::to_string(&large_msg).unwrap();
    benchmark("Large JSON deserialize (10KB)", 10_000, || {
        let _ = serde_json::from_str::<serde_json::Value>(&large_str);
    });
}

fn bench_protocol_types() {
    use fastmcp_protocol::{CallToolParams, Content, InitializeParams, JsonRpcRequest, Tool};

    println!("\n=== Protocol Type Operations ===");

    // Initialize params
    benchmark("InitializeParams create", 100_000, || {
        let _ = InitializeParams {
            protocol_version: "2024-11-05".to_string(),
            capabilities: fastmcp_protocol::ClientCapabilities::default(),
            client_info: fastmcp_protocol::ClientInfo {
                name: "test".to_string(),
                version: "1.0".to_string(),
            },
        };
    });

    // Tool definition
    benchmark("Tool definition create", 100_000, || {
        let _ = Tool {
            name: "my_tool".to_string(),
            description: Some("A test tool".to_string()),
            input_schema: serde_json::json!({"type": "object"}),
            output_schema: None,
            icon: None,
            version: None,
            tags: vec![],
            annotations: None,
        };
    });

    // CallToolParams
    benchmark("CallToolParams serialize", 100_000, || {
        let params = CallToolParams {
            name: "echo".to_string(),
            arguments: Some(serde_json::json!({"message": "hello"})),
            meta: None,
        };
        let _ = serde_json::to_string(&params);
    });

    // Content creation
    benchmark("Content::Text create", 100_000, || {
        let _ = Content::Text {
            text: "Hello, world!".to_string(),
        };
    });

    // JSON-RPC request
    benchmark("JsonRpcRequest create", 100_000, || {
        let _ = JsonRpcRequest::new("tools/call", Some(serde_json::json!({"name": "test"})), 1);
    });
}

fn bench_transport_codec() {
    use fastmcp_transport::Codec;

    println!("\n=== Transport Codec ===");

    // Create test messages
    let request = fastmcp_protocol::JsonRpcRequest::new(
        "tools/call",
        Some(serde_json::json!({"name": "echo", "arguments": {"msg": "hello"}})),
        1,
    );

    let response = fastmcp_protocol::JsonRpcResponse {
        jsonrpc: std::borrow::Cow::Borrowed(fastmcp_protocol::JSONRPC_VERSION),
        result: Some(serde_json::json!({"content": [{"type": "text", "text": "hello"}]})),
        error: None,
        id: Some(fastmcp_protocol::RequestId::Number(1)),
    };

    let codec = Codec::new();

    // Encode benchmarks
    benchmark("Encode request", 100_000, || {
        let _ = codec.encode_request(&request);
    });

    benchmark("Encode response", 100_000, || {
        let _ = codec.encode_response(&response);
    });

    // Decode benchmarks
    let encoded_request = codec.encode_request(&request).unwrap();
    let encoded_response = codec.encode_response(&response).unwrap();

    benchmark("Decode request", 100_000, || {
        let mut c = Codec::new();
        let _ = c.decode(&encoded_request);
    });

    benchmark("Decode response", 100_000, || {
        let mut c = Codec::new();
        let _ = c.decode(&encoded_response);
    });

    // Large message
    let large_content = "x".repeat(10_000);
    let large_response = fastmcp_protocol::JsonRpcResponse {
        jsonrpc: std::borrow::Cow::Borrowed(fastmcp_protocol::JSONRPC_VERSION),
        result: Some(serde_json::json!({"content": [{"type": "text", "text": large_content}]})),
        error: None,
        id: Some(fastmcp_protocol::RequestId::Number(1)),
    };

    benchmark("Encode large response (10KB)", 10_000, || {
        let _ = codec.encode_response(&large_response);
    });

    let encoded_large = codec.encode_response(&large_response).unwrap();

    benchmark("Decode large response (10KB)", 10_000, || {
        let mut c = Codec::new();
        let _ = c.decode(&encoded_large);
    });
}

fn bench_context_operations() {
    println!("\n=== Context Operations ===");

    let cx = asupersync::Cx::for_testing();
    let ctx = McpContext::new(cx.clone(), 1);

    // is_cancelled check (should be very fast)
    benchmark("is_cancelled check", 1_000_000, || {
        let _ = ctx.is_cancelled();
    });

    // checkpoint (also very fast)
    benchmark("checkpoint call", 1_000_000, || {
        let _ = ctx.checkpoint();
    });

    // budget check
    benchmark("budget().is_exhausted()", 1_000_000, || {
        let _ = ctx.budget().is_exhausted();
    });

    // create new context
    benchmark("McpContext::new", 100_000, || {
        let _ = McpContext::new(cx.clone(), 1);
    });
}

fn bench_memory_usage() {
    use fastmcp_protocol::{
        CallToolParams, CallToolResult, Content, InitializeParams, JsonRpcMessage, JsonRpcRequest,
        JsonRpcResponse, Tool,
    };

    println!("\n=== Memory Usage (size_of) ===");

    println!(
        "McpContext:       {:>4} bytes",
        std::mem::size_of::<McpContext>()
    );
    println!("Tool:             {:>4} bytes", std::mem::size_of::<Tool>());
    println!(
        "Content:          {:>4} bytes",
        std::mem::size_of::<Content>()
    );
    println!(
        "JsonRpcRequest:   {:>4} bytes",
        std::mem::size_of::<JsonRpcRequest>()
    );
    println!(
        "JsonRpcResponse:  {:>4} bytes",
        std::mem::size_of::<JsonRpcResponse>()
    );
    println!(
        "JsonRpcMessage:   {:>4} bytes",
        std::mem::size_of::<JsonRpcMessage>()
    );
    println!(
        "CallToolParams:   {:>4} bytes",
        std::mem::size_of::<CallToolParams>()
    );
    println!(
        "CallToolResult:   {:>4} bytes",
        std::mem::size_of::<CallToolResult>()
    );
    println!(
        "InitializeParams: {:>4} bytes",
        std::mem::size_of::<InitializeParams>()
    );
}

fn bench_schema_validation() {
    use fastmcp_protocol::schema::validate;

    println!("\n=== Schema Validation ===");

    // Simple schema
    let simple_schema = serde_json::json!({
        "type": "object",
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"}
        },
        "required": ["name"]
    });

    let valid_value = serde_json::json!({"name": "Alice", "age": 30});
    let invalid_value = serde_json::json!({"age": 30});

    benchmark("Validate simple (valid)", 100_000, || {
        let _ = validate(&valid_value, &simple_schema);
    });

    benchmark("Validate simple (invalid)", 100_000, || {
        let _ = validate(&invalid_value, &simple_schema);
    });

    // Complex schema
    let complex_schema = serde_json::json!({
        "type": "object",
        "properties": {
            "users": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "id": {"type": "integer"},
                        "name": {"type": "string"},
                        "email": {"type": "string"}
                    },
                    "required": ["id", "name"]
                }
            },
            "metadata": {
                "type": "object",
                "properties": {
                    "version": {"type": "string"},
                    "timestamp": {"type": "integer"}
                }
            }
        },
        "required": ["users"]
    });

    let complex_value = serde_json::json!({
        "users": [
            {"id": 1, "name": "Alice", "email": "alice@example.com"},
            {"id": 2, "name": "Bob"}
        ],
        "metadata": {"version": "1.0", "timestamp": 1234567890}
    });

    benchmark("Validate complex (valid)", 50_000, || {
        let _ = validate(&complex_value, &complex_schema);
    });
}

// ============================================================================
// Main
// ============================================================================

fn main() {
    println!("FastMCP Performance Benchmarks");
    println!("==============================");
    println!();
    println!("Note: Run with --release for accurate results!");
    println!("      cargo run --example bench --release");

    bench_server_creation();
    bench_json_serialization();
    bench_protocol_types();
    bench_transport_codec();
    bench_context_operations();
    bench_schema_validation();
    bench_memory_usage();

    println!("\n==============================");
    println!("Benchmarks complete!");
}