Skip to content

Commit bc566a4

Browse files
committed
fix(server): device tools accept a host's label and reject unknown hosts
1 parent f3b058c commit bc566a4

2 files changed

Lines changed: 64 additions & 6 deletions

File tree

‎apps/server/src/mcp/toolkits/device/handlers.test.ts‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { describe, expect, it } from "vite-plus/test";
1+
import type { DeviceHostSummary } from "@t3tools/contracts";
2+
import * as Effect from "effect/Effect";
3+
import { describe, expect, it } from "@effect/vitest";
24

3-
import { agentDeviceQuickStart, pngDimensions } from "./handlers.ts";
5+
import { agentDeviceQuickStart, pngDimensions, resolveHostId } from "./handlers.ts";
46

57
const device = {
68
hostId: "local",
@@ -12,7 +14,39 @@ const device = {
1214
physical: false,
1315
};
1416

17+
const hosts = [
18+
{ id: "local", label: "This machine" },
19+
{ id: "0dc4f37b-5e0a-4f4c-9a51-0c6b8e8a1f2d", label: "oliverMac" },
20+
{ id: "mini-1", label: "Mac mini" },
21+
{ id: "mini-2", label: "Mac mini" },
22+
].map((host): DeviceHostSummary => ({
23+
...host,
24+
kind: host.id === "local" ? "local" : "ssh",
25+
platforms: [],
26+
hubInstalled: true,
27+
agentDeviceInstalled: true,
28+
}));
29+
1530
describe("device tool helpers", () => {
31+
it.effect("resolves a host by exact id or case-insensitive label", () =>
32+
Effect.gen(function* () {
33+
const mac = "0dc4f37b-5e0a-4f4c-9a51-0c6b8e8a1f2d";
34+
expect(yield* resolveHostId(hosts, mac)).toBe(mac);
35+
expect(yield* resolveHostId(hosts, "olivermac")).toBe(mac);
36+
}),
37+
);
38+
39+
it.effect("rejects an unknown or ambiguous host and lists the candidates", () =>
40+
Effect.gen(function* () {
41+
const unknown = yield* Effect.flip(resolveHostId(hosts, "studio"));
42+
expect(unknown.reason).toContain("This machine (local)");
43+
expect(unknown.reason).toContain("oliverMac (0dc4f37b-5e0a-4f4c-9a51-0c6b8e8a1f2d)");
44+
const ambiguous = yield* Effect.flip(resolveHostId(hosts, "mac mini"));
45+
expect(ambiguous.reason).toContain("Mac mini (mini-1), Mac mini (mini-2)");
46+
expect(ambiguous.reason).not.toContain("oliverMac");
47+
}),
48+
);
49+
1650
it("uses the absolute launcher in every quick-start command", () => {
1751
const text = agentDeviceQuickStart(
1852
device,

‎apps/server/src/mcp/toolkits/device/handlers.ts‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
type DeviceError,
33
type DeviceHostId,
4+
type DeviceHostSummary,
45
type DeviceId,
56
type DevicePlatform,
67
type DeviceSummary,
@@ -72,6 +73,26 @@ const requireDeviceAccess = McpInvocationContext.requireThreadMcpCapability("dev
7273
),
7374
);
7475

76+
export const resolveHostId = Effect.fn("handlers.resolveHostId")(function* (
77+
hosts: ReadonlyArray<DeviceHostSummary>,
78+
hostId: string | undefined,
79+
): Effect.fn.Return<DeviceHostId | undefined, DeviceToolUnavailableError> {
80+
if (!hostId) return undefined;
81+
const exact = hosts.find((host) => host.id === hostId);
82+
if (exact) return exact.id;
83+
const labelMatches = hosts.filter((host) => host.label.toLowerCase() === hostId.toLowerCase());
84+
if (labelMatches.length === 1) return labelMatches[0]!.id;
85+
const listed = (labelMatches.length > 1 ? labelMatches : hosts)
86+
.map((host) => `${host.label} (${host.id})`)
87+
.join(", ");
88+
return yield* new DeviceToolUnavailableError({
89+
reason:
90+
labelMatches.length > 1
91+
? `Several device hosts are named ${hostId}; pass one id: ${listed}.`
92+
: `No device host ${hostId}. Known hosts: ${listed || "none"}.`,
93+
});
94+
});
95+
7596
const pickDevice = Effect.fn("handlers.pickDevice")(function* (
7697
devices: ReadonlyArray<DeviceSummary>,
7798
input: {
@@ -126,7 +147,7 @@ const handlers = {
126147
"Device support is off. Ask the user to enable it in the Device panel before installing or starting device tools.",
127148
});
128149
}
129-
const hostId = input?.hostId;
150+
const hostId = yield* resolveHostId(state.hosts, input?.hostId);
130151
const open = state.sessions
131152
.filter((session) => session.threadId === scope.thread.threadId)
132153
.map((session) => ({ hostId: session.hostId, deviceId: session.deviceId }));
@@ -153,7 +174,8 @@ const handlers = {
153174
"Device support is off. Ask the user to enable it in the Device panel before installing or starting device tools.",
154175
});
155176
}
156-
const target = yield* pickDevice(state.devices, input);
177+
const hostId = yield* resolveHostId(state.hosts, input.hostId);
178+
const target = yield* pickDevice(state.devices, { ...input, hostId });
157179
// Resolve consent and agent connectivity before booting or registering a session.
158180
if (!(yield* devices.agentReadinessIfSupported(target.hostId))) {
159181
return yield* new DeviceToolUnavailableError({
@@ -221,8 +243,9 @@ const handlers = {
221243
const scope = yield* requireDeviceAccess;
222244
const devices = yield* DeviceService.DeviceService;
223245
const sessions = yield* devices.sessionsForThread(scope.thread.threadId);
246+
const inputHostId = yield* resolveHostId((yield* devices.state).hosts, input.hostId);
224247
const hostId =
225-
input.deviceId === undefined ? input.hostId : (input.hostId ?? LOCAL_DEVICE_HOST_ID);
248+
input.deviceId === undefined ? inputHostId : (inputHostId ?? LOCAL_DEVICE_HOST_ID);
226249
const target = sessions.findLast(
227250
(session) =>
228251
(hostId === undefined || session.hostId === hostId) &&
@@ -251,9 +274,10 @@ const handlers = {
251274
Effect.gen(function* () {
252275
const scope = yield* requireDeviceAccess;
253276
const devices = yield* DeviceService.DeviceService;
277+
const hostId = yield* resolveHostId((yield* devices.state).hosts, input.hostId);
254278
yield* devices.close({
255279
threadId: scope.thread.threadId,
256-
...(input.hostId === undefined ? {} : { hostId: input.hostId }),
280+
...(hostId === undefined ? {} : { hostId }),
257281
...(input.deviceId === undefined ? {} : { deviceId: input.deviceId }),
258282
...(input.shutdown === undefined ? {} : { shutdown: input.shutdown }),
259283
});

0 commit comments

Comments
 (0)