Skip to content

Commit 14e776d

Browse files
authored
mcp: optimize re-marshalling of jsonschema.Schema (#581)
mcp: optimize re-marshalling of jsonschema.Schema Avoid going through function remarshal when InputSchema/OutputSchema is of type *jsonschema.Schema.
1 parent 9d6e307 commit 14e776d

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

mcp/server.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,10 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) {
197197
// discovered until runtime, when the LLM sent bad data.
198198
panic(fmt.Errorf("AddTool %q: missing input schema", t.Name))
199199
}
200-
if s, ok := t.InputSchema.(*jsonschema.Schema); ok && s.Type != "object" {
201-
panic(fmt.Errorf(`AddTool %q: input schema must have type "object"`, t.Name))
200+
if s, ok := t.InputSchema.(*jsonschema.Schema); ok {
201+
if s.Type != "object" {
202+
panic(fmt.Errorf(`AddTool %q: input schema must have type "object"`, t.Name))
203+
}
202204
} else {
203205
var m map[string]any
204206
if err := remarshal(t.InputSchema, &m); err != nil {
@@ -209,8 +211,10 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) {
209211
}
210212
}
211213
if t.OutputSchema != nil {
212-
if s, ok := t.OutputSchema.(*jsonschema.Schema); ok && s.Type != "object" {
213-
panic(fmt.Errorf(`AddTool %q: output schema must have type "object"`, t.Name))
214+
if s, ok := t.OutputSchema.(*jsonschema.Schema); ok {
215+
if s.Type != "object" {
216+
panic(fmt.Errorf(`AddTool %q: output schema must have type "object"`, t.Name))
217+
}
214218
} else {
215219
var m map[string]any
216220
if err := remarshal(t.OutputSchema, &m); err != nil {
@@ -370,10 +374,8 @@ func setSchema[T any](sfield *any, rfield **jsonschema.Resolved) (zero any, err
370374
if err == nil {
371375
*sfield = internalSchema
372376
}
373-
} else {
374-
if err := remarshal(*sfield, &internalSchema); err != nil {
375-
return zero, err
376-
}
377+
} else if err := remarshal(*sfield, &internalSchema); err != nil {
378+
return zero, err
377379
}
378380
if err != nil {
379381
return zero, err

0 commit comments

Comments
 (0)