Skip to content

Commit f42ff72

Browse files
committed
fix(mcp): 保留设备截图失败的可操作提示
在文本和结构化 MCP 返回中保留已知设备错误消息,同时继续隐藏内部原因及原始输出。 将截图 HTTP 拒绝与传输失败区分,提示调用方检查设备状态并按需重新打开。 通过 53 项定向测试、真实本地 HTTP 失败探测与 check:changed。
1 parent d7a8d6f commit f42ff72

4 files changed

Lines changed: 127 additions & 3 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

@@ -236,7 +236,10 @@ export const makeDeviceHubOperations = (options: {
236236
(cause) =>
237237
new DeviceOperationError({
238238
operation: "screenshot",
239-
reason: "request_failed",
239+
reason:
240+
HttpClientError.isHttpClientError(cause) && cause.reason._tag === "StatusCodeError"
241+
? "hub_rejected"
242+
: "request_failed",
240243
cause,
241244
}),
242245
),

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

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

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

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { expect, it } from "@effect/vitest";
44
import { NodeHttpServer } from "@effect/platform-node";
55
import * as NodeServices from "@effect/platform-node/NodeServices";
66
import {
7+
DeviceId,
8+
DeviceOperationError,
79
EnvironmentId,
810
OrchestratorMcpFailure,
911
PreviewTabId,
@@ -19,6 +21,7 @@ import { McpSchema, McpServer, Tool, Toolkit } from "effect/ai";
1921
import { HttpBody, HttpClient, HttpClientRequest, HttpRouter } from "effect/http";
2022

2123
import * as ProjectService from "../project/ProjectService.ts";
24+
import * as DeviceService from "../device/DeviceService.ts";
2225
import * as PullRequestReviewGuideService from "../pullRequest/PullRequestReviewGuideService.ts";
2326
import * as ServerConfig from "../config.ts";
2427
import * as McpHttpServer from "./McpHttpServer.ts";
@@ -132,6 +135,85 @@ const callTool = (name: string, args: Record<string, unknown>) =>
132135

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

138+
it.effect.each([
139+
{ input: {}, open: false, message: "No device is open in this thread. Call device_open first." },
140+
{
141+
input: { deviceId: "missing", hostId: "local" },
142+
open: false,
143+
message: "Device missing on host local is not open in this thread. Call device_open first.",
144+
},
145+
{
146+
input: {},
147+
open: true,
148+
message:
149+
"Device screenshot failed: Could not communicate with device support. Try refreshing devices.",
150+
},
151+
{
152+
input: {},
153+
open: true,
154+
rejected: true,
155+
message:
156+
"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.",
157+
},
158+
])(
159+
"preserves actionable screenshot failure text: $message",
160+
({ input, open, message, ...options }) =>
161+
Effect.gen(function* () {
162+
const server = yield* McpServer.McpServer;
163+
const result = yield* server.callTool({ name: "device_screenshot", arguments: input }).pipe(
164+
Effect.provideService(McpInvocationContext.McpInvocationContext, {
165+
...invocation,
166+
capabilities: new Set(["device"] as const),
167+
}),
168+
Effect.provideService(McpSchema.McpServerClient, client),
169+
);
170+
expect(result.isError).toBe(true);
171+
expect(result.content).toEqual([{ type: "text", text: message }]);
172+
expect(result.structuredContent).toMatchObject({
173+
error: { message, operation: "screenshot" },
174+
});
175+
expect(JSON.stringify(result)).not.toContain("private hub output");
176+
}).pipe(
177+
Effect.provide(
178+
McpHttpServer.layerDeviceToolkit.pipe(
179+
Layer.provideMerge(McpServer.McpServer.layer),
180+
Layer.provide(McpToolAccessTestkit.liveThreadsLayer),
181+
Layer.provide(
182+
Layer.mock(DeviceService.DeviceService)({
183+
sessionsForThread: () =>
184+
Effect.succeed(
185+
open
186+
? [
187+
{
188+
threadId,
189+
hostId: "local",
190+
deviceId: DeviceId.make("phone"),
191+
platform: "ios",
192+
openedAt: "2026-10-23T00:00:00Z",
193+
},
194+
]
195+
: [],
196+
),
197+
screenshot: () =>
198+
Effect.fail(
199+
new DeviceOperationError({
200+
operation: "screenshot",
201+
reason:
202+
"rejected" in options && options.rejected ? "hub_rejected" : "request_failed",
203+
cause: new Error("private hub output"),
204+
}),
205+
),
206+
}),
207+
),
208+
Layer.provide(Layer.mock(ProjectService.ProjectService)({})),
209+
Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-device-mcp-test-" })),
210+
Layer.provide(NodeServices.layer),
211+
),
212+
),
213+
Effect.scoped,
214+
),
215+
);
216+
135217
// Real tool definitions with handlers that record what they decoded.
136218
const layerRecordingTools = (calls: Array<unknown>) => {
137219
const RecordingToolkit = Toolkit.make(

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type * as Types from "effect/Types";
1414
import { AiError, McpProtocol, McpSchema, McpServer, Tool, Toolkit } from "effect/ai";
1515
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
1616
import {
17+
DeviceToolError,
1718
OrchestratorMcpFailure,
1819
PreviewAutomationError,
1920
type RunId,
@@ -717,6 +718,8 @@ const registerImageTool = Effect.fn("McpHttpServer.registerImageTool")(function*
717718
});
718719
});
719720

721+
const isDeviceToolError = Schema.is(DeviceToolError);
722+
720723
const registerDeviceScreenshot = Effect.fn("McpHttpServer.registerDeviceScreenshot")(function* () {
721724
const devices = yield* DeviceService.DeviceService;
722725
const threads = yield* ThreadManagementService.ThreadManagementService;
@@ -733,7 +736,12 @@ const registerDeviceScreenshot = Effect.fn("McpHttpServer.registerDeviceScreensh
733736
Effect.provideService(ThreadManagementService.ThreadManagementService, threads),
734737
),
735738
"screenshot",
736-
"Device screenshot failed.",
739+
(error) => {
740+
if (!isDeviceToolError(error)) return "Device screenshot failed.";
741+
return error._tag === "DeviceOperationError" && error.reason === "hub_rejected"
742+
? `${error.message} Call device_list to check the device state, and device_open if it needs to be reopened.`
743+
: error.message;
744+
},
737745
);
738746
});
739747

0 commit comments

Comments
 (0)