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

main
ts 562 lines 19.8 KB
Raw
1 export type T3McpToolLogo = "t3-code";
2
3 export interface T3McpToolPresentation {
4 readonly displayName: string;
5 readonly logo: T3McpToolLogo;
6 }
7
8 export type T3McpToolSummaryAction =
9 | "capabilities"
10 | "delegate"
11 | "task-status"
12 | "task-cancel"
13 | "schedule-run"
14 | "schedule-create"
15 | "schedule-list"
16 | "schedule-update"
17 | "schedule-delete"
18 | "thread-create"
19 | "thread-list"
20 | "thread-read"
21 | "thread-send"
22 | "thread-wait"
23 | "thread-interrupt"
24 | "workflow-stop"
25 | "background-wait"
26 | "thread-configuration"
27 | "thread-configure"
28 | "thread-fork"
29 | "thread-merge"
30 | "thread-search"
31 | "thread-transfers"
32 | "thread-organize"
33 | "thread-update"
34 | "queue-list"
35 | "queue-read"
36 | "queue-edit"
37 | "queue-cancel"
38 | "queue-reorder"
39 | "queue-steer"
40 | "question-list"
41 | "question-read"
42 | "question-respond"
43 | "secret-request"
44 | "worktree-handoff"
45 | "worktree-list"
46 | "worktree-status"
47 | "project-list"
48 | "project-read"
49 | "project-create"
50 | "project-update"
51 | "project-delete"
52 | "project-clone"
53 | "environment-read"
54 | "environment-links"
55 | "environment-link"
56 | "environment-unlink"
57 | "environment-update"
58 | "theme-read"
59 | "theme-set"
60 | "attachment-prepare"
61 | "attachment-discard"
62 | "attachment-send"
63 | "link-pr"
64 | "unlink-pr"
65 | "list-prs"
66 | "watch-pr"
67 | "unwatch-pr"
68 | "write-pr-guides"
69 | "read-pr-guide"
70 | "list-issues"
71 | "read-issue"
72 | "create-issue"
73 | "comment-issue"
74 | "set-issue-state"
75 | "link-issue"
76 | "unlink-issue"
77 | "list-thread-issues"
78 | "read-pr"
79 | "list-repo-prs"
80 | "create-pr"
81 | "update-pr"
82 | "respond-pr"
83 | "merge-pr"
84 | "review-pr"
85 | "ci-logs"
86 | "ci-rerun"
87 | "browser"
88 | "device"
89 | "html-preview"
90 | "html-render"
91 | "send-to-user";
92
93 export interface T3McpToolDefinition {
94 readonly displayName: string;
95 readonly labels: readonly [action: string, running: string, completed: string, detail: string];
96 readonly icon: "t3-code" | "browser" | "device" | "pull-request";
97 readonly summaryAction: T3McpToolSummaryAction;
98 }
99
100 function tool(
101 labels: T3McpToolDefinition["labels"],
102 summaryAction: T3McpToolSummaryAction,
103 icon: T3McpToolDefinition["icon"] = "t3-code",
104 displayName = `${labels[0]} ${labels[3]}`,
105 ): T3McpToolDefinition {
106 return { displayName, labels, icon, summaryAction };
107 }
108
109 // Codex registers "t3-code-direct" for T3 tools kept out of Code Mode.
110 const T3_MCP_SERVER_ALIASES = new Set([
111 "t3-code",
112 "t3_code",
113 "t3code",
114 "t3-code-direct",
115 "t3_code_direct",
116 ]);
117
118 const T3_MCP_TOOLS: Readonly<Record<string, T3McpToolDefinition>> = {
119 link_pull_request: tool(
120 ["Link", "Linking", "Linked", "a pull request"],
121 "link-pr",
122 "pull-request",
123 ),
124 link_pull_requests: tool(
125 ["Link", "Linking", "Linked", "pull requests"],
126 "link-pr",
127 "pull-request",
128 ),
129 unlink_pull_request: tool(
130 ["Unlink", "Unlinking", "Unlinked", "a pull request"],
131 "unlink-pr",
132 "pull-request",
133 ),
134 unlink_pull_requests: tool(
135 ["Unlink", "Unlinking", "Unlinked", "pull requests by state"],
136 "unlink-pr",
137 "pull-request",
138 ),
139 list_thread_pull_requests: tool(
140 ["Check", "Checking", "Checked", "linked pull requests"],
141 "list-prs",
142 "pull-request",
143 ),
144 watch_pull_request: tool(
145 ["Watch", "Watching", "Watching", "a pull request"],
146 "watch-pr",
147 "pull-request",
148 ),
149 unwatch_pull_request: tool(
150 ["Stop watching", "Unwatching", "Stopped watching", "a pull request"],
151 "unwatch-pr",
152 "pull-request",
153 ),
154 write_pull_request_guides: tool(
155 ["Write", "Writing", "Started writing", "review guides"],
156 "write-pr-guides",
157 "pull-request",
158 ),
159 read_pull_request_guide: tool(
160 ["Read", "Reading", "Read", "a review guide"],
161 "read-pr-guide",
162 "pull-request",
163 ),
164 list_issues: tool(["List", "Listing", "Listed", "issues"], "list-issues"),
165 read_issue: tool(["Read", "Reading", "Read", "an issue"], "read-issue"),
166 create_issue: tool(["File", "Filing", "Filed", "an issue"], "create-issue"),
167 comment_on_issue: tool(
168 ["Comment on", "Commenting on", "Commented on", "an issue"],
169 "comment-issue",
170 ),
171 set_issue_state: tool(["Change", "Changing", "Changed", "an issue's state"], "set-issue-state"),
172 link_issue: tool(["Link", "Linking", "Linked", "an issue"], "link-issue"),
173 unlink_issue: tool(["Unlink", "Unlinking", "Unlinked", "an issue"], "unlink-issue"),
174 list_thread_issues: tool(["Check", "Checking", "Checked", "linked issues"], "list-thread-issues"),
175 pull_request_read: tool(["Read", "Reading", "Read", "a pull request"], "read-pr", "pull-request"),
176 pull_request_list: tool(
177 ["List", "Listing", "Listed", "pull requests"],
178 "list-repo-prs",
179 "pull-request",
180 ),
181 pull_request_create: tool(
182 ["Open", "Opening", "Opened", "a pull request"],
183 "create-pr",
184 "pull-request",
185 ),
186 pull_request_update: tool(
187 ["Update", "Updating", "Updated", "a pull request"],
188 "update-pr",
189 "pull-request",
190 ),
191 pull_request_respond: tool(
192 ["Respond", "Responding", "Responded", "on a pull request"],
193 "respond-pr",
194 "pull-request",
195 ),
196 pull_request_review: tool(
197 ["Review", "Reviewing", "Reviewed", "a pull request"],
198 "review-pr",
199 "pull-request",
200 ),
201 ci_logs: tool(["Read", "Reading", "Read", "CI logs"], "ci-logs", "pull-request"),
202 ci_rerun: tool(["Re-run", "Re-running", "Re-ran", "CI"], "ci-rerun", "pull-request"),
203 pull_request_merge: tool(
204 ["Merge", "Merging", "Merged", "a pull request"],
205 "merge-pr",
206 "pull-request",
207 ),
208 orchestrator_capabilities: tool(
209 ["Get", "Getting", "Got", "orchestration capabilities"],
210 "capabilities",
211 ),
212 delegate_task: tool(["Delegate", "Delegating", "Delegated", "a child task"], "delegate"),
213 task_status: tool(["Get", "Getting", "Got", "delegated task status"], "task-status"),
214 task_cancel: tool(
215 ["Cancel", "Canceling", "Requested cancellation of", "a delegated task"],
216 "task-cancel",
217 ),
218 schedule_task: tool(["Schedule", "Scheduling", "Scheduled", "a task"], "schedule-create"),
219 list_scheduled_tasks: tool(["List", "Listing", "Listed", "scheduled tasks"], "schedule-list"),
220 update_scheduled_task: tool(
221 ["Update", "Updating", "Updated", "a scheduled task"],
222 "schedule-update",
223 ),
224 delete_scheduled_task: tool(
225 ["Delete", "Deleting", "Requested deletion of", "a scheduled task"],
226 "schedule-delete",
227 ),
228 request_secret: tool(["Ask for", "Asking for", "Asked for", "a secret"], "secret-request"),
229 create_threads: tool(["Create", "Creating", "Created", "T3 threads"], "thread-create"),
230 t3_thread_start: tool(["Start", "Starting", "Started", "a T3 thread"], "thread-create"),
231 t3_thread_list: tool(["List", "Listing", "Listed", "T3 threads"], "thread-list"),
232 t3_thread_read: tool(["Read", "Reading", "Read", "a T3 thread"], "thread-read"),
233 t3_thread_send: tool(["Send", "Sending", "Sent", "to a T3 thread"], "thread-send"),
234 t3_thread_wait: tool(["Wait", "Waiting", "Waited", "for a T3 thread"], "thread-wait"),
235 t3_workflow_stop: tool(
236 ["Stop", "Stopping", "Requested a stop for", "a workflow"],
237 "workflow-stop",
238 ),
239 t3_background_wait: tool(
240 ["Update", "Updating", "Updated", "background waits"],
241 "background-wait",
242 ),
243 t3_thread_interrupt: tool(
244 ["Interrupt", "Interrupting", "Requested an interrupt of", "a T3 thread"],
245 "thread-interrupt",
246 ),
247 t3_worktree_handoff: tool(
248 ["Hand off", "Handing off", "Handed off", "thread to a git worktree"],
249 "worktree-handoff",
250 ),
251 t3_worktree_status: tool(["Get", "Getting", "Got", "thread worktree status"], "worktree-status"),
252 preview_status: tool(["Get", "Getting", "Got", "preview browser status"], "browser", "browser"),
253 preview_open: tool(
254 ["Open", "Opening", "Opened", "a page in the preview browser"],
255 "browser",
256 "browser",
257 ),
258 preview_navigate: tool(
259 ["Navigate", "Navigating", "Navigated", "the preview browser"],
260 "browser",
261 "browser",
262 ),
263 preview_dialog: tool(
264 ["Respond", "Responding", "Responded", "to a preview browser dialog"],
265 "browser",
266 "browser",
267 ),
268 preview_snapshot: tool(
269 ["Take a snapshot of", "Taking a snapshot of", "Took a snapshot of", "the preview page"],
270 "browser",
271 "browser",
272 "Snapshot the preview page",
273 ),
274 preview_act: tool(["Use", "Using", "Used", "the preview browser"], "browser", "browser"),
275 preview_click: tool(
276 ["Click", "Clicking", "Clicked", "in the preview browser"],
277 "browser",
278 "browser",
279 ),
280 preview_press: tool(
281 ["Press", "Pressing", "Pressed", "a key in the preview browser"],
282 "browser",
283 "browser",
284 ),
285 preview_type: tool(["Type", "Typing", "Typed", "in the preview browser"], "browser", "browser"),
286 preview_hover: tool(
287 ["Hover", "Hovering", "Hovered", "in the preview browser"],
288 "browser",
289 "browser",
290 ),
291 preview_select: tool(
292 ["Choose", "Choosing", "Chose", "an option in the preview browser"],
293 "browser",
294 "browser",
295 ),
296 preview_drag: tool(
297 ["Drag", "Dragging", "Dragged", "in the preview browser"],
298 "browser",
299 "browser",
300 ),
301 preview_upload: tool(
302 ["Upload", "Uploading", "Uploaded", "files to the preview browser"],
303 "browser",
304 "browser",
305 ),
306 preview_scroll: tool(
307 ["Scroll", "Scrolling", "Scrolled", "the preview browser"],
308 "browser",
309 "browser",
310 ),
311 preview_resize: tool(
312 ["Resize", "Resizing", "Resized", "the preview browser"],
313 "browser",
314 "browser",
315 ),
316 preview_evaluate: tool(
317 ["Evaluate", "Evaluating", "Evaluated", "script in the preview browser"],
318 "browser",
319 "browser",
320 ),
321 preview_wait_for: tool(
322 ["Wait", "Waiting", "Waited", "for the preview page"],
323 "browser",
324 "browser",
325 ),
326 preview_set_appearance: tool(
327 ["Set", "Setting", "Set", "preview browser appearance"],
328 "browser",
329 "browser",
330 ),
331 preview_recording_start: tool(
332 ["Start", "Starting", "Started", "recording the preview browser"],
333 "browser",
334 "browser",
335 ),
336 preview_recording_stop: tool(
337 ["Stop", "Stopping", "Stopped", "recording the preview browser"],
338 "browser",
339 "browser",
340 ),
341 device_list: tool(["List", "Listing", "Listed", "simulators and emulators"], "device", "device"),
342 device_open: tool(
343 ["Open", "Opening", "Opened", "a device in the Device panel"],
344 "device",
345 "device",
346 ),
347 device_screenshot: tool(
348 ["Take a screenshot of", "Taking a screenshot of", "Took a screenshot of", "the device"],
349 "device",
350 "device",
351 ),
352 device_close: tool(["Close", "Closing", "Closed", "a device"], "device", "device"),
353 run_scheduled_task_now: tool(
354 ["Run", "Running", "Requested a run of", "a scheduled task"],
355 "schedule-run",
356 ),
357 t3_queue: tool(["Manage", "Managing", "Managed", "queued messages"], "queue-edit"),
358 t3_pending_request: tool(
359 ["Handle", "Handling", "Handled", "pending questions"],
360 "question-respond",
361 ),
362 t3_submit_output: tool(["Submit", "Submitting", "Submitted", "the result"], "thread-send"),
363 t3_queue_list: tool(["List", "Listing", "Listed", "queued messages"], "queue-list"),
364 t3_queue_read: tool(["Read", "Reading", "Read", "a queued message"], "queue-read"),
365 t3_queue_edit: tool(["Edit", "Editing", "Edited", "a queued message"], "queue-edit"),
366 t3_queue_cancel: tool(
367 ["Cancel", "Canceling", "Requested cancellation of", "a queued run"],
368 "queue-cancel",
369 ),
370 t3_queue_reorder: tool(["Reorder", "Reordering", "Reordered", "a queued run"], "queue-reorder"),
371 t3_queue_promote_to_steer: tool(
372 ["Steer with", "Steering with", "Requested steering with", "a queued message"],
373 "queue-steer",
374 ),
375 t3_pending_request_list: tool(
376 ["List", "Listing", "Listed", "pending questions"],
377 "question-list",
378 ),
379 t3_pending_request_read: tool(["Read", "Reading", "Read", "pending questions"], "question-read"),
380 t3_pending_request_respond: tool(
381 ["Answer", "Answering", "Answered", "pending questions"],
382 "question-respond",
383 ),
384 t3_thread_configuration: tool(
385 ["Read", "Reading", "Read", "thread configuration"],
386 "thread-configuration",
387 ),
388 t3_thread_configure: tool(["Use", "Using", "Used", "thread model settings"], "thread-configure"),
389 t3_thread_transfer: tool(["Use", "Using", "Used", "thread transfers"], "thread-fork"),
390 t3_thread_fork: tool(["Fork", "Forking", "Requested a fork of", "this thread"], "thread-fork"),
391 t3_thread_merge_back: tool(
392 ["Merge", "Merging", "Requested a merge of", "thread context"],
393 "thread-merge",
394 ),
395 t3_thread_search: tool(["Search", "Searching", "Searched", "thread content"], "thread-search"),
396 t3_thread_transfers: tool(["Read", "Reading", "Read", "thread transfers"], "thread-transfers"),
397 t3_thread_organize: tool(["Organize", "Organizing", "Organized", "a thread"], "thread-organize"),
398 t3_thread_update: tool(["Update", "Updating", "Updated", "T3 thread metadata"], "thread-update"),
399 t3_worktree_list: tool(["List", "Listing", "Listed", "workspace branches"], "worktree-list"),
400 t3_preview_list: tool(["List", "Listing", "Listed", "preview tabs"], "browser", "browser"),
401 t3_preview_close: tool(["Close", "Closing", "Closed", "a preview tab"], "browser", "browser"),
402 t3_environment_read: tool(
403 ["Read", "Reading", "Read", "environment preferences"],
404 "environment-read",
405 ),
406 t3_environment_links: tool(
407 ["List", "Listing", "Listed", "linked environments"],
408 "environment-links",
409 ),
410 t3_environment_link: tool(
411 ["Ask to link", "Asking to link", "Asked to link", "an environment"],
412 "environment-link",
413 ),
414 t3_environment_unlink: tool(
415 ["Forget", "Forgetting", "Forgot", "a linked environment"],
416 "environment-unlink",
417 ),
418 t3_environment_preferences_update: tool(
419 ["Update", "Updating", "Updated", "environment preferences"],
420 "environment-update",
421 ),
422 t3_theme_read: tool(["Read", "Reading", "Read", "themes"], "theme-read"),
423 t3_theme_set: tool(["Set", "Setting", "Set", "the theme"], "theme-set"),
424 t3_thread_launch: tool(["Launch", "Launching", "Launched", "a project thread"], "thread-create"),
425 t3_project_list: tool(["List", "Listing", "Listed", "projects"], "project-list"),
426 t3_project_read: tool(["Read", "Reading", "Read", "a project"], "project-read"),
427 t3_project_create: tool(["Register", "Registering", "Registered", "a project"], "project-create"),
428 t3_project_update: tool(["Update", "Updating", "Updated", "a project"], "project-update"),
429 t3_project_delete: tool(["Delete", "Deleting", "Deleted", "a project"], "project-delete"),
430 t3_project_clone: tool(["Clone", "Cloning", "Cloned", "a repository"], "project-clone"),
431 t3_attachment_prepare_upload: tool(
432 ["Prepare", "Preparing", "Prepared", "an attachment upload"],
433 "attachment-prepare",
434 ),
435 t3_attachment_discard: tool(
436 ["Discard", "Discarding", "Discarded", "a pending attachment"],
437 "attachment-discard",
438 ),
439 t3_thread_send_attachments: tool(["Send", "Sending", "Sent", "attachments"], "attachment-send"),
440 html_preview: tool(["Preview", "Previewing", "Previewed", "an HTML page"], "html-preview"),
441 html_render: tool(["Render", "Rendering", "Rendered", "an HTML page"], "html-render"),
442 send_to_user: tool(["Send", "Sending", "Sent", "a message"], "send-to-user"),
443 };
444
445 export const T3_MCP_TOOL_NAMES: ReadonlySet<string> = new Set(Object.keys(T3_MCP_TOOLS));
446
447 function normalizeT3McpToolLabel(value: string): string {
448 return value.replace(/\s+(?:complete|completed)\s*$/i, "").trim();
449 }
450
451 // Timelines resolve every work row's tool name on each update; names repeat across updates.
452 // ACP titles can carry whole commands, so long labels are parsed but not kept.
453 const toolNameCache = new Map<string, string | null>();
454 const TOOL_NAME_CACHE_LIMIT = 10_000;
455 const TOOL_NAME_CACHE_MAX_KEY_LENGTH = 256;
456
457 function resolveT3McpToolName(value: string): string | null {
458 if (value.length > TOOL_NAME_CACHE_MAX_KEY_LENGTH) return parseT3McpToolName(value);
459 const cached = toolNameCache.get(value);
460 if (cached !== undefined) return cached;
461 const name = parseT3McpToolName(value);
462 if (toolNameCache.size >= TOOL_NAME_CACHE_LIMIT) toolNameCache.clear();
463 toolNameCache.set(value, name);
464 return name;
465 }
466
467 // ACP tool prefixes vary; match loosely but require a known display-name entry.
468 function parseT3McpToolName(value: string): string | null {
469 const label = normalizeT3McpToolLabel(value);
470 const mcpMatch = /^mcp__(?<server>.+?)__(?<tool>.+)$/i.exec(label);
471 if (mcpMatch?.groups) {
472 const { server, tool } = mcpMatch.groups;
473 return server !== undefined &&
474 tool !== undefined &&
475 T3_MCP_SERVER_ALIASES.has(server.toLowerCase())
476 ? tool
477 : null;
478 }
479
480 const namespaceMatch =
481 /^(?<server>t3[-_]code[-_]direct|t3-code|t3_code|t3code)(?:[.:/]|\s*·\s*)(?<tool>.+)$/i.exec(
482 label,
483 );
484 if (namespaceMatch?.groups) {
485 return namespaceMatch.groups.tool ?? null;
486 }
487
488 const prefixed = /^(?:mcp[-_]{1,2})?t3[-_ ]?code(?:__|[-_.:/ ])(?<tool>.+)$/i.exec(label);
489 const candidate = prefixed?.groups?.tool ?? label;
490 if (Object.hasOwn(T3_MCP_TOOLS, candidate)) return candidate;
491 // OpenCode 2 thread prefixes can contain underscores; choose the longest known tool-name suffix.
492 if (!/^t3-code-/i.test(label)) return null;
493 let longest: string | null = null;
494 for (const tool of Object.keys(T3_MCP_TOOLS)) {
495 if (label.endsWith(`_${tool}`) && tool.length > (longest?.length ?? 0)) longest = tool;
496 }
497 return longest;
498 }
499
500 export function resolveT3McpToolId(toolName: string | null | undefined): string | null {
501 const name = toolName == null ? null : resolveT3McpToolName(toolName);
502 return name !== null && Object.hasOwn(T3_MCP_TOOLS, name) ? name : null;
503 }
504
505 type SummaryInput = Readonly<Record<string, unknown>>;
506
507 const asSummaryInput = (input: unknown): SummaryInput | undefined =>
508 typeof input === "object" && input !== null ? (input as SummaryInput) : undefined;
509
510 /** Action tools present each call as the single-purpose tool its action replaced. */
511 const ACTION_TOOLS: Readonly<Record<string, (action: string) => string | undefined>> = {
512 preview_act: (action) => `preview_${action}`,
513 t3_queue: (action) => `t3_queue_${action}`,
514 t3_pending_request: (action) => `t3_pending_request_${action}`,
515 t3_thread_transfer: (action) =>
516 ({ fork: "t3_thread_fork", merge_back: "t3_thread_merge_back", status: "t3_thread_transfers" })[
517 action
518 ],
519 };
520
521 /** Pass the call's input so an action tool resolves to the labels and summary of its action. */
522 export function resolveT3McpToolDefinition(
523 toolName: string | null | undefined,
524 input?: unknown,
525 ): T3McpToolDefinition | null {
526 const name = toolName == null ? null : resolveT3McpToolName(toolName);
527 if (name === null || !Object.hasOwn(T3_MCP_TOOLS, name)) return null;
528 const action = asSummaryInput(input)?.action;
529 const actionTool = typeof action === "string" ? ACTION_TOOLS[name]?.(action) : undefined;
530 return actionTool !== undefined && Object.hasOwn(T3_MCP_TOOLS, actionTool)
531 ? T3_MCP_TOOLS[actionTool]!
532 : T3_MCP_TOOLS[name]!;
533 }
534
535 /** Tools whose summary depends on which fields a call sets. */
536 const ACTION_SUMMARIES: Readonly<
537 Record<string, (input: SummaryInput) => T3McpToolSummaryAction | undefined>
538 > = {
539 t3_thread_configure: ({ modelSelection }) =>
540 modelSelection === undefined ? "thread-configuration" : "thread-configure",
541 schedule_task: ({ scheduledTaskId }) =>
542 scheduledTaskId === undefined ? "schedule-create" : "schedule-update",
543 };
544
545 export function resolveT3McpSummaryAction(
546 toolName: string | null | undefined,
547 input: unknown,
548 ): T3McpToolSummaryAction | null {
549 const definition = resolveT3McpToolDefinition(toolName, input);
550 if (definition === null) return null;
551 const summarize = ACTION_SUMMARIES[resolveT3McpToolName(toolName!) ?? ""];
552 const fields = asSummaryInput(input);
553 return (fields && summarize?.(fields)) ?? definition.summaryAction;
554 }
555
556 export function resolveT3McpToolPresentation(
557 toolName: string | null | undefined,
558 input?: unknown,
559 ): T3McpToolPresentation | null {
560 const definition = resolveT3McpToolDefinition(toolName, input);
561 return definition === null ? null : { displayName: definition.displayName, logo: "t3-code" };
562 }