Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion mcp/mcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func TestCancellation(t *testing.T) {
return nil, nil
}
_, cs := basicConnection(t, func(s *Server) {
s.AddTool(&Tool{Name: "slow"}, slowRequest)
s.AddTool(&Tool{Name: "slow", InputSchema: &jsonschema.Schema{}}, slowRequest)
})
defer cs.Close()

Expand Down
14 changes: 9 additions & 5 deletions mcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"encoding/json"
"fmt"
"iter"
"log"
"maps"
"net/url"
"path/filepath"
Expand Down Expand Up @@ -132,13 +131,18 @@ func (s *Server) RemovePrompts(names ...string) {
}

// AddTool adds a [Tool] to the server, or replaces one with the same name.
// The tool's input schema must be non-nil.
// The Tool argument must not be modified after this call.
//
// The tool's input schema must be non-nil. For a tool that takes no input,
// or one where any input is valid, set [Tool.InputSchema] to the empty schema,
// &jsonschema.Schema{}.
func (s *Server) AddTool(t *Tool, h ToolHandler) {
// TODO(jba): This is a breaking behavior change. Add before v0.2.0?
if t.InputSchema == nil {
log.Printf("mcp: tool %q has a nil input schema. This will panic in a future release.", t.Name)
// panic(fmt.Sprintf("adding tool %q: nil input schema", t.Name))
// This prevents the tool author from forgetting to write a schema where
// one should be provided. If we papered over this by supplying the empty
// schema, then every input would be validated and the problem wouldn't be
// discovered until runtime, when the LLM sent bad data.
panic(fmt.Sprintf("adding tool %q: nil input schema", t.Name))
}
if err := addToolErr(s, t, h); err != nil {
panic(err)
Expand Down
6 changes: 4 additions & 2 deletions mcp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/modelcontextprotocol/go-sdk/jsonschema"
)

type testItem struct {
Expand Down Expand Up @@ -230,6 +231,7 @@ func TestServerPaginateVariousPageSizes(t *testing.T) {
}

func TestServerCapabilities(t *testing.T) {
tool := &Tool{Name: "t", InputSchema: &jsonschema.Schema{}}
testCases := []struct {
name string
configureServer func(s *Server)
Expand Down Expand Up @@ -299,7 +301,7 @@ func TestServerCapabilities(t *testing.T) {
{
name: "With tools",
configureServer: func(s *Server) {
s.AddTool(&Tool{Name: "t"}, nil)
s.AddTool(tool, nil)
},
wantCapabilities: &serverCapabilities{
Completions: &completionCapabilities{},
Expand All @@ -313,7 +315,7 @@ func TestServerCapabilities(t *testing.T) {
s.AddPrompt(&Prompt{Name: "p"}, nil)
s.AddResource(&Resource{URI: "file:///r"}, nil)
s.AddResourceTemplate(&ResourceTemplate{URITemplate: "file:///rt"}, nil)
s.AddTool(&Tool{Name: "t"}, nil)
s.AddTool(tool, nil)
},
serverOpts: ServerOptions{
SubscribeHandler: func(ctx context.Context, sp *SubscribeParams) error {
Expand Down
Loading