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

+13
View File
@@ -79,6 +79,12 @@ func (a *mcpToolAdapter) Execute(ctx context.Context, input json.RawMessage) (to
// ToolResult: TextContent concatenated into Output (or Error if
// IsError), ImageContent collected into Images. Other content types
// (audio, embedded resources) are described in Output as a placeholder.
//
// StructuredContent is used only when there is no text: the MCP spec says
// a tool returning structured content SHOULD also send it serialized as a
// TextContent block, so when both are present the text already carries it.
// Servers that send structured content alone would otherwise reach the
// agent as an empty result.
func mcpResultToToolResult(res *sdk.CallToolResult) tool.ToolResult {
var text strings.Builder
var images []llm.ImageContent
@@ -103,6 +109,13 @@ func mcpResultToToolResult(res *sdk.CallToolResult) tool.ToolResult {
fmt.Fprintf(&text, "[unsupported MCP content: %T]", c)
}
}
if text.Len() == 0 && res.StructuredContent != nil {
if b, err := json.Marshal(res.StructuredContent); err == nil {
text.Write(b)
} else {
fmt.Fprintf(&text, "[unserializable MCP structured content: %v]", err)
}
}
out := tool.ToolResult{Images: images}
if res.IsError {
out.Error = text.String()
+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) {