The best way to control Claude Code, Codex and more from any computer or phone
fix(server): enforce authorization on MCP retries (#37822)
Julius Marminge committed
Nov 16, 2026 at 17:14 UTC
1adc05b844584b48873a37d786a84ed4876b489f
5 files changed
+78
-24
apps/server/src/mcp/OrchestratorMcpService.test.ts
+18
-1
@@ -2866,7 +2866,7 @@ describe("OrchestratorMcpService provider resolution", () => {
2866
projectId,
2867
title: "MCP parent",
2868
modelSelection: { instanceId: codexInstanceId, model: "gpt-5.4" },
2869
- runtimeMode: "full-access",
2869
+ runtimeMode: "approval-required",
2870
interactionMode: "default",
2871
branch: null,
2872
worktreePath: cwd,
@@ -2910,6 +2910,23 @@ describe("OrchestratorMcpService provider resolution", () => {
2910
...request,
2911
clientRequestId: "empty",
2912
});
2913
+ const raised = yield* service.createThreads(scope, {
2914
+ ...request,
2915
+ clientRequestId: "raised",
2916
+ });
2917
+ const raisedId = raised.threads[0]!.threadId;
2918
+ yield* orchestrator.dispatch({
2919
+ type: "thread.runtime-mode.set",
2920
+ commandId: CommandId.make("raise-created-thread"),
2921
+ threadId: raisedId,
2922
+ runtimeMode: "full-access",
2923
+ });
2924
+ const rejected = yield* service
2925
+ .createThreads(scope, { ...withPrompt, clientRequestId: "raised" })
2926
+ .pipe(Effect.flip);
2927
+ assert.equal(rejected.code, "runtime_mode_escalation_denied");
2928
+ assert.equal((yield* orchestrator.getThreadProjection(raisedId)).runs.length, 0);
2929
+
2930
const emojiAtLimit = yield* service.createThreads(scope, {
2931
threads: [{ ...request.threads[0]!, prompt: `${"a".repeat(76)}😀 and more work.` }],
2932
clientRequestId: "emoji-title",
apps/server/src/mcp/OrchestratorMcpService.ts
+21
@@ -3127,6 +3127,8 @@ const make = Effect.gen(function* () {
3127
initialMessage !== undefined &&
3128
!created.storedEvents.some(({ event }) => event.type === "run.created")
3129
) {
3130
+ const currentParent = (yield* loadThreadRecords(scope.thread.threadId, [])).thread;
3131
+ const refused = yield* Ref.make<DispatchModeRefusal | undefined>(undefined);
3132
yield* threadManagement
3133
.dispatch({
3134
...initialMessage,
@@ -3144,6 +3146,11 @@ const make = Effect.gen(function* () {
3146
dispatchMode: { type: "start_immediately" },
3147
})
3148
.pipe(
3149
+ Effect.provideService(DispatchModeLimit, {
3150
+ runtimeMode: currentParent.runtimeMode,
3151
+ interactionMode: currentParent.interactionMode,
3152
+ refused,
3153
+ }),
3154
Effect.mapError((error) =>
3155
error._tag === "ThreadManagementProviderInstanceDisabledError"
3156
? threadManagementFailure(error)
@@ -3152,6 +3159,20 @@ const make = Effect.gen(function* () {
3159
`Unable to start thread ${index + 1}: ${userFacingDispatchErrorMessage(error) ?? errorMessage(error)}`,
3160
),
3161
),
3162
+ Effect.catch((error) =>
3163
+ Effect.flatMap(Ref.get(refused), (refusal) =>
3164
+ Effect.fail(
3165
+ refusal === undefined
3166
+ ? error
3167
+ : failure(
3168
+ refusal.mode === "runtime"
3169
+ ? "runtime_mode_escalation_denied"
3170
+ : "interaction_mode_escalation_denied",
3171
+ `Thread ${refusal.threadId} now runs in ${refusal.runtimeMode}/${refusal.interactionMode} mode, above this thread's.`,
3172
+ ),
3173
+ ),
3174
+ ),
3175
+ ),
3176
);
3177
}
3178
const projection = yield* loadThreadRecords(threadId, ["runs"]);
apps/server/src/mcp/toolkits/project/handlers.test.ts
+8
-2
@@ -540,16 +540,22 @@ it.effect("a launch binds only an existing checkout that is one of the project's
540
McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)),
541
),
542
);
543
- const launchInto = (worktreePath: string) =>
543
+ const launchInto = (worktreePath: string, clientRequestId?: string) =>
544
toolkit
545
.handle("t3_thread_launch", {
546
title: "Fix",
547
projectId,
548
+ ...(clientRequestId === undefined ? {} : { clientRequestId }),
549
workspaceStrategy: { type: "existing_worktree", worktreePath },
550
})
551
.pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies));
552
552
- expect((yield* launchInto(worktree)).at(-1)?.result).toMatchObject({ projectId });
553
+ expect((yield* launchInto(worktree, "existing-checkout")).at(-1)?.result).toMatchObject({
554
+ projectId,
555
+ });
556
+ expect((yield* launchInto(outside, "existing-checkout")).at(-1)?.result).toMatchObject({
557
+ code: "invalid_request",
558
+ });
559
expect((yield* launchInto(repo)).at(-1)?.result).toMatchObject({ projectId });
560
for (const elsewhere of [outside, path.join(worktree, "..", "outside"), "/"]) {
561
expect((yield* launchInto(elsewhere)).at(-1)?.result).toMatchObject({
apps/server/src/mcp/toolkits/project/handlers.ts
+25
-19
@@ -161,24 +161,30 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
161
const launchedModel =
162
launched === null || launched.deletedAt !== null ? undefined : launched.modelSelection;
163
const replaying = launchedModel !== undefined;
164
- const project = replaying
165
- ? undefined
166
- : yield* Project.ProjectService.pipe(
167
- Effect.flatMap((projects) => projects.getById(projectId)),
168
- Effect.mapError(unavailable),
169
- Effect.flatMap(
170
- Option.match({
171
- onNone: () =>
172
- Effect.fail(
173
- new OrchestratorMcpFailure({
174
- code: "invalid_request",
175
- message: "The project was not found.",
176
- }),
177
- ),
178
- onSome: Effect.succeed,
179
- }),
180
- ),
181
- );
164
+ if (launched !== null && replaying && launched.projectId !== projectId)
165
+ return yield* new OrchestratorMcpFailure({
166
+ code: "invalid_request",
167
+ message: "A launch retry must use the original project.",
168
+ });
169
+ const project =
170
+ replaying && input.workspaceStrategy?.type !== "existing_worktree"
171
+ ? undefined
172
+ : yield* Project.ProjectService.pipe(
173
+ Effect.flatMap((projects) => projects.getById(projectId)),
174
+ Effect.mapError(unavailable),
175
+ Effect.flatMap(
176
+ Option.match({
177
+ onNone: () =>
178
+ Effect.fail(
179
+ new OrchestratorMcpFailure({
180
+ code: "invalid_request",
181
+ message: "The project was not found.",
182
+ }),
183
+ ),
184
+ onSome: Effect.succeed,
185
+ }),
186
+ ),
187
+ );
188
if (project !== undefined && input.workspaceStrategy?.type === "existing_worktree")
189
yield* assertProjectWorktree(project.workspaceRoot, input.workspaceStrategy.worktreePath);
190
if (!replaying && input.modelSelection !== undefined) {
@@ -189,7 +195,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
195
yield* OrchestrationMcp.requireAdvertisedModel(provider, model);
196
}
197
const projectSettings =
192
- project === undefined
198
+ replaying || project === undefined
199
? undefined
200
: yield* ServerSettings.ServerSettingsService.pipe(
201
Effect.flatMap((settings) => settings.getSettings),
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+6
-2
@@ -1334,7 +1334,10 @@ const make = Effect.gen(function* () {
1334
const threadId =
1335
claimed.storedEvents.find((stored) => stored.event.type.startsWith("thread."))?.event
1336
.threadId ?? candidateThreadId;
1337
- if (project.id !== input.projectId) {
1337
+ const committedThread = yield* threads
1338
+ .getThreadShell(threadId)
1339
+ .pipe(Effect.mapError(mapError(input, "resolve-project", threadId)));
1340
+ if (committedThread?.projectId !== input.projectId) {
1341
return yield* mapError(input, "resolve-project", threadId)("Project identity changed.");
1342
}
1343
let runId: RunId | null = null;
@@ -1410,7 +1413,8 @@ const make = Effect.gen(function* () {
1413
// retry's own strategy, whose defaults may resolve differently.
1414
const accepted = current?.runs.find((run) => run.id === runId)?.workspacePreparation;
1415
const preparationStrategy: ThreadLaunchWorkspaceStrategy =
1413
- Option.isSome(launchReceipt) && accepted?.type === "worktree"
1416
+ Option.isSome(launchReceipt) &&
1417
+ (accepted?.type === "worktree" || accepted?.type === "existing_worktree")
1418
? accepted
1419
: Option.isSome(launchReceipt) &&
1420
workspaceStrategy.type === "root" &&