feat(server): every T3 MCP tool declares who may call it #16335
Mergedjuliusmarmingemerged 14 commits into
main fromrefs/pull/16335/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
juliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/mcp/McpToolAccess.ts line 105Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +26,21 @@26+ * modes, and a thread caller changes things only while its own run is live.27+ * A refusal is an `OrchestratorMcpFailure`, so the compiler requires it in the28+ * tool's failure schema, and `ThreadManagementService` in its dependencies.29+ *30+ * The declaration checks the caller before the handler runs. Handlers still31+ * check what only they can see, such as a queued run belonging to its thread.32+ */33+const AccessDeclared: unique symbol = Symbol.for("t3/mcp/McpToolAccess/AccessDeclared");34+35+/** Marks a handler, or a layer of handlers, whose access was declared here. */36+export interface Declared {37+ readonly [AccessDeclared]: true;38+}39+40+const declared = { [AccessDeclared]: true } as const;41+42+const declare = <P, A, E, R>(handle: (params: P) => Effect.Effect<A, E, R>) =>43+ Object.assign(handle, declared);44+45+/** The caller of a tool that changes something. */46+const writingCaller = loadCaller().pipe(Effect.tap(assertLiveCaller));apps/server/src/mcp/toolkits/environment/handlers.ts line 57OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -57,4 +53,5 @@5753 preferences: preferences(current),5854 };5955 }),60- t3_environment_preferences_update: (patch) =>56+ ),57+ t3_environment_preferences_update: McpToolAccess.writesEnvironment((patch) =>apps/server/src/mcp/toolkits/preview/handlers.ts line 211Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -192,9 +192,16 @@192- preview_open: (input) =>192+ preview_status: McpToolAccess.readsAsCaller((input) =>193+ invokeTargeted<PreviewAutomationStatus>("status", input ?? {}),194+ ),195+ preview_open: McpToolAccess.actsAsCaller((input) =>193196 invokeTargeted<PreviewAutomationStatus>("open", normalizePreviewOpenInput(input)),194- preview_navigate: (input) =>197+ ),198+ preview_navigate: McpToolAccess.actsAsCaller((input) =>195199 invokeTargeted<PreviewAutomationStatus>("navigate", input, input.timeoutMs),196- preview_resize: (input) =>200+ ),201+ preview_resize: McpToolAccess.actsAsCaller((input) =>197202 invokeTargeted<PreviewAutomationResizeResult>("resize", input, input.timeoutMs),198- preview_set_appearance: (input) =>203+ ),204+ preview_set_appearance: McpToolAccess.actsAsCaller((input) =>199205 invokeTargeted<PreviewAutomationSetColorSchemeResult>("setColorScheme", input),200- preview_snapshot: (input) => {206+ ),207+ preview_snapshot: McpToolAccess.readsAsCaller((input) => {
- apps/server/src/mcp/toolkits/project/tools.ts line 122Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -118,4 +119,4 @@118119 workspaceStrategy: Schema.optional(119120 OrchestrationV2ThreadLaunchWorkspaceStrategy.annotate({120121 description:121- "Choose where this thread runs before starting its agent: worktree creates and binds a new checkout from baseRef; existing_worktree binds worktreePath; root uses the project checkout. Omitted means root, not the caller's worktree. For a PR stack use the parent branch as baseRef and startFromOrigin:false. Uncommitted changes are not copied.",122+ "Choose where this thread runs before starting its agent: worktree creates and binds a new checkout from baseRef; existing_worktree binds worktreePath, which must be one of the project's git worktrees; root uses the project checkout. Omitted means root, not the caller's worktree. For a PR stack use the parent branch as baseRef and startFromOrigin:false. Uncommitted changes are not copied.",
- apps/server/src/mcp/McpToolAccess.ts line 52OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +32,21 @@32+ *33+ * Parameters choose the target; the caller sets the limits: a thread caller34+ * its own runtime and interaction modes, an outside client the ceiling it was35+ * approved with. Nothing a caller starts or changes may run with broader36+ * modes, and a thread caller changes things only while its own run is live.37+ * A refusal is an `OrchestratorMcpFailure`, so the compiler requires it in the38+ * tool's failure schema, and `ThreadManagementService` in its dependencies.39+ *40+ * The declaration checks the caller before the handler runs. Handlers still41+ * check what only they can see, such as a queued run belonging to its thread.42+ */43+export class Declaration<out Handler> {44+ // A private field makes the class nominal: only `make` below produces one,45+ // and copying a declaration's fields onto anything else fails to typecheck46+ // and, at runtime, to run.47+ readonly #handle: Handler;48+ private constructor(handle: Handler) {49+ this.#handle = handle;50+ }51+ /** @internal Builds a declaration; only the functions below call it. */52+ static make<P, A, E, R>(handle: (params: P) => Effect.Effect<A, E, R>) {
- apps/server/src/mcp/OrchestratorMcpService.ts line 1502OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1451,4 +1499,4 @@14511499 failure("orchestration_error", `Could not schedule task: ${error.message}`),14521500 ),14531501 );1454- return scheduledTaskSummary(task);1502+ return yield* summarizeScheduledTask(task, limits);
- oxlint-plugin-t3code/rules/no-raw-mcp-registration.ts line 54OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +34,21 @@34+ meta: {35+ type: "problem",36+ docs: {37+ description:38+ "Disallow registering on the MCP server without McpToolAccess; tools must declare who may call them.",39+ },40+ },41+ createOnce(context) {42+ return {43+ CallExpression(node) {44+ const callee = unwrapExpression(node.callee);45+ if (Option.isNone(callee) || callee.value.type !== "MemberExpression") return;46+ const method = getPropertyName(callee.value.property);47+ if (Option.isNone(method)) return;48+ if (SERVICE_REGISTRATIONS.has(method.value)) {49+ context.report({ node, message: message(`.${method.value}`) });50+ return;51+ }52+ if (53+ MODULE_REGISTRATIONS.has(method.value) &&54+ isIdentifier(unwrapExpression(callee.value.object), "McpServer")
- apps/server/src/vcs/GitVcsDriverCore.ts line 3967OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3954,3 +3954,14 @@39543954 }),39553955 );395639563957+ const listWorktreePaths: GitVcsDriver.GitVcsDriver["Service"]["listWorktreePaths"] = (cwd) =>3958+ runGitStdout("GitVcsDriver.listWorktreePaths", cwd, [3959+ "worktree",3960+ "list",3961+ "--porcelain",3962+ "-z",3963+ ]).pipe(3964+ Effect.map((stdout) =>3965+ stdout3966+ .split("\0")3967+ .filter((field) => field.startsWith("worktree "))
- apps/server/src/orchestration-v2/Orchestrator.ts line 10434OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -10404,3 +10424,11 @@1040410424 } satisfies OrchestratorV2DispatchResult;1040510425 }104061042610427+ // A limited sender checked these modes before dispatching; the thread's10428+ // user may have raised them since, and only here can they not change.10429+ const limit = yield* DispatchModeLimit;10430+ if (limit !== undefined) {10431+ // A fork or merge-back also copies its source thread, modes included.10432+ const threadIds =10433+ command.type === "thread.fork" || command.type === "thread.merge_back"10434+ ? [commandThreadId(command), command.sourceThreadId]
juliusmarminge merged this2f85686
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed