feat: MCP adapter falls back to structured content when a tool returns no text

Servers that return their result only as structuredContent (no TextContent
block) reached the agent as an empty result. Serialize StructuredContent to
JSON in that case; when text is present it still wins, since the MCP spec
has tools send the same data as text.
This commit is contained in:
sausheong committed 2026-09-30 21:04:09 +08:00
1 parent 611198dc64
commit 40fb986b3f
2 files changed
+44

No files matched your search

+31
View File
@@ -135,6 +135,37 @@ func TestMCPAdapter_ImageContentMapping(t *testing.T) {
var _ llm.ImageContent = res.Images[0]
}
func TestMCPAdapter_StructuredContentFallback(t *testing.T) {
cli := startInProcessServer(t, "srv", []sdkToolFixture{
stringArgTool("structured_only", "hits as structured content only", func(_ context.Context, _ *sdk.CallToolRequest) (*sdk.CallToolResult, error) {
return &sdk.CallToolResult{
Content: []sdk.Content{},
StructuredContent: map[string]any{"hits": []any{map[string]any{"text": "leave policy", "score": 0.9}}},
}, nil
}),
stringArgTool("both", "text and structured content", func(_ context.Context, _ *sdk.CallToolRequest) (*sdk.CallToolResult, error) {
return &sdk.CallToolResult{
Content: []sdk.Content{&sdk.TextContent{Text: "summary text"}},
StructuredContent: map[string]any{"hits": []any{}},
}, nil
}),
})
byName := map[string]int{}
for i, tl := range cli.Tools() {
byName[tl.Name()] = i
}
require.Len(t, byName, 2)
res, err := cli.Tools()[byName["mcp__srv__structured_only"]].Execute(context.Background(), json.RawMessage(`{}`))
require.NoError(t, err)
assert.Empty(t, res.Error)
assert.JSONEq(t, `{"hits":[{"text":"leave policy","score":0.9}]}`, res.Output)
res, err = cli.Tools()[byName["mcp__srv__both"]].Execute(context.Background(), json.RawMessage(`{}`))
require.NoError(t, err)
assert.Equal(t, "summary text", res.Output, "text wins when present; structured content is not duplicated")
}
func TestMCPClient_ConnectDiscovers(t *testing.T) {
cli := startInProcessServer(t, "srv", []sdkToolFixture{
stringArgTool("a", "first", func(_ context.Context, _ *sdk.CallToolRequest) (*sdk.CallToolResult, error) {