mcp: validate user-provided output schemas (#408)
toolForErr was ignoring the output schema if the output type was any. That neglected the case where the user provided their own output schema. Fixes #371.
This commit is contained in:
parent
07b9cee8cc
commit
30261728f7
1 changed files with 59 additions and 3 deletions
|
|
@ -212,7 +212,7 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
|
|||
elemZero any // only non-nil if Out is a pointer type
|
||||
outputResolved *jsonschema.Resolved
|
||||
)
|
||||
if reflect.TypeFor[Out]() != reflect.TypeFor[any]() {
|
||||
if t.OutputSchema != nil || reflect.TypeFor[Out]() != reflect.TypeFor[any]() {
|
||||
var err error
|
||||
elemZero, err = setSchema[Out](&tt.OutputSchema, &outputResolved)
|
||||
if err != nil {
|
||||
|
|
@ -302,8 +302,8 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
|
|||
// TODO(rfindley): we really shouldn't ever return 'null' results. Maybe we
|
||||
// should have a jsonschema.Zero(schema) helper?
|
||||
func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved) (zero any, err error) {
|
||||
rt := reflect.TypeFor[T]()
|
||||
if *sfield == nil {
|
||||
rt := reflect.TypeFor[T]()
|
||||
if rt.Kind() == reflect.Pointer {
|
||||
rt = rt.Elem()
|
||||
zero = reflect.Zero(rt).Interface()
|
||||
|
|
|
|||
Loading…
Reference in a new issue