Skip to content

Commit 38f5444

Browse files
committed
fix(mcp): preserve resolved tool failures
1 parent 9a9e688 commit 38f5444

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎src/mcp/plugin-tools-handlers.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
wrapToolWithBeforeToolCallHook,
99
} from "../agents/agent-tools.before-tool-call.js";
1010
import { BEFORE_TOOL_CALL_HOOK_CONTEXT } from "../agents/before-tool-call-metadata.js";
11+
import { isToolResultError } from "../agents/tool-result-error.js";
1112
import { isAutomationsToolName } from "../agents/tools/automations-tool-name.js";
1213
import type { AnyAgentTool } from "../agents/tools/common.js";
1314
import { formatErrorMessage } from "../infra/errors.js";
@@ -104,6 +105,7 @@ export function createPluginToolsMcpHandlers(tools: AnyAgentTool[]) {
104105
const toolCallId = `mcp-${randomUUID()}`;
105106
try {
106107
const result = await entry.tool.execute(toolCallId, params.arguments ?? {}, signal);
108+
const isError = isToolResultError(result);
107109
const rawContent =
108110
result && typeof result === "object" && "content" in result
109111
? (result as { content?: unknown }).content
@@ -112,6 +114,7 @@ export function createPluginToolsMcpHandlers(tools: AnyAgentTool[]) {
112114
content: Array.isArray(rawContent)
113115
? rawContent.map(toMcpContentBlock)
114116
: [{ type: "text", text: coerceChatContentText(rawContent) }],
117+
...(isError ? { isError: true } : {}),
115118
};
116119
} catch (err) {
117120
return {

‎src/mcp/plugin-tools-mcp-client.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,37 @@ describe("plugin tools MCP client bridge", () => {
5959
await server.close();
6060
}
6161
});
62+
63+
it("delivers resolved tool failures as MCP errors", async () => {
64+
const content = [{ type: "text", text: "backend unavailable" }];
65+
const tool = {
66+
name: "memory_search",
67+
description: "Search memory",
68+
parameters: { type: "object", properties: {} },
69+
execute: vi.fn().mockResolvedValue({
70+
content,
71+
details: { status: "failed", error: "backend unavailable" },
72+
}),
73+
} as unknown as AnyAgentTool;
74+
const server = createPluginToolsMcpServer({
75+
config: { plugins: { enabled: true } } as OpenClawConfig,
76+
tools: [tool],
77+
});
78+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
79+
const client = new Client(
80+
{ name: "plugin-tools-error-test-client", version: "0.0.0" },
81+
{ capabilities: {} },
82+
);
83+
84+
await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]);
85+
try {
86+
const result = await client.callTool({ name: "memory_search", arguments: {} });
87+
88+
expect(result.content).toEqual(content);
89+
expect(result.isError).toBe(true);
90+
} finally {
91+
await client.close();
92+
await server.close();
93+
}
94+
});
6295
});

‎src/mcp/plugin-tools-serve.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,34 @@ describe("plugin tools MCP server", () => {
370370
]);
371371
});
372372

373+
it.each([
374+
["failed status", { status: "failed", error: "backend unavailable" }, true],
375+
["blocked status", { status: "blocked" }, true],
376+
["timeout flag", { timedOut: true }, true],
377+
["explicit failure", { ok: false }, true],
378+
["successful status", { status: "success" }, undefined],
379+
["completed nonzero shell exit", { status: "completed", exitCode: 23 }, undefined],
380+
])(
381+
"projects a resolved %s through the canonical error contract",
382+
async (_label, details, isError) => {
383+
const content = [{ type: "text", text: "original tool result" }];
384+
const execute = vi.fn().mockResolvedValue({ content, details });
385+
const handlers = createPluginToolsMcpHandlers([
386+
{
387+
name: "result_probe",
388+
description: "Return a structured result",
389+
parameters: { type: "object", properties: {} },
390+
execute,
391+
} as unknown as AnyAgentTool,
392+
]);
393+
394+
const result = await handlers.callTool({ name: "result_probe", arguments: {} });
395+
396+
expect(result.content).toEqual(content);
397+
expect(result.isError).toBe(isError);
398+
},
399+
);
400+
373401
it("returns MCP errors for unknown tools and thrown tool errors", async () => {
374402
const failingTool = {
375403
name: "memory_forget",

0 commit comments

Comments
 (0)