Skip to content
TypeScript

fix(server): device tools accept a host's label and reject unknown hosts (#25836)

oliver7808c1e30037
diff --git a/apps/server/src/mcp/McpDeviceToolkit.test.ts b/apps/server/src/mcp/McpDeviceToolkit.test.ts
index 6341d23b6bf..fe0cd9fda09 100644
--- a/apps/server/src/mcp/McpDeviceToolkit.test.ts
+++ b/apps/server/src/mcp/McpDeviceToolkit.test.ts
@@ -210,6 +210,22 @@ it.effect("registers the device tools and returns the screenshot as image conten
expect(foreign.isError).toBe(true);
expect(foreign.content.map((entry) => entry.type)).toEqual(["text"]);
+ const listed = yield* server
+ .callTool({ name: "device_list", arguments: { hostId: "THIS MACHINE" } })
+ .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
+ expect(listed.structuredContent).toMatchObject({
+ hosts: [{ id: "local" }],
+ devices: [device],
+ });
+
+ const unknownHost = yield* server
+ .callTool({ name: "device_screenshot", arguments: { hostId: "oliverMac" } })
+ .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
+ expect(unknownHost.isError).toBe(true);
+ expect(unknownHost.content).toEqual([
+ { type: "text", text: "No device host oliverMac. Known hosts: This machine (local)." },
+ ]);
+
const denied = yield* server
.callTool({ name: "device_list", arguments: {} })
.pipe(callWith(["preview"]), Effect.provideService(McpSchema.McpServerClient, client));
@@ -419,9 +435,14 @@ describe("device targeting across hosts", () => {
run([], ({ opened, call }) =>
Effect.gen(function* () {
const result = yield* call("device_open", { deviceId: "MAC-UDID" });
+ const byLabel = yield* call("device_open", { platform: "ios", hostId: "MAC MINI" });
+ expect(byLabel.isError).toBe(false);
expect(text(result)).not.toContain("No device");
expect(result.isError).toBe(false);
- expect(opened).toEqual([{ hostId: "mac-mini", deviceId: "MAC-UDID" }]);
+ expect(opened).toEqual([
+ { hostId: "mac-mini", deviceId: "MAC-UDID" },
+ { hostId: "mac-mini", deviceId: "MAC-UDID" },
+ ]);
}),
),
);
@@ -515,7 +536,7 @@ describe("device targeting across hosts", () => {
"No device Pixel_9 on 'Mac Mini' (hostId mac-mini). Call device_list for current ids.",
);
expect(yield* missing({ hostId: "unknown-host" })).toBe(
- "No device Pixel_9 on hostId unknown-host. Call device_list for current ids.",
+ "No device host unknown-host. Known hosts: This machine (local), Mac Mini (mac-mini).",
);
expect(opened).toEqual([]);
}),
@@ -528,6 +549,9 @@ describe("device targeting across hosts", () => {
const result = yield* call("device_screenshot", { deviceId: "MAC-UDID" });
expect(result.isError).toBe(false);
expect(result.structuredContent).toMatchObject({ device: { hostId: "mac-mini" } });
+ const byLabel = yield* call("device_screenshot", { hostId: "MAC MINI" });
+ expect(byLabel.isError).toBe(false);
+ expect(byLabel.structuredContent).toMatchObject({ device: { hostId: "mac-mini" } });
}),
),
);
@@ -598,9 +622,14 @@ describe("device targeting across hosts", () => {
Effect.gen(function* () {
expect((yield* call("device_close", { deviceId: "MAC-UDID" })).isError).toBe(false);
expect((yield* call("device_close", { deviceId: "Pixel_9" })).isError).toBe(false);
+ expect((yield* call("device_close", { hostId: "MAC MINI" })).isError).toBe(false);
+ const unknown = yield* call("device_close", { hostId: "unknown-host" });
+ expect(unknown.isError).toBe(true);
+ expect(text(unknown)).toContain("No device host unknown-host.");
expect(closed).toEqual([
{ hostId: "mac-mini", deviceId: "MAC-UDID" },
{ hostId: undefined, deviceId: "Pixel_9" },
+ { hostId: "mac-mini", deviceId: undefined },
]);
}),
),
diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts
index 902785778a9..3ddccf9eb07 100644
--- a/apps/server/src/mcp/McpHttpServer.test.ts
+++ b/apps/server/src/mcp/McpHttpServer.test.ts
@@ -146,7 +146,7 @@ const callSnapshot = (args: Record<string, unknown>) => callTool("preview_snapsh
it.effect.each([
{ input: {}, open: false, message: "No device is open in this thread. Call device_open first." },
{
- input: { deviceId: "missing", hostId: "local" },
+ input: { deviceId: "missing" },
open: false,
message: "Device missing is not open in this thread. Call device_open first.",
},
diff --git a/apps/server/src/mcp/toolkits/device/handlers.test.ts b/apps/server/src/mcp/toolkits/device/handlers.test.ts
index 666a244ed50..e51ad9af291 100644
--- a/apps/server/src/mcp/toolkits/device/handlers.test.ts
+++ b/apps/server/src/mcp/toolkits/device/handlers.test.ts
@@ -1,6 +1,8 @@
-import { describe, expect, it } from "vite-plus/test";
+import type { DeviceHostSummary } from "@t3tools/contracts";
+import * as Effect from "effect/Effect";
+import { describe, expect, it } from "@effect/vitest";
-import { agentDeviceQuickStart, pngDimensions } from "./handlers.ts";
+import { agentDeviceQuickStart, pngDimensions, resolveHostId } from "./handlers.ts";
const device = {
hostId: "local",
@@ -12,7 +14,43 @@ const device = {
physical: false,
};
+const hosts = [
+ { id: "local", label: "This machine" },
+ { id: "0dc4f37b-5e0a-4f4c-9a51-0c6b8e8a1f2d", label: "oliverMac" },
+ { id: "mini-1", label: "Mac mini" },
+ { id: "mini-2", label: "Mac mini" },
+].map((host): DeviceHostSummary => ({
+ ...host,
+ kind: host.id === "local" ? "local" : "ssh",
+ platforms: [],
+ hubInstalled: true,
+ agentDeviceInstalled: true,
+}));
+
describe("device tool helpers", () => {
+ it.effect("resolves a host by exact id or case-insensitive label", () =>
+ Effect.gen(function* () {
+ const mac = "0dc4f37b-5e0a-4f4c-9a51-0c6b8e8a1f2d";
+ expect(yield* resolveHostId(hosts, undefined)).toBeUndefined();
+ expect(yield* resolveHostId([...hosts, { ...hosts[1]!, label: "local" }], "local")).toBe(
+ "local",
+ );
+ expect(yield* resolveHostId(hosts, mac)).toBe(mac);
+ expect(yield* resolveHostId(hosts, "olivermac")).toBe(mac);
+ }),
+ );
+
+ it.effect("rejects an unknown or ambiguous host and lists the candidates", () =>
+ Effect.gen(function* () {
+ const unknown = yield* Effect.flip(resolveHostId(hosts, "studio"));
+ expect(unknown.reason).toContain("This machine (local)");
+ expect(unknown.reason).toContain("oliverMac (0dc4f37b-5e0a-4f4c-9a51-0c6b8e8a1f2d)");
+ const ambiguous = yield* Effect.flip(resolveHostId(hosts, "mac mini"));
+ expect(ambiguous.reason).toContain("Mac mini (mini-1), Mac mini (mini-2)");
+ expect(ambiguous.reason).not.toContain("oliverMac");
+ }),
+ );
+
it("uses the absolute launcher in every quick-start command", () => {
const text = agentDeviceQuickStart(
device,
diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts
index 6812754af19..b73661899c8 100644
--- a/apps/server/src/mcp/toolkits/device/handlers.ts
+++ b/apps/server/src/mcp/toolkits/device/handlers.ts
@@ -93,6 +93,29 @@ const requireDeviceAccess = McpInvocationContext.requireThreadMcpCapability("dev
),
);
+export const resolveHostId = Effect.fn("handlers.resolveHostId")(function* (
+ hosts: ReadonlyArray<DeviceHostSummary>,
+ hostId: string | undefined,
+): Effect.fn.Return<DeviceHostId | undefined, DeviceToolUnavailableError> {
+ if (!hostId) return undefined;
+ const exact = hosts.find((host) => host.id === hostId);
+ if (exact) return exact.id;
+ const labelMatches = hosts.filter((host) => host.label.toLowerCase() === hostId.toLowerCase());
+ if (labelMatches.length === 1) return labelMatches[0]!.id;
+ const candidates = labelMatches.length > 1 ? labelMatches : hosts;
+ const listed = [
+ ...candidates.slice(0, 10).map((host) => `${host.label.slice(0, 64)} (${host.id})`),
+ ...(candidates.length > 10 ? [`${candidates.length - 10} more`] : []),
+ ].join(", ");
+ const selector = hostId.slice(0, 128);
+ return yield* new DeviceToolUnavailableError({
+ reason:
+ labelMatches.length > 1
+ ? `Several device hosts are named ${selector}; pass one id: ${listed}.`
+ : `No device host ${selector}. Known hosts: ${listed || "none"}.`,
+ });
+});
+
const hostName = (host: { readonly id: DeviceHostId; readonly label?: string | undefined }) =>
host.label === undefined ? `hostId ${host.id}` : `'${host.label}' (hostId ${host.id})`;
@@ -177,7 +200,7 @@ const handlers = {
"Device support is off. Ask the user to enable it in the Device panel before installing or starting device tools.",
});
}
- const hostId = input?.hostId;
+ const hostId = yield* resolveHostId(state.hosts, input?.hostId);
const open = state.sessions
.filter((session) => session.threadId === scope.thread.threadId)
.map((session) => ({ hostId: session.hostId, deviceId: session.deviceId }));
@@ -204,7 +227,8 @@ const handlers = {
"Device support is off. Ask the user to enable it in the Device panel before installing or starting device tools.",
});
}
- const target = yield* pickDevice(state, input);
+ const hostId = yield* resolveHostId(state.hosts, input.hostId);
+ const target = yield* pickDevice(state, { ...input, hostId });
// Resolve consent and agent connectivity before booting or registering a session.
if (!(yield* devices.agentReadinessIfSupported(target.hostId))) {
return yield* new DeviceToolUnavailableError({
@@ -273,14 +297,18 @@ const handlers = {
const scope = yield* requireDeviceAccess;
const devices = yield* DeviceService.DeviceService;
const threadId = scope.thread.threadId;
+ const hostId =
+ input.hostId === undefined
+ ? undefined
+ : yield* resolveHostId((yield* devices.state).hosts, input.hostId);
const target =
input.deviceId === undefined
? (yield* devices.sessionsForThread(threadId)).findLast(
- (session) => input.hostId === undefined || session.hostId === input.hostId,
+ (session) => hostId === undefined || session.hostId === hostId,
)
: Option.getOrUndefined(
yield* devices
- .locateSession({ threadId, deviceId: input.deviceId, hostId: input.hostId })
+ .locateSession({ threadId, deviceId: input.deviceId, hostId })
.pipe(mapLocateErrors),
);
if (!target) {
@@ -307,13 +335,17 @@ const handlers = {
const scope = yield* requireDeviceAccess;
const devices = yield* DeviceService.DeviceService;
const deviceId = input.deviceId;
+ const inputHostId =
+ input.hostId === undefined
+ ? undefined
+ : yield* resolveHostId((yield* devices.state).hosts, input.hostId);
const open =
deviceId === undefined
? Option.none()
: yield* devices
- .locateSession({ threadId: scope.thread.threadId, deviceId, hostId: input.hostId })
+ .locateSession({ threadId: scope.thread.threadId, deviceId, hostId: inputHostId })
.pipe(mapLocateErrors);
- const hostId = Option.isSome(open) ? open.value.hostId : input.hostId;
+ const hostId = Option.isSome(open) ? open.value.hostId : inputHostId;
yield* devices.close({
threadId: scope.thread.threadId,
...(hostId === undefined ? {} : { hostId }),
diff --git a/evals/mcp-tool-use/run.test.ts b/evals/mcp-tool-use/run.test.ts
index 33edb6465ab..d825c3b8fce 100644
--- a/evals/mcp-tool-use/run.test.ts
+++ b/evals/mcp-tool-use/run.test.ts
@@ -1156,7 +1156,7 @@ it.effect("accepts a successful exact-host recovery after a real device_open err
hostId: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
deviceId: "BBBBBBBB-BBBB-4BBB-8BBB-BBBBBBBBBBBB",
};
- const wrong = { ...input, hostId: "build-mac" };
+ const wrong = { ...input, hostId: "unknown-host" };
const error = yield* sample.devices!.call("device_open", wrong);
assert.isTrue(error.isError);
const opened = yield* sample.devices!.call("device_open", input);
@@ -3012,7 +3012,7 @@ it.effect("R12 OWN-01 protects occupied panel sessions from selected and wildcar
);
it.effect(
- "R12 DISC-01 allows the retained label/all-host/refresh route and bounds failed discovery",
+ "R12 DISC-01 allows the retained label/all-host route and bounds repeated discovery failures",
() =>
Effect.gen(function* () {
const c = deviceCases().find((c) => c.id === "DISC-01")!;
@@ -3021,11 +3021,7 @@ it.effect(
yield* withDevices(sample);
const tools = sample.devices!;
const calls: Array<SampleRun["calls"][number]> = [];
- for (const input of [
- { hostId: "build-mac" },
- {},
- { hostId: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa" },
- ]) {
+ for (const input of [{ hostId: "build-mac" }, {}]) {
const result = yield* tools.call("device_list", input);
calls.push({
step: calls.length,