Skip to content

Commit 7121652

Browse files
committed
unexport ToolFor
1 parent 8f8587f commit 7121652

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

mcp/client_list_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ func TestList(t *testing.T) {
2424
t.Run("tools", func(t *testing.T) {
2525
var wantTools []*mcp.Tool
2626
for _, name := range []string{"apple", "banana", "cherry"} {
27-
t := &mcp.Tool{Name: name, Description: name + " tool"}
28-
// Previously, we could add t to wantTools, because AddTool modified
29-
// it. Now, AddTool gets a copy.
30-
tt, h := mcp.ToolFor(t, SayHi)
27+
tt := &mcp.Tool{Name: name, Description: name + " tool"}
28+
mcp.AddTool(server, tt, SayHi)
29+
is, err := jsonschema.For[SayHiParams](nil)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
tt.InputSchema = is
3134
wantTools = append(wantTools, tt)
32-
server.AddTool(tt, h)
3335
}
3436
t.Run("list", func(t *testing.T) {
3537
res, err := clientSession.ListTools(ctx, nil)

mcp/server.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ func (s *Server) RemovePrompts(names ...string) {
150150
// When the handler is invoked as part of a CallTool request, req.Params.Arguments
151151
// will be a json.RawMessage. Unmarshaling the arguments and validating them against the
152152
// input schema are the handler author's responsibility.
153+
//
154+
// Most users will prefer the top-level function [AddTool].
153155
func (s *Server) AddTool(t *Tool, h ToolHandler) {
154156
if t.InputSchema == nil {
155157
// This prevents the tool author from forgetting to write a schema where
@@ -167,15 +169,15 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) {
167169
func() bool { s.tools.add(st); return true })
168170
}
169171

170-
// ToolFor returns a shallow copy of t and a [ToolHandler] that wraps h.
172+
// toolFor returns a shallow copy of t and a [ToolHandler] that wraps h.
171173
// If the tool's input schema is nil, it is set to the schema inferred from the In
172174
// type parameter, using [jsonschema.For].
173175
// If the tool's output schema is nil and the Out type parameter is not the empty
174176
// interface, then the output schema is set to the schema inferred from Out.
175177
//
176-
// Most users will call [AddTool]. Use [ToolFor] if you wish to wrap the ToolHandler
178+
// Most users will call [AddTool]. Use [toolFor] if you wish to wrap the ToolHandler
177179
// before calling [Server.AddTool].
178-
func ToolFor[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHandler) {
180+
func toolFor[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHandler) {
179181
tt, hh, err := toolForErr(t, h)
180182
if err != nil {
181183
panic(fmt.Sprintf("ToolFor: tool %q: %v", t.Name, err))
@@ -250,9 +252,15 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
250252
return &tt, th, nil
251253
}
252254

253-
// AddTool is a convenience for s.AddTool(ToolFor(t, h)).
255+
// AddTool adds a tool and handler to the server.
256+
//
257+
// A shallow copy of the tool is made first.
258+
// If the tool's input schema is nil, the copy's input schema is set to the schema
259+
// inferred from the In type parameter, using [jsonschema.For].
260+
// If the tool's output schema is nil and the Out type parameter is not the empty
261+
// interface, then the copy's output schema is set to the schema inferred from Out.
254262
func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) {
255-
s.AddTool(ToolFor(t, h))
263+
s.AddTool(toolFor(t, h))
256264
}
257265

258266
// RemoveTools removes the tools with the given names.

mcp/tool.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ import (
1818
// against the input schema.
1919
type ToolHandler func(context.Context, *ServerRequest[*CallToolParams]) (*CallToolResult, error)
2020

21-
// type CallToolRequest struct {
22-
// Request *ServerRequest
23-
// Params *CallToolParams
24-
// }
25-
2621
// A ToolHandlerFor handles a call to tools/call with typed arguments and results.
2722
type ToolHandlerFor[In, Out any] func(context.Context, *ServerRequest[*CallToolParams], In) (*CallToolResult, Out, error)
2823

0 commit comments

Comments
 (0)