Skip to content

Commit 203792d

Browse files
authored
mcp: improve docs around tool handlers (#414)
1 parent 4257528 commit 203792d

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

mcp/server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,22 @@ func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved)
319319
}
320320

321321
// AddTool adds a tool and typed tool handler to the server.
322-
//
323322
// If the tool's input schema is nil, it is set to the schema inferred from the
324323
// In type parameter, using [jsonschema.For]. The In type parameter must be a
325324
// map or a struct, so that its inferred JSON Schema has type "object".
326325
//
327326
// For tools that don't return structured output, Out should be 'any'.
328327
// Otherwise, if the tool's output schema is nil the output schema is set to
329328
// the schema inferred from Out, which must be a map or a struct.
329+
//
330+
// The In argument to the handler will contain the unmarshaled arguments from
331+
// CallToolRequest.Params.Arguments. Most users can ignore the [CallToolRequest]
332+
// argument to the handler.
333+
//
334+
// The handler's Out return value will be used to populate [CallToolResult.StructuredContent].
335+
// If the handler returns a non-nil error, [CallToolResult.IsError] will be set to true,
336+
// and [CallToolResult.Content] will be set to the text of the error.
337+
// Most users can ignore the [CallToolResult] return value.
330338
func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) {
331339
tt, hh, err := toolForErr(t, h)
332340
if err != nil {

mcp/tool.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import (
1515
)
1616

1717
// A ToolHandler handles a call to tools/call.
18-
// [CallToolParams.Arguments] will contain a map[string]any that has been validated
19-
// against the input schema.
18+
// This is a low-level API, for use with [Server.AddTool].
19+
// Most users will write a [ToolHandlerFor] and install it with [AddTool].
2020
type ToolHandler func(context.Context, *CallToolRequest) (*CallToolResult, error)
2121

2222
// A ToolHandlerFor handles a call to tools/call with typed arguments and results.
23+
// Use [AddTool] to add a ToolHandlerFor to a server.
24+
// Most users can ignore the [CallToolRequest] argument and [CallToolResult] return value.
2325
type ToolHandlerFor[In, Out any] func(context.Context, *CallToolRequest, In) (*CallToolResult, Out, error)
2426

2527
// A serverTool is a tool definition that is bound to a tool handler.

0 commit comments

Comments
 (0)