Skip to content

Commit fbac167

Browse files
author
Zheng Li
committed
add UnmarshalJSON to Tool
1 parent 5dab240 commit fbac167

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

mcp/tools.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,39 @@ func (t Tool) MarshalJSON() ([]byte, error) {
103103
return json.Marshal(m)
104104
}
105105

106+
107+
// UnmarshalJSON implements the json.Unmarshaler interface for Tool.
108+
// It handles unmarshaling InputSchema while supporting raw schema formats.
109+
func (t *Tool) UnmarshalJSON(b []byte) error {
110+
// Temporary structure for decoding
111+
var raw struct {
112+
Name string `json:"name"`
113+
Description string `json:"description"`
114+
InputSchema json.RawMessage `json:"inputSchema"`
115+
}
116+
117+
// Unmarshal into the temporary structure
118+
if err := json.Unmarshal(b, &raw); err != nil {
119+
return err
120+
}
121+
122+
// Assign name and description
123+
t.Name = raw.Name
124+
t.Description = raw.Description
125+
126+
// Try to unmarshal InputSchema into structured format
127+
var schema ToolInputSchema
128+
if err := json.Unmarshal(raw.InputSchema, &schema); err == nil {
129+
// Successfully parsed structured schema
130+
t.InputSchema = schema
131+
t.RawInputSchema = nil
132+
} else {
133+
return err
134+
}
135+
136+
return nil
137+
}
138+
106139
type ToolInputSchema struct {
107140
Type string `json:"type"`
108141
Properties map[string]interface{} `json:"properties,omitempty"`

0 commit comments

Comments
 (0)