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