Skip to content

Commit 063fb12

Browse files
authored
mcp: unexport ToolFor (#402)
The rationale is provided on the issue. Also, document the low-level nature of Server.AddTool. (drive-by change) Fixes #401.
1 parent 7769b2a commit 063fb12

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

mcp/server.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,18 @@ func (s *Server) RemovePrompts(names ...string) {
154154
// If present, the output schema must also have type "object".
155155
//
156156
// When the handler is invoked as part of a CallTool request, req.Params.Arguments
157-
// will be a json.RawMessage. Unmarshaling the arguments and validating them against the
158-
// input schema are the handler author's responsibility.
157+
// will be a json.RawMessage.
159158
//
160-
// Most users should use the top-level function [AddTool].
159+
// Unmarshaling the arguments and validating them against the input schema are the
160+
// caller's responsibility.
161+
//
162+
// Validating the result against the output schema, if any, is the caller's responsibility.
163+
//
164+
// Setting the result's Content, StructuredContent and IsError fields are the caller's
165+
// responsibility.
166+
//
167+
// Most users should use the top-level function [AddTool], which handles all these
168+
// responsibilities.
161169
func (s *Server) AddTool(t *Tool, h ToolHandler) {
162170
if t.InputSchema == nil {
163171
// This prevents the tool author from forgetting to write a schema where
@@ -181,26 +189,6 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) {
181189
func() bool { s.tools.add(st); return true })
182190
}
183191

184-
// ToolFor returns a shallow copy of t and a [ToolHandler] that wraps h.
185-
//
186-
// If the tool's input schema is nil, it is set to the schema inferred from the In
187-
// type parameter, using [jsonschema.For]. The In type parameter must be a map
188-
// or a struct, so that its inferred JSON Schema has type "object".
189-
//
190-
// For tools that don't return structured output, Out should be 'any'.
191-
// Otherwise, if the tool's output schema is nil the output schema is set to
192-
// the schema inferred from Out, which must be a map or a struct.
193-
//
194-
// Most users will call [AddTool]. Use [ToolFor] if you wish to modify the
195-
// tool's schemas or wrap the ToolHandler before calling [Server.AddTool].
196-
func ToolFor[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHandler) {
197-
tt, hh, err := toolForErr(t, h)
198-
if err != nil {
199-
panic(fmt.Sprintf("ToolFor: tool %q: %v", t.Name, err))
200-
}
201-
return tt, hh
202-
}
203-
204192
// TODO(v0.3.0): test
205193
func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHandler, error) {
206194
tt := *t
@@ -335,10 +323,12 @@ func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved)
335323
// For tools that don't return structured output, Out should be 'any'.
336324
// Otherwise, if the tool's output schema is nil the output schema is set to
337325
// the schema inferred from Out, which must be a map or a struct.
338-
//
339-
// It is a convenience for s.AddTool(ToolFor(t, h)).
340326
func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) {
341-
s.AddTool(ToolFor(t, h))
327+
tt, hh, err := toolForErr(t, h)
328+
if err != nil {
329+
panic(fmt.Sprintf("AddTool: tool %q: %v", t.Name, err))
330+
}
331+
s.AddTool(tt, hh)
342332
}
343333

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

0 commit comments

Comments
 (0)