mcp: fix bugs handling pointer types (#344)
Some bad merging in #338 led to a bad bug in the handling of structured output for pointer output types. Add a conformance test and fix the bug.
This commit is contained in:
parent
6378df6f24
commit
d8e18b34cf
3 changed files with 46 additions and 4 deletions
|
|
@ -8,6 +8,7 @@ package mcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
|
|
@ -96,6 +97,18 @@ func TestServerConformance(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type input struct {
|
||||||
|
In string `jsonschema:"the input"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type output struct {
|
||||||
|
Out string `jsonschema:"the output"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func structuredTool(ctx context.Context, req *CallToolRequest, args *input) (*CallToolResult, *output, error) {
|
||||||
|
return nil, &output{"Ack " + args.In}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// runServerTest runs the server conformance test.
|
// runServerTest runs the server conformance test.
|
||||||
// It must be executed in a synctest bubble.
|
// It must be executed in a synctest bubble.
|
||||||
func runServerTest(t *testing.T, test *conformanceTest) {
|
func runServerTest(t *testing.T, test *conformanceTest) {
|
||||||
|
|
@ -109,6 +122,8 @@ func runServerTest(t *testing.T, test *conformanceTest) {
|
||||||
Name: "greet",
|
Name: "greet",
|
||||||
Description: "say hi",
|
Description: "say hi",
|
||||||
}, sayHi)
|
}, sayHi)
|
||||||
|
case "structured":
|
||||||
|
AddTool(s, &Tool{Name: "structured"}, structuredTool)
|
||||||
default:
|
default:
|
||||||
t.Fatalf("unknown tool %q", tn)
|
t.Fatalf("unknown tool %q", tn)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -206,7 +206,7 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
|
||||||
)
|
)
|
||||||
if reflect.TypeFor[Out]() != reflect.TypeFor[any]() {
|
if reflect.TypeFor[Out]() != reflect.TypeFor[any]() {
|
||||||
var err error
|
var err error
|
||||||
elemZero, err = setSchema[Out](&t.OutputSchema, &outputResolved)
|
elemZero, err = setSchema[Out](&tt.OutputSchema, &outputResolved)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("output schema: %v", err)
|
return nil, nil, fmt.Errorf("output schema: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -258,9 +258,6 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
|
||||||
res.StructuredContent = elemZero
|
res.StructuredContent = elemZero
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if tt.OutputSchema != nil && elemZero != nil {
|
|
||||||
res.StructuredContent = elemZero
|
|
||||||
}
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
30
mcp/testdata/conformance/server/tools.txtar
vendored
30
mcp/testdata/conformance/server/tools.txtar
vendored
|
|
@ -8,6 +8,7 @@ Fixed bugs:
|
||||||
|
|
||||||
-- tools --
|
-- tools --
|
||||||
greet
|
greet
|
||||||
|
structured
|
||||||
|
|
||||||
-- client --
|
-- client --
|
||||||
{
|
{
|
||||||
|
|
@ -63,6 +64,35 @@ greet
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
"name": "greet"
|
"name": "greet"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"In"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"In": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "the input"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false
|
||||||
|
},
|
||||||
|
"name": "structured",
|
||||||
|
"outputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"Out"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"Out": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "the output"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue