Skip to content

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

TypeScript

fix(mcp-host): include tool-call arguments in the heuristic token count (#731)

Alfredo Lopezcc2b97a5db5a
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 65074c658..d9d139b9d 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.365",
+ "version": "0.1.366",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.365",
+ "version": "0.1.366",
"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 4c87a1cbb..7b940acdd 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.365",
+ "version": "0.1.366",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/tokenizer/__tests__/heuristic.test.ts b/mcp-host/src/core/tokenizer/__tests__/heuristic.test.ts
index b6251e00e..a4500b063 100644
--- a/mcp-host/src/core/tokenizer/__tests__/heuristic.test.ts
+++ b/mcp-host/src/core/tokenizer/__tests__/heuristic.test.ts
@@ -20,10 +20,12 @@ describe('heuristicCount', () => {
const msgs: ChatMessage[] = [
{ role: 'assistant', content: '', tool_calls: [{ id: 'x', name: 'y', arguments: {} }] },
]
- // '' → ceil(0/4)=0, +4 = 4. The framing overhead is all that remains, and
- // the `tool_calls` payload is still uncounted here — A2 (#731, step 3) is
- // what adds it.
- expect(heuristicCount(msgs)).toBe(4)
+ // '' → ceil(0/4)=0, +4 = 4, plus the empty arguments object: `JSON.stringify({})`
+ // is '{}', 2 chars → ceil(2/4) = 1. The 1 is the whole point — it is the
+ // smallest observable contribution of the `tool_calls` walk A2 added, so
+ // this pin doubles as the witness that the walk runs on every message and
+ // not only on the large payload T-A2 feeds it.
+ expect(heuristicCount(msgs)).toBe(5)
})
it('T-A1 counts minified JSON by characters, not by whitespace-separated words', () => {
diff --git a/mcp-host/src/core/tokenizer/heuristic.ts b/mcp-host/src/core/tokenizer/heuristic.ts
index a5ee7cd44..827e0e022 100644
--- a/mcp-host/src/core/tokenizer/heuristic.ts
+++ b/mcp-host/src/core/tokenizer/heuristic.ts
@@ -25,6 +25,12 @@ export function heuristicCount(messages: ChatMessage[]): number {
let total = 0
for (const msg of messages) {
total += Math.ceil((msg.content ?? '').length / 4) + 4
+ // An assistant message that issues a tool call carries its payload here,
+ // never in `content`; without this the whole call bills at the framing
+ // overhead alone. Mirrors `openaiTokenCounter.ts:58-62` (#731).
+ for (const tc of msg.tool_calls ?? []) {
+ total += Math.ceil(JSON.stringify(tc.arguments ?? {}).length / 4)
+ }
}
return total
}