Skip to content

Commit 912fdd7

Browse files
feat(server): evaluate incoming ACP tool results locally (#41)
1 parent d55032f commit 912fdd7

7 files changed

Lines changed: 830 additions & 8 deletions

File tree

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

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ 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 * as TestClock from "effect/testing/TestClock";
89
import { vi } from "vite-plus/test";
910

11+
import { makeIncomingDecisionShadow } from "./IncomingDecisionShadow.ts";
1012
import { mcpToolSession, runAcpMcpCliFastPath, runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts";
1113

1214
function makeHarness(responder: (request: Request) => Promise<Response> | Response) {
@@ -39,6 +41,149 @@ function pendingFetch(started: Deferred.Deferred<AbortSignal | undefined>) {
3941
}
4042

4143
describe("AcpMcpStdioBridge", () => {
44+
it.effect(
45+
"shadows only matching tool responses before delivery and preserves the delivered prefix",
46+
() =>
47+
Effect.gen(function* () {
48+
const input = new NodeStream.PassThrough();
49+
const written: string[] = [];
50+
let decisions = 0;
51+
const result = { content: [{ type: "text", text: "original evidence\n".repeat(30) }] };
52+
const incomingDecision = yield* makeIncomingDecisionShadow(
53+
JSON.stringify({ mode: "shadow", port: 8009, model: "test-candidate", device: "metal" }),
54+
async () => {
55+
decisions++;
56+
expect(written).toHaveLength(2);
57+
return Response.json({
58+
model: "test-candidate",
59+
answers: {
60+
"0": {
61+
type: "choice",
62+
choice: "drop",
63+
confidence: 1,
64+
probabilities: { keep: 0, drop: 1, abstain: 0 },
65+
},
66+
},
67+
usage: { input_tokens: 1, output_tokens: 1 },
68+
});
69+
},
70+
);
71+
using stderr = vi.spyOn(process.stderr, "write").mockReturnValue(true);
72+
const bridge = runAcpMcpStdioBridge({
73+
endpoint: "http://127.0.0.1:1/mcp",
74+
authorization: "Bearer bridge-test",
75+
input,
76+
output: { write: (chunk) => written.push(chunk) },
77+
incomingDecision,
78+
fetchImplementation: async (_url, init) => {
79+
const request = JSON.parse(String(init?.body));
80+
return Response.json([
81+
{ jsonrpc: "2.0", method: "notifications/progress", params: { unchanged: true } },
82+
{ jsonrpc: "2.0", id: 999, result },
83+
{ jsonrpc: "2.0", id: request.id, result },
84+
]);
85+
},
86+
});
87+
input.end('{"jsonrpc":"2.0","id":1,"method":"tools/call"}\n');
88+
yield* bridge;
89+
expect(decisions).toBe(1);
90+
expect(written).toEqual([
91+
JSON.stringify({
92+
jsonrpc: "2.0",
93+
method: "notifications/progress",
94+
params: { unchanged: true },
95+
}) + "\n",
96+
JSON.stringify({ jsonrpc: "2.0", id: 999, result }) + "\n",
97+
JSON.stringify({ jsonrpc: "2.0", id: 1, result }) + "\n",
98+
]);
99+
expect(stderr).toHaveBeenCalledWith(expect.stringContaining('"applied":false'));
100+
expect(stderr.mock.calls.flat().join("")).not.toContain("original evidence");
101+
}),
102+
);
103+
104+
it.effect.each(["decision", "malformed", "timeout"] as const)(
105+
"preserves the provider prefix through %s selection",
106+
(outcome) =>
107+
Effect.gen(function* () {
108+
const input = new NodeStream.PassThrough();
109+
const written: string[] = [];
110+
const started = yield* Deferred.make<void>();
111+
const first = `${"é evidence ".repeat(30)}\n\n`;
112+
const result = {
113+
meta: { x: 1 },
114+
content: [
115+
{ type: "text", text: first + "remove\n\n", extra: 2 },
116+
{ type: "text", text: "留める" },
117+
],
118+
};
119+
const incomingDecision = yield* makeIncomingDecisionShadow(
120+
JSON.stringify({ mode: "shadow", port: 8009, model: "test", device: "metal" }),
121+
async () => {
122+
if (outcome === "timeout") {
123+
Deferred.doneUnsafe(started, Effect.void);
124+
return new Promise<Response>(() => {});
125+
}
126+
return Response.json({
127+
model: "test",
128+
answers: Object.fromEntries(
129+
["keep", "drop", "abstain"].map((choice, id) => [
130+
outcome === "malformed" && id === 1 ? "unknown-block" : String(id),
131+
{
132+
type: "choice",
133+
choice,
134+
confidence: 0.5,
135+
probabilities: { keep: 0.3, drop: 0.3, abstain: 0.4 },
136+
},
137+
]),
138+
),
139+
usage: { input_tokens: 1, output_tokens: 1 },
140+
});
141+
},
142+
"test-select",
143+
);
144+
using _stderr = vi.spyOn(process.stderr, "write").mockReturnValue(true);
145+
const bridge = runAcpMcpStdioBridge({
146+
endpoint: "http://127.0.0.1:1/mcp",
147+
authorization: "test",
148+
input,
149+
output: { write: (chunk) => written.push(chunk) },
150+
incomingDecision,
151+
fetchImplementation: async (_url, init) => {
152+
const request = JSON.parse(String(init?.body));
153+
return Response.json([
154+
{ jsonrpc: "2.0", method: "prefix", params: { text: "prior" } },
155+
{ jsonrpc: "2.0", id: request.id, result, extra: true },
156+
]);
157+
},
158+
});
159+
input.end('{"jsonrpc":"2.0","id":1,"method":"tools/call"}\n');
160+
const fiber = yield* Effect.forkChild(bridge);
161+
if (outcome === "timeout") {
162+
yield* Deferred.await(started);
163+
yield* TestClock.adjust("1 second");
164+
}
165+
yield* Fiber.join(fiber);
166+
expect(written).toEqual([
167+
JSON.stringify({ jsonrpc: "2.0", method: "prefix", params: { text: "prior" } }) + "\n",
168+
JSON.stringify({
169+
jsonrpc: "2.0",
170+
id: 1,
171+
result:
172+
outcome === "decision"
173+
? {
174+
...result,
175+
content: [{ ...result.content[0], text: first }, result.content[1]],
176+
}
177+
: result,
178+
extra: true,
179+
}) + "\n",
180+
]);
181+
if (outcome === "timeout") {
182+
expect(_stderr).toHaveBeenCalledWith(expect.stringContaining('"elapsedMs":1000'));
183+
}
184+
}),
185+
);
186+
42187
it("prints structured results once in the terminal while retaining other content and errors", async () => {
43188
const originalEnvironment = process.env;
44189
using _environment = {

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

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
// @effect-diagnostics nodeBuiltinImport:off preferSchemaOverJson:off
33
import * as NodeReadline from "node:readline";
44

5+
import {
6+
makeIncomingDecisionShadow,
7+
type IncomingDecisionShadow,
8+
type ShadowReceipt,
9+
} from "./IncomingDecisionShadow.ts";
10+
11+
import * as Clock from "effect/Clock";
512
import * as Cause from "effect/Cause";
613
import * as Effect from "effect/Effect";
714
import * as Result from "effect/Result";
@@ -33,6 +40,7 @@ const MCP_PROTOCOL_VERSION = "2025-06-18";
3340
export interface AcpMcpStdioBridgeOptions {
3441
readonly endpoint: string;
3542
readonly authorization: string;
43+
readonly incomingDecision?: IncomingDecisionShadow["Service"];
3644
readonly input: NodeJS.ReadableStream;
3745
readonly output: { write(chunk: string): unknown };
3846
readonly fetchImplementation?: (url: string, init?: RequestInit) => Promise<Response>;
@@ -187,10 +195,36 @@ export function runAcpMcpStdioBridge(options: AcpMcpStdioBridgeOptions): Effect.
187195

188196
const handleServerPayload = Effect.fn("AcpMcpStdioBridge.handleServerPayload")(function* (
189197
payload: unknown,
198+
request: JsonRpcEnvelope,
190199
): Effect.fn.Return<void> {
191200
for (const entry of Array.isArray(payload) ? payload : [payload]) {
201+
const started = yield* Clock.currentTimeNanos;
202+
let receipt: ShadowReceipt | undefined;
192203
protocolVersion = protocolVersionOf(entry) ?? protocolVersion;
193-
yield* writeMessage(entry);
204+
const envelope = asEnvelope(entry);
205+
let output = entry;
206+
if (
207+
options.incomingDecision !== undefined &&
208+
request.method === "tools/call" &&
209+
request.id !== undefined &&
210+
envelope?.id === request.id &&
211+
envelope.error === undefined
212+
) {
213+
const observed = yield* options.incomingDecision.observe(envelope.result);
214+
receipt = observed.receipt;
215+
output = { ...(entry as Record<string, unknown>), result: observed.result };
216+
}
217+
const serialized = `${JSON.stringify(output)}\n`;
218+
const ready = yield* Clock.currentTimeNanos;
219+
if (receipt !== undefined && receipt.status !== "disabled") {
220+
const completed = { ...receipt, elapsedMs: Number(ready - started) / 1e6 };
221+
yield* Effect.sync(() => {
222+
process.stderr.write(`${JSON.stringify({ incomingDecision: completed })}\n`);
223+
}).pipe(Effect.ignoreCause);
224+
}
225+
yield* Effect.sync(() => {
226+
options.output.write(serialized);
227+
});
194228
}
195229
});
196230

@@ -235,7 +269,9 @@ export function runAcpMcpStdioBridge(options: AcpMcpStdioBridgeOptions): Effect.
235269
}
236270
return yield* discardResponseBody(response);
237271
}
238-
yield* Stream.runForEach(responsePayloads(response, bridgeError), handleServerPayload);
272+
yield* Stream.runForEach(responsePayloads(response, bridgeError), (payload) =>
273+
handleServerPayload(payload, envelope),
274+
);
239275
},
240276
(effect, _line, envelope) =>
241277
effect.pipe(
@@ -333,11 +369,17 @@ export async function runAcpMcpCliFastPath(
333369
}
334370
if (command === "acp-mcp-bridge") {
335371
await Effect.runPromise(
336-
runAcpMcpStdioBridge({
337-
endpoint,
338-
authorization,
339-
input: process.stdin,
340-
output: process.stdout,
372+
Effect.gen(function* () {
373+
const incomingDecision = yield* makeIncomingDecisionShadow(
374+
process.env.T3_INCOMING_DECISION,
375+
);
376+
yield* runAcpMcpStdioBridge({
377+
incomingDecision,
378+
endpoint,
379+
authorization,
380+
input: process.stdin,
381+
output: process.stdout,
382+
});
341383
}),
342384
);
343385
return;

0 commit comments

Comments
 (0)