Open History to see the last commit
1import {
2 ToolActivityIcon,
3 PreviewAutomationClickInput,
4 PreviewAutomationError,
5 PreviewAutomationEvaluateInput,
6 PreviewAutomationNavigateInput,
7 PreviewAutomationOpenInput,
8 PreviewAutomationPressInput,
9 PreviewAutomationRecordingArtifact,
10 PreviewAutomationRecordingStatus,
11 PreviewAutomationResizeInput,
12 PreviewAutomationResizeResult,
13 PreviewAutomationScrollInput,
14 PreviewAutomationSetColorSchemeInput,
15 PreviewAutomationSetColorSchemeResult,
16 PreviewAutomationSnapshot,
17 PreviewAutomationStatus,
18 PreviewAutomationTabTargetInput,
19 PreviewAutomationTypeInput,
20 PreviewAutomationWaitForInput,
21} from "@t3tools/contracts";
22import * as Schema from "effect/Schema";
23import * as FileSystem from "effect/FileSystem";
24import { Tool, Toolkit } from "effect/unstable/ai";
25
26import * as McpInvocationContext from "../../McpInvocationContext.ts";
27import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
28import * as ServerConfig from "../../../config.ts";
29
30const dependencies = [
31 McpInvocationContext.McpInvocationContext,
32 PreviewAutomationBroker.PreviewAutomationBroker,
33];
34
35const presentationFields = { toolIcon: Schema.optional(ToolActivityIcon) };
36
37const PreviewActionResult = Schema.Struct(presentationFields).annotate({
38 description: "The preview action completed successfully.",
39});
40
41/** Drives the real browser and can destroy page state. */
42const browserTool = <T extends Tool.Any>(tool: T): T =>
43 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
44
45/** Same open-world browser access, but the action does not destroy page state. */
46const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
47 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T;
48
49/** A safe browser action that only observes, so it is also repeatable. */
50const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
51 safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
52
53const PreviewStatusTool = Tool.make("preview_status", {
54 description:
55 "Report whether a collaborative browser tab is automation-capable, including its URL, title, visibility, loading state, viewport mode, and measured CSS-pixel size. Pass tabId to inspect a specific tab; omit it to use this agent session's current tab.",
56 parameters: PreviewAutomationTabTargetInput,
57 success: PreviewAutomationStatus,
58 failure: PreviewAutomationError,
59 dependencies,
60})
61 .annotate(Tool.Title, "Get preview status")
62 .annotate(Tool.Readonly, true)
63 .annotate(Tool.Destructive, false)
64 .annotate(Tool.Idempotent, true);
65
66const PreviewOpenTool = browserTool(
67 Tool.make("preview_open", {
68 description:
69 "Initialize a collaborative browser tab and open its thread-bound inline preview by default. Set open=false for background-only automation. Pass tabId to reuse a specific existing tab, set reuseExistingTab=false to create another tab, or omit both to use this agent session's current tab.",
70 parameters: PreviewAutomationOpenInput,
71 success: PreviewAutomationStatus,
72 failure: PreviewAutomationError,
73 dependencies,
74 })
75 .annotate(Tool.Title, "Open browser preview")
76 .annotate(Tool.Destructive, false),
77);
78
79const PreviewNavigateTool = safeBrowserTool(
80 Tool.make("preview_navigate", {
81 description:
82 "Navigate a collaborative browser tab. Pass tabId to target a specific tab, plus {url:'https://t3.chat'} for a website or {target:{kind:'environment-port',port:5173}} for a dev server. Exactly one of url or target is required.",
83 parameters: PreviewAutomationNavigateInput,
84 success: PreviewAutomationStatus,
85 failure: PreviewAutomationError,
86 dependencies,
87 }).annotate(Tool.Title, "Navigate browser preview"),
88);
89
90const PreviewResizeTool = safeBrowserTool(
91 Tool.make("preview_resize", {
92 description:
93 "Resize a collaborative browser tab, optionally selected by tabId. Use {mode:'fill'}, {mode:'freeform',width:1024,height:768}, or {mode:'preset',preset:'iphone-12-pro',orientation:'portrait'}. This changes CSS layout breakpoints without changing the desktop browser user agent.",
94 parameters: PreviewAutomationResizeInput,
95 success: Schema.Struct({ ...PreviewAutomationResizeResult.fields, ...presentationFields }),
96 failure: PreviewAutomationError,
97 dependencies,
98 })
99 .annotate(Tool.Title, "Resize browser viewport")
100 .annotate(Tool.Idempotent, true),
101);
102
103const PreviewSetAppearanceTool = safeBrowserTool(
104 Tool.make("preview_set_appearance", {
105 description:
106 "Emulate prefers-color-scheme in a collaborative browser tab, optionally selected by tabId. Use {colorScheme:'dark'} or {colorScheme:'light'} to preview the page in that appearance, and {colorScheme:'system'} to clear the override and follow the OS appearance.",
107 parameters: PreviewAutomationSetColorSchemeInput,
108 success: Schema.Struct({
109 ...PreviewAutomationSetColorSchemeResult.fields,
110 ...presentationFields,
111 }),
112 failure: PreviewAutomationError,
113 dependencies,
114 })
115 .annotate(Tool.Title, "Set preview appearance")
116 .annotate(Tool.Idempotent, true),
117);
118
119export const PreviewSnapshotTool = readonlyBrowserTool(
120 Tool.make("preview_snapshot", {
121 description:
122 "Inspect a page before interacting. Pass tabId to inspect a specific tab; omit it to use this agent session's current tab. Returns page state, semantic elements, diagnostics, action history, and a PNG screenshot. Set includeImage=false for text-only output with the same page metadata. Set save=true to also write the PNG to disk and get screenshotPath back; embed that path in your reply as  so the user sees it. This is the only way to show the user a screenshot; the image in the tool result is not saved anywhere.",
123 parameters: Schema.Struct({
124 ...PreviewAutomationTabTargetInput.fields,
125 includeImage: Schema.optional(
126 Schema.Boolean.annotate({
127 description:
128 "Include the PNG image in the tool response. Defaults to true. Set false for text-only output.",
129 }),
130 ),
131 save: Schema.optional(
132 Schema.Boolean.annotate({
133 description:
134 "Write the screenshot PNG to disk and return its absolute path as screenshotPath. Defaults to false.",
135 }),
136 ),
137 }),
138 success: PreviewAutomationSnapshot,
139 failure: PreviewAutomationError,
140 dependencies,
141 }).annotate(Tool.Title, "Inspect browser page"),
142);
143
144const PreviewClickTool = browserTool(
145 Tool.make("preview_click", {
146 description:
147 "Click exactly one target in the tab selected by tabId, or this agent session's current tab when omitted. Prefer a Playwright locator; selector accepts legacy CSS; x and y must be supplied together.",
148 parameters: PreviewAutomationClickInput,
149 success: PreviewActionResult,
150 failure: PreviewAutomationError,
151 dependencies,
152 }).annotate(Tool.Title, "Click preview page"),
153);
154
155const PreviewTypeTool = browserTool(
156 Tool.make("preview_type", {
157 description:
158 "Insert literal text into one input in the tab selected by tabId, or this agent session's current tab when omitted. Prefer a Playwright locator; set clear=true to replace existing text.",
159 parameters: PreviewAutomationTypeInput,
160 success: PreviewActionResult,
161 failure: PreviewAutomationError,
162 dependencies,
163 }).annotate(Tool.Title, "Type into preview page"),
164);
165
166const PreviewPressTool = browserTool(
167 Tool.make("preview_press", {
168 description:
169 "Press one keyboard key in the tab selected by tabId, or this agent session's current tab when omitted. Examples: {key:'Enter'}, {key:'Escape'}, or {key:'a',modifiers:['Meta']}.",
170 parameters: PreviewAutomationPressInput,
171 success: PreviewActionResult,
172 failure: PreviewAutomationError,
173 dependencies,
174 }).annotate(Tool.Title, "Press key in preview page"),
175);
176
177const PreviewScrollTool = safeBrowserTool(
178 Tool.make("preview_scroll", {
179 description:
180 "Scroll the tab selected by tabId, or this agent session's current tab when omitted. Positive deltaY scrolls down and positive deltaX scrolls right; a locator/selector targets a container.",
181 parameters: PreviewAutomationScrollInput,
182 success: PreviewActionResult,
183 failure: PreviewAutomationError,
184 dependencies,
185 }).annotate(Tool.Title, "Scroll preview page"),
186);
187
188/**
189 * MCP `structuredContent` must be a JSON object, and Claude Code rejects the
190 * whole result when it is not. Wrapping keeps arrays, strings, numbers, and
191 * null valid instead of failing only for non-object expressions.
192 */
193export const PreviewEvaluateResult = Schema.Struct({
194 ...presentationFields,
195 value: Schema.Unknown.annotate({
196 description: "The JSON-serializable value the expression produced, or null.",
197 }),
198}).annotate({ description: "The evaluated expression result." });
199
200const PreviewEvaluateTool = browserTool(
201 Tool.make("preview_evaluate", {
202 description:
203 "Evaluate JavaScript in the tab selected by tabId, or this agent session's current tab when omitted. Returns {value} with a serializable result up to 64 KB; the expression may mutate page state.",
204 parameters: PreviewAutomationEvaluateInput,
205 success: PreviewEvaluateResult,
206 failure: PreviewAutomationError,
207 dependencies,
208 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
209);
210
211const PreviewWaitForTool = readonlyBrowserTool(
212 Tool.make("preview_wait_for", {
213 description:
214 "Wait in the tab selected by tabId, or this agent session's current tab when omitted, until all supplied locator, selector, text, and URL conditions match.",
215 parameters: PreviewAutomationWaitForInput,
216 success: PreviewActionResult,
217 failure: PreviewAutomationError,
218 dependencies,
219 }).annotate(Tool.Title, "Wait for preview page condition"),
220);
221
222const PreviewRecordingStartTool = safeBrowserTool(
223 Tool.make("preview_recording_start", {
224 description:
225 "Start recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted.",
226 parameters: PreviewAutomationTabTargetInput,
227 success: Schema.Struct({ ...PreviewAutomationRecordingStatus.fields, ...presentationFields }),
228 failure: PreviewAutomationError,
229 dependencies,
230 }).annotate(Tool.Title, "Start browser recording"),
231);
232
233const PreviewRecordingStopTool = safeBrowserTool(
234 Tool.make("preview_recording_stop", {
235 description:
236 "Stop recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted, and transfer the compressed recording once (up to 50 MiB) to an evidence file readable in this agent's environment. Returns its environment-local path after transfer succeeds.",
237 parameters: PreviewAutomationTabTargetInput,
238 success: Schema.Struct({ ...PreviewAutomationRecordingArtifact.fields, ...presentationFields }),
239 failure: PreviewAutomationError,
240 dependencies: [...dependencies, FileSystem.FileSystem, ServerConfig.ServerConfig],
241 }).annotate(Tool.Title, "Stop browser recording"),
242);
243
244export const PreviewToolkit = Toolkit.make(
245 PreviewStatusTool,
246 PreviewOpenTool,
247 PreviewNavigateTool,
248 PreviewResizeTool,
249 PreviewSetAppearanceTool,
250 PreviewSnapshotTool,
251 PreviewClickTool,
252 PreviewTypeTool,
253 PreviewPressTool,
254 PreviewScrollTool,
255 PreviewEvaluateTool,
256 PreviewWaitForTool,
257 PreviewRecordingStartTool,
258 PreviewRecordingStopTool,
259);
260
261export const PreviewStandardToolkit = Toolkit.make(
262 PreviewStatusTool,
263 PreviewOpenTool,
264 PreviewNavigateTool,
265 PreviewResizeTool,
266 PreviewSetAppearanceTool,
267 PreviewClickTool,
268 PreviewTypeTool,
269 PreviewPressTool,
270 PreviewScrollTool,
271 PreviewEvaluateTool,
272 PreviewWaitForTool,
273 PreviewRecordingStartTool,
274 PreviewRecordingStopTool,
275);
276
277export const PreviewSnapshotToolkit = Toolkit.make(PreviewSnapshotTool);