@@ -145,15 +145,17 @@ func (s *Server) RemovePrompts(names ...string) {
145
145
// AddTool adds a [Tool] to the server, or replaces one with the same name.
146
146
// The Tool argument must not be modified after this call.
147
147
//
148
- // The tool's input schema must be non-nil. For a tool that takes no input,
149
- // or one where any input is valid, set [Tool.InputSchema] to the empty schema,
150
- // &jsonschema.Schema{}.
148
+ // The tool's input schema must be non-nil and have the type "object". For a tool
149
+ // that takes no input, or one where any input is valid, set [Tool.InputSchema] to
150
+ // &jsonschema.Schema{Type: "object"}.
151
+ //
152
+ // If present, the output schema must also have type "object".
151
153
//
152
154
// When the handler is invoked as part of a CallTool request, req.Params.Arguments
153
155
// will be a json.RawMessage. Unmarshaling the arguments and validating them against the
154
156
// input schema are the handler author's responsibility.
155
157
//
156
- // Most users will prefer the top-level function [AddTool].
158
+ // Most users should use the top-level function [AddTool].
157
159
func (s * Server ) AddTool (t * Tool , h ToolHandler ) {
158
160
if t .InputSchema == nil {
159
161
// This prevents the tool author from forgetting to write a schema where
@@ -165,6 +167,9 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) {
165
167
if t .InputSchema .Type != "object" {
166
168
panic (fmt .Errorf (`AddTool %q: input schema must have type "object"` , t .Name ))
167
169
}
170
+ if t .OutputSchema != nil && t .OutputSchema .Type != "object" {
171
+ panic (fmt .Errorf (`AddTool %q: output schema must have type "object"` , t .Name ))
172
+ }
168
173
st := & serverTool {tool : t , handler : h }
169
174
// Assume there was a change, since add replaces existing tools.
170
175
// (It's possible a tool was replaced with an identical one, but not worth checking.)
@@ -176,9 +181,12 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) {
176
181
177
182
// ToolFor returns a shallow copy of t and a [ToolHandler] that wraps h.
178
183
// If the tool's input schema is nil, it is set to the schema inferred from the In
179
- // type parameter, using [jsonschema.For].
184
+ // type parameter, using [jsonschema.For]. The In type parameter must be a map
185
+ // or a struct, so that its inferred JSON Schema has type "object".
186
+ //
180
187
// If the tool's output schema is nil and the Out type parameter is not the empty
181
- // interface, then the output schema is set to the schema inferred from Out.
188
+ // interface, then the output schema is set to the schema inferred from Out, which
189
+ // must be a map or a struct.
182
190
//
183
191
// Most users will call [AddTool]. Use [ToolFor] if you wish to modify the tool's
184
192
// schemas or wrap the ToolHandler before calling [Server.AddTool].
@@ -305,12 +313,7 @@ func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved)
305
313
}
306
314
307
315
// AddTool adds a tool and handler to the server.
308
- //
309
- // A shallow copy of the tool is made first.
310
- // If the tool's input schema is nil, the copy's input schema is set to the schema
311
- // inferred from the In type parameter, using [jsonschema.For].
312
- // If the tool's output schema is nil and the Out type parameter is not the empty
313
- // interface, then the copy's output schema is set to the schema inferred from Out.
316
+ // It is a convenience for s.AddTool(ToolFor(t, h)).
314
317
func AddTool [In , Out any ](s * Server , t * Tool , h ToolHandlerFor [In , Out ]) {
315
318
s .AddTool (ToolFor (t , h ))
316
319
}
0 commit comments