|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestLoadMCPToolDefinitions(t *testing.T) { |
| 8 | + toolJSON := []byte(`{ |
| 9 | + "tools": [ |
| 10 | + { |
| 11 | + "name": "test_tool", |
| 12 | + "description": "test description", |
| 13 | + "inputSchema": { |
| 14 | + "type": "object", |
| 15 | + "$schema": "https://localhost/schema-draft/2025-07", |
| 16 | + "properties": { |
| 17 | + "tags": { |
| 18 | + "type": "array", |
| 19 | + "items": { |
| 20 | + "type": "object", |
| 21 | + "properties": { |
| 22 | + "key": { "type": "string" }, |
| 23 | + "value": { "type": "string" } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + }, |
| 29 | + "outputSchema": { |
| 30 | + "type": "object", |
| 31 | + "$schema": "https://localhost/schema-draft/2025-07", |
| 32 | + "properties": { |
| 33 | + "result": { "type": "string" } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + ] |
| 38 | + }`) |
| 39 | + |
| 40 | + tools, err := LoadMCPToolDefinitions(toolJSON) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("Failed to load tool definitions: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + if len(tools) != 1 { |
| 46 | + t.Fatalf("Expected 1 tool, got %d", len(tools)) |
| 47 | + } |
| 48 | + |
| 49 | + tool := tools["test_tool"] |
| 50 | + if tool == nil { |
| 51 | + t.Fatal("Tool 'test_tool' not found") |
| 52 | + } |
| 53 | + |
| 54 | + if tool.Name != "test_tool" { |
| 55 | + t.Errorf("Expected name 'test_tool', got '%s'", tool.Name) |
| 56 | + } |
| 57 | + |
| 58 | + inputSchema := tool.InputSchema |
| 59 | + outputSchema := tool.OutputSchema |
| 60 | + schemaVersion := "https://localhost/schema-draft/2025-07" |
| 61 | + |
| 62 | + if inputSchema.Schema != schemaVersion { |
| 63 | + t.Errorf("Expected input schema version %q, got %q", schemaVersion, inputSchema.Schema) |
| 64 | + } |
| 65 | + if outputSchema.Schema != schemaVersion { |
| 66 | + t.Errorf("Expected output schema version %q, got %q", schemaVersion, outputSchema.Schema) |
| 67 | + } |
| 68 | + |
| 69 | + tagsProp, ok := inputSchema.Properties["tags"] |
| 70 | + if !ok { |
| 71 | + t.Fatal("Property 'tags' not found in inputSchema") |
| 72 | + } |
| 73 | + |
| 74 | + if tagsProp.Type() != "array" { |
| 75 | + t.Errorf("Expected tags type 'array', got '%s'", tagsProp.Type()) |
| 76 | + } |
| 77 | + |
| 78 | + arraySchema, ok := tagsProp.(*SchemaArray) |
| 79 | + if !ok { |
| 80 | + t.Fatal("Expected SchemaArray for tags") |
| 81 | + } |
| 82 | + |
| 83 | + if arraySchema.Items == nil { |
| 84 | + t.Fatal("Expected items schema in array, got nil") |
| 85 | + } |
| 86 | + |
| 87 | + itemSchema := arraySchema.Items |
| 88 | + if itemSchema.Type() != "object" { |
| 89 | + t.Errorf("Expected item type 'object', got '%s'", itemSchema.Type()) |
| 90 | + } |
| 91 | + |
| 92 | + objectSchema, ok := itemSchema.(*SchemaObject) |
| 93 | + if !ok { |
| 94 | + t.Fatal("Expected SchemaObject for item") |
| 95 | + } |
| 96 | + |
| 97 | + if _, ok := objectSchema.Properties["key"]; !ok { |
| 98 | + t.Error("Property 'key' not found in item schema") |
| 99 | + } |
| 100 | +} |
0 commit comments