@devries-cameron20 / t3code-1 / commits / 8bc1891711
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): avoid duplicate JSON in ACP terminal results (#23755)

Co-authored-by: maria-rcks <maria@kuuro.net>

Florent committed Oct 30, 2026 at 21:59 UTC 8bc18917113fcfc8fe9eba33507ca06cbebba0c4
2 files changed +76 -2
apps/server/src/mcp/AcpMcpStdioBridge.test.ts
+50 -1
@@ -5,8 +5,9 @@ import { describe, expect, it } from "@effect/vitest";
5 import * as Deferred from "effect/Deferred";
6 import * as Effect from "effect/Effect";
7 import * as Fiber from "effect/Fiber";
8 +import { vi } from "vite-plus/test";
9
9 -import { mcpToolSession, runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
10 +import { mcpToolSession, runAcpMcpCliFastPath, runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
11
12 function makeHarness(responder: (request: Request) => Promise<Response> | Response) {
13 const input = new NodeStream.PassThrough();
@@ -38,6 +39,54 @@ function pendingFetch(started: Deferred.Deferred<AbortSignal | undefined>) {
39 }
40
41 describe("AcpMcpStdioBridge", () => {
42 + it("prints structured results once in the terminal while retaining other content and errors", async () => {
43 + const originalEnvironment = process.env;
44 + using _environment = {
45 + [Symbol.dispose]: () => {
46 + process.env = originalEnvironment;
47 + },
48 + };
49 + process.env = {
50 + ...originalEnvironment,
51 + T3_ACP_MCP_ENDPOINT: "http://127.0.0.1:1/mcp",
52 + T3_ACP_MCP_AUTHORIZATION: "Bearer bridge-test",
53 + };
54 + const structuredContent = { taskId: "task-1", status: "failed" };
55 + const explanation = { type: "text", text: "The command failed." };
56 + const image = { type: "image", data: "image-data", mimeType: "image/png" };
57 + const result = {
58 + structuredContent,
59 + content: [{ type: "text", text: JSON.stringify(structuredContent) }, explanation, image],
60 + isError: true,
61 + };
62 + using fetch = vi.spyOn(globalThis, "fetch").mockImplementation(async (_url, init) => {
63 + if (init?.method === "DELETE") return new Response(null, { status: 204 });
64 + const message = JSON.parse(String(init?.body)) as { id?: string; method: string };
65 + if (message.method === "notifications/initialized") {
66 + return new Response(null, { status: 202 });
67 + }
68 + return Response.json(
69 + {
70 + jsonrpc: "2.0",
71 + id: message.id,
72 + result: message.method === "initialize" ? { protocolVersion: "2025-06-18" } : result,
73 + },
74 + { headers: { "mcp-session-id": "session-42" } },
75 + );
76 + });
77 + using output = vi.spyOn(process.stdout, "write").mockReturnValue(true);
78 +
79 + await runAcpMcpCliFastPath("acp-mcp-call", ["task_status", '{"taskId":"task-1"}']);
80 +
81 + expect(output).toHaveBeenCalledExactlyOnceWith(
82 + `${JSON.stringify({ ...result, content: [explanation, image] })}\n`,
83 + );
84 + expect(fetch).toHaveBeenLastCalledWith(
85 + "http://127.0.0.1:1/mcp",
86 + expect.objectContaining({ method: "DELETE" }),
87 + );
88 + });
89 +
90 it.effect("forwards requests and replays the session id and protocol version", () =>
91 Effect.gen(function* () {
92 const { input, written, requests, bridge } = makeHarness((request) => {
apps/server/src/mcp/AcpMcpStdioBridge.ts
+26 -1
@@ -361,7 +361,7 @@ export async function runAcpMcpCliFastPath(
361 process.exitCode = 2;
362 return;
363 }
364 - const result = await Effect.runPromise(
364 + let result = await Effect.runPromise(
365 callAcpMcpTool({
366 endpoint,
367 authorization,
@@ -369,5 +369,30 @@ export async function runAcpMcpCliFastPath(
369 arguments: parsed as Record<string, unknown>,
370 }),
371 );
372 + // Terminal callers see both fields, unlike native MCP clients that choose one.
373 + if (
374 + typeof result === "object" &&
375 + result !== null &&
376 + "structuredContent" in result &&
377 + result.structuredContent !== undefined &&
378 + "content" in result &&
379 + Array.isArray(result.content)
380 + ) {
381 + const encoded = JSON.stringify(result.structuredContent);
382 + result = {
383 + ...result,
384 + content: result.content.filter(
385 + (part: unknown) =>
386 + !(
387 + typeof part === "object" &&
388 + part !== null &&
389 + "type" in part &&
390 + part.type === "text" &&
391 + "text" in part &&
392 + part.text === encoded
393 + ),
394 + ),
395 + };
396 + }
397 process.stdout.write(`${JSON.stringify(result)}\n`);
398 }