Skip to content

Commit 8b66fe7

Browse files
committed
feat(mcp): deliver requester-scoped MCP to shared-thread harnesses
Requester-scoped servers never enter harness-native MCP config: Codex thread and CLI user-MCP projections partition them out (fail-closed, static-partition fingerprints stay sender-independent so shared threads never rotate). Codex delivers them as per-attempt dynamic tools through a scoped-only runtime path on the same core manager, with a session-stable advertised catalog so authed and unauthed senders share one spec surface; unauthed calls get a clean not-connected tool error. New SDK seam materializeRequesterScopedMcpToolsForHarnessRun lets any future harness consume the same contract.
1 parent d50fece commit 8b66fe7

17 files changed

Lines changed: 1062 additions & 17 deletions

‎docs/plugins/sdk-overview.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,20 @@ Contract notes:
264264
identity, so app previews stay fail-closed for these servers. Tool results
265265
are unaffected.
266266
- Static servers without a resolver keep the existing session-scoped lifecycle.
267+
- **Harness delivery rule:** requester-scoped servers never enter harness-native
268+
MCP client config (Codex thread `mcp_servers`, CLI `-c mcp_servers=…`, or any
269+
other session-shared MCP projection). Harnesses deliver them as run-scoped
270+
tools instead:
271+
- Embedded runner: session MCP runtime + bundle tools (static + scoped).
272+
- Codex app-server: dynamic tools via
273+
`materializeRequesterScopedMcpToolsForHarnessRun` (scoped-only; static
274+
servers stay on Codex's native MCP client).
275+
- Scoped tool **specs** are session-stable after the first successful resolve in
276+
that session, so shared-thread harnesses (Codex) do not rotate threads when
277+
senders change. Before any requester resolves, no scoped specs are advertised.
278+
- Unauthenticated requesters on a shared-thread harness still see the advertised
279+
scoped tools; calling one returns a clean not-connected tool error for that
280+
requester. OpenClaw never falls back to another requester's credentials.
267281

268282
Memory prompt supplement builders receive optional `agentId`,
269283
`agentSessionKey`, and `sandboxed` context. Memory corpus supplement `search`

‎extensions/codex/src/app-server/run-attempt-cleanup.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ export async function cleanupCodexAttempt(
9797
}
9898
}
9999
await releaseSandboxExecEnvironment();
100+
await runAgentCleanupStep({
101+
runId: params.runId,
102+
sessionId: params.sessionId,
103+
step: "codex-scoped-mcp-dispose",
104+
log: embeddedAgentLog,
105+
cleanup: async () => {
106+
await prompt.context.attemptTools.scopedMcpTools?.dispose();
107+
},
108+
});
100109
runAbortController.signal.removeEventListener("abort", abortListener);
101110
steeringQueueRef.current?.cancel();
102111
freezeRunTerminalOutcome();

‎extensions/codex/src/app-server/run-attempt-tool-setup.ts‎

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import {
22
embeddedAgentLog,
33
isHostScopedAgentToolActive,
4+
materializeRequesterScopedMcpToolsForHarnessRun,
5+
resolveAgentDir,
46
} from "openclaw/plugin-sdk/agent-harness-runtime";
57
import {
68
buildDynamicTools,
79
formatCodexDynamicToolBuildStageSummary,
810
resolveCodexMessageToolProvider,
911
shouldWarnCodexDynamicToolBuildStageSummary,
1012
} from "./dynamic-tool-build.js";
11-
import { resolveCodexDynamicToolsLoadingForRuntime } from "./dynamic-tool-profile.js";
13+
import {
14+
filterCodexDynamicTools,
15+
resolveCodexDynamicToolsLoadingForRuntime,
16+
} from "./dynamic-tool-profile.js";
1217
import { createCodexDynamicToolBridge } from "./dynamic-tools.js";
1318
import { emitCodexAppServerEvent } from "./run-attempt-lifecycle.js";
1419
import type { CodexAttemptRuntime } from "./run-attempt-runtime.js";
@@ -38,6 +43,7 @@ export async function prepareCodexAttemptTools(runtime: CodexAttemptRuntime) {
3843
sessionAgentId,
3944
pluginConfig,
4045
profilerEnabled,
46+
agentDir,
4147
} = connection;
4248
const preDynamicSummary = preDynamicStartupStages.snapshot();
4349
if (shouldWarnCodexDynamicToolBuildStageSummary(preDynamicSummary)) {
@@ -138,9 +144,79 @@ export async function prepareCodexAttemptTools(runtime: CodexAttemptRuntime) {
138144
ignoreDisableMessageTool: true,
139145
ignoreRuntimePlan: true,
140146
});
147+
// Requester-scoped MCP: dynamic tools on a shared thread (never harness-native MCP).
148+
// Specs come from the session advertised-catalog cache so fingerprints stay stable.
149+
const scopedMcpTools = await materializeRequesterScopedMcpToolsForHarnessRun({
150+
sessionId: params.sessionId,
151+
sessionKey: params.sessionKey,
152+
workspaceDir: effectiveWorkspace,
153+
agentDir: agentDir ?? resolveAgentDir(params.config ?? {}, sessionAgentId),
154+
cfg: params.config,
155+
requesterSenderId: params.senderId,
156+
agentAccountId: params.agentAccountId,
157+
messageChannel: params.messageChannel ?? params.messageProvider,
158+
reservedToolNames: [
159+
...tools.map((tool) => tool.name),
160+
...registeredTools.map((tool) => tool.name),
161+
],
162+
toolsAllow: params.toolsAllow,
163+
policyContext: {
164+
config: params.config,
165+
sessionKey: sandboxSessionKey,
166+
runSessionKey:
167+
params.sessionKey && params.sessionKey !== sandboxSessionKey
168+
? params.sessionKey
169+
: undefined,
170+
sessionId: params.sessionId,
171+
runId: params.runId,
172+
agentId: sessionAgentId,
173+
agentDir: agentDir ?? resolveAgentDir(params.config ?? {}, sessionAgentId),
174+
agentAccountId: params.agentAccountId,
175+
messageProvider: params.messageProvider ?? params.messageChannel,
176+
messageChannel: params.messageChannel,
177+
chatType: params.chatType,
178+
messageTo: params.messageTo,
179+
messageThreadId: params.messageThreadId,
180+
currentChannelId: params.currentChannelId,
181+
currentMessagingTarget: params.currentMessagingTarget,
182+
currentThreadTs: params.currentThreadTs,
183+
currentMessageId: params.currentMessageId,
184+
groupId: params.groupId,
185+
groupChannel: params.groupChannel,
186+
groupSpace: params.groupSpace,
187+
memberRoleIds: params.memberRoleIds,
188+
spawnedBy: params.spawnedBy,
189+
senderId: params.senderId,
190+
senderName: params.senderName,
191+
senderUsername: params.senderUsername,
192+
senderE164: params.senderE164,
193+
senderIsOwner: params.senderIsOwner,
194+
modelProvider: params.provider,
195+
modelId: params.modelId,
196+
modelApi: params.model.api,
197+
modelContextWindowTokens: params.model.contextWindow,
198+
modelHasVision: params.model.input?.includes("image") ?? false,
199+
workspaceDir: effectiveWorkspace,
200+
cwd: effectiveCwd ?? effectiveWorkspace,
201+
sandboxToolPolicy: sandbox?.tools,
202+
},
203+
warn: (message) => embeddedAgentLog.warn(message),
204+
});
205+
// Restricted dynamic-tool profiles (private QA, exclusion lists) gate scoped
206+
// MCP tools exactly like every other dynamic tool. Filter both lists with the
207+
// same rule so execution and advertised specs stay name-aligned.
208+
const scopedExecutable = scopedMcpTools
209+
? filterCodexDynamicTools(scopedMcpTools.tools, pluginConfig)
210+
: [];
211+
const scopedAdvertised = scopedMcpTools
212+
? filterCodexDynamicTools(scopedMcpTools.advertisedTools, pluginConfig)
213+
: [];
214+
const toolsWithScopedMcp = scopedExecutable.length > 0 ? [...tools, ...scopedExecutable] : tools;
215+
const registeredWithScopedMcp =
216+
scopedAdvertised.length > 0 ? [...registeredTools, ...scopedAdvertised] : registeredTools;
141217
const toolBridge = createCodexDynamicToolBridge({
142-
tools,
143-
registeredTools,
218+
tools: toolsWithScopedMcp,
219+
registeredTools: registeredWithScopedMcp,
144220
signal: runAbortController.signal,
145221
computerContextEpoch,
146222
loading: resolveCodexDynamicToolsLoadingForRuntime(pluginConfig, effectiveRuntimeModelId, {
@@ -171,8 +247,9 @@ export async function prepareCodexAttemptTools(runtime: CodexAttemptRuntime) {
171247
},
172248
});
173249
return {
174-
tools,
175-
registeredTools,
250+
tools: toolsWithScopedMcp,
251+
registeredTools: registeredWithScopedMcp,
252+
scopedMcpTools,
176253
dynamicToolParams,
177254
computerContextEpoch,
178255
toolBridge,

‎scripts/plugin-sdk-surface-report.mjs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,14 @@ export function readPluginSdkSurfaceBudgets(env = process.env) {
209209
publicExports: readPluginSdkSurfaceBudgetEnv(
210210
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_EXPORTS",
211211
// +3: registerMcpServerConnectionResolver context/result/resolver types (#106229).
212-
10687,
212+
// +2: materializeRequesterScopedMcpToolsForHarnessRun (agent-harness-runtime + compat mirror).
213+
10689,
213214
env,
214215
),
215216
publicFunctionExports: readPluginSdkSurfaceBudgetEnv(
216217
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_FUNCTION_EXPORTS",
217-
5379,
218+
// +2: materializeRequesterScopedMcpToolsForHarnessRun (agent-harness-runtime + compat mirror).
219+
5381,
218220
env,
219221
),
220222
publicDeprecatedExports: readPluginSdkSurfaceBudgetEnv(

0 commit comments

Comments
 (0)