|
1 | 1 | import { |
2 | 2 | embeddedAgentLog, |
3 | 3 | isHostScopedAgentToolActive, |
| 4 | + materializeRequesterScopedMcpToolsForHarnessRun, |
| 5 | + resolveAgentDir, |
4 | 6 | } from "openclaw/plugin-sdk/agent-harness-runtime"; |
5 | 7 | import { |
6 | 8 | buildDynamicTools, |
7 | 9 | formatCodexDynamicToolBuildStageSummary, |
8 | 10 | resolveCodexMessageToolProvider, |
9 | 11 | shouldWarnCodexDynamicToolBuildStageSummary, |
10 | 12 | } 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"; |
12 | 17 | import { createCodexDynamicToolBridge } from "./dynamic-tools.js"; |
13 | 18 | import { emitCodexAppServerEvent } from "./run-attempt-lifecycle.js"; |
14 | 19 | import type { CodexAttemptRuntime } from "./run-attempt-runtime.js"; |
@@ -38,6 +43,7 @@ export async function prepareCodexAttemptTools(runtime: CodexAttemptRuntime) { |
38 | 43 | sessionAgentId, |
39 | 44 | pluginConfig, |
40 | 45 | profilerEnabled, |
| 46 | + agentDir, |
41 | 47 | } = connection; |
42 | 48 | const preDynamicSummary = preDynamicStartupStages.snapshot(); |
43 | 49 | if (shouldWarnCodexDynamicToolBuildStageSummary(preDynamicSummary)) { |
@@ -138,9 +144,79 @@ export async function prepareCodexAttemptTools(runtime: CodexAttemptRuntime) { |
138 | 144 | ignoreDisableMessageTool: true, |
139 | 145 | ignoreRuntimePlan: true, |
140 | 146 | }); |
| 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; |
141 | 217 | const toolBridge = createCodexDynamicToolBridge({ |
142 | | - tools, |
143 | | - registeredTools, |
| 218 | + tools: toolsWithScopedMcp, |
| 219 | + registeredTools: registeredWithScopedMcp, |
144 | 220 | signal: runAbortController.signal, |
145 | 221 | computerContextEpoch, |
146 | 222 | loading: resolveCodexDynamicToolsLoadingForRuntime(pluginConfig, effectiveRuntimeModelId, { |
@@ -171,8 +247,9 @@ export async function prepareCodexAttemptTools(runtime: CodexAttemptRuntime) { |
171 | 247 | }, |
172 | 248 | }); |
173 | 249 | return { |
174 | | - tools, |
175 | | - registeredTools, |
| 250 | + tools: toolsWithScopedMcp, |
| 251 | + registeredTools: registeredWithScopedMcp, |
| 252 | + scopedMcpTools, |
176 | 253 | dynamicToolParams, |
177 | 254 | computerContextEpoch, |
178 | 255 | toolBridge, |
|
0 commit comments