The best way to control Claude Code, Codex and more from any computer or phone

main
ts 112 lines 4.18 KB
Raw
1 import {
2 OrchestratorMcpFailure,
3 DeviceToolCloseInput,
4 DeviceToolError,
5 DeviceToolListResult,
6 DeviceToolOpenInput,
7 DeviceToolOpenResult,
8 DeviceToolScreenshotResult,
9 DeviceToolTargetInput,
10 } from "@t3tools/contracts";
11 import * as Schema from "effect/Schema";
12 import * as FileSystem from "effect/FileSystem";
13 import * as Path from "effect/Path";
14 import * as ServerConfig from "../../../config.ts";
15 import { McpSchema, Tool, Toolkit } from "effect/ai";
16
17 import * as McpInvocationContext from "../../McpInvocationContext.ts";
18 import * as DeviceService from "../../../device/DeviceService.ts";
19 import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
20 import * as ProjectService from "../../../project/ProjectService.ts";
21
22 const dependencies = [
23 McpInvocationContext.McpInvocationContext,
24 ThreadManagementService.ThreadManagementService,
25 DeviceService.DeviceService,
26 ];
27
28 const DeviceToolFailure = Schema.Union([DeviceToolError, OrchestratorMcpFailure]);
29
30 const DeviceListTool = Tool.make("device_list", {
31 description:
32 "List iOS Simulators and Android Emulators on every device host, what each host can run, and what is open in this thread's Device panel. Call before `device_open` when you do not know a device id.",
33 // An empty struct serializes as `anyOf [object, array]`, which some
34 // providers reject and then drop every tool on the server with it.
35 parameters: Schema.Struct({
36 hostId: Schema.optional(
37 Schema.String.annotate({ description: "Limit to one device host. Defaults to all hosts." }),
38 ),
39 }),
40 success: DeviceToolListResult,
41 failure: DeviceToolFailure,
42 dependencies,
43 })
44 .annotate(Tool.Title, "List devices")
45 .annotate(Tool.Readonly, true)
46 .annotate(Tool.Destructive, false)
47 .annotate(Tool.Idempotent, true)
48 .annotate(Tool.OpenWorld, false)
49 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
50
51 const DeviceOpenTool = Tool.make("device_open", {
52 description:
53 "Open a simulator or emulator for this thread. Pass `deviceId` and `hostId` from `device_list`. Boots it if needed and shows it in the user's Device panel. Returns the `agent-device` CLI call pinned to that device. Drive the device with that CLI.",
54 parameters: DeviceToolOpenInput,
55 success: DeviceToolOpenResult,
56 failure: DeviceToolFailure,
57 dependencies: [
58 ...dependencies,
59 FileSystem.FileSystem,
60 Path.Path,
61 ServerConfig.ServerConfig,
62 // The thread's project can name the Expo project a cloud device runs under.
63 ProjectService.ProjectService,
64 ],
65 })
66 .annotate(Tool.Title, "Open device")
67 .annotate(Tool.Readonly, false)
68 .annotate(Tool.Destructive, false)
69 .annotate(Tool.Idempotent, true)
70 .annotate(Tool.OpenWorld, true)
71 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
72
73 export const DeviceScreenshotTool = Tool.make("device_screenshot", {
74 description:
75 "Get a PNG of an open device's screen. For taps and text, use the `agent-device` CLI.",
76 parameters: DeviceToolTargetInput,
77 success: DeviceToolScreenshotResult,
78 failure: DeviceToolFailure,
79 dependencies,
80 })
81 .annotate(Tool.Title, "Screenshot device")
82 .annotate(Tool.Readonly, true)
83 .annotate(Tool.Destructive, false)
84 .annotate(Tool.Idempotent, true)
85 .annotate(Tool.OpenWorld, true)
86 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
87
88 const DeviceCloseTool = Tool.make("device_close", {
89 description:
90 "Remove a device from this thread's Device panel. `shutdown=true` also turns it off.",
91 parameters: DeviceToolCloseInput,
92 success: Schema.Record(Schema.String, Schema.Never),
93 failure: DeviceToolFailure,
94 dependencies,
95 })
96 .annotate(Tool.Title, "Close device")
97 .annotate(Tool.Readonly, false)
98 .annotate(Tool.Destructive, true)
99 .annotate(Tool.Idempotent, true)
100 .annotate(Tool.OpenWorld, true)
101 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
102
103 export const DeviceStandardToolkit = Toolkit.make(DeviceListTool, DeviceOpenTool, DeviceCloseTool);
104
105 export const DeviceScreenshotToolkit = Toolkit.make(DeviceScreenshotTool);
106
107 export const DeviceToolkit = Toolkit.make(
108 DeviceListTool,
109 DeviceOpenTool,
110 DeviceScreenshotTool,
111 DeviceCloseTool,
112 );