Skip to content

Commit d8e18b3

Browse files
authored
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.
1 parent 6378df6 commit d8e18b3

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

mcp/conformance_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package mcp
88

99
import (
1010
"bytes"
11+
"context"
1112
"encoding/json"
1213
"errors"
1314
"flag"
@@ -96,6 +97,18 @@ func TestServerConformance(t *testing.T) {
9697
}
9798
}
9899

100+
type input struct {
101+
In string `jsonschema:"the input"`
102+
}
103+
104+
type output struct {
105+
Out string `jsonschema:"the output"`
106+
}
107+
108+
func structuredTool(ctx context.Context, req *CallToolRequest, args *input) (*CallToolResult, *output, error) {
109+
return nil, &output{"Ack " + args.In}, nil
110+
}
111+
99112
// runServerTest runs the server conformance test.
100113
// It must be executed in a synctest bubble.
101114
func runServerTest(t *testing.T, test *conformanceTest) {
@@ -109,6 +122,8 @@ func runServerTest(t *testing.T, test *conformanceTest) {
109122
Name: "greet",
110123
Description: "say hi",
111124
}, sayHi)
125+
case "structured":
126+
AddTool(s, &Tool{Name: "structured"}, structuredTool)
112127
default:
113128
t.Fatalf("unknown tool %q", tn)
114129
}

mcp/server.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
206206
)
207207
if reflect.TypeFor[Out]() != reflect.TypeFor[any]() {
208208
var err error
209-
elemZero, err = setSchema[Out](&t.OutputSchema, &outputResolved)
209+
elemZero, err = setSchema[Out](&tt.OutputSchema, &outputResolved)
210210
if err != nil {
211211
return nil, nil, fmt.Errorf("output schema: %v", err)
212212
}
@@ -258,9 +258,6 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
258258
res.StructuredContent = elemZero
259259
}
260260
}
261-
if tt.OutputSchema != nil && elemZero != nil {
262-
res.StructuredContent = elemZero
263-
}
264261
return res, nil
265262
}
266263

mcp/testdata/conformance/server/tools.txtar

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Fixed bugs:
88

99
-- tools --
1010
greet
11+
structured
1112

1213
-- client --
1314
{
@@ -63,6 +64,35 @@ greet
6364
"additionalProperties": false
6465
},
6566
"name": "greet"
67+
},
68+
{
69+
"inputSchema": {
70+
"type": "object",
71+
"required": [
72+
"In"
73+
],
74+
"properties": {
75+
"In": {
76+
"type": "string",
77+
"description": "the input"
78+
}
79+
},
80+
"additionalProperties": false
81+
},
82+
"name": "structured",
83+
"outputSchema": {
84+
"type": "object",
85+
"required": [
86+
"Out"
87+
],
88+
"properties": {
89+
"Out": {
90+
"type": "string",
91+
"description": "the output"
92+
}
93+
},
94+
"additionalProperties": false
95+
}
6696
}
6797
]
6898
}

0 commit comments

Comments
 (0)