Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): count only the presented tools and their prompt in context pressure

Alfredo Lopez1194e7aa313c
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 1c982b5aa..92cfc5bd3 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.437",
+ "version": "0.1.438",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.437",
+ "version": "0.1.438",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 1462f23fd..70f5f14dd 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.437",
+ "version": "0.1.438",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/agent/taskExecutor.ts b/mcp-host/src/agent/taskExecutor.ts
index 40021557d..f1116b42f 100644
--- a/mcp-host/src/agent/taskExecutor.ts
+++ b/mcp-host/src/agent/taskExecutor.ts
@@ -1473,14 +1473,19 @@ export class TaskExecutor {
const reasoning = parts
? reasoningFactory.createWithParts(parts)
: reasoningFactory.create(identity)
- // R9-14 — the text of the system prompt `reasoning` sends, for the context
- // manager to count. The cache path joins its tiers as `LlmPortAdapter`
- // does; the legacy path runs the builder `DefaultReasoningPort` runs, over
- // the registry's full tool list, a superset of what the loop presents.
- const systemPrompt = parts
- ? [parts.stable, parts.context].filter(s => s.length > 0).join('\n\n')
- : new DefaultPromptBuilder().buildSystemPrompt(registry.listDefinitions(), identity, metadata)
- .content
+ // R9-14 / R21-1 — the text of the system prompt `reasoning` sends with a
+ // request that presents `tools`, for the context manager to count. The
+ // cache path sends the parts it built once, joined as `LlmPortAdapter`
+ // joins them, whatever the loop presents; the legacy path runs the builder
+ // `DefaultReasoningPort` runs, over the presented list.
+ let systemPromptFor: (tools: ToolDefinition[]) => string
+ if (parts) {
+ const cachedPrompt = [parts.stable, parts.context].filter(s => s.length > 0).join('\n\n')
+ systemPromptFor = () => cachedPrompt
+ } else {
+ const promptBuilder = new DefaultPromptBuilder()
+ systemPromptFor = tools => promptBuilder.buildSystemPrompt(tools, identity, metadata).content
+ }
const contextManager = new PressureContextManager(
this.contextMaxTokens(),
@@ -1539,7 +1544,7 @@ export class TaskExecutor {
toolProgressInterval: appConfig.nativeTool.toolProgressInterval,
})
loopConfig.abortSignal = this.abortController.signal
- loopConfig.systemPrompt = systemPrompt
+ loopConfig.systemPromptFor = systemPromptFor
loopConfig.imageSourceIdentity = this.providerChainRequiresImageSourceIdentity()
loopConfig.onAttachments = attachments =>
mergeCollectedAttachments(this.completedAttachments, attachments)
diff --git a/mcp-host/src/core/orchestration/loopConfig.ts b/mcp-host/src/core/orchestration/loopConfig.ts
index d24740ff9..92e8ef4d5 100644
--- a/mcp-host/src/core/orchestration/loopConfig.ts
+++ b/mcp-host/src/core/orchestration/loopConfig.ts
@@ -44,12 +44,13 @@ export interface LoopConfig {
toolOutputProcessor: ToolOutputProcessor
/**
- * The system prompt `reasoning` sends with every request, including the
- * daily-log snapshot. The loop hands it to `contextManager` so pressure
- * counts it (R9-14). Set by `TaskExecutor.buildLoopConfig()`; absent means
- * the caller sends no system prompt of its own.
+ * The system prompt `reasoning` sends with a request that presents `tools`,
+ * including the daily-log snapshot. The loop calls it with the list it
+ * presents on each iteration and hands the text to `contextManager` so
+ * pressure counts it (R9-14, R21-1). Set by `TaskExecutor.buildLoopConfig()`;
+ * absent means the caller sends no system prompt of its own.
*/
- systemPrompt?: string
+ systemPromptFor?: (tools: ToolDefinition[]) => string
/**
* Tool-lane guardrail (spec §6). Absent = no guardrails configured = today's
diff --git a/mcp-host/src/core/orchestration/toolUseLoop.ts b/mcp-host/src/core/orchestration/toolUseLoop.ts
index 93196a4b1..e5c8586a9 100644
--- a/mcp-host/src/core/orchestration/toolUseLoop.ts
+++ b/mcp-host/src/core/orchestration/toolUseLoop.ts
@@ -110,8 +110,8 @@ export async function runToolUseLoop(
timestamp: new Date(),
})
- messages = await manageMessagesForIteration(config, messages, iteration, true)
-
+ // R21-1: the presented list is selected before pressure is measured, so
+ // the context manager counts the schemas this request actually carries.
let tools: ToolDefinition[]
try {
tools = await loopController.refreshTools(toolRegistry.listDefinitions())
@@ -125,6 +125,8 @@ export async function runToolUseLoop(
}
}
+ messages = await manageMessagesForIteration(config, messages, iteration, tools, true)
+
const context: ReasoningContext = {
messages,
available_tools: tools,
@@ -398,7 +400,7 @@ export async function runToolUseLoop(
config.imageSourceIdentity === true
)
lastToolResults = toolResults
- messages = await manageMessagesForIteration(config, messages, iteration)
+ messages = await manageMessagesForIteration(config, messages, iteration, tools)
validateToolLinkages(messages)
continue
}
diff --git a/mcp-host/src/core/orchestration/toolUseLoopRuntime.ts b/mcp-host/src/core/orchestration/toolUseLoopRuntime.ts
index 0e63b140d..68b6df85f 100644
--- a/mcp-host/src/core/orchestration/toolUseLoopRuntime.ts
+++ b/mcp-host/src/core/orchestration/toolUseLoopRuntime.ts
@@ -8,6 +8,7 @@ import type {
ReasoningContext,
RespondResult,
TokenUsage,
+ ToolDefinition,
ToolResult,
} from '../types'
import type { LoopConfig } from './loopConfig'
@@ -19,6 +20,7 @@ export async function manageMessagesForIteration(
config: LoopConfig,
messages: ChatMessage[],
iteration: number,
+ tools: ToolDefinition[],
logCompaction = false
): Promise<ChatMessage[]> {
// IronClaw invariant #1 (P.3 §4.1): if the conversation has a pending_approval,
@@ -45,9 +47,11 @@ export async function manageMessagesForIteration(
const beforeCount = messages.length
// P.5: the canonical signature requires the conversation so the manager's
// defensive guard and tier selection can see `pending_approval`/state.
+ // R21-1: pressure counts the tools this iteration presents, which in Codex
+ // discovery mode or under the bridge latch exclude the deferred MCP schemas.
const managed = await config.contextManager.manage(messages, config.conversation, {
- tools: config.toolRegistry.listDefinitions(),
- systemPrompt: config.systemPrompt,
+ tools,
+ systemPrompt: config.systemPromptFor?.(tools),
})
if (managed.length < beforeCount) {
if (logCompaction) {