diff --git a/tools/mcp/adapter.go b/tools/mcp/adapter.go index d11cae4..bc86fb7 100644 --- a/tools/mcp/adapter.go +++ b/tools/mcp/adapter.go @@ -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() diff --git a/tools/mcp/mcp_test.go b/tools/mcp/mcp_test.go index 1516e4d..e54322c 100644 --- a/tools/mcp/mcp_test.go +++ b/tools/mcp/mcp_test.go @@ -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) {