The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 19:45 UTC
fix(grok): Grok MCP calls store the real tool, arguments, and result (#35502)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 12, 2026 at 11:05 UTC
3c9a54979a0b198a65d7f899c886712803a443ab
4 files changed
+125
-16
packages/provider-grok/src/server/xaiAcpExtension.test.ts
+76
@@ -30,9 +30,11 @@ import {
30
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
31
import {
32
type AcpToolCallState,
33
+ extractMcpToolCallIdentity,
34
mergeToolCallState,
35
parseSessionUpdateEvent,
36
} from "@t3tools/provider-acp/server/runtimeModel";
37
+import { compactDynamicToolOutput, htmlRenderFromToolItem } from "@t3tools/shared/toolOutput";
38
39
const decodeXAiAskUserQuestionRequest = Schema.decodeUnknownSync(XAiAskUserQuestionRequest);
40
@@ -340,6 +342,80 @@ describe("XAiAcpExtension", () => {
342
expect(extractXAiMonitorTaskId(normalized)).toBe("019f44a5-87d1-7640-8e35-6a4667ffc873");
343
});
344
345
+ // regression: #23714 Recorded from Grok 1.0.77 calling stdio MCP tools through `use_tool`.
346
+ it("rewrites use_tool MCP calls and results into the generic ACP MCP shapes", () => {
347
+ const meta = { "x.ai/tool": { version: 1, name: "use_tool", kind: "use_tool" } };
348
+ const grokCall = (
349
+ toolCallId: string,
350
+ tool: string,
351
+ toolInput: Record<string, unknown>,
352
+ completion: Record<string, unknown>,
353
+ ) => {
354
+ const rawInput = { tool_name: tool, tool_input: toolInput };
355
+ const updates = [
356
+ { sessionUpdate: "tool_call", toolCallId, title: "use_tool", rawInput, _meta: meta },
357
+ {
358
+ sessionUpdate: "tool_call_update",
359
+ toolCallId,
360
+ kind: "other",
361
+ title: tool,
362
+ rawInput: { variant: "UseTool", ...rawInput },
363
+ _meta: meta,
364
+ },
365
+ { sessionUpdate: "tool_call_update", toolCallId, ...completion },
366
+ ];
367
+ let state: AcpToolCallState | undefined;
368
+ for (const update of updates) {
369
+ state = normalizeXAiAcpToolCallState(
370
+ mergeToolCallState(state, parseRecordedToolCallUpdate(update)),
371
+ );
372
+ }
373
+ return state!;
374
+ };
375
+ const page = { attachmentId: "abc-html", title: "T", height: 300 };
376
+
377
+ const rendered = grokCall(
378
+ "call-85597c6c-6fb8-4c9c-a6a8-12c4a74ebc96-1",
379
+ "t3-code__html_render",
380
+ { path: "a.html" },
381
+ {
382
+ status: "completed",
383
+ rawOutput: {
384
+ type: "MCP",
385
+ tool_name: "html_render",
386
+ server_name: "t3-code",
387
+ output: { OkayOutput: JSON.stringify({ htmlRender: page, message: "Shown" }) },
388
+ },
389
+ },
390
+ );
391
+ expect(extractMcpToolCallIdentity(rendered)).toEqual({
392
+ server: "t3-code",
393
+ tool: "html_render",
394
+ });
395
+ expect(rendered.data.rawInput).toMatchObject({ arguments: { path: "a.html" } });
396
+ expect(
397
+ htmlRenderFromToolItem({ toolName: "t3-code.html_render", output: rendered.data.rawOutput }),
398
+ ).toEqual(page);
399
+
400
+ const failed = grokCall(
401
+ "call-2baebdc5-2446-4ac2-9652-85f97a76a05b-2",
402
+ "pencil__browser",
403
+ { action: "load-page" },
404
+ {
405
+ status: "failed",
406
+ rawOutput: {
407
+ type: "MCP",
408
+ tool_name: "browser",
409
+ server_name: "pencil",
410
+ output: { Error: "Page not found" },
411
+ is_error: true,
412
+ },
413
+ },
414
+ );
415
+ expect(extractMcpToolCallIdentity(failed)).toEqual({ server: "pencil", tool: "browser" });
416
+ expect(compactDynamicToolOutput(failed.data.rawOutput)?.isError).toBe(true);
417
+ });
418
+
419
it("keeps Monitor ticks running while Grok reports them in progress", () => {
420
// Grok 1.0.41 ticks report in_progress with exit_code 0; only _x.ai/task_completed ends the monitor.
421
const command = "for i in 1 2 3; do sleep 8; echo tick $i; done";
packages/provider-grok/src/server/xaiAcpExtension.ts
+48
-1
@@ -558,7 +558,54 @@ function resolveXAiAcpToolTitle(toolCall: AcpToolCallState): string | undefined
558
}
559
560
/** Grok 1.0.41 monitor ticks report exit_code 0 before ending via _x.ai/task_completed. */
561
-export function normalizeXAiAcpToolCallState(toolCall: AcpToolCallState): AcpToolCallState {
561
+// Grok calls MCP tools through its `use_tool` built-in as "<server>__<tool>" and wraps results as
562
+// { type: "MCP", output: { OkayOutput } | { Error } }; rewrite both into the generic ACP MCP shapes.
563
+function normalizeXAiMcpToolCall(toolCall: AcpToolCallState): AcpToolCallState {
564
+ const meta = unknownRecord(toolCall.data.meta);
565
+ const rawInput = unknownRecord(toolCall.data.rawInput);
566
+ const toolName = nonEmptyString(rawInput?.tool_name) ?? "";
567
+ const separator = toolName.indexOf("__");
568
+ const isUseTool =
569
+ unknownRecord(meta?.["x.ai/tool"])?.name === "use_tool" &&
570
+ separator > 0 &&
571
+ separator + 2 < toolName.length;
572
+ const rawOutput = unknownRecord(toolCall.data.rawOutput);
573
+ const result = rawOutput?.type === "MCP" ? unknownRecord(rawOutput.output) : undefined;
574
+ const text =
575
+ typeof result?.Error === "string"
576
+ ? result.Error
577
+ : typeof result?.OkayOutput === "string"
578
+ ? result.OkayOutput
579
+ : undefined;
580
+ if (!isUseTool && text === undefined) return toolCall;
581
+ return {
582
+ ...toolCall,
583
+ data: {
584
+ ...toolCall.data,
585
+ ...(isUseTool
586
+ ? {
587
+ meta: { ...meta, is_mcp_tool_call: true },
588
+ rawInput: {
589
+ server: toolName.slice(0, separator),
590
+ tool: toolName.slice(separator + 2),
591
+ arguments: unknownRecord(rawInput?.tool_input) ?? {},
592
+ },
593
+ }
594
+ : {}),
595
+ ...(text === undefined
596
+ ? {}
597
+ : {
598
+ rawOutput: {
599
+ ...(typeof result?.Error === "string" ? { isError: true } : {}),
600
+ content: [{ type: "text", text }],
601
+ },
602
+ }),
603
+ },
604
+ };
605
+}
606
+
607
+export function normalizeXAiAcpToolCallState(incoming: AcpToolCallState): AcpToolCallState {
608
+ const toolCall = normalizeXAiMcpToolCall(incoming);
609
const resolvedTitle = resolveXAiAcpToolTitle(toolCall);
610
const withTitle =
611
resolvedTitle !== undefined && resolvedTitle !== toolCall.title
packages/shared/src/htmlRender.test.ts
-10
@@ -169,16 +169,6 @@ describe("htmlRenderFromToolItem", () => {
169
["html_render", result],
170
// OpenCode 2 Code Mode reports a script's nested html_render call as one `execute` item.
171
["execute", JSON.stringify(result)],
172
- // Grok CLI wraps MCP results as { type: "MCP", output: { OkayOutput: "<json>" } }.
173
- [
174
- "t3-code.html_render",
175
- {
176
- type: "MCP",
177
- tool_name: "html_render",
178
- server_name: "t3-code",
179
- output: { OkayOutput: JSON.stringify(result) },
180
- },
181
- ],
172
] as const) {
173
expect(htmlRenderFromToolItem({ toolName, output })).toEqual(reference);
174
}
packages/shared/src/toolOutput.ts
+1
-5
@@ -100,11 +100,7 @@ function readResult(value: unknown, budget: ResultReadBudget, depth = 0): Result
100
value.is_error === true ||
101
value._tag === "OrchestratorMcpFailure" ||
102
value.error != null;
103
- // Grok CLI wraps MCP results as { type: "MCP", output: { OkayOutput: "<result json>" } }.
104
- const content =
105
- value.structuredContent ??
106
- value.content ??
107
- (Predicate.isObject(value.output) ? value.output.OkayOutput : undefined);
103
+ const content = value.structuredContent ?? value.content;
104
if (content !== undefined) {
105
const nested = readResult(content, budget, depth + 1);
106
return { ...nested, failed: failed || nested.failed };