Open History to see the last commit
1import {
2 PreviewAutomationClickInput,
3 PreviewAutomationError,
4 PreviewAutomationEvaluateInput,
5 PreviewAutomationNavigateInput,
6 PreviewAutomationOpenInput,
7 PreviewAutomationPressInput,
8 PreviewAutomationRecordingArtifact,
9 PreviewAutomationRecordingStatus,
10 PreviewAutomationResizeInput,
11 PreviewAutomationResizeResult,
12 PreviewAutomationScrollInput,
13 PreviewAutomationSetColorSchemeInput,
14 PreviewAutomationSetColorSchemeResult,
15 PreviewAutomationSnapshot,
16 PreviewAutomationStatus,
17 PreviewAutomationTabTargetInput,
18 PreviewAutomationTypeInput,
19 PreviewAutomationWaitForInput,
20} from "@t3tools/contracts";
21import * as Schema from "effect/Schema";
22import * as FileSystem from "effect/FileSystem";
23import { Tool, Toolkit } from "effect/unstable/ai";
24
25import * as McpInvocationContext from "../../McpInvocationContext.ts";
26import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
27import * as ServerConfig from "../../../config.ts";
28
29const dependencies = [
30 McpInvocationContext.McpInvocationContext,
31 PreviewAutomationBroker.PreviewAutomationBroker,
32];
33
34const PreviewActionResult = Schema.Record(Schema.String, Schema.Never).annotate({
35 description: "The preview action completed successfully.",
36});
37
38/** Drives the real browser and can destroy page state. */
39const browserTool = <T extends Tool.Any>(tool: T): T =>
40 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
41
42/** Same open-world browser access, but the action does not destroy page state. */
43const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
44 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T;
45
46/** A safe browser action that only observes, so it is also repeatable. */
47const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
48 safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
49
50const PreviewStatusTool = Tool.make("preview_status", {
51 description:
52 "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.",
53 parameters: PreviewAutomationTabTargetInput,
54 success: PreviewAutomationStatus,
55 failure: PreviewAutomationError,
56 dependencies,
57})
58 .annotate(Tool.Title, "Get preview status")
59 .annotate(Tool.Readonly, true)
60 .annotate(Tool.Destructive, false)
61 .annotate(Tool.Idempotent, true);
62
63const PreviewOpenTool = browserTool(
64 Tool.make("preview_open", {
65 description:
66 "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.",
67 parameters: PreviewAutomationOpenInput,
68 success: PreviewAutomationStatus,
69 failure: PreviewAutomationError,
70 dependencies,
71 })
72 .annotate(Tool.Title, "Open browser preview")
73 .annotate(Tool.Destructive, false),
74);
75
76const PreviewNavigateTool = safeBrowserTool(
77 Tool.make("preview_navigate", {
78 description:
79 "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.",
80 parameters: PreviewAutomationNavigateInput,
81 success: PreviewAutomationStatus,
82 failure: PreviewAutomationError,
83 dependencies,
84 }).annotate(Tool.Title, "Navigate browser preview"),
85);
86
87const PreviewResizeTool = safeBrowserTool(
88 Tool.make("preview_resize", {
89 description:
90 "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.",
91 parameters: PreviewAutomationResizeInput,
92 success: PreviewAutomationResizeResult,
93 failure: PreviewAutomationError,
94 dependencies,
95 })
96 .annotate(Tool.Title, "Resize browser viewport")
97 .annotate(Tool.Idempotent, true),
98);
99
100const PreviewSetAppearanceTool = safeBrowserTool(
101 Tool.make("preview_set_appearance", {
102 description:
103 "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.",
104 parameters: PreviewAutomationSetColorSchemeInput,
105 success: PreviewAutomationSetColorSchemeResult,
106 failure: PreviewAutomationError,
107 dependencies,
108 })
109 .annotate(Tool.Title, "Set preview appearance")
110 .annotate(Tool.Idempotent, true),
111);
112
113export const PreviewSnapshotTool = readonlyBrowserTool(
114 Tool.make("preview_snapshot", {
115 description:
116 "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.",
117 parameters: Schema.Struct({
118 ...PreviewAutomationTabTargetInput.fields,
119 includeImage: Schema.optional(
120 Schema.Boolean.annotate({
121 description:
122 "Include the PNG image in the tool response. Defaults to true. Set false for text-only output.",
123 }),
124 ),
125 save: Schema.optional(
126 Schema.Boolean.annotate({
127 description:
128 "Write the screenshot PNG to disk and return its absolute path as screenshotPath. Defaults to false.",
129 }),
130 ),
131 }),
132 success: PreviewAutomationSnapshot,
133 failure: PreviewAutomationError,
134 dependencies,
135 }).annotate(Tool.Title, "Inspect browser page"),
136);
137
138const PreviewClickTool = browserTool(
139 Tool.make("preview_click", {
140 description:
141 "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.",
142 parameters: PreviewAutomationClickInput,
143 success: PreviewActionResult,
144 failure: PreviewAutomationError,
145 dependencies,
146 }).annotate(Tool.Title, "Click preview page"),
147);
148
149const PreviewTypeTool = browserTool(
150 Tool.make("preview_type", {
151 description:
152 "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.",
153 parameters: PreviewAutomationTypeInput,
154 success: PreviewActionResult,
155 failure: PreviewAutomationError,
156 dependencies,
157 }).annotate(Tool.Title, "Type into preview page"),
158);
159
160const PreviewPressTool = browserTool(
161 Tool.make("preview_press", {
162 description:
163 "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']}.",
164 parameters: PreviewAutomationPressInput,
165 success: PreviewActionResult,
166 failure: PreviewAutomationError,
167 dependencies,
168 }).annotate(Tool.Title, "Press key in preview page"),
169);
170
171const PreviewScrollTool = safeBrowserTool(
172 Tool.make("preview_scroll", {
173 description:
174 "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.",
175 parameters: PreviewAutomationScrollInput,
176 success: PreviewActionResult,
177 failure: PreviewAutomationError,
178 dependencies,
179 }).annotate(Tool.Title, "Scroll preview page"),
180);
181
182/**
183 * MCP `structuredContent` must be a JSON object, and Claude Code rejects the
184 * whole result when it is not. Wrapping keeps arrays, strings, numbers, and
185 * null valid instead of failing only for non-object expressions.
186 */
187export const PreviewEvaluateResult = Schema.Struct({
188 value: Schema.Unknown.annotate({
189 description: "The JSON-serializable value the expression produced, or null.",
190 }),
191}).annotate({ description: "The evaluated expression result." });
192
193const PreviewEvaluateTool = browserTool(
194 Tool.make("preview_evaluate", {
195 description:
196 "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.",
197 parameters: PreviewAutomationEvaluateInput,
198 success: PreviewEvaluateResult,
199 failure: PreviewAutomationError,
200 dependencies,
201 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
202);
203
204const PreviewWaitForTool = readonlyBrowserTool(
205 Tool.make("preview_wait_for", {
206 description:
207 "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.",
208 parameters: PreviewAutomationWaitForInput,
209 success: PreviewActionResult,
210 failure: PreviewAutomationError,
211 dependencies,
212 }).annotate(Tool.Title, "Wait for preview page condition"),
213);
214
215const PreviewRecordingStartTool = safeBrowserTool(
216 Tool.make("preview_recording_start", {
217 description:
218 "Start recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted.",
219 parameters: PreviewAutomationTabTargetInput,
220 success: PreviewAutomationRecordingStatus,
221 failure: PreviewAutomationError,
222 dependencies,
223 }).annotate(Tool.Title, "Start browser recording"),
224);
225
226const PreviewRecordingStopTool = safeBrowserTool(
227 Tool.make("preview_recording_stop", {
228 description:
229 "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.",
230 parameters: PreviewAutomationTabTargetInput,
231 success: PreviewAutomationRecordingArtifact,
232 failure: PreviewAutomationError,
233 dependencies: [...dependencies, FileSystem.FileSystem, ServerConfig.ServerConfig],
234 }).annotate(Tool.Title, "Stop browser recording"),
235);
236
237export const PreviewToolkit = Toolkit.make(
238 PreviewStatusTool,
239 PreviewOpenTool,
240 PreviewNavigateTool,
241 PreviewResizeTool,
242 PreviewSetAppearanceTool,
243 PreviewSnapshotTool,
244 PreviewClickTool,
245 PreviewTypeTool,
246 PreviewPressTool,
247 PreviewScrollTool,
248 PreviewEvaluateTool,
249 PreviewWaitForTool,
250 PreviewRecordingStartTool,
251 PreviewRecordingStopTool,
252);
253
254export const PreviewStandardToolkit = Toolkit.make(
255 PreviewStatusTool,
256 PreviewOpenTool,
257 PreviewNavigateTool,
258 PreviewResizeTool,
259 PreviewSetAppearanceTool,
260 PreviewClickTool,
261 PreviewTypeTool,
262 PreviewPressTool,
263 PreviewScrollTool,
264 PreviewEvaluateTool,
265 PreviewWaitForTool,
266 PreviewRecordingStartTool,
267 PreviewRecordingStopTool,
268);
269
270export const PreviewSnapshotToolkit = Toolkit.make(PreviewSnapshotTool);