The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | WorktreeMcpFailure, |
| 3 | OrchestratorMcpEnvironmentTarget, |
| 4 | OrchestratorMcpFailure, |
| 5 | VcsListRefsInput, |
| 6 | VcsListRefsResult, |
| 7 | WorktreeMcpHandoffInput, |
| 8 | WorktreeMcpHandoffResult, |
| 9 | WorktreeMcpStatusResult, |
| 10 | ThreadId, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import * as Schema from "effect/Schema"; |
| 13 | import * as ProjectService from "../../../project/ProjectService.ts"; |
| 14 | import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; |
| 15 | import { McpSchema, Tool, Toolkit } from "effect/ai"; |
| 16 | |
| 17 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 18 | import * as WorktreeMcpService from "../../WorktreeMcpService.ts"; |
| 19 | |
| 20 | const dependencies = [ |
| 21 | McpInvocationContext.McpInvocationContext, |
| 22 | ThreadManagementService.ThreadManagementService, |
| 23 | WorktreeMcpService.WorktreeMcpService, |
| 24 | ]; |
| 25 | |
| 26 | const WorktreeToolFailure = Schema.Union([WorktreeMcpFailure, OrchestratorMcpFailure]); |
| 27 | |
| 28 | const WorktreeHandoffTool = Tool.make("t3_worktree_handoff", { |
| 29 | description: |
| 30 | "Move THIS thread into a new git worktree on a new branch. Runs the setup script by default. A separate agent in a worktree: `t3_thread_launch` with `workspaceStrategy`. Call last in your turn. The move ends the live session. `continuationPrompt` resumes work there next turn. Without it, the thread waits. The worktree stays after the thread is deleted. Fails if this thread has a worktree.", |
| 31 | parameters: WorktreeMcpHandoffInput, |
| 32 | success: WorktreeMcpHandoffResult, |
| 33 | failure: WorktreeToolFailure, |
| 34 | failureMode: "return", |
| 35 | dependencies, |
| 36 | }) |
| 37 | .annotate(Tool.Title, "Hand off thread to a git worktree") |
| 38 | .annotate(Tool.Readonly, false) |
| 39 | .annotate(Tool.Destructive, true) |
| 40 | .annotate(Tool.Idempotent, false) |
| 41 | .annotate(Tool.OpenWorld, true) |
| 42 | .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); |
| 43 | |
| 44 | const WorktreeStatusTool = Tool.make("t3_worktree_status", { |
| 45 | description: |
| 46 | "Report this thread's worktree: attached or not, path, branch, project root, and the `startFromOrigin` default. Check before `t3_worktree_handoff`.", |
| 47 | // No `parameters`: Tool.make defaults to Tool.EmptyParams, which serializes |
| 48 | // to a top-level `type: "object"` JSON Schema. An explicit empty |
| 49 | // Schema.Struct({}) serializes to `anyOf: [object, array]`, which is not a |
| 50 | // valid MCP tool input schema and makes clients reject the whole server. |
| 51 | success: WorktreeMcpStatusResult, |
| 52 | failure: WorktreeToolFailure, |
| 53 | failureMode: "return", |
| 54 | dependencies, |
| 55 | }) |
| 56 | .annotate(Tool.Title, "Get thread worktree status") |
| 57 | .annotate(Tool.Readonly, true) |
| 58 | .annotate(Tool.Destructive, false) |
| 59 | .annotate(Tool.Idempotent, true) |
| 60 | .annotate(Tool.OpenWorld, false) |
| 61 | .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); |
| 62 | |
| 63 | const WorktreeListTool = Tool.make("t3_worktree_list", { |
| 64 | description: |
| 65 | "List branch refs and checkout paths for a thread's workspace. Omit `threadId` for this thread. Detached worktrees with no branch are left out.", |
| 66 | parameters: Schema.Struct({ |
| 67 | environmentId: OrchestratorMcpEnvironmentTarget, |
| 68 | threadId: Schema.optional(ThreadId), |
| 69 | query: VcsListRefsInput.fields.query, |
| 70 | cursor: VcsListRefsInput.fields.cursor, |
| 71 | limit: VcsListRefsInput.fields.limit, |
| 72 | refKind: VcsListRefsInput.fields.refKind, |
| 73 | includeMatchingRemoteRefs: VcsListRefsInput.fields.includeMatchingRemoteRefs, |
| 74 | }), |
| 75 | success: VcsListRefsResult, |
| 76 | failure: OrchestratorMcpFailure, |
| 77 | failureMode: "return", |
| 78 | dependencies: [ |
| 79 | McpInvocationContext.McpInvocationContext, |
| 80 | ThreadManagementService.ThreadManagementService, |
| 81 | ProjectService.ProjectService, |
| 82 | WorktreeMcpService.WorktreeMcpService, |
| 83 | ], |
| 84 | }) |
| 85 | .annotate(Tool.Readonly, true) |
| 86 | .annotate(Tool.Destructive, false); |
| 87 | export const WorktreeToolkit = Toolkit.make( |
| 88 | WorktreeHandoffTool, |
| 89 | WorktreeStatusTool, |
| 90 | WorktreeListTool, |
| 91 | ); |