Skip to content

Commit dbd8343

Browse files
fix(server): agent browser tools stop bloating history, fall back sensibly, and respect ownership (#16956)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c724c7c commit dbd8343

24 files changed

Lines changed: 743 additions & 122 deletions

‎apps/server/src/auth/RpcAuthorization.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ describe("RPC authorization scopes", () => {
152152
WS_METHODS.previewReportStatus,
153153
WS_METHODS.previewAdjust,
154154
WS_METHODS.previewClearProfile,
155+
WS_METHODS.previewReportProfiles,
155156
]) {
156157
expect(requiredScopeForRpcMethod(method)).toBe(AuthPreviewOperateScope);
157158
}

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export const RPC_REQUIRED_SCOPES = {
189189
[WS_METHODS.previewClose]: AuthPreviewOperateScope,
190190
[WS_METHODS.previewList]: AuthOrchestrationReadScope,
191191
[WS_METHODS.previewClearProfile]: AuthPreviewOperateScope,
192+
[WS_METHODS.previewReportProfiles]: AuthPreviewOperateScope,
192193
[WS_METHODS.previewReportStatus]: AuthPreviewOperateScope,
193194
[WS_METHODS.subscribePreviewEvents]: AuthOrchestrationReadScope,
194195
[WS_METHODS.subscribeDiscoveredLocalServers]: AuthOrchestrationReadScope,

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import {
44
DeviceHostUnavailableError,
5+
DeviceId,
56
EnvironmentId,
67
ProviderInstanceId,
78
ThreadId,
@@ -93,7 +94,21 @@ const layerDeviceServiceMock = Layer.mock(DeviceService.DeviceService)({
9394
platform: input.platform,
9495
openedAt: "2026-09-08T00:00:00.000Z",
9596
}),
96-
sessionsForThread: () => Effect.succeed([]),
97+
// UDID-1 is open in the test thread; UDID-2 exists but belongs to another thread.
98+
sessionsForThread: (id) =>
99+
Effect.succeed(
100+
id === threadId
101+
? [
102+
{
103+
threadId,
104+
hostId: "local",
105+
deviceId: DeviceId.make("UDID-1"),
106+
platform: "ios" as const,
107+
openedAt: "2026-09-08T00:00:00.000Z",
108+
},
109+
]
110+
: [],
111+
),
97112
screenshot: () => Effect.succeed({ device, png }),
98113
close: () => Effect.void,
99114
agentCli: Effect.succeed("/cli"),
@@ -135,6 +150,12 @@ it.effect("registers the device tools and returns the screenshot as image conten
135150
screenshot: { mimeType: "image/png", width: 1206, height: 2622 },
136151
});
137152

153+
const foreign = yield* server
154+
.callTool({ name: "device_screenshot", arguments: { deviceId: "UDID-2" } })
155+
.pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
156+
expect(foreign.isError).toBe(true);
157+
expect(foreign.content.map((entry) => entry.type)).toEqual(["text"]);
158+
138159
const denied = yield* server
139160
.callTool({ name: "device_list", arguments: {} })
140161
.pipe(callWith(["preview"]), Effect.provideService(McpSchema.McpServerClient, client));

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ it.effect("tells the agent how to fall back when no desktop app can run the snap
244244
);
245245

246246
it.effect.each([
247-
{ mode: "default", input: {}, images: true },
247+
{ mode: "default", input: {}, images: false },
248248
{ mode: "explicit image", input: { includeImage: true }, images: true },
249249
{ mode: "text only", input: { includeImage: false }, images: false },
250250
])("returns fresh $mode snapshots on repeated MCP calls", ({ input, images }) =>
@@ -351,12 +351,7 @@ it.effect.each([
351351
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
352352
Effect.provideService(McpSchema.McpServerClient, client),
353353
);
354-
expect(nextDefault.content.map((content) => content.type)).toEqual([
355-
"text",
356-
"text",
357-
"text",
358-
"image",
359-
]);
354+
expect(nextDefault.content.map((content) => content.type)).toEqual(["text", "text", "text"]);
360355
expect(nextDefault.structuredContent).toMatchObject({ title: "Snapshot 7", screenshot });
361356
expect(nextDefault.structuredContent).not.toHaveProperty("accessibilityTree");
362357
expect(requests).toBe(7);
@@ -416,11 +411,12 @@ it.effect("saves the snapshot PNG on request and reports its path", () =>
416411
const path = yield* Path.Path;
417412
const inputs = yield* serveSnapshots("mcp-save-client", snapshotResult);
418413

419-
const snapshot = yield* callSnapshot({ save: true });
414+
const snapshot = yield* callSnapshot({ save: true, includeImage: true });
420415

421416
expect(snapshot.isError).toBe(false);
422417
// The browser never receives the server-only `save` flag.
423418
expect(inputs).toEqual([{}]);
419+
expect(snapshot.content.map((content) => content.type)).toContain("image");
424420
const structured = snapshot.structuredContent as { readonly screenshotPath?: string };
425421
const screenshotPath = structured.screenshotPath;
426422
expect(typeof screenshotPath).toBe("string");
@@ -436,7 +432,7 @@ it.effect("saves the snapshot PNG on request and reports its path", () =>
436432
expect(unsaved.structuredContent).not.toHaveProperty("screenshotPath");
437433

438434
// A save without the image skips the page dump.
439-
const pathOnly = yield* callSnapshot({ save: true, includeImage: false });
435+
const pathOnly = yield* callSnapshot({ save: true });
440436
const saved = pathOnly.structuredContent as { readonly screenshotPath: string };
441437
expect(saved).toEqual({ url: snapshotResult.url, screenshotPath: expect.any(String) });
442438
expect(Buffer.from(yield* fileSystem.readFile(saved.screenshotPath)).toString()).toBe("png");
@@ -851,8 +847,8 @@ it.effect("registers annotated tools and preserves authenticated request context
851847
expect(statusTool?.tool.annotations?.destructiveHint).toBe(false);
852848

853849
const snapshotTool = server.tools.find(({ tool }) => tool.name === "preview_snapshot");
854-
expect(snapshotTool?.tool.annotations?.readOnlyHint).toBe(true);
855-
expect(snapshotTool?.tool.annotations?.idempotentHint).toBe(true);
850+
expect(snapshotTool?.tool.annotations?.readOnlyHint).toBe(false);
851+
expect(snapshotTool?.tool.annotations?.idempotentHint).toBe(false);
856852
expect(snapshotTool?.tool.annotations?.openWorldHint).toBe(true);
857853

858854
const clickTool = server.tools.find(({ tool }) => tool.name === "preview_click");
@@ -891,7 +887,10 @@ it.effect("registers annotated tools and preserves authenticated request context
891887
expect(malformed._tag).toBe("InvalidParams");
892888

893889
const snapshot = yield* server
894-
.callTool({ name: "preview_snapshot", arguments: { tabId: alternateTabId } })
890+
.callTool({
891+
name: "preview_snapshot",
892+
arguments: { tabId: alternateTabId, includeImage: true },
893+
})
895894
.pipe(
896895
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
897896
Effect.provideService(McpSchema.McpServerClient, client),

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,10 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot
503503
const png = new Uint8Array(Buffer.from(screenshot.data, "base64"));
504504
const screenshotPath =
505505
payload?.save === true ? yield* saveScreenshot(snapshot.url, png) : undefined;
506-
if (screenshotPath !== undefined && payload?.includeImage === false) {
506+
// Images stay out of tool history unless asked for: providers replay them on every
507+
// later request, and some reject inline images outright.
508+
const includeImage = payload?.includeImage === true;
509+
if (screenshotPath !== undefined && !includeImage) {
507510
// The agent only wants a file to show the user. The url keeps the site icon on the tool row.
508511
const saved = {
509512
url: cutText(snapshot.url, MAX_SNAPSHOT_IDENTIFIER_CHARS),
@@ -548,9 +551,9 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot
548551
text: `Snapshot text was bounded. Omitted: ${bounded.omitted.join("; ")}.`,
549552
},
550553
]),
551-
...(payload?.includeImage === false
552-
? []
553-
: [{ type: "image" as const, data: png, mimeType: screenshot.mimeType }]),
554+
...(includeImage
555+
? [{ type: "image" as const, data: png, mimeType: screenshot.mimeType }]
556+
: []),
554557
],
555558
});
556559
}),
@@ -815,7 +818,7 @@ const layerEnvironmentRegistration = toolkitRegistration(
815818

816819
const layerProjectRegistration = toolkitRegistration(ProjectToolkit, ProjectHandlers.layer);
817820

818-
const layerAttachmentRegistration = toolkitRegistration(
821+
export const layerAttachmentToolkit = toolkitRegistration(
819822
AttachmentToolkit,
820823
AttachmentHandlers.layer,
821824
);
@@ -851,7 +854,7 @@ export const layer = Layer.mergeAll(
851854
layerPreviewToolkit,
852855
layerOrchestratorToolkit,
853856
layerThreadToolkit,
854-
layerAttachmentRegistration,
857+
layerAttachmentToolkit,
855858
layerProjectRegistration,
856859
layerEnvironmentRegistration,
857860
layerPreviewControlsRegistration,
Lines changed: 99 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { type ChatAttachment, MessageId, OrchestratorMcpFailure } from "@t3tools/contracts";
1+
import {
2+
ATTACHMENT_UPLOAD_URL_TTL_MS,
3+
type ChatAttachment,
4+
MessageId,
5+
OrchestratorMcpFailure,
6+
} from "@t3tools/contracts";
7+
import * as Clock from "effect/Clock";
28
import * as Effect from "effect/Effect";
9+
import * as McpInvocationContext from "../../McpInvocationContext.ts";
310
import * as Upload from "../../../assets/AttachmentUpload.ts";
411
import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts";
512
import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts";
@@ -27,53 +34,94 @@ export function resolveAttachmentReferences(
2734
});
2835
}
2936

30-
export const layer = McpToolAccess.toLayer(AttachmentToolkit, {
31-
t3_attachment_prepare_upload: McpToolAccess.writes((input) =>
32-
Upload.issueAttachmentUploadUrl(input.upload).pipe(Effect.mapError(unavailable)),
33-
),
34-
t3_attachment_discard: McpToolAccess.writes((input) =>
35-
Upload.deletePendingAttachment(input.attachmentId).pipe(Effect.as({})),
36-
),
37-
t3_thread_send_attachments: McpToolAccess.writesThreads(
38-
(input) => [input.threadId],
39-
(input) =>
40-
Effect.gen(function* () {
41-
const { caller, projection } = yield* readThread(input.threadId, ["messages"]);
42-
if (projection.thread.archivedAt !== null)
43-
return yield* new OrchestratorMcpFailure({
44-
code: "invalid_request",
45-
message: "Unarchive the target thread before sending attachments.",
46-
});
47-
const attachments = yield* resolveAttachmentReferences(
48-
input.attachments,
49-
projection.messages.flatMap((message) => message.attachments),
50-
);
51-
const commandId = yield* newCommandId();
52-
const messageId = MessageId.make(commandId);
53-
const result = yield* ThreadMessageIntake.sendToThread({
54-
projectId: projection.thread.projectId,
55-
threadId: projection.thread.id,
56-
commandId,
57-
messageId,
58-
...(caller === undefined ? {} : { senderThreadId: caller.id }),
59-
text: input.message ?? "",
60-
attachments,
61-
mode: "auto",
62-
createdBy: "agent",
63-
creationSource: "mcp",
64-
}).pipe(
65-
Effect.mapError((error) =>
66-
error._tag === "AttachmentClaimError"
67-
? new OrchestratorMcpFailure({ code: "orchestration_error", message: error.message })
68-
: unavailable(),
69-
),
70-
);
71-
return {
72-
threadId: projection.thread.id,
73-
messageId,
74-
runId: result.run.id,
75-
status: result.run.status,
76-
};
77-
}),
78-
),
79-
});
37+
/**
38+
* As long as the pending file can live. The sweep counts its 24 hours from when
39+
* the upload finishes, which can be up to the upload URL's lifetime after issue.
40+
*/
41+
const UPLOAD_OWNER_TTL_MS = 24 * 60 * 60 * 1000 + ATTACHMENT_UPLOAD_URL_TTL_MS;
42+
43+
/** A thread owns its uploads across provider sessions; an outside client per MCP session. */
44+
const uploadOwner = McpInvocationContext.McpInvocationContext.pipe(
45+
Effect.map((scope) => scope.thread?.threadId ?? scope.requestNamespace),
46+
);
47+
48+
export const layer = McpToolAccess.toLayer(
49+
AttachmentToolkit,
50+
Effect.sync(() => {
51+
// Which caller prepared each pending upload, so only that caller can discard it.
52+
const uploadOwners = new Map<string, { readonly owner: string; readonly issuedAt: number }>();
53+
return {
54+
t3_attachment_prepare_upload: McpToolAccess.writes((input) =>
55+
Effect.gen(function* () {
56+
const result = yield* Upload.issueAttachmentUploadUrl(input.upload).pipe(
57+
Effect.mapError(unavailable),
58+
);
59+
const now = yield* Clock.currentTimeMillis;
60+
for (const [id, entry] of uploadOwners) {
61+
if (now - entry.issuedAt > UPLOAD_OWNER_TTL_MS) uploadOwners.delete(id);
62+
}
63+
uploadOwners.set(result.attachmentId, { owner: yield* uploadOwner, issuedAt: now });
64+
return result;
65+
}),
66+
),
67+
t3_attachment_discard: McpToolAccess.writes((input) =>
68+
Effect.gen(function* () {
69+
if (uploadOwners.get(input.attachmentId)?.owner !== (yield* uploadOwner)) {
70+
return yield* new OrchestratorMcpFailure({
71+
code: "invalid_request",
72+
message: "Only the caller that prepared a pending upload can discard it.",
73+
});
74+
}
75+
yield* Upload.deletePendingAttachment(input.attachmentId);
76+
uploadOwners.delete(input.attachmentId);
77+
return {};
78+
}),
79+
),
80+
t3_thread_send_attachments: McpToolAccess.writesThreads(
81+
(input) => [input.threadId],
82+
(input) =>
83+
Effect.gen(function* () {
84+
const { caller, projection } = yield* readThread(input.threadId, ["messages"]);
85+
if (projection.thread.archivedAt !== null)
86+
return yield* new OrchestratorMcpFailure({
87+
code: "invalid_request",
88+
message: "Unarchive the target thread before sending attachments.",
89+
});
90+
const attachments = yield* resolveAttachmentReferences(
91+
input.attachments,
92+
projection.messages.flatMap((message) => message.attachments),
93+
);
94+
const commandId = yield* newCommandId();
95+
const messageId = MessageId.make(commandId);
96+
const result = yield* ThreadMessageIntake.sendToThread({
97+
projectId: projection.thread.projectId,
98+
threadId: projection.thread.id,
99+
commandId,
100+
messageId,
101+
...(caller === undefined ? {} : { senderThreadId: caller.id }),
102+
text: input.message ?? "",
103+
attachments,
104+
mode: "auto",
105+
createdBy: "agent",
106+
creationSource: "mcp",
107+
}).pipe(
108+
Effect.mapError((error) =>
109+
error._tag === "AttachmentClaimError"
110+
? new OrchestratorMcpFailure({
111+
code: "orchestration_error",
112+
message: error.message,
113+
})
114+
: unavailable(),
115+
),
116+
);
117+
return {
118+
threadId: projection.thread.id,
119+
messageId,
120+
runId: result.run.id,
121+
status: result.run.status,
122+
};
123+
}),
124+
),
125+
};
126+
}),
127+
);

0 commit comments

Comments
 (0)