The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 10:45 UTC
fix(mcp): preserve actionable device screenshot errors (#25216)
Co-authored-by: Yukun Shan <92423096+nateEc@users.noreply.github.com>
maria committed
Oct 30, 2026 at 21:11 UTC
c3d8c4b9403305e75d995d5e00f4394a7ac32263
4 files changed
+127
-6
apps/server/src/device/DeviceHubOperations.ts
+5
-2
@@ -7,7 +7,7 @@ import {
7
import * as Duration from "effect/Duration";
8
import * as Effect from "effect/Effect";
9
import * as Schema from "effect/Schema";
10
-import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
10
+import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
11
12
import type { DeviceHostReady, DeviceOperations, HostDevice } from "./DeviceHost.ts";
13
@@ -239,7 +239,10 @@ export const makeDeviceHubOperations = (options: {
239
(cause) =>
240
new DeviceOperationError({
241
operation: "screenshot",
242
- reason: "request_failed",
242
+ reason:
243
+ HttpClientError.isHttpClientError(cause) && cause.reason._tag === "StatusCodeError"
244
+ ? "hub_rejected"
245
+ : "request_failed",
246
cause,
247
}),
248
),
apps/server/src/device/DeviceService.test.ts
+31
@@ -42,6 +42,37 @@ const decodeJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown
42
const decodeBootError = Schema.decodeUnknownSync(Schema.fromJsonString(DeviceBootError));
43
const encodeBootError = Schema.encodeSync(Schema.fromJsonString(DeviceBootError));
44
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
+
76
const fixture = Effect.fn("fixture")(function* (
77
onBoot: Effect.Effect<void> = Effect.void,
78
bootError?: string | { error?: string; errors?: Array<{ message: string; error?: string }> },
apps/server/src/mcp/McpHttpServer.test.ts
+83
@@ -5,6 +5,8 @@ import { expect, it } from "@effect/vitest";
5
import { NodeHttpServer } from "@effect/platform-node";
6
import * as NodeServices from "@effect/platform-node/NodeServices";
7
import {
8
+ DeviceId,
9
+ DeviceOperationError,
10
EnvironmentId,
11
OrchestratorMcpEnvironmentTarget,
12
OrchestratorMcpFailure,
@@ -24,6 +26,7 @@ import { McpSchema, McpServer, Tool, Toolkit } from "effect/ai";
26
import { HttpBody, HttpClient, HttpClientRequest, HttpRouter } from "effect/http";
27
28
import * as ProjectService from "../project/ProjectService.ts";
29
+import * as DeviceService from "../device/DeviceService.ts";
30
import * as PullRequestReviewGuideService from "../pullRequest/PullRequestReviewGuideService.ts";
31
import * as ServerConfig from "../config.ts";
32
import * as ServerSettings from "../serverSettings.ts";
@@ -140,6 +143,86 @@ const callTool = (name: string, args: Record<string, unknown>) =>
143
144
const callSnapshot = (args: Record<string, unknown>) => callTool("preview_snapshot", args);
145
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
+
226
const layerRecordingTools = (calls: Array<unknown>) => {
227
const RecordingToolkit = Toolkit.make(
228
OrchestratorToolkit.tools.delegate_task,
apps/server/src/mcp/McpHttpServer.ts
+8
-4
@@ -766,10 +766,14 @@ const registerDeviceScreenshot = Effect.fn("McpHttpServer.registerDeviceScreensh
766
Effect.provideService(ThreadManagementService.ThreadManagementService, threads),
767
),
768
"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
+ },
777
);
778
});
779