Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions mcp/mcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,4 +938,30 @@ func TestKeepAliveFailure(t *testing.T) {
t.Errorf("expected connection to be closed by keepalive, but it wasn't. Last error: %v", err)
}

func TestAddTool_DuplicateNoPanicAndNoDuplicate(t *testing.T) {
// Adding the same tool pointer twice should not panic and should not
// produce duplicates in the server's tool list.
_, cs := basicConnection(t, func(s *Server) {
tool := &Tool{Name: "dup", InputSchema: &jsonschema.Schema{}}
s.AddTool(tool, nopHandler)
s.AddTool(tool, nopHandler)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug was only in the json schema resolution, right? Meaning, it would also be reprouced if we use a different Tool instance? In that case, use a different description and assert that the tool value is the last tool that was added, by checking its description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. Made the change and tests are passing.

})
defer cs.Close()

ctx := context.Background()
res, err := cs.ListTools(ctx, nil)
if err != nil {
t.Fatal(err)
}
var count int
for _, tt := range res.Tools {
if tt.Name == "dup" {
count++
}
}
if count != 1 {
t.Fatalf("expected exactly one 'dup' tool, got %d", count)
}
}

var testImpl = &Implementation{Name: "test", Version: "v1.0.0"}
22 changes: 21 additions & 1 deletion mcp/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,31 @@ func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved)
var err error
if *sfield == nil {
*sfield, err = jsonschema.For[T](nil)
if err != nil {
return err
}
}
// Resolve operates with internal state and cannot be called twice on the same
// Schema instance. Deep-copy the schema before resolving to avoid mutating the
// original (which may be reused when adding the same tool again).
cloned, err := func(orig *jsonschema.Schema) (*jsonschema.Schema, error) {
if orig == nil {
return nil, nil
}
b, err := json.Marshal(orig)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the best way to handle this, but @jba will know. Added him as a reviewer.

if err != nil {
return nil, err
}
var cp jsonschema.Schema
if err := json.Unmarshal(b, &cp); err != nil {
return nil, err
}
return &cp, nil
}(*sfield)
if err != nil {
return err
}
*rfield, err = (*sfield).Resolve(&jsonschema.ResolveOptions{ValidateDefaults: true})
*rfield, err = cloned.Resolve(&jsonschema.ResolveOptions{ValidateDefaults: true})
return err
}

Expand Down