Skip to content

Commit c273e1a

Browse files
📝 Add docstrings to issue-345-withany (#619)
* 📝 Add docstrings to `issue-345-withany` Docstrings generation was requested by @otaviof. * #618 (comment) The following files were modified: * `examples/typed_tools/main.go` * `mcp/tools.go` * Update tools.go --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Ed Zynda <[email protected]>
1 parent 881e142 commit c273e1a

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

examples/typed_tools/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type GreetingArgs struct {
2121
AnyData any `json:"any_data"`
2222
}
2323

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

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

112120
return mcp.NewToolResultText(greeting), nil
113-
}
121+
}

mcp/tools.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,10 @@ func WithObject(name string, opts ...PropertyOption) ToolOption {
10911091
}
10921092
}
10931093

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

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

1136-
// Properties defines the properties for an object schema
1139+
// Properties sets the "properties" map for an object schema.
1140+
// The returned PropertyOption stores the provided map under the schema's "properties" key.
11371141
func Properties(props map[string]any) PropertyOption {
11381142
return func(schema map[string]any) {
11391143
schema["properties"] = props

0 commit comments

Comments
 (0)