fix(mcp): serialize raw plugin tool results · openclaw/openclaw@e4b09e1
vincentkoc
·
2026-04-29
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -52,10 +52,14 @@ export function createPluginToolsMcpHandlers(tools: AnyAgentTool[]) {
|
52 | 52 | } |
53 | 53 | try { |
54 | 54 | const result = await tool.execute(`mcp-${Date.now()}`, params.arguments ?? {}); |
| 55 | +const rawContent = |
| 56 | +result && typeof result === "object" && "content" in result |
| 57 | + ? (result as { content?: unknown }).content |
| 58 | + : result; |
55 | 59 | return { |
56 | | -content: Array.isArray(result.content) |
57 | | - ? result.content |
58 | | - : [{ type: "text", text: coerceChatContentText(result.content) }], |
| 60 | +content: Array.isArray(rawContent) |
| 61 | + ? rawContent |
| 62 | + : [{ type: "text", text: coerceChatContentText(rawContent) }], |
59 | 63 | }; |
60 | 64 | } catch (err) { |
61 | 65 | return { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -65,6 +65,39 @@ describe("plugin tools MCP server", () => {
|
65 | 65 | expect(result.content).toEqual([{ type: "text", text: "Stored." }]); |
66 | 66 | }); |
67 | 67 | |
| 68 | +it("serializes plugin tool results that do not use the MCP content envelope", async () => { |
| 69 | +const execute = vi.fn().mockResolvedValue({ |
| 70 | +provider: "kitchen-sink-search", |
| 71 | +results: [{ title: "Kitchen Sink image fixture" }], |
| 72 | +}); |
| 73 | +const tool = { |
| 74 | +name: "kitchen_sink_search", |
| 75 | +description: "Search Kitchen Sink fixture content", |
| 76 | +parameters: { |
| 77 | +type: "object", |
| 78 | +properties: { |
| 79 | +query: { type: "string" }, |
| 80 | +}, |
| 81 | +}, |
| 82 | + execute, |
| 83 | +} as unknown as AnyAgentTool; |
| 84 | + |
| 85 | +const handlers = createPluginToolsMcpHandlers([tool]); |
| 86 | +const result = await handlers.callTool({ |
| 87 | +name: "kitchen_sink_search", |
| 88 | +arguments: { query: "kitchen sink" }, |
| 89 | +}); |
| 90 | +expect(result.content).toEqual([ |
| 91 | +{ |
| 92 | +type: "text", |
| 93 | +text: JSON.stringify({ |
| 94 | +provider: "kitchen-sink-search", |
| 95 | +results: [{ title: "Kitchen Sink image fixture" }], |
| 96 | +}), |
| 97 | +}, |
| 98 | +]); |
| 99 | +}); |
| 100 | + |
68 | 101 | it("returns MCP errors for unknown tools and thrown tool errors", async () => { |
69 | 102 | const failingTool = { |
70 | 103 | name: "memory_forget", |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。