Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/core/tools/UseMcpToolTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ export class UseMcpToolTool extends BaseTool<"use_mcp_tool"> {
return { isValid: false }
}

// Native-only: arguments are already a structured object.
// Some LLMs emit arguments as JSON-encoded strings rather than objects.
// Parse them early so the type check below sees the unwrapped object.
if (typeof params.arguments === "string") {
try {
params.arguments = JSON.parse(params.arguments)
} catch {}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test in useMcpToolTool.spec.ts that passes nativeArgs.arguments as a JSON-encoded string (e.g. '{"headless": true}' as unknown as Record<string, unknown>) and verifies callTool receives the parsed object? This is the primary behavior change and currently has no regression coverage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edelauna Done! Added the test in useMcpToolTool.spec.ts — the new test "should parse JSON-string arguments and pass parsed object to callTool" passes nativeArgs.arguments as the JSON string '{"headless": true}' and verifies via callToolMock that callTool receives the parsed object { headless: true }, not the raw string.

The test has been pushed to this PR branch (commit c92ab3f). Ready for re-review.


let parsedArguments: Record<string, unknown> | undefined
if (params.arguments !== undefined) {
if (typeof params.arguments !== "object" || params.arguments === null || Array.isArray(params.arguments)) {
Expand Down
47 changes: 47 additions & 0 deletions src/core/tools/__tests__/useMcpToolTool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,53 @@ describe("useMcpToolTool", () => {
expect(mockPushToolResult).toHaveBeenCalledWith("Tool result: Tool executed successfully")
})

it("should parse JSON-string arguments and pass parsed object to callTool", async () => {
const callToolMock = vi.fn().mockResolvedValue({
content: [{ type: "text", text: "Browser session started" }],
isError: false,
})

mockProviderRef.deref.mockReturnValue({
getMcpHub: () => ({
callTool: callToolMock,
getAllServers: vi.fn().mockReturnValue([
{ name: "test_server", tools: [{ name: "test_tool", description: "Test Tool" }] },
]),
}),
postMessageToWebview: vi.fn(),
})

const block: ToolUse = {
type: "tool_use",
name: "use_mcp_tool",
params: {
server_name: "test_server",
tool_name: "test_tool",
arguments: '{"headless": true}',
},
nativeArgs: {
server_name: "test_server",
tool_name: "test_tool",
arguments: '{"headless": true}' as unknown as Record<string, unknown>,
},
partial: false,
}

mockAskApproval.mockResolvedValue(true)

await useMcpToolTool.handle(mockTask as Task, block as any, {
askApproval: mockAskApproval,
handleError: mockHandleError,
pushToolResult: mockPushToolResult,
})

expect(mockTask.consecutiveMistakeCount).toBe(0)
expect(mockTask.recordToolError).not.toHaveBeenCalled()
expect(callToolMock).toHaveBeenCalledWith("test_server", "test_tool", { headless: true })
expect(mockTask.say).toHaveBeenCalledWith("mcp_server_request_started")
expect(mockTask.say).toHaveBeenCalledWith("mcp_server_response", "Browser session started", [])
})

it("should handle user rejection", async () => {
const block: ToolUse = {
type: "tool_use",
Expand Down
Loading