mcp: export ToolFor (#347)
This is needed so clients can modify tool schemas, and not merely to wrap ToolHandlers. (A consequence of returning a copy of Tool instead of modifying it.)
This commit is contained in:
parent
dd8e3af18d
commit
62db914082
2 changed files with 5 additions and 6 deletions
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
package mcp
|
package mcp
|
||||||
|
|
||||||
// TODO: expand the aliases
|
|
||||||
type (
|
type (
|
||||||
CallToolRequest = ServerRequest[*CallToolParams]
|
CallToolRequest = ServerRequest[*CallToolParams]
|
||||||
CompleteRequest = ServerRequest[*CompleteParams]
|
CompleteRequest = ServerRequest[*CompleteParams]
|
||||||
|
|
|
||||||
|
|
@ -171,15 +171,15 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) {
|
||||||
func() bool { s.tools.add(st); return true })
|
func() bool { s.tools.add(st); return true })
|
||||||
}
|
}
|
||||||
|
|
||||||
// toolFor returns a shallow copy of t and a [ToolHandler] that wraps h.
|
// ToolFor returns a shallow copy of t and a [ToolHandler] that wraps h.
|
||||||
// If the tool's input schema is nil, it is set to the schema inferred from the In
|
// If the tool's input schema is nil, it is set to the schema inferred from the In
|
||||||
// type parameter, using [jsonschema.For].
|
// type parameter, using [jsonschema.For].
|
||||||
// If the tool's output schema is nil and the Out type parameter is not the empty
|
// If the tool's output schema is nil and the Out type parameter is not the empty
|
||||||
// interface, then the output schema is set to the schema inferred from Out.
|
// interface, then the output schema is set to the schema inferred from Out.
|
||||||
//
|
//
|
||||||
// Most users will call [AddTool]. Use [toolFor] if you wish to wrap the ToolHandler
|
// Most users will call [AddTool]. Use [ToolFor] if you wish to modify the tool's
|
||||||
// before calling [Server.AddTool].
|
// schemas or wrap the ToolHandler before calling [Server.AddTool].
|
||||||
func toolFor[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHandler) {
|
func ToolFor[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHandler) {
|
||||||
tt, hh, err := toolForErr(t, h)
|
tt, hh, err := toolForErr(t, h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("ToolFor: tool %q: %v", t.Name, err))
|
panic(fmt.Sprintf("ToolFor: tool %q: %v", t.Name, err))
|
||||||
|
|
@ -303,7 +303,7 @@ func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved)
|
||||||
// If the tool's output schema is nil and the Out type parameter is not the empty
|
// If the tool's output schema is nil and the Out type parameter is not the empty
|
||||||
// interface, then the copy's output schema is set to the schema inferred from Out.
|
// interface, then the copy's output schema is set to the schema inferred from Out.
|
||||||
func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) {
|
func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) {
|
||||||
s.AddTool(toolFor(t, h))
|
s.AddTool(ToolFor(t, h))
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveTools removes the tools with the given names.
|
// RemoveTools removes the tools with the given names.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue