Skip to content

Commit c3d8c4b

Browse files
maria-rcksnateEc
andauthored
fix(mcp): preserve actionable device screenshot errors (#25216)
Co-authored-by: Yukun Shan <[email protected]>
1 parent 3a6b018 commit c3d8c4b

4 files changed

Lines changed: 127 additions & 6 deletions

File tree

‎apps/server/src/device/DeviceHubOperations.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import * as Duration from "effect/Duration";
88
import * as Effect from "effect/Effect";
99
import * as Schema from "effect/Schema";
10-
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
10+
import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
1111

1212
import type { DeviceHostReady, DeviceOperations, HostDevice } from "./DeviceHost.ts";
1313

@@ -239,7 +239,10 @@ export const makeDeviceHubOperations = (options: {
239239
(cause) =>
240240
new DeviceOperationError({
241241
operation: "screenshot",
242-
reason: "request_failed",
242+
reason:
243+
HttpClientError.isHttpClientError(cause) && cause.reason._tag === "StatusCodeError"
244+
? "hub_rejected"
245+
: "request_failed",
243246
cause,
244247
}),
245248
),

‎apps/server/src/device/DeviceService.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,37 @@ const decodeJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown
4242
const decodeBootError = Schema.decodeUnknownSync(Schema.fromJsonString(DeviceBootError));
4343
const encodeBootError = Schema.encodeSync(Schema.fromJsonString(DeviceBootError));
4444

45+
it.effect.each([400, 500])(
46+
"distinguishes screenshot HTTP rejection (%s) from transport failure",
47+
(status) =>
48+
Effect.gen(function* () {
49+
const operations = makeDeviceHubOperations({
50+
hostId: LOCAL_DEVICE_HOST_ID,
51+
origin: "http://device.test",
52+
http: HttpClient.make((request) =>
53+
Effect.succeed(
54+
HttpClientResponse.fromWeb(request, new Response("private hub output", { status })),
55+
),
56+
),
57+
run: () => Effect.die("Screenshot does not run a command"),
58+
androidAvailable: Effect.succeed(false),
59+
});
60+
const result = yield* operations
61+
.screenshot({
62+
id: "phone",
63+
name: "Phone",
64+
version: "iOS 26.3",
65+
platform: "ios",
66+
booted: true,
67+
physical: false,
68+
})
69+
.pipe(Effect.result);
70+
expect(result).toMatchObject({
71+
failure: { _tag: "DeviceOperationError", reason: "hub_rejected" },
72+
});
73+
}),
74+
);
75+
4576
const fixture = Effect.fn("fixture")(function* (
4677
onBoot: Effect.Effect<void> = Effect.void,
4778
bootError?: string | { error?: string; errors?: Array<{ message: string; error?: string }> },

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { expect, it } from "@effect/vitest";
55
import { NodeHttpServer } from "@effect/platform-node";
66
import * as NodeServices from "@effect/platform-node/NodeServices";
77
import {
8+
DeviceId,
9+
DeviceOperationError,
810
EnvironmentId,
911
OrchestratorMcpEnvironmentTarget,
1012
OrchestratorMcpFailure,
@@ -24,6 +26,7 @@ import { McpSchema, McpServer, Tool, Toolkit } from "effect/ai";
2426
import { HttpBody, HttpClient, HttpClientRequest, HttpRouter } from "effect/http";
2527

2628
import * as ProjectService from "../project/ProjectService.ts";
29+
import * as DeviceService from "../device/DeviceService.ts";
2730
import * as PullRequestReviewGuideService from "../pullRequest/PullRequestReviewGuideService.ts";
2831
import * as ServerConfig from "../config.ts";
2932
import * as ServerSettings from "../serverSettings.ts";
@@ -140,6 +143,86 @@ const callTool = (name: string, args: Record<string, unknown>) =>
140143

141144
const callSnapshot = (args: Record<string, unknown>) => callTool("preview_snapshot", args);
142145

146+
it.effect.each([
147+
{ input: {}, open: false, message: "No device is open in this thread. Call device_open first." },
148+
{
149+
input: { deviceId: "missing", hostId: "local" },
150+
open: false,
151+
message: "Device missing is not open in this thread. Call device_open first.",
152+
},
153+
{
154+
input: {},
155+
open: true,
156+
message:
157+
"Device screenshot failed: Could not communicate with device support. Try refreshing devices.",
158+
},
159+
{
160+
input: {},
161+
open: true,
162+
rejected: true,
163+
message:
164+
"Device screenshot failed: The device hub could not complete the request. Call device_list to check the device state, and device_open if it needs to be reopened.",
165+
},
166+
])(
167+
"preserves actionable screenshot failure text: $message",
168+
({ input, open, message, ...options }) =>
169+
Effect.gen(function* () {
170+
const server = yield* McpServer.McpServer;
171+
const result = yield* server.callTool({ name: "device_screenshot", arguments: input }).pipe(
172+
Effect.provideService(McpInvocationContext.McpInvocationContext, {
173+
...invocation,
174+
capabilities: new Set(["device"] as const),
175+
}),
176+
Effect.provideService(McpSchema.McpServerClient, client),
177+
);
178+
expect(result.isError).toBe(true);
179+
expect(result.content).toEqual([{ type: "text", text: message }]);
180+
expect(result.structuredContent).toMatchObject({
181+
error: { message, operation: "screenshot" },
182+
});
183+
expect(JSON.stringify(result)).not.toContain("private hub output");
184+
}).pipe(
185+
Effect.provide(
186+
McpHttpServer.layerDeviceToolkit.pipe(
187+
Layer.provideMerge(McpServer.McpServer.layer),
188+
Layer.provide(McpToolAccessTestkit.liveThreadsLayer),
189+
Layer.provide(
190+
Layer.mock(DeviceService.DeviceService)({
191+
locateSession: () => Effect.succeedNone,
192+
sessionsForThread: () =>
193+
Effect.succeed(
194+
open
195+
? [
196+
{
197+
threadId,
198+
hostId: "local",
199+
deviceId: DeviceId.make("phone"),
200+
platform: "ios",
201+
openedAt: "2026-10-23T00:00:00Z",
202+
},
203+
]
204+
: [],
205+
),
206+
screenshot: () =>
207+
Effect.fail(
208+
new DeviceOperationError({
209+
operation: "screenshot",
210+
reason:
211+
"rejected" in options && options.rejected ? "hub_rejected" : "request_failed",
212+
cause: new Error("private hub output"),
213+
}),
214+
),
215+
}),
216+
),
217+
Layer.provide(Layer.mock(ProjectService.ProjectService)({})),
218+
Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-device-mcp-test-" })),
219+
Layer.provide(NodeServices.layer),
220+
),
221+
),
222+
Effect.scoped,
223+
),
224+
);
225+
143226
const layerRecordingTools = (calls: Array<unknown>) => {
144227
const RecordingToolkit = Toolkit.make(
145228
OrchestratorToolkit.tools.delegate_task,

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,14 @@ const registerDeviceScreenshot = Effect.fn("McpHttpServer.registerDeviceScreensh
766766
Effect.provideService(ThreadManagementService.ThreadManagementService, threads),
767767
),
768768
"screenshot",
769-
(error) =>
770-
isDeviceToolError(error) || isOrchestratorMcpFailure(error)
771-
? error.message
772-
: "Device screenshot failed.",
769+
(error) => {
770+
if (!isDeviceToolError(error)) {
771+
return isOrchestratorMcpFailure(error) ? error.message : "Device screenshot failed.";
772+
}
773+
return error._tag === "DeviceOperationError" && error.reason === "hub_rejected"
774+
? `${error.message} Call device_list to check the device state, and device_open if it needs to be reopened.`
775+
: error.message;
776+
},
773777
);
774778
});
775779

0 commit comments

Comments
 (0)