Skip to content

Commit 9a87442

Browse files
fix(server): task_cancel checks every task it stops, and McpServer stays in McpHttpServer
Review found two more gaps: - task_cancel checked only the child's modes, but stopping the child also stops every task under it. A user can raise one of those to full access, and a Supervised parent could still stop it. Every thread the cancel stops is now checked before anything is stopped. - The lint rule matched the name `McpServer` at the call site, so an aliased import, `effect/ai/McpServer`, or a destructured `addTool` registered a tool with no access declaration. The rule now reports any runtime import of Effect's McpServer outside McpHttpServer, and any read of a registration method or function. Tests may still import it to build a server, but not register on it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d47d7ca commit 9a87442

5 files changed

Lines changed: 322 additions & 46 deletions

File tree

‎apps/server/src/mcp/McpToolAccess.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ let handlersLayer: <Tools extends Record<string, Tool.Any>, EX, RX>(
6060
* declarations below, which say what the tool does. `toLayer` accepts only
6161
* declarations, and `/mcp` registers only layers `toLayer` built, so a tool
6262
* without a decision here does not compile. Both are nominal classes, so a
63-
* handler or layer cannot pass for one by copying its fields, and the
64-
* `t3code/no-raw-mcp-registration` lint rule keeps registrations on `/mcp`
65-
* going through them.
63+
* handler or layer cannot pass for one by copying its fields. Effect's own
64+
* registration functions accept any handler, so the
65+
* `t3code/no-raw-mcp-registration` lint rule keeps Effect's `McpServer` inside
66+
* McpHttpServer.
6667
*
6768
* Parameters choose the target; the caller sets the limits: a thread caller
6869
* its own runtime and interaction modes, an outside client the ceiling it was

‎apps/server/src/mcp/OrchestratorMcpService.test.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,115 @@ describe("OrchestratorMcpService", () => {
571571
}).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(layerDependencies))));
572572
}),
573573
);
574+
575+
it.effect("refuses to cancel a task when a task under it now runs above the parent's modes", () =>
576+
Effect.gen(function* () {
577+
const parentThreadId = ThreadId.make("thread:mcp-cancel-grandchild-parent");
578+
const childThreadId = ThreadId.make("thread:mcp-cancel-grandchild-child");
579+
const grandchildThreadId = ThreadId.make("thread:mcp-cancel-grandchild-grandchild");
580+
const taskId = NodeId.make("node:mcp-cancel-grandchild-task");
581+
const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);
582+
const stoppedBelow = yield* Ref.make(false);
583+
const appOwnedTask = (id: string, threadId: ThreadId, childId: ThreadId) => ({
584+
id: NodeId.make(id),
585+
threadId,
586+
origin: "app_owned",
587+
childThreadId: childId,
588+
driver: "codex",
589+
model: "gpt-5.6-terra",
590+
result: null,
591+
completionDelivery: { state: "pending" },
592+
});
593+
// A Supervised parent delegated a Supervised child, which delegated a task of its own.
594+
const projections = new Map([
595+
[
596+
parentThreadId,
597+
{
598+
thread: {
599+
id: parentThreadId,
600+
runtimeMode: "approval-required",
601+
interactionMode: "default",
602+
},
603+
runs: [],
604+
contextTransfers: [],
605+
subagents: [appOwnedTask(taskId, parentThreadId, childThreadId)],
606+
},
607+
],
608+
[
609+
childThreadId,
610+
{
611+
thread: { id: childThreadId },
612+
runs: [{ id: RunId.make("run:mcp-cancel-grandchild-child"), status: "running" }],
613+
contextTransfers: [],
614+
messages: [],
615+
subagents: [
616+
appOwnedTask("node:mcp-cancel-grandchild-below", childThreadId, grandchildThreadId),
617+
],
618+
providerThreads: [],
619+
},
620+
],
621+
[
622+
grandchildThreadId,
623+
{
624+
thread: { id: grandchildThreadId },
625+
runs: [],
626+
contextTransfers: [],
627+
messages: [],
628+
subagents: [],
629+
providerThreads: [],
630+
},
631+
],
632+
]) as unknown as ReadonlyMap<ThreadId, OrchestrationV2ThreadProjection>;
633+
const layerDependencies = Layer.mergeAll(
634+
NodeServices.layer,
635+
Layer.mock(ThreadManagementService.ThreadManagementService)({
636+
getThreadRecords: (threadId) => Effect.succeed(projections.get(threadId)!),
637+
// The child still runs Supervised; its user has since raised the task under it
638+
// to full access.
639+
getThreadShell: (threadId) =>
640+
Effect.succeed(
641+
threadId === grandchildThreadId
642+
? liveThreadShell(threadId)
643+
: liveThreadShell(threadId, { runtimeMode: "approval-required" }),
644+
),
645+
dispatch: (command) =>
646+
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
647+
Effect.as({} as never),
648+
),
649+
stopDelegatedTasks: () => Ref.set(stoppedBelow, true),
650+
}),
651+
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
652+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
653+
list: () => Effect.succeed([]),
654+
}),
655+
Layer.mock(ProjectService.ProjectService)({}),
656+
Layer.mock(SecretRequests.SecretRequests)({}),
657+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
658+
);
659+
const scope: McpInvocationScope = {
660+
environmentId: EnvironmentId.make("environment:mcp-cancel-grandchild"),
661+
requestNamespace: "provider-session:mcp-cancel-grandchild",
662+
thread: {
663+
threadId: parentThreadId,
664+
providerSessionId: "provider-session:mcp-cancel-grandchild",
665+
providerInstanceId: ProviderInstanceId.make("codex"),
666+
},
667+
client: undefined,
668+
capabilities: new Set(["orchestration"]),
669+
issuedAt: 1,
670+
};
671+
672+
yield* Effect.gen(function* () {
673+
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
674+
const error = yield* service
675+
.cancelTask(scope, { taskId, clientRequestId: "cancel-grandchild-above-modes" })
676+
.pipe(Effect.flip);
677+
assert.equal(error.code, "runtime_mode_escalation_denied");
678+
assert.deepEqual(yield* Ref.get(dispatched), []);
679+
assert.isFalse(yield* Ref.get(stoppedBelow));
680+
}).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(layerDependencies))));
681+
}),
682+
);
574683
});
575684

576685
describe("OrchestratorMcpService provider resolution", () => {

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,18 +1911,32 @@ const make = Effect.gen(function* () {
19111911
const current = yield* readTask(scope, input.taskId);
19121912
const key = yield* requestKey(input.clientRequestId);
19131913
const parentProjection = yield* loadProjection(scope.thread.threadId);
1914-
// Stopping the child is a write to its thread, which its user may have
1915-
// raised above the parent's modes since it was delegated.
1916-
const childShell = yield* threadManagement
1917-
.getThreadShell(current.childThreadId)
1918-
.pipe(Effect.mapError(threadManagementFailure));
1919-
if (childShell !== null && childShell.deletedAt === null) {
1920-
yield* resolveRuntimeMode(parentProjection.thread.runtimeMode, childShell.runtimeMode);
1921-
yield* resolveInteractionMode(
1922-
parentProjection.thread.interactionMode,
1923-
childShell.interactionMode,
1924-
);
1925-
}
1914+
// Cancelling stops the child and every task under it, each a write to a
1915+
// thread its user may have raised above the parent's modes since it was
1916+
// delegated. All of them are checked before anything is stopped.
1917+
const assertStoppable = (threadId: ThreadId): Effect.Effect<void, OrchestratorMcpFailure> =>
1918+
Effect.gen(function* () {
1919+
const shell = yield* threadManagement
1920+
.getThreadShell(threadId)
1921+
.pipe(Effect.mapError(threadManagementFailure));
1922+
if (shell === null) return;
1923+
if (shell.deletedAt === null) {
1924+
yield* resolveRuntimeMode(parentProjection.thread.runtimeMode, shell.runtimeMode);
1925+
yield* resolveInteractionMode(
1926+
parentProjection.thread.interactionMode,
1927+
shell.interactionMode,
1928+
);
1929+
}
1930+
const { subagents } = yield* threadManagement
1931+
.getThreadRecords(threadId, ["subagents"])
1932+
.pipe(Effect.mapError(threadManagementFailure));
1933+
for (const task of subagents) {
1934+
if (task.origin === "app_owned" && task.childThreadId !== null) {
1935+
yield* assertStoppable(task.childThreadId);
1936+
}
1937+
}
1938+
});
1939+
yield* assertStoppable(current.childThreadId);
19261940
const parentTask = parentProjection.subagents.find(
19271941
(task) => task.id === input.taskId && task.origin === "app_owned",
19281942
);

‎oxlint-plugin-t3code/rules/no-raw-mcp-registration.test.ts‎

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,91 @@ import { assert, describe } from "@effect/vitest";
33
import { createOxlintRuleHarness } from "../test/utils.ts";
44

55
const rule = createOxlintRuleHarness("t3code/no-raw-mcp-registration");
6+
const testFile = createOxlintRuleHarness("t3code/no-raw-mcp-registration", {
7+
filename: "server.test.ts",
8+
});
69

710
describe("t3code/no-raw-mcp-registration", () => {
811
rule.valid(
9-
"allows other McpServer members",
12+
"allows the rest of effect/ai and type-only McpServer imports",
1013
`
11-
import { McpServer } from "effect/ai";
12-
export const server = McpServer.McpServer;
14+
import { McpSchema, Tool, Toolkit } from "effect/ai";
15+
import type { McpServer } from "effect/ai";
16+
import { type McpServer as Server } from "effect/ai";
17+
export type Service = McpServer.McpServer | Server.McpServer;
18+
export const used = [McpSchema, Tool, Toolkit];
1319
`,
1420
);
1521

1622
rule.invalid(
17-
"reports McpServer.toolkit",
23+
"reports importing McpServer from effect/ai",
1824
`
1925
import { McpServer } from "effect/ai";
20-
export const registration = McpServer.toolkit(SomeToolkit);
26+
export const server = McpServer.McpServer;
2127
`,
2228
(output) => {
23-
assert.match(output, /McpToolAccess/);
29+
assert.match(output, /Only McpHttpServer may use Effect's McpServer/);
2430
},
2531
);
2632

2733
rule.invalid(
28-
"reports McpServer.registerToolkit",
34+
"reports importing McpServer under another name",
2935
`
30-
import { McpServer } from "effect/ai";
31-
export const registration = McpServer.registerToolkit(SomeToolkit);
36+
import { McpServer as Server } from "effect/ai";
37+
export const registration = Server.toolkit(SomeToolkit);
38+
`,
39+
);
40+
41+
rule.invalid(
42+
"reports a namespace import of effect/ai",
43+
`
44+
import * as Ai from "effect/ai";
45+
export const registration = Ai.McpServer.toolkit(SomeToolkit);
46+
`,
47+
);
48+
49+
rule.invalid(
50+
"reports importing the McpServer module directly",
51+
`
52+
import { toolkit } from "effect/ai/McpServer";
53+
export const registration = toolkit(SomeToolkit);
3254
`,
3355
);
3456

57+
rule.invalid(
58+
"reports re-exporting McpServer",
59+
`
60+
export { McpServer as Server } from "effect/ai";
61+
`,
62+
);
63+
64+
rule.invalid(
65+
"reports re-exporting all of effect/ai",
66+
`
67+
export * from "effect/ai";
68+
`,
69+
);
70+
71+
rule.invalid(
72+
"reports a dynamic import of the McpServer module",
73+
`
74+
export const load = () => import("effect/ai/McpServer");
75+
`,
76+
);
77+
78+
rule.invalid(
79+
"reports McpServer.toolkit",
80+
`
81+
export const registration = McpServer.toolkit(SomeToolkit);
82+
`,
83+
(output) => {
84+
assert.match(output, /McpServer\.toolkit registers on \/mcp/);
85+
},
86+
);
87+
3588
rule.invalid(
3689
"reports McpServer.resource and McpServer.prompt",
3790
`
38-
import { McpServer } from "effect/ai";
3991
export const resource = McpServer.resource({ uri: "t3://x", name: "x", content: "x" });
4092
export const prompt = McpServer.prompt({ name: "x", content: () => "x" });
4193
`,
@@ -45,14 +97,47 @@ describe("t3code/no-raw-mcp-registration", () => {
4597
"reports addTool on the McpServer service",
4698
`
4799
import * as Effect from "effect/Effect";
48-
import { McpServer } from "effect/ai";
49100
export const register = Effect.gen(function* () {
50-
const server = yield* McpServer.McpServer;
101+
const server = yield* Service;
51102
yield* server.addTool({ tool, annotations, handle: () => Effect.die("unchecked") });
52103
});
53104
`,
54105
(output) => {
55106
assert.match(output, /\.addTool registers on \/mcp/);
56107
},
57108
);
109+
110+
rule.invalid(
111+
"reports a registration method read without calling it",
112+
`
113+
export const add = server.addTool;
114+
`,
115+
);
116+
117+
rule.invalid(
118+
"reports a registration method taken by destructuring",
119+
`
120+
import * as Effect from "effect/Effect";
121+
export const register = Effect.gen(function* () {
122+
const { addTool } = yield* Service;
123+
yield* addTool({ tool, annotations, handle: () => Effect.die("unchecked") });
124+
});
125+
`,
126+
);
127+
128+
testFile.valid(
129+
"lets tests import McpServer to build a server",
130+
`
131+
import { McpServer } from "effect/ai";
132+
export const layer = McpServer.McpServer.layer;
133+
`,
134+
);
135+
136+
testFile.invalid(
137+
"reports a registration in a test",
138+
`
139+
import { McpServer } from "effect/ai";
140+
export const registration = McpServer.toolkit(SomeToolkit);
141+
`,
142+
);
58143
});

0 commit comments

Comments
 (0)