mcp: improve docs around tool handlers (#414)

This commit is contained in:
Jonathan Amsterdam 2025-09-04 11:13:17 -04:00 • committed by GitHub
commit 203792dfdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View file

@ -319,7 +319,6 @@ func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved)
}
// AddTool adds a tool and typed tool handler to the server.
//
// If the tool's input schema is nil, it is set to the schema inferred from the
// In type parameter, using [jsonschema.For]. The In type parameter must be a
// map or a struct, so that its inferred JSON Schema has type "object".
@ -327,6 +326,15 @@ func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved)
// For tools that don't return structured output, Out should be 'any'.
// Otherwise, if the tool's output schema is nil the output schema is set to
// the schema inferred from Out, which must be a map or a struct.
//
// The In argument to the handler will contain the unmarshaled arguments from
// CallToolRequest.Params.Arguments. Most users can ignore the [CallToolRequest]
// argument to the handler.
//
// The handler's Out return value will be used to populate [CallToolResult.StructuredContent].
// If the handler returns a non-nil error, [CallToolResult.IsError] will be set to true,
// and [CallToolResult.Content] will be set to the text of the error.
// Most users can ignore the [CallToolResult] return value.
func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) {
tt, hh, err := toolForErr(t, h)
if err != nil {

View file

@ -15,11 +15,13 @@ import (
)
// A ToolHandler handles a call to tools/call.
// [CallToolParams.Arguments] will contain a map[string]any that has been validated
// against the input schema.
// This is a low-level API, for use with [Server.AddTool].
// Most users will write a [ToolHandlerFor] and install it with [AddTool].
type ToolHandler func(context.Context, *CallToolRequest) (*CallToolResult, error)
// A ToolHandlerFor handles a call to tools/call with typed arguments and results.
// Use [AddTool] to add a ToolHandlerFor to a server.
// Most users can ignore the [CallToolRequest] argument and [CallToolResult] return value.
type ToolHandlerFor[In, Out any] func(context.Context, *CallToolRequest, In) (*CallToolResult, Out, error)
// A serverTool is a tool definition that is bound to a tool handler.