Skip to content

Commit b795d99

Browse files
committed
fix parsing for when items: true
1 parent 2c45a43 commit b795d99

File tree

2 files changed

+114
-8
lines changed

2 files changed

+114
-8
lines changed

cmd/src/mcp_parse.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ type SchemaObject struct {
5555
func (s SchemaObject) Type() string { return s.Kind }
5656

5757
type SchemaArray struct {
58-
Kind string `json:"type"`
59-
Description string `json:"description"`
60-
Items []SchemaValue `json:"items"`
58+
Kind string `json:"type"`
59+
Description string `json:"description"`
60+
Items SchemaValue `json:"items,omitempty"`
6161
}
6262

6363
func (s SchemaArray) Type() string { return s.Kind }
@@ -101,13 +101,19 @@ func (p *Parser) parseSchema(r *RawSchema) SchemaValue {
101101
Properties: p.parseProperties(r.Properties),
102102
}
103103
case "array":
104-
var items []SchemaValue
104+
var items SchemaValue
105105
if len(r.Items) > 0 {
106-
var itemRaw RawSchema
107-
if err := json.Unmarshal(r.Items, &itemRaw); err == nil {
108-
items = append(items, p.parseSchema(&itemRaw))
106+
var boolItems bool
107+
if err := json.Unmarshal(r.Items, &boolItems); err == nil {
108+
// Sometimes items is defined as "items: true", so we handle it here and
109+
// consider it "empty" array
109110
} else {
110-
p.errors = append(p.errors, fmt.Errorf("failed to unmarshal array items: %w", err))
111+
var itemRaw RawSchema
112+
if err := json.Unmarshal(r.Items, &itemRaw); err == nil {
113+
items = p.parseSchema(&itemRaw)
114+
} else {
115+
p.errors = append(p.errors, fmt.Errorf("failed to unmarshal array items: %w", err))
116+
}
111117
}
112118
}
113119
return &SchemaArray{

cmd/src/mcp_parse_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)