Skip to content

Commit 8bc1891

Browse files
fix(server): avoid duplicate JSON in ACP terminal results (#23755)
Co-authored-by: maria-rcks <[email protected]>
1 parent fd78ea4 commit 8bc1891

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎apps/server/src/mcp/AcpMcpStdioBridge.test.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { describe, expect, it } from "@effect/vitest";
55
import * as Deferred from "effect/Deferred";
66
import * as Effect from "effect/Effect";
77
import * as Fiber from "effect/Fiber";
8+
import { vi } from "vite-plus/test";
89

9-
import { mcpToolSession, runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
10+
import { mcpToolSession, runAcpMcpCliFastPath, runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
1011

1112
function makeHarness(responder: (request: Request) => Promise<Response> | Response) {
1213
const input = new NodeStream.PassThrough();
@@ -38,6 +39,54 @@ function pendingFetch(started: Deferred.Deferred<AbortSignal | undefined>) {
3839
}
3940

4041
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+
4190
it.effect("forwards requests and replays the session id and protocol version", () =>
4291
Effect.gen(function* () {
4392
const { input, written, requests, bridge } = makeHarness((request) => {

‎apps/server/src/mcp/AcpMcpStdioBridge.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,38 @@ export async function runAcpMcpCliFastPath(
361361
process.exitCode = 2;
362362
return;
363363
}
364-
const result = await Effect.runPromise(
364+
let result = await Effect.runPromise(
365365
callAcpMcpTool({
366366
endpoint,
367367
authorization,
368368
tool,
369369
arguments: parsed as Record<string, unknown>,
370370
}),
371371
);
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+
}
372397
process.stdout.write(`${JSON.stringify(result)}\n`);
373398
}

0 commit comments

Comments
 (0)