feat(server): let MCP agents inspect thread diffs and respond to approvals #15464
Mergedmaria-rcksmerged 34 commits into
main fromrefs/pull/15464/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- apps/server/src/mcp/toolkits/review/handlers.ts lines 50 to 53OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +33,21 @@33+ .pipe(34+ Effect.mapError((error) => {35+ switch (error._tag) {36+ case "VcsRepositoryDetectionError":37+ case "VcsUnsupportedOperationError":38+ return invalid(error.detail);39+ // A baseRef with no common commit with HEAD is the caller's mistake.40+ case "GitCommandError":41+ return error.operation === "GitVcsDriver.resolveReviewMergeBase"42+ ? invalid(error.detail)43+ : unavailable();44+ default:45+ return unavailable();46+ }47+ }),48+ );49+ let preview;50+ if (input.file !== undefined && sourceKind !== undefined) {51+ // A renamed file needs its old path, which only the full preview's stats know. The lookup52+ // is best effort: a working-tree read must not fail on a branch range it does not need.53+ const full = yield* readPreview(input.baseRef).pipe(Effect.orElseSucceed(() => undefined));apps/server/src/mcp/toolkits/thread/handlers.ts lines 349 to 349OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -224,0 +329,21 @@329+ message: "Only user questions can be dismissed. Decline an approval with a decision.",330+ });331+ const result = yield* threads332+ .dispatch({333+ type: "thread.user-input.dismiss",334+ threadId: projection.thread.id,335+ commandId: yield* newCommandId(),336+ requestId: input.requestId,337+ })338+ .pipe(Effect.mapError(unavailable));339+ return { sequence: result.sequence };340+ }),341+ t3_inbox: (input) =>342+ Effect.gen(function* () {343+ const { threads, caller } = yield* readCaller();344+ // Like t3_thread_search, an omitted project means the caller's own; a client outside345+ // a thread sees every project.346+ const projectId = input.projectId ?? caller?.projectId;347+ const snapshot = yield* threads.getShellSnapshot().pipe(Effect.mapError(unavailable));348+ const now = yield* DateTime.now;349+ const items = snapshot.threadsapps/server/src/mcp/toolkits/review/handlers.ts lines 11 to 11OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,11 @@1+import { OrchestratorMcpFailure, type ReviewDiffPreviewInput } from "@t3tools/contracts";2+import * as Effect from "effect/Effect";3+import * as Option from "effect/Option";4+import * as Project from "../../../project/ProjectService.ts";5+import * as Review from "../../../review/ReviewService.ts";6+import { readThread, unavailable } from "../../threadAccess.ts";7+import { ReviewToolkit } from "./tools.ts";8+9+const DEFAULT_DIFF_CHARACTERS = 20_000;10+11+const invalid = (message: string) =>
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge removed labelssize:L
juliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- @@ -0,0 +71,21 @@71+ const now = yield* DateTime.now;72+ return snapshot.threads73+ .flatMap((shell) => {74+ if (shell.archivedAt !== null || shell.deletedAt !== null) return [];75+ if (input.projectId !== undefined && shell.projectId !== input.projectId) return [];76+ const reason = inboxReason(shell, now);77+ return reason === null ? [] : [{ shell, reason }];78+ })79+ .toSorted(80+ (left, right) =>81+ inboxReasonOrder.indexOf(left.reason) - inboxReasonOrder.indexOf(right.reason) ||82+ DateTime.toEpochMillis(right.shell.updatedAt) -83+ DateTime.toEpochMillis(left.shell.updatedAt),84+ )85+ .slice(0, input.limit ?? DEFAULT_INBOX_LIMIT);86+ });87+88+ return ThreadInbox.of({ list });89+});90+91+export const layer = Layer.effect(ThreadInbox, make);apps/server/src/mcp/toolkits/thread/handlers.ts lines 261 to 261OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -207,4 +242,20 @@242+ ...(approval?.options === undefined ? {} : { options: approval.options }),243+ };207244 }),208245 t3_pending_request_respond: (input) =>209246 Effect.gen(function* () {210- const { threads, projection } = yield* readQuestion(input, true);247+ const { threads, projection, request, item } = yield* readPendingRequest(input, true);248+ const approval = request.kind !== "user_input";249+ const response = approval250+ ? input.decision === undefined251+ ? undefined252+ : { decision: input.decision }253+ : input.answers === undefined254+ ? undefined255+ : { answers: input.answers };256+ if (response === undefined)257+ return yield* new OrchestratorMcpFailure({258+ code: "invalid_request",259+ message: approval ? "Approvals need a decision." : "User questions need answers.",260+ });261+ // Like the composer, offer only the provider's options, else the same defaults.
- apps/server/src/mcp/toolkits/thread/handlers.ts lines 100 to 103OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -81,16 +101,9 @@81- const { caller } = yield* readMutationCaller();82- if (83- caller.archivedAt !== null ||84- caller.runtimeMode !== "full-access" ||85- caller.interactionMode !== "default"86- )87- return yield* new OrchestratorMcpFailure({88- code: "capability_denied",89- message: "Running a scheduled task requires a live full-access/default thread.",90- });101+ yield* readFullAccessCaller(102+ "Running a scheduled task requires a live full-access/default thread or a full-access client.",103+ );91104 const scheduler = yield* ScheduledTasks.ScheduledTaskService;92105 const { tasks } = yield* scheduler.list().pipe(Effect.mapError(unavailable));93- if (!tasks.some((task) => task.id === input.taskId && task.projectId === caller.projectId))106+ if (!tasks.some((task) => task.id === input.taskId))94107 return yield* new OrchestratorMcpFailure({95108 code: "invalid_request",96- message: "The task was not found in the calling project.",109+ message: "The scheduled task was not found.",apps/server/src/mcp/toolkits/thread/handlers.ts lines 120 to 129OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -109,6 +122,15 @@109122 t3_thread_search: (input) =>110123 Effect.gen(function* () {111124 const { caller } = yield* readCaller();125+ const { projectId: requested, ...query } = input;126+ // Like the other project tools, an omitted project means the caller's own; a client127+ // outside a thread searches every project.128+ const projectId = requested ?? caller?.projectId;112129 const threadSearch = yield* ThreadSearch.ThreadSearch;113- const result = yield* threadSearch.search(input).pipe(Effect.mapError(unavailable));114- return { matches: result.matches.filter((match) => match.projectId === caller.projectId) };130+ const result = yield* threadSearch.search(query).pipe(Effect.mapError(unavailable));131+ return {132+ matches:133+ projectId === undefined134+ ? result.matches135+ : result.matches.filter((match) => match.projectId === projectId),136+ };
- apps/server/src/mcp/OrchestratorMcpService.ts line 1426OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1393,3 +1406,21 @@13931406 });1394140713951408 return OrchestratorMcpService.of({1409+ respondToPendingRequest: (scope, input) =>1410+ Effect.gen(function* () {1411+ const { parent, limits } = yield* loadCaller(scope);1412+ if (parent !== undefined) yield* assertLiveCaller(scope, parent);1413+ const threadId = input.threadId ?? parent?.thread.id;1414+ if (threadId === undefined)1415+ return yield* failure(1416+ "target_required",1417+ "Pass threadId: this MCP client is not running inside a T3 thread.",1418+ );1419+ const shell = yield* threadManagement1420+ .getThreadShell(threadId)1421+ .pipe(Effect.mapError(threadManagementFailure));1422+ if (shell === null || shell.deletedAt !== null)1423+ return yield* failure("thread_not_found", "The thread was not found.");1424+ yield* resolveRuntimeMode(limits.runtimeMode, shell.runtimeMode);1425+ yield* resolveInteractionMode(limits.interactionMode, shell.interactionMode);1426+ const projection = yield* threadManagement
- apps/server/src/mcp/toolkits/review/handlers.ts line 27OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +7,21 @@7+import { ReviewToolkit } from "./tools.ts";8+9+const DEFAULT_DIFF_CHARACTERS = 20_000;10+11+export const layer = ReviewToolkit.toLayer({12+ t3_thread_diff: (input) =>13+ Effect.gen(function* () {14+ const {15+ projection: { thread },16+ } = yield* readThread(input.threadId);17+ const projects = yield* Project.ProjectService;18+ const project = yield* projects.getById(thread.projectId).pipe(Effect.mapError(unavailable));19+ if (Option.isNone(project))20+ return yield* new OrchestratorMcpFailure({21+ code: "invalid_request",22+ message: "The project was not found.",23+ });24+ const review = yield* Review.ReviewService;25+ const preview = yield* review26+ .getScopedDiffPreview({27+ cwd: thread.worktreePath ?? project.value.workspaceRoot,
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge assignedjuliusmarminge
juliusmarminge changed the title
- apps/server/src/mcp/toolkits/worktree/handlers.ts line 48OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -36,3 +39,10 @@3639 .pipe(Effect.mapError(unavailable));3740 }),3841 ),42+ t3_thread_diff: McpToolAccess.reads((input) =>43+ Effect.gen(function* () {44+ const {45+ projection: { thread },46+ } = yield* readThread(input.threadId);47+ const projects = yield* Project.ProjectService;48+ const project = yield* projects.getById(thread.projectId).pipe(Effect.mapError(unavailable));
juliusmarminge merged this44f977f
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed