From 48b344dec987830e2db49f92a01ebb134a239a5f Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Sat, 18 Oct 2025 12:35:27 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`iss?= =?UTF-8?q?ue-345-withany`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docstrings generation was requested by @otaviof. * https://github.com/mark3labs/mcp-go/pull/618#issuecomment-3418401266 The following files were modified: * `examples/typed_tools/main.go` * `mcp/tools.go` --- examples/typed_tools/main.go | 20 ++++++++++++++++++-- mcp/tools.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/examples/typed_tools/main.go b/examples/typed_tools/main.go index f9bd3c21..bae49316 100644 --- a/examples/typed_tools/main.go +++ b/examples/typed_tools/main.go @@ -18,8 +18,13 @@ type GreetingArgs struct { Location string `json:"location"` Timezone string `json:"timezone"` } `json:"metadata"` + 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( @@ -61,6 +66,9 @@ func main() { }, }), ), + mcp.WithAny("any_data", + mcp.Description("Any kind of data, e.g., an integer"), + ), ) // Add tool handler using the typed handler @@ -72,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 @@ -101,5 +113,9 @@ func typedGreetingHandler(ctx context.Context, request mcp.CallToolRequest, args } } + if args.AnyData != nil { + greeting += fmt.Sprintf(" I also received some other data: %v.", args.AnyData) + } + return mcp.NewToolResultText(greeting), nil -} +} \ No newline at end of file diff --git a/mcp/tools.go b/mcp/tools.go index fc4a6447..5e357e2d 100644 --- a/mcp/tools.go +++ b/mcp/tools.go @@ -1092,7 +1092,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{ @@ -1113,7 +1116,30 @@ func WithArray(name string, opts ...PropertyOption) ToolOption { } } -// Properties defines the properties for an object schema +// WithAny adds a property of any type to the tool schema. +// 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{} + + for _, opt := range opts { + opt(schema) + } + + // Remove required from property schema and add to InputSchema.required + if required, ok := schema["required"].(bool); ok && required { + delete(schema, "required") + t.InputSchema.Required = append(t.InputSchema.Required, name) + } + + t.InputSchema.Properties[name] = 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 @@ -1279,4 +1305,4 @@ func WithBooleanItems(opts ...PropertyOption) PropertyOption { schema["items"] = itemSchema } -} +} \ No newline at end of file From abf69e47ea273af21aebebacbfebb662923200b0 Mon Sep 17 00:00:00 2001 From: Ed Zynda Date: Mon, 20 Oct 2025 17:04:49 +0300 Subject: [PATCH 2/2] Update tools.go --- mcp/tools.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mcp/tools.go b/mcp/tools.go index 004ac406..80ae5091 100644 --- a/mcp/tools.go +++ b/mcp/tools.go @@ -1091,7 +1091,6 @@ func WithObject(name string, opts ...PropertyOption) ToolOption { } } -// WithArray adds an array property to the tool schema. // 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 @@ -1304,4 +1303,4 @@ func WithBooleanItems(opts ...PropertyOption) PropertyOption { schema["items"] = itemSchema } -} \ No newline at end of file +}