sdforge 0.3.0

Multi-protocol SDK framework with unified macro configuration
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
// MCP Protocol Integration Tests
// Comprehensive integration tests for MCP protocol functionality
// Tests cover: tool discovery, request handling, response formatting,
// input validation, and error handling
//
// Migrated from mcp_sdk to rmcp: JSON-RPC transport tests removed (handled
// internally by rmcp). Tests now focus on SdForgeTool behavior and
// SdForgeMcpServer (ServerHandler) integration.

#[cfg(feature = "mcp")]
mod mcp_protocol_tests {
    use rmcp::handler::server::ServerHandler;
    use rmcp::model::{CallToolResult, ContentBlock, ErrorData as McpError};
    use sdforge::core::{ApiMetadata, Registration};
    use sdforge::mcp::{
        get_mcp_tools, McpToolInstance, McpToolRegistration, SdForgeMcpServer, SdForgeTool,
    };
    use serde_json::Value;
    use std::sync::Arc;

    // ============================================================================
    // Helper Functions
    // ============================================================================

    /// Creates a simple echo tool for testing
    fn create_echo_tool() -> Arc<dyn SdForgeTool> {
        struct EchoTool;
        impl SdForgeTool for EchoTool {
            fn name(&self) -> &str {
                "echo"
            }
            fn description(&self) -> &str {
                "Echoes the input message back"
            }
            fn input_schema(&self) -> Value {
                serde_json::json!({
                    "type": "object",
                    "properties": {
                        "message": {"type": "string"},
                        "count": {"type": "integer", "default": 1}
                    }
                })
            }
            fn call(&self, input: Option<Value>) -> Result<CallToolResult, McpError> {
                let message = input
                    .as_ref()
                    .and_then(|v| v.get("message"))
                    .and_then(|m| m.as_str())
                    .unwrap_or("");

                let count = input
                    .as_ref()
                    .and_then(|v| v.get("count"))
                    .and_then(|c| c.as_i64())
                    .unwrap_or(1) as usize;

                let output = format!("{}x{}", message, count);
                Ok(CallToolResult::success(vec![ContentBlock::text(output)]))
            }
        }
        Arc::new(EchoTool) as Arc<dyn SdForgeTool>
    }

    /// Creates a math tool for testing with multiple operations
    fn create_math_tool() -> Arc<dyn SdForgeTool> {
        struct MathTool;
        impl SdForgeTool for MathTool {
            fn name(&self) -> &str {
                "math"
            }
            fn description(&self) -> &str {
                "Performs basic arithmetic operations"
            }
            fn input_schema(&self) -> Value {
                serde_json::json!({
                    "type": "object",
                    "properties": {
                        "a": {"type": "number"},
                        "b": {"type": "number"},
                        "operation": {
                            "type": "string",
                            "enum": ["add", "subtract", "multiply", "divide"]
                        }
                    },
                    "required": ["a", "b", "operation"]
                })
            }
            fn call(&self, input: Option<Value>) -> Result<CallToolResult, McpError> {
                let input =
                    input.ok_or_else(|| McpError::invalid_params("Input required", None))?;
                let a = input
                    .get("a")
                    .and_then(|v| v.as_f64())
                    .ok_or_else(|| McpError::invalid_params("Missing 'a'", None))?;
                let b = input
                    .get("b")
                    .and_then(|v| v.as_f64())
                    .ok_or_else(|| McpError::invalid_params("Missing 'b'", None))?;
                let op = input
                    .get("operation")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| McpError::invalid_params("Missing 'operation'", None))?;

                let result = match op {
                    "add" => a + b,
                    "subtract" => a - b,
                    "multiply" => a * b,
                    "divide" => {
                        if b == 0.0 {
                            return Err(McpError::invalid_params("Division by zero", None));
                        }
                        a / b
                    }
                    _ => {
                        return Err(McpError::invalid_params(
                            format!("Unknown operation: {}", op),
                            None,
                        ))
                    }
                };

                Ok(CallToolResult::success(vec![ContentBlock::text(
                    result.to_string(),
                )]))
            }
        }
        Arc::new(MathTool) as Arc<dyn SdForgeTool>
    }

    /// Creates a tool that always returns an error
    fn create_error_tool() -> Arc<dyn SdForgeTool> {
        struct ErrorTool;
        impl SdForgeTool for ErrorTool {
            fn name(&self) -> &str {
                "error_tool"
            }
            fn description(&self) -> &str {
                "Always returns an error"
            }
            fn input_schema(&self) -> Value {
                serde_json::json!({"type": "object"})
            }
            fn call(&self, _input: Option<Value>) -> Result<CallToolResult, McpError> {
                Err(McpError::invalid_params("Intentional test error", None))
            }
        }
        Arc::new(ErrorTool) as Arc<dyn SdForgeTool>
    }

    /// Creates a tool with no parameters
    fn create_no_param_tool() -> Arc<dyn SdForgeTool> {
        struct NoParamTool;
        impl SdForgeTool for NoParamTool {
            fn name(&self) -> &str {
                "no_param"
            }
            fn description(&self) -> &str {
                "Tool with no parameters"
            }
            fn input_schema(&self) -> Value {
                serde_json::json!({"type": "object"})
            }
            fn call(&self, _input: Option<Value>) -> Result<CallToolResult, McpError> {
                Ok(CallToolResult::success(vec![ContentBlock::text(
                    "no params needed".to_string(),
                )]))
            }
        }
        Arc::new(NoParamTool) as Arc<dyn SdForgeTool>
    }

    /// Creates a tool with complex nested schema
    fn create_complex_tool() -> Arc<dyn SdForgeTool> {
        struct ComplexTool;
        impl SdForgeTool for ComplexTool {
            fn name(&self) -> &str {
                "complex"
            }
            fn description(&self) -> &str {
                "Tool with complex nested schema"
            }
            fn input_schema(&self) -> Value {
                serde_json::json!({
                    "type": "object",
                    "properties": {
                        "user": {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string"},
                                "age": {"type": "integer"}
                            },
                            "required": ["name"]
                        }
                    },
                    "required": ["user"]
                })
            }
            fn call(&self, input: Option<Value>) -> Result<CallToolResult, McpError> {
                let input = input.unwrap_or_default();
                let name = input
                    .get("user")
                    .and_then(|u| u.get("name"))
                    .and_then(|n| n.as_str())
                    .unwrap_or("unknown");
                Ok(CallToolResult::success(vec![ContentBlock::text(format!(
                    "Hello, {}!",
                    name
                ))]))
            }
        }
        Arc::new(ComplexTool) as Arc<dyn SdForgeTool>
    }

    // ============================================================================
    // SdForgeMcpServer Tests (ServerHandler integration)
    // ============================================================================

    /// Test: Create server with explicit tools
    #[test]
    fn test_server_creation_with_tools() {
        let tools = vec![
            McpToolInstance::new(create_echo_tool(), ApiMetadata::default()),
            McpToolInstance::new(create_math_tool(), ApiMetadata::default()),
        ];
        let server = SdForgeMcpServer::with_tools(tools);
        assert_eq!(server.tool_count(), 2);
    }

    /// Test: Create empty server
    #[test]
    fn test_server_creation_empty() {
        let server = SdForgeMcpServer::empty();
        assert_eq!(server.tool_count(), 0);
    }

    /// Test: Find tool by name
    #[test]
    fn test_find_tool_by_name() {
        let tools = vec![
            McpToolInstance::new(create_echo_tool(), ApiMetadata::default()),
            McpToolInstance::new(create_math_tool(), ApiMetadata::default()),
        ];
        let server = SdForgeMcpServer::with_tools(tools);

        assert!(server.find_tool("echo").is_some());
        assert!(server.find_tool("math").is_some());
        assert!(server.find_tool("nonexistent").is_none());
    }

    /// Test: Server get_info returns server info
    #[test]
    fn test_server_get_info() {
        let server = SdForgeMcpServer::empty();
        let info = server.get_info();
        assert!(!info.server_info.name.is_empty());
        assert!(!info.server_info.version.is_empty());
    }

    // ============================================================================
    // Tool Execution Tests
    // ============================================================================

    /// Test: Echo tool with valid input
    #[test]
    fn test_echo_tool_execution() {
        let tool = create_echo_tool();
        let input = serde_json::json!({"message": "hello", "count": 3});

        let result = tool.call(Some(input));
        assert!(result.is_ok());

        let response = result.unwrap();
        assert!(!response.content.is_empty());

        // Verify content is text
        assert!(matches!(
            response.content.first(),
            Some(c) if c.as_text().is_some()
        ));
    }

    /// Test: Echo tool with default count
    #[test]
    fn test_echo_tool_default_count() {
        let tool = create_echo_tool();
        let input = serde_json::json!({"message": "test"});

        let result = tool.call(Some(input)).unwrap();
        assert!(!result.content.is_empty());
    }

    /// Test: Math tool addition
    #[test]
    fn test_math_tool_addition() {
        let tool = create_math_tool();
        let input = serde_json::json!({"a": 5, "b": 3, "operation": "add"});

        let result = tool.call(Some(input));
        assert!(result.is_ok());

        let response = result.unwrap();
        assert!(!response.content.is_empty());
    }

    /// Test: Math tool subtraction
    #[test]
    fn test_math_tool_subtraction() {
        let tool = create_math_tool();
        let input = serde_json::json!({"a": 10, "b": 4, "operation": "subtract"});

        let result = tool.call(Some(input)).unwrap();
        assert!(!result.content.is_empty());
    }

    /// Test: Math tool multiplication
    #[test]
    fn test_math_tool_multiplication() {
        let tool = create_math_tool();
        let input = serde_json::json!({"a": 6, "b": 7, "operation": "multiply"});

        let result = tool.call(Some(input)).unwrap();
        assert!(!result.content.is_empty());
    }

    /// Test: Math tool division
    #[test]
    fn test_math_tool_division() {
        let tool = create_math_tool();
        let input = serde_json::json!({"a": 20, "b": 4, "operation": "divide"});

        let result = tool.call(Some(input));
        assert!(result.is_ok());
    }

    /// Test: Math tool division by zero
    #[test]
    fn test_math_tool_division_by_zero() {
        let tool = create_math_tool();
        let input = serde_json::json!({"a": 10, "b": 0, "operation": "divide"});

        let result = tool.call(Some(input));
        assert!(result.is_err());
    }

    /// Test: Math tool missing required field
    #[test]
    fn test_math_tool_missing_field() {
        let tool = create_math_tool();
        let input = serde_json::json!({"a": 5}); // Missing 'b' and 'operation'

        let result = tool.call(Some(input));
        assert!(result.is_err());
    }

    /// Test: Math tool invalid operation
    #[test]
    fn test_math_tool_invalid_operation() {
        let tool = create_math_tool();
        let input = serde_json::json!({"a": 5, "b": 3, "operation": "modulo"});

        let result = tool.call(Some(input));
        assert!(result.is_err());
    }

    /// Test: Math tool no input
    #[test]
    fn test_math_tool_no_input() {
        let tool = create_math_tool();
        let result = tool.call(None);
        assert!(result.is_err());
    }

    /// Test: Error tool returns error
    #[test]
    fn test_error_tool_returns_error() {
        let tool = create_error_tool();
        let result = tool.call(None);
        assert!(result.is_err());
    }

    /// Test: No-param tool works without input
    #[test]
    fn test_no_param_tool() {
        let tool = create_no_param_tool();
        let result = tool.call(None);
        assert!(result.is_ok());

        let response = result.unwrap();
        assert!(!response.content.is_empty());
    }

    /// Test: Complex tool with valid input
    #[test]
    fn test_complex_tool_valid_input() {
        let tool = create_complex_tool();
        let input = serde_json::json!({"user": {"name": "Alice", "age": 30}});

        let result = tool.call(Some(input));
        assert!(result.is_ok());
    }

    /// Test: Complex tool with missing required field
    #[test]
    fn test_complex_tool_missing_field() {
        let tool = create_complex_tool();
        let input = serde_json::json!({"user": {"age": 30}}); // Missing 'name'

        // Complex tool doesn't validate required fields in call, only in schema
        let result = tool.call(Some(input));
        assert!(result.is_ok()); // Uses default "unknown"
    }

    // ============================================================================
    // Input Schema Validation Tests
    // ============================================================================

    /// Test: Echo tool schema structure
    #[test]
    fn test_echo_tool_schema() {
        let tool = create_echo_tool();
        let schema = tool.input_schema();

        assert_eq!(schema["type"], "object");
        assert!(schema["properties"]["message"].is_object());
        assert!(schema["properties"]["count"].is_object());
    }

    /// Test: Math tool schema with required fields
    #[test]
    fn test_math_tool_schema() {
        let tool = create_math_tool();
        let schema = tool.input_schema();

        assert_eq!(schema["type"], "object");
        let required = schema["required"].as_array().unwrap();
        assert!(required.contains(&serde_json::json!("a")));
        assert!(required.contains(&serde_json::json!("b")));
        assert!(required.contains(&serde_json::json!("operation")));

        // Verify enum constraint
        let op_schema = &schema["properties"]["operation"];
        let enum_values = op_schema["enum"].as_array().unwrap();
        assert!(enum_values.contains(&serde_json::json!("add")));
        assert!(enum_values.contains(&serde_json::json!("subtract")));
        assert!(enum_values.contains(&serde_json::json!("multiply")));
        assert!(enum_values.contains(&serde_json::json!("divide")));
    }

    /// Test: Complex tool nested schema
    #[test]
    fn test_complex_tool_nested_schema() {
        let tool = create_complex_tool();
        let schema = tool.input_schema();

        let user_props = &schema["properties"]["user"]["properties"];
        assert!(user_props.get("name").is_some());
        assert!(user_props.get("age").is_some());

        let user_required = schema["properties"]["user"]["required"].as_array().unwrap();
        assert!(user_required.contains(&serde_json::json!("name")));
    }

    /// Test: Schema is valid JSON
    #[test]
    fn test_schema_serialization() {
        let tool = create_complex_tool();
        let schema = tool.input_schema();

        let serialized = serde_json::to_string(&schema).unwrap();
        let deserialized: Value = serde_json::from_str(&serialized).unwrap();
        assert!(deserialized.is_object());
    }

    // ============================================================================
    // Response Content Tests
    // ============================================================================

    /// Test: Text content type
    #[test]
    fn test_text_content_type() {
        let tool = create_echo_tool();
        let input = serde_json::json!({"message": "test"});

        let response = tool.call(Some(input)).unwrap();
        assert!(!response.content.is_empty());

        // Verify content is text type
        assert!(matches!(
            response.content.first(),
            Some(c) if c.as_text().is_some()
        ));
    }

    /// Test: is_error flag is None for successful calls
    #[test]
    fn test_is_error_none_on_success() {
        let tool = create_echo_tool();
        let input = serde_json::json!({"message": "test"});

        let response = tool.call(Some(input)).unwrap();
        assert!(response.is_error.is_none() || response.is_error == Some(false));
    }

    /// Test: meta field is None by default
    #[test]
    fn test_meta_none_by_default() {
        let tool = create_no_param_tool();
        let response = tool.call(None).unwrap();
        assert!(response.meta.is_none());
    }

    /// Test: structured_content is None by default
    #[test]
    fn test_structured_content_none_by_default() {
        let tool = create_no_param_tool();
        let response = tool.call(None).unwrap();
        assert!(response.structured_content.is_none());
    }

    // ============================================================================
    // Multiple Tools Coexistence Tests
    // ============================================================================

    /// Test: Multiple tools can coexist with different names
    #[test]
    fn test_multiple_tools_coexistence() {
        let tools = vec![
            McpToolInstance::new(create_echo_tool(), ApiMetadata::default()),
            McpToolInstance::new(create_math_tool(), ApiMetadata::default()),
            McpToolInstance::new(create_error_tool(), ApiMetadata::default()),
            McpToolInstance::new(create_no_param_tool(), ApiMetadata::default()),
            McpToolInstance::new(create_complex_tool(), ApiMetadata::default()),
        ];
        let server = SdForgeMcpServer::with_tools(tools);

        assert_eq!(server.tool_count(), 5);
        assert!(server.find_tool("echo").is_some());
        assert!(server.find_tool("math").is_some());
        assert!(server.find_tool("error_tool").is_some());
        assert!(server.find_tool("no_param").is_some());
        assert!(server.find_tool("complex").is_some());
    }

    // ============================================================================
    // get_mcp_tools Integration Tests
    // ============================================================================

    /// Test: get_mcp_tools returns instances with accessible metadata
    #[test]
    fn test_get_mcp_tools_metadata_access() {
        let tools = get_mcp_tools();
        for instance in tools {
            let metadata = instance.metadata();
            let _ = metadata.name();
            let _ = metadata.version();
            let _ = metadata.cache_ttl();
            let _ = metadata.is_streaming();
        }
    }

    /// Test: get_mcp_tools returns instances with accessible tools
    #[test]
    fn test_get_mcp_tools_tool_access() {
        let tools = get_mcp_tools();
        for instance in tools {
            let tool = instance.tool();
            let _ = tool.name();
            let _ = tool.description();
            let _ = tool.input_schema();
        }
    }

    // ============================================================================
    // McpToolRegistration Tests
    // ============================================================================

    /// Test: Registration creates valid tool instances
    #[test]
    fn test_registration_creates_valid_tool() {
        fn create_test_tool() -> Arc<dyn SdForgeTool> {
            create_echo_tool()
        }
        fn create_test_metadata() -> ApiMetadata {
            ApiMetadata::default()
        }
        let reg = McpToolRegistration::new("echo", "v1", create_test_tool, create_test_metadata);
        assert_eq!(reg.name, "echo");
        assert_eq!(reg.version, "v1");

        let tool = reg.create();
        assert_eq!(tool.name(), "echo");
    }

    /// Test: Registration metadata is accessible
    #[test]
    fn test_registration_metadata_access() {
        fn create_test_tool() -> Arc<dyn SdForgeTool> {
            create_math_tool()
        }
        fn create_test_metadata() -> ApiMetadata {
            ApiMetadata::new(
                "math".to_string(),
                "v2".to_string(),
                "Math tool".to_string(),
                Some(300),
                false,
            )
        }
        let reg = McpToolRegistration::new("math", "v2", create_test_tool, create_test_metadata);

        let metadata = reg.metadata();
        assert_eq!(metadata.name(), "math");
        assert_eq!(metadata.version(), "v2");
    }
}