Skip to content

Commit 78f35a5

Browse files
authored
add UnmarshalJSON to Tool (#35)
* add UnmarshalJSON to Tool * #35 (comment) * add test --------- Co-authored-by: Zheng Li <>
1 parent 56273d2 commit 78f35a5

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

mcp/tools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type Tool struct {
7272
// A human-readable description of the tool.
7373
Description string `json:"description,omitempty"`
7474
// A JSON Schema object defining the expected parameters for the tool.
75-
InputSchema ToolInputSchema `json:"-"` // Hide this from JSON marshaling
75+
InputSchema ToolInputSchema `json:"inputSchema"`
7676
// Alternative to InputSchema - allows arbitrary JSON Schema to be provided
7777
RawInputSchema json.RawMessage `json:"-"` // Hide this from JSON marshaling
7878
}

mcp/tools_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,72 @@ func TestToolWithRawSchema(t *testing.T) {
7373
assert.True(t, ok)
7474
assert.Contains(t, required, "query")
7575
}
76+
77+
func TestUnmarshalToolWithRawSchema(t *testing.T) {
78+
// Create a complex raw schema
79+
rawSchema := json.RawMessage(`{
80+
"type": "object",
81+
"properties": {
82+
"query": {"type": "string", "description": "Search query"},
83+
"limit": {"type": "integer", "minimum": 1, "maximum": 50}
84+
},
85+
"required": ["query"]
86+
}`)
87+
88+
// Create a tool with raw schema
89+
tool := NewToolWithRawSchema("search-tool", "Search API", rawSchema)
90+
91+
// Marshal to JSON
92+
data, err := json.Marshal(tool)
93+
assert.NoError(t, err)
94+
95+
// Unmarshal to verify the structure
96+
var toolUnmarshalled Tool
97+
err = json.Unmarshal(data, &toolUnmarshalled)
98+
assert.NoError(t, err)
99+
100+
// Verify tool properties
101+
assert.Equal(t, tool.Name, toolUnmarshalled.Name)
102+
assert.Equal(t, tool.Description, toolUnmarshalled.Description)
103+
104+
// Verify schema was properly included
105+
assert.Equal(t, "object", toolUnmarshalled.InputSchema.Type)
106+
assert.Contains(t, toolUnmarshalled.InputSchema.Properties, "query")
107+
assert.Subset(t, toolUnmarshalled.InputSchema.Properties["query"], map[string]interface{}{
108+
"type": "string",
109+
"description": "Search query",
110+
})
111+
assert.Contains(t, toolUnmarshalled.InputSchema.Properties, "limit")
112+
assert.Subset(t, toolUnmarshalled.InputSchema.Properties["limit"], map[string]interface{}{
113+
"type": "integer",
114+
"minimum": 1.0,
115+
"maximum": 50.0,
116+
})
117+
assert.Subset(t, toolUnmarshalled.InputSchema.Required, []string{"query"})
118+
}
119+
120+
func TestUnmarshalToolWithoutRawSchema(t *testing.T) {
121+
// Create a tool with both schemas set
122+
tool := NewTool("dual-schema-tool",
123+
WithDescription("A tool with both schemas set"),
124+
WithString("input", Description("Test input")),
125+
)
126+
127+
data, err := json.Marshal(tool)
128+
assert.Nil(t, err)
129+
130+
// Unmarshal to verify the structure
131+
var toolUnmarshalled Tool
132+
err = json.Unmarshal(data, &toolUnmarshalled)
133+
assert.NoError(t, err)
134+
135+
// Verify tool properties
136+
assert.Equal(t, tool.Name, toolUnmarshalled.Name)
137+
assert.Equal(t, tool.Description, toolUnmarshalled.Description)
138+
assert.Subset(t, toolUnmarshalled.InputSchema.Properties["input"], map[string]interface{}{
139+
"type": "string",
140+
"description": "Test input",
141+
})
142+
assert.Empty(t, toolUnmarshalled.InputSchema.Required)
143+
assert.Empty(t, toolUnmarshalled.RawInputSchema)
144+
}

0 commit comments

Comments
 (0)