Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions examples/typed_tools/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type GreetingArgs struct {
AnyData any `json:"any_data"`
}

// main starts the MCP-based example server, registers a typed "greeting" tool, and serves it over standard I/O.
//
// The registered tool exposes a schema for typed inputs (name, age, is_vip, languages, metadata, and any_data)
// and uses a typed handler to produce personalized greetings. If the server fails to start, an error is printed to stdout.
func main() {
// Create a new MCP server
s := server.NewMCPServer(
Expand Down Expand Up @@ -76,7 +80,11 @@ func main() {
}
}

// Our typed handler function that receives strongly-typed arguments
// typedGreetingHandler constructs a personalized greeting from the provided GreetingArgs and returns it as a text tool result.
//
// If args.Name is empty the function returns a tool error result with the message "name is required" and a nil error.
// The returned greeting may include the caller's age, a VIP acknowledgement, the number and list of spoken languages,
// location and timezone from metadata, and a formatted representation of AnyData when present.
func typedGreetingHandler(ctx context.Context, request mcp.CallToolRequest, args GreetingArgs) (*mcp.CallToolResult, error) {
if args.Name == "" {
return mcp.NewToolResultError("name is required"), nil
Expand Down Expand Up @@ -110,4 +118,4 @@ func typedGreetingHandler(ctx context.Context, request mcp.CallToolRequest, args
}

return mcp.NewToolResultText(greeting), nil
}
}
14 changes: 9 additions & 5 deletions mcp/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,10 @@ func WithObject(name string, opts ...PropertyOption) ToolOption {
}
}

// WithArray adds an array property to the tool schema.
// It accepts property options to configure the array property's behavior and constraints.
// WithArray returns a ToolOption that adds an array-typed property with the given name to a Tool's input schema.
// It applies provided PropertyOption functions to configure the property's schema, moves a `required` flag
// from the property schema into the Tool's InputSchema.Required slice when present, and registers the resulting
// schema under InputSchema.Properties[name].
func WithArray(name string, opts ...PropertyOption) ToolOption {
return func(t *Tool) {
schema := map[string]any{
Expand All @@ -1113,8 +1115,9 @@ func WithArray(name string, opts ...PropertyOption) ToolOption {
}
}

// WithAny adds a property of any type to the tool schema.
// It accepts property options to configure the property's behavior and constraints.
// WithAny adds an input property named name with no predefined JSON Schema type to the Tool's input schema.
// The returned ToolOption applies the provided PropertyOption functions to the property's schema, moves a property-level
// `required` flag into the Tool's InputSchema.Required list if present, and stores the resulting schema under InputSchema.Properties[name].
func WithAny(name string, opts ...PropertyOption) ToolOption {
return func(t *Tool) {
schema := map[string]any{}
Expand All @@ -1133,7 +1136,8 @@ func WithAny(name string, opts ...PropertyOption) ToolOption {
}
}

// Properties defines the properties for an object schema
// Properties sets the "properties" map for an object schema.
// The returned PropertyOption stores the provided map under the schema's "properties" key.
func Properties(props map[string]any) PropertyOption {
return func(schema map[string]any) {
schema["properties"] = props
Expand Down
Loading