all: add examples of customizing tool schemas, and clarify documentation (#421)

Add example/server/toolschemas, which demonstrates various ways to
customize tool schemas, and illustrates the differences between
ToolHandler and ToolHandlerFor.

Fixes #385
Fixes #386
For #368
This commit is contained in:
Robert Findley 2025-09-05 14:11:11 -04:00 • committed by GitHub
commit 40b6bd30f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -321,22 +321,18 @@ 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
// In type parameter, using [jsonschema.For]. The In type argument must be a
// map or a struct, so that its inferred JSON Schema has type "object".
//
// 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.
// If the tool's output schema is nil, and the Out type is not 'any', the
// output schema is set to the schema inferred from the Out type argument,
// which also must be a map or 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.
// Unlike [Server.AddTool], AddTool does a lot automatically, and forces tools
// to conform to the MCP spec. See [ToolHandlerFor] for a detailed description
// of this automatic behavior.
func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) {
tt, hh, err := toolForErr(t, h)
if err != nil {