mcp-context-cost 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -25
- package/dist/audit/audit.d.ts +38 -0
- package/dist/audit/audit.js +372 -7
- package/dist/audit/config.d.ts +38 -0
- package/dist/audit/config.js +64 -0
- package/dist/audit/deferral.d.ts +346 -0
- package/dist/audit/deferral.js +376 -0
- package/dist/audit/run.d.ts +22 -0
- package/dist/audit/run.js +17 -1
- package/dist/core/adoption.d.ts +226 -0
- package/dist/core/adoption.js +432 -0
- package/dist/core/canonical.d.ts +6 -0
- package/dist/core/canonical.js +3 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/session-start.d.ts +102 -0
- package/dist/core/session-start.js +186 -0
- package/dist/core/types.d.ts +8 -0
- package/dist/sweep/client.d.ts +6 -1
- package/dist/sweep/client.js +1 -0
- package/dist/sweep/dashboard.d.ts +10 -0
- package/dist/sweep/dashboard.js +32 -16
- package/dist/sweep/docker.d.ts +23 -0
- package/dist/sweep/docker.js +16 -12
- package/dist/sweep/harness-guard.d.ts +57 -0
- package/dist/sweep/harness-guard.js +144 -0
- package/dist/sweep/history.d.ts +33 -1
- package/dist/sweep/history.js +60 -5
- package/dist/sweep/regen.js +6 -1
- package/dist/sweep/report.d.ts +16 -0
- package/dist/sweep/report.js +79 -5
- package/dist/sweep/run.d.ts +41 -0
- package/dist/sweep/run.js +129 -36
- package/dist/sweep/server-pages.js +31 -6
- package/dist/sweep/session-start.d.ts +3 -0
- package/dist/sweep/session-start.js +103 -0
- package/dist/sweep/shard.d.ts +41 -0
- package/dist/sweep/shard.js +58 -0
- package/dist/sweep/sweep-all.js +56 -2
- package/package.json +3 -1
package/dist/audit/audit.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import { METHODOLOGY_VERSION } from '../core/canonical.js';
|
|
16
16
|
import { isCurrent } from '../core/divergence.js';
|
|
17
|
+
import { evaluateDeferral, PUBLISHED_WIRE_TO_CLIENT_RATIO, SHELL_SOURCE, } from './deferral.js';
|
|
17
18
|
import { formatDiff, formatGate } from './diff.js';
|
|
18
19
|
export const DEFAULT_CONTEXT_WINDOW = 200_000;
|
|
19
20
|
const TRIM_TOOL_COUNT = 3;
|
|
@@ -63,9 +64,100 @@ export function planBudgetFit(config, limit) {
|
|
|
63
64
|
export function serverKey(s) {
|
|
64
65
|
return JSON.stringify(s.argv ?? [s.url ?? s.name]);
|
|
65
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* One entry's environment, as a value two entries can be compared by.
|
|
69
|
+
*
|
|
70
|
+
* Values are read here and compared here, and nothing derived from them leaves
|
|
71
|
+
* this function — the count that does is a count of entries. Same rule as
|
|
72
|
+
* `config.ts`: env values are read to spawn a server, never written to a report.
|
|
73
|
+
*/
|
|
74
|
+
function envSignature(s) {
|
|
75
|
+
const env = s.env ?? {};
|
|
76
|
+
return JSON.stringify(Object.keys(env).sort().map((k) => [k, env[k]]));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The measurement keys that stand for more than one distinct server.
|
|
80
|
+
*
|
|
81
|
+
* `serverKey` is the argv alone, so two entries running the same command under
|
|
82
|
+
* different environments are measured once and both are given that one number.
|
|
83
|
+
* Environment decides what a server serves — `GITHUB_TOOLSETS` on
|
|
84
|
+
* `github-mcp-server` selects which toolsets it lists — so for entries under one
|
|
85
|
+
* of these keys, the number reported is one entry's, not each one's.
|
|
86
|
+
*
|
|
87
|
+
* Same argv AND same environment is not collapsed: two clients pointing at an
|
|
88
|
+
* identical server are one measurement, which is the reuse this key is for.
|
|
89
|
+
*/
|
|
90
|
+
export function collapsedKeys(configs) {
|
|
91
|
+
const envs = new Map();
|
|
92
|
+
for (const cfg of configs) {
|
|
93
|
+
if (cfg.error)
|
|
94
|
+
continue;
|
|
95
|
+
for (const s of cfg.servers) {
|
|
96
|
+
if (s.transport !== 'stdio')
|
|
97
|
+
continue;
|
|
98
|
+
const key = serverKey(s);
|
|
99
|
+
const seen = envs.get(key);
|
|
100
|
+
if (seen)
|
|
101
|
+
seen.add(envSignature(s));
|
|
102
|
+
else
|
|
103
|
+
envs.set(key, new Set([envSignature(s)]));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return new Set([...envs].filter(([, sigs]) => sigs.size > 1).map(([key]) => key));
|
|
107
|
+
}
|
|
66
108
|
function measuredOk(m) {
|
|
67
109
|
return (m.status === 'measured' || m.status === 'dynamic') && typeof m.totalTokens === 'number';
|
|
68
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Clients whose several config files are read into ONE session.
|
|
113
|
+
*
|
|
114
|
+
* Claude Code loads user-scope `~/.claude.json` and project-scope
|
|
115
|
+
* `<cwd>/.mcp.json` together, so a stack split across them faces the deferral
|
|
116
|
+
* threshold as a sum. Judging each file alone tells the standard setup it is
|
|
117
|
+
* under a line the session it actually runs is over.
|
|
118
|
+
*
|
|
119
|
+
* This does not merge the reported totals, which stay per file: a context
|
|
120
|
+
* window belongs to one session, and that is the argument for adding these two
|
|
121
|
+
* together — not for adding one client's servers to another's.
|
|
122
|
+
*/
|
|
123
|
+
const ONE_SESSION_PER_CLIENT = new Set(['claude-code']);
|
|
124
|
+
/** Which configs share a deferral verdict. */
|
|
125
|
+
function deferralScopeKey(client, source) {
|
|
126
|
+
return ONE_SESSION_PER_CLIENT.has(client) ? client : `${client}\0${source}`;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Give every config a deferral verdict, computed once per session scope and
|
|
130
|
+
* shared by identity across the configs that scope covers — so the report can
|
|
131
|
+
* print it once and a `--json` consumer can see which files it spans.
|
|
132
|
+
*/
|
|
133
|
+
function attachDeferral(configs, contextWindow, opts) {
|
|
134
|
+
const scopes = new Map();
|
|
135
|
+
for (const cfg of configs) {
|
|
136
|
+
const key = deferralScopeKey(cfg.client, cfg.source);
|
|
137
|
+
const group = scopes.get(key);
|
|
138
|
+
if (group)
|
|
139
|
+
group.push(cfg);
|
|
140
|
+
else
|
|
141
|
+
scopes.set(key, [cfg]);
|
|
142
|
+
}
|
|
143
|
+
const verdicts = new Map();
|
|
144
|
+
for (const [key, group] of scopes) {
|
|
145
|
+
verdicts.set(key,
|
|
146
|
+
// Computed against the same context window the share uses, so any
|
|
147
|
+
// threshold moves with `--context` instead of being pinned to 200,000.
|
|
148
|
+
evaluateDeferral({
|
|
149
|
+
client: group[0].client,
|
|
150
|
+
sources: group.map((c) => c.source),
|
|
151
|
+
servers: group.flatMap((c) => c.servers.map((s) => ({ tokens: s.tokens ?? 0, claudeTokens: s.claudeTokens }))),
|
|
152
|
+
skippedCount: group.reduce((a, c) => a + c.skipped.length, 0),
|
|
153
|
+
sharedMeasurements: group.reduce((a, c) => a + (opts.shared.get(c) ?? 0), 0),
|
|
154
|
+
}, { contextWindow, env: opts.env, settings: opts.settings, divergence: opts.divergence }));
|
|
155
|
+
}
|
|
156
|
+
return configs.map((cfg) => ({
|
|
157
|
+
...cfg,
|
|
158
|
+
deferral: verdicts.get(deferralScopeKey(cfg.client, cfg.source)),
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
69
161
|
/**
|
|
70
162
|
* Assemble the report from configs + measurements. Pure: `runAudit` does the
|
|
71
163
|
* spawning, this does the arithmetic, so totals and shares are testable without
|
|
@@ -74,7 +166,11 @@ function measuredOk(m) {
|
|
|
74
166
|
export function buildReport(configs, measured, opts = {}) {
|
|
75
167
|
const contextWindow = opts.contextWindow ?? DEFAULT_CONTEXT_WINDOW;
|
|
76
168
|
const problems = [];
|
|
77
|
-
const
|
|
169
|
+
const built = [];
|
|
170
|
+
// Across every config at once: a twin in one client's file is measured for
|
|
171
|
+
// the other client's entry just the same.
|
|
172
|
+
const collapsed = collapsedKeys(configs);
|
|
173
|
+
const shared = new Map();
|
|
78
174
|
for (const cfg of configs) {
|
|
79
175
|
if (cfg.error) {
|
|
80
176
|
problems.push(`${cfg.source}: ${cfg.error}`);
|
|
@@ -83,6 +179,9 @@ export function buildReport(configs, measured, opts = {}) {
|
|
|
83
179
|
const ok = [];
|
|
84
180
|
const skipped = [];
|
|
85
181
|
const tools = [];
|
|
182
|
+
// Counted only for servers that put a number into the total: a twin that
|
|
183
|
+
// failed to launch is already a floor, and adds nothing to a sum.
|
|
184
|
+
let sharedHere = 0;
|
|
86
185
|
for (const s of cfg.servers) {
|
|
87
186
|
const base = {
|
|
88
187
|
name: s.name,
|
|
@@ -118,6 +217,8 @@ export function buildReport(configs, measured, opts = {}) {
|
|
|
118
217
|
});
|
|
119
218
|
continue;
|
|
120
219
|
}
|
|
220
|
+
if (collapsed.has(serverKey(s)))
|
|
221
|
+
sharedHere++;
|
|
121
222
|
const divRow = opts.divergence?.servers[s.name];
|
|
122
223
|
ok.push({
|
|
123
224
|
...base,
|
|
@@ -138,7 +239,7 @@ export function buildReport(configs, measured, opts = {}) {
|
|
|
138
239
|
for (const s of ok)
|
|
139
240
|
s.share = totalTokens > 0 ? (s.tokens ?? 0) / totalTokens : 0;
|
|
140
241
|
tools.sort((a, b) => b.tokens - a.tokens);
|
|
141
|
-
|
|
242
|
+
const result = {
|
|
142
243
|
client: cfg.client,
|
|
143
244
|
source: cfg.source,
|
|
144
245
|
totalTokens,
|
|
@@ -153,9 +254,12 @@ export function buildReport(configs, measured, opts = {}) {
|
|
|
153
254
|
skipped,
|
|
154
255
|
heaviestTools: tools.slice(0, 5),
|
|
155
256
|
trimAdvice: buildTrimAdvice(tools, totalTokens),
|
|
156
|
-
}
|
|
257
|
+
};
|
|
258
|
+
built.push(result);
|
|
259
|
+
shared.set(result, sharedHere);
|
|
157
260
|
}
|
|
158
|
-
|
|
261
|
+
built.sort((a, b) => b.totalTokens - a.totalTokens);
|
|
262
|
+
const results = attachDeferral(built, contextWindow, { ...opts, shared });
|
|
159
263
|
const report = {
|
|
160
264
|
methodologyVersion: METHODOLOGY_VERSION,
|
|
161
265
|
encoding: 'o200k_base',
|
|
@@ -184,11 +288,264 @@ export function buildReport(configs, measured, opts = {}) {
|
|
|
184
288
|
}
|
|
185
289
|
const n = (x) => x.toLocaleString('en-US');
|
|
186
290
|
const pct = (x) => `${(x * 100).toFixed(1)}%`;
|
|
291
|
+
/** The measurement itself — unconditional, and separate from any claim about who pays it. */
|
|
292
|
+
function measurementLine(cfg, contextWindow) {
|
|
293
|
+
return (` ${n(cfg.totalTokens)} tokens of tool schemas — ${pct(cfg.contextShare)} of a ` +
|
|
294
|
+
`${n(contextWindow)}-token context window.`);
|
|
295
|
+
}
|
|
296
|
+
/** How a place is named in a sentence; the shell has a path-shaped stand-in in JSON. */
|
|
297
|
+
function sourceName(source) {
|
|
298
|
+
return source === SHELL_SOURCE ? 'this shell' : source;
|
|
299
|
+
}
|
|
300
|
+
/** "ENABLE_TOOL_SEARCH=auto on this machine" / "unset here, the documented default". */
|
|
301
|
+
function settingPhrase(d) {
|
|
302
|
+
const s = d.setting;
|
|
303
|
+
if (!s || !s.variable)
|
|
304
|
+
return 'by default';
|
|
305
|
+
// Which place it was read in is not spelled here: a settings path is longer
|
|
306
|
+
// than this report's line, and both cases — the value and the silence — are
|
|
307
|
+
// answered by the list `postureSourceLines` prints directly underneath, which
|
|
308
|
+
// marks the place that decided it.
|
|
309
|
+
if (!s.readFromMachine)
|
|
310
|
+
return `${s.variable} is unset here, which is the documented default`;
|
|
311
|
+
// A base URL is reported by hostname only (see ToolSearchSetting.value), so it
|
|
312
|
+
// is phrased as where the variable points and never as what it equals.
|
|
313
|
+
if (s.variable === 'ANTHROPIC_BASE_URL')
|
|
314
|
+
return `${s.variable} points at ${s.value} on this machine`;
|
|
315
|
+
return `${s.variable}=${s.value} on this machine`;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Which places this posture was read from, and what each one held.
|
|
319
|
+
*
|
|
320
|
+
* The sentence above this list states a verdict about tokens on somebody's
|
|
321
|
+
* machine, and the commonest of those verdicts — the documented default — is
|
|
322
|
+
* an argument from silence. Silence is only evidence across the places that
|
|
323
|
+
* were opened, so they are named: a reader who sets `ENABLE_TOOL_SEARCH` in a
|
|
324
|
+
* file this audit did not read can see that from the report instead of
|
|
325
|
+
* believing a default that does not apply to them.
|
|
326
|
+
*/
|
|
327
|
+
function postureSourceLines(d) {
|
|
328
|
+
const recs = d.setting?.sources ?? [];
|
|
329
|
+
if (recs.length === 0)
|
|
330
|
+
return [];
|
|
331
|
+
const lines = [
|
|
332
|
+
' Where this was read — Claude Code takes these variables from the shell it',
|
|
333
|
+
' starts in and from the env block of its own settings files:',
|
|
334
|
+
];
|
|
335
|
+
let absent = 0;
|
|
336
|
+
for (const r of recs) {
|
|
337
|
+
if (r.state === 'absent') {
|
|
338
|
+
absent++;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
const held = r.state === 'unreadable'
|
|
342
|
+
? 'could not be read — what it sets is unknown'
|
|
343
|
+
: r.sets.length
|
|
344
|
+
? `sets ${r.sets.join(', ')}`
|
|
345
|
+
: 'sets none of them';
|
|
346
|
+
// Which place the verdict came out of, said once rather than left to a
|
|
347
|
+
// reader to work out from two lists.
|
|
348
|
+
const decided = d.setting?.source === r.source ? ', which decided this' : '';
|
|
349
|
+
lines.push(` ${sourceName(r.source)} — ${held}${decided}`);
|
|
350
|
+
}
|
|
351
|
+
if (absent > 0) {
|
|
352
|
+
lines.push(` ${absent} other settings file(s) it reads are not on this machine`);
|
|
353
|
+
}
|
|
354
|
+
if (!recs.some((r) => r.scope !== 'shell')) {
|
|
355
|
+
lines.push(" its settings files were NOT read here, so what they set is unknown");
|
|
356
|
+
}
|
|
357
|
+
return lines;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* The total the lines around this one are about was summed from a measurement
|
|
361
|
+
* that stood for more than one entry, so it is not a number to reason from.
|
|
362
|
+
*
|
|
363
|
+
* Printed in EVERY mode, not only the one that weighs the total against a
|
|
364
|
+
* threshold. The other modes state what these tokens cost just as plainly —
|
|
365
|
+
* `loads-upfront` says every request carries them, and there the number IS the
|
|
366
|
+
* whole cost claim — so a total this report will not stand behind must not be
|
|
367
|
+
* left uncaveated in any of them. `evaluateDeferral` additionally withholds
|
|
368
|
+
* `clientTokens` and `crosses`, which only threshold mode derives; the other
|
|
369
|
+
* modes derive nothing from the total, so this paragraph is the whole of the
|
|
370
|
+
* rule there.
|
|
371
|
+
*/
|
|
372
|
+
function sharedMeasurementLines(d, skippedNames, consequence) {
|
|
373
|
+
const lines = [
|
|
374
|
+
` How big this stack is cannot be said here: ${d.sharedMeasurements} of the servers above run`,
|
|
375
|
+
' the same command as another entry and differ only in the environment they',
|
|
376
|
+
' are given, so one launch was measured and its number counted for each of',
|
|
377
|
+
' them. Environment decides what a server serves, so that sum can be wrong in',
|
|
378
|
+
...consequence,
|
|
379
|
+
];
|
|
380
|
+
if (d.isFloor) {
|
|
381
|
+
lines.push(` ${skippedNames} server(s) here also produced no number — see "not measured" above.`);
|
|
382
|
+
}
|
|
383
|
+
return lines;
|
|
384
|
+
}
|
|
385
|
+
/** Where a threshold is in play, the unknown size is the whole verdict. */
|
|
386
|
+
const SIDE_UNKNOWN = [
|
|
387
|
+
' either direction — which side of the threshold this stack falls on cannot be',
|
|
388
|
+
' read off it. Measurements here are keyed by command line alone; nothing is',
|
|
389
|
+
' wrong with the config.',
|
|
390
|
+
];
|
|
391
|
+
/** Where none is, the size is simply not a figure to quote. */
|
|
392
|
+
const SIZE_UNKNOWN = [
|
|
393
|
+
' either direction — how many tokens this stack costs cannot be read off it.',
|
|
394
|
+
' Measurements here are keyed by command line alone; nothing is wrong with',
|
|
395
|
+
' the config.',
|
|
396
|
+
];
|
|
397
|
+
/**
|
|
398
|
+
* Whether the tokens above are paid up front, and — only where the client
|
|
399
|
+
* decides that by a threshold — which side of it this stack is on.
|
|
400
|
+
*
|
|
401
|
+
* Two things this must not do, both of which an earlier version did. It must
|
|
402
|
+
* not present the threshold as what decides the default case: Claude Code's
|
|
403
|
+
* default defers every MCP tool definition unconditionally, so a stack of any
|
|
404
|
+
* size is deferred and telling its owner they pay those tokens is wrong in the
|
|
405
|
+
* commonest case there is. And where a threshold does apply, it must not
|
|
406
|
+
* compare the audit's wire count against it as though they were the same unit;
|
|
407
|
+
* they differ by a factor this repository publishes and cannot narrow, so the
|
|
408
|
+
* answer is a range and sometimes an admission.
|
|
409
|
+
*/
|
|
410
|
+
function deferralLines(d, skippedNames) {
|
|
411
|
+
const lines = [];
|
|
412
|
+
if (d.sources.length > 1) {
|
|
413
|
+
lines.push(` These ${d.sources.length} config files are read into one ${d.client} session:`);
|
|
414
|
+
for (const s of d.sources)
|
|
415
|
+
lines.push(` ${s}`);
|
|
416
|
+
lines.push(' so they face the question below together, as their sum.');
|
|
417
|
+
}
|
|
418
|
+
if (d.mode === 'client-unknown') {
|
|
419
|
+
lines.push(' Which client reads this config is not known here, so whether it defers');
|
|
420
|
+
lines.push(' tool definitions by default is not known either. Read as loaded up front.');
|
|
421
|
+
if (d.sharedMeasurements > 0)
|
|
422
|
+
lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
|
|
423
|
+
return lines;
|
|
424
|
+
}
|
|
425
|
+
if (d.mode === 'no-deferral-on-record') {
|
|
426
|
+
lines.push(` No default deferral is on record for ${d.client}, so every request`);
|
|
427
|
+
lines.push(' carries these tokens before you type anything — an absence of a record');
|
|
428
|
+
lines.push(' about the client, not a measurement of it.');
|
|
429
|
+
if (d.sharedMeasurements > 0)
|
|
430
|
+
lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
|
|
431
|
+
return lines;
|
|
432
|
+
}
|
|
433
|
+
if (d.mode === 'setting-unrecognized') {
|
|
434
|
+
lines.push(` ${d.setting?.variable} is set to "${d.setting?.value}" on this machine, which is not`);
|
|
435
|
+
lines.push(' one of the values Claude Code documents (unset, true, false, auto, auto:N).');
|
|
436
|
+
lines.push(' Whether these tokens are deferred cannot be said from it.');
|
|
437
|
+
lines.push(...postureSourceLines(d));
|
|
438
|
+
if (d.sharedMeasurements > 0)
|
|
439
|
+
lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
|
|
440
|
+
return lines;
|
|
441
|
+
}
|
|
442
|
+
if (d.mode === 'setting-unresolved') {
|
|
443
|
+
if (d.setting?.unresolved === 'sources-disagree') {
|
|
444
|
+
lines.push(` ${d.setting.variable} is set to different values by more than one place this`);
|
|
445
|
+
lines.push(' machine reads it from, and which one Claude Code takes is not on record');
|
|
446
|
+
lines.push(' here. Whether these tokens are deferred cannot be said from them.');
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
lines.push(' A settings file Claude Code reads exists here and could not be read, so');
|
|
450
|
+
lines.push(' what it sets is unknown — and it can set the variable that decides this.');
|
|
451
|
+
lines.push(' Whether these tokens are deferred cannot be said from them.');
|
|
452
|
+
}
|
|
453
|
+
lines.push(...postureSourceLines(d));
|
|
454
|
+
if (d.sharedMeasurements > 0)
|
|
455
|
+
lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
|
|
456
|
+
return lines;
|
|
457
|
+
}
|
|
458
|
+
if (d.mode === 'loads-upfront') {
|
|
459
|
+
lines.push(` ${d.client} loads every tool definition up front here: ${d.mechanism} is off`);
|
|
460
|
+
lines.push(` because ${settingPhrase(d)}. Every request carries these`);
|
|
461
|
+
lines.push(' tokens before you type anything.');
|
|
462
|
+
lines.push(...postureSourceLines(d));
|
|
463
|
+
if (d.sharedMeasurements > 0)
|
|
464
|
+
lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
|
|
465
|
+
return lines;
|
|
466
|
+
}
|
|
467
|
+
if (d.mode === 'defers-all') {
|
|
468
|
+
lines.push(` ${d.client} defers every MCP tool definition (${d.mechanism}), with no threshold —`);
|
|
469
|
+
lines.push(` ${settingPhrase(d)}. These tokens are NOT loaded`);
|
|
470
|
+
lines.push(' up front at any size; they load when the model reaches for a tool. Size');
|
|
471
|
+
lines.push(' decides nothing here, so none of the arithmetic above changes the answer.');
|
|
472
|
+
lines.push(...postureSourceLines(d));
|
|
473
|
+
if (d.sharedMeasurements > 0)
|
|
474
|
+
lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
|
|
475
|
+
lines.push(' The full number is paid where deferral does not apply:');
|
|
476
|
+
for (const e of d.exceptions)
|
|
477
|
+
lines.push(` ${e}`);
|
|
478
|
+
return lines;
|
|
479
|
+
}
|
|
480
|
+
// Threshold mode: the only mode where how big this stack is matters at all.
|
|
481
|
+
const t = d.thresholdTokens ?? 0;
|
|
482
|
+
lines.push(` ${d.client} defers tool definitions above a threshold here (${d.mechanism}):`);
|
|
483
|
+
lines.push(` ${settingPhrase(d)}, so deferral activates once the`);
|
|
484
|
+
lines.push(` definitions reach ${n(t)} tokens — ${pct(d.thresholdShare ?? 0)} of the context window.`);
|
|
485
|
+
lines.push(...postureSourceLines(d));
|
|
486
|
+
const c = d.clientTokens;
|
|
487
|
+
if (!c) {
|
|
488
|
+
// No total was established, so there is no side to be on and no number to
|
|
489
|
+
// say it with. Both are withheld together: printing the sum here and only
|
|
490
|
+
// withholding the verdict would leave a figure a reader would compare
|
|
491
|
+
// against the threshold themselves.
|
|
492
|
+
lines.push(...sharedMeasurementLines(d, skippedNames, SIDE_UNKNOWN));
|
|
493
|
+
return lines;
|
|
494
|
+
}
|
|
495
|
+
const total = d.isFloor ? `at least ${n(d.wireTokens)}` : n(d.wireTokens);
|
|
496
|
+
if (c.estimated === 0) {
|
|
497
|
+
lines.push(` This stack is ${total} tokens on the wire, and ${n(c.low)} by the published`);
|
|
498
|
+
lines.push(` Anthropic counts for all ${c.exact} measured server(s) — which is the side the`);
|
|
499
|
+
lines.push(' threshold is counted on.');
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
lines.push(` This stack is ${total} tokens on the wire. The threshold is counted in what`);
|
|
503
|
+
lines.push(` the client sends to the API, which is a different number: across ${d.ratio.servers}`);
|
|
504
|
+
lines.push(` servers in ${d.ratio.source} the two differ by ${ratioBand(d)}, putting this stack`);
|
|
505
|
+
lines.push(` between ${n(c.low)} and ${n(c.high)} tokens on that side` +
|
|
506
|
+
(c.exact > 0 ? `, with ${c.exact} of them taken from published counts.` : '.'));
|
|
507
|
+
}
|
|
508
|
+
if (d.crosses === true) {
|
|
509
|
+
lines.push(` That is at or above the threshold — over by ${n(d.distanceTokens.low)} at the low end —`);
|
|
510
|
+
lines.push(' so these tokens are NOT loaded up front. The full number is still paid');
|
|
511
|
+
lines.push(' where deferral does not apply:');
|
|
512
|
+
for (const e of d.exceptions)
|
|
513
|
+
lines.push(` ${e}`);
|
|
514
|
+
}
|
|
515
|
+
else if (d.crosses === false) {
|
|
516
|
+
lines.push(` That is below the threshold — under by ${n(-d.distanceTokens.high)} at the high end —`);
|
|
517
|
+
lines.push(' so deferral does not activate and every request carries these tokens');
|
|
518
|
+
lines.push(' before you type anything.');
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
lines.push(' Which side this stack falls on cannot be said:');
|
|
522
|
+
if (c.low < t && c.high >= t) {
|
|
523
|
+
lines.push(` that range straddles the ${n(t)}-token threshold`);
|
|
524
|
+
}
|
|
525
|
+
if (d.isFloor) {
|
|
526
|
+
lines.push(` ${skippedNames} server(s) here produced no number, and what they serve`);
|
|
527
|
+
lines.push(' counts toward it too — see "not measured" above');
|
|
528
|
+
}
|
|
529
|
+
if (c.estimated > 0) {
|
|
530
|
+
lines.push(' run with --claude to replace the estimate with published Anthropic');
|
|
531
|
+
lines.push(' counts wherever a capture still matches');
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return lines;
|
|
535
|
+
}
|
|
536
|
+
/** "0.20×–1.92×", from whichever band the verdict was actually computed against. */
|
|
537
|
+
function ratioBand(d) {
|
|
538
|
+
const r = d.ratio ?? PUBLISHED_WIRE_TO_CLIENT_RATIO;
|
|
539
|
+
return `${r.low.toFixed(2)}×–${r.high.toFixed(2)}×`;
|
|
540
|
+
}
|
|
187
541
|
/** Human output. JSON output is the report object itself. */
|
|
188
542
|
export function formatReport(report) {
|
|
189
543
|
const lines = [];
|
|
190
544
|
lines.push(`mcp-context-cost audit · methodology ${report.methodologyVersion} · ${report.encoding} · context window ${n(report.contextWindow)}`);
|
|
191
545
|
const showClaude = !!report.claudeDivergence;
|
|
546
|
+
// Configs one session loads together share a verdict object; it answers for
|
|
547
|
+
// all of them at once, so it is printed under the first one and not repeated.
|
|
548
|
+
const verdictPrinted = new Set();
|
|
192
549
|
for (const cfg of report.configs) {
|
|
193
550
|
lines.push('');
|
|
194
551
|
lines.push(`${cfg.client} ${cfg.source}`);
|
|
@@ -213,8 +570,15 @@ export function formatReport(report) {
|
|
|
213
570
|
lines.push(` ${'─'.repeat(w.name + w.tools + w.tokens + 14 + (showClaude ? w.claude + 2 : 0))}`);
|
|
214
571
|
lines.push(line('total', String(cfg.toolCount), n(cfg.totalTokens), '', ''));
|
|
215
572
|
lines.push('');
|
|
216
|
-
lines.push(
|
|
217
|
-
|
|
573
|
+
lines.push(measurementLine(cfg, report.contextWindow));
|
|
574
|
+
if (!verdictPrinted.has(cfg.deferral)) {
|
|
575
|
+
verdictPrinted.add(cfg.deferral);
|
|
576
|
+
const skippedInScope = report.configs
|
|
577
|
+
.filter((c) => c.deferral === cfg.deferral)
|
|
578
|
+
.reduce((a, c) => a + c.skipped.length, 0);
|
|
579
|
+
for (const line of deferralLines(cfg.deferral, skippedInScope))
|
|
580
|
+
lines.push(line);
|
|
581
|
+
}
|
|
218
582
|
if (cfg.heaviestTools.length) {
|
|
219
583
|
lines.push('');
|
|
220
584
|
lines.push(' heaviest tools');
|
|
@@ -300,7 +664,8 @@ export function formatReport(report) {
|
|
|
300
664
|
}
|
|
301
665
|
lines.push('');
|
|
302
666
|
lines.push('These are wire tokens — what the server puts on the wire, counted with o200k_base. What your model is billed');
|
|
303
|
-
lines.push(
|
|
667
|
+
lines.push(`differs per provider: measured ratios run ${PUBLISHED_WIRE_TO_CLIENT_RATIO.low.toFixed(2)}×–` +
|
|
668
|
+
`${PUBLISHED_WIRE_TO_CLIENT_RATIO.high.toFixed(2)}× on Anthropic requests. See docs/METHODOLOGY.md §claude-divergence.`);
|
|
304
669
|
return lines.map((l) => l.replace(/\s+$/, '')).join('\n');
|
|
305
670
|
}
|
|
306
671
|
/** Top-level tool list across every config — used by nothing yet, handy for --json consumers. */
|
package/dist/audit/config.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ToolSearchScope, type ToolSearchSource } from './deferral.js';
|
|
1
2
|
export interface ConfiguredServer {
|
|
2
3
|
name: string;
|
|
3
4
|
/** Which client's config this came from ('claude-desktop', 'cursor', ...). */
|
|
@@ -51,3 +52,40 @@ export interface LoadedConfig {
|
|
|
51
52
|
}
|
|
52
53
|
/** Read + parse the candidates that exist. Unreadable files are reported, not thrown. */
|
|
53
54
|
export declare function loadConfigs(candidates: ConfigCandidate[], cwd: string): LoadedConfig[];
|
|
55
|
+
/** One file Claude Code reads its `env` block from. */
|
|
56
|
+
export interface SettingsCandidate {
|
|
57
|
+
scope: ToolSearchScope;
|
|
58
|
+
path: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Every settings file Claude Code takes environment variables from, highest
|
|
62
|
+
* precedence first.
|
|
63
|
+
*
|
|
64
|
+
* A client config says which servers there are; these say how the client
|
|
65
|
+
* behaves — including whether it defers their tool definitions. They are a
|
|
66
|
+
* different set of files from `configCandidates` above and are read for a
|
|
67
|
+
* different question, so they are listed separately rather than folded in.
|
|
68
|
+
*
|
|
69
|
+
* Order is Claude Code's documented settings precedence (enterprise managed
|
|
70
|
+
* policy, then project-local, then project, then user), read 2026-08-20. Paths
|
|
71
|
+
* in, candidates out — nothing here touches a disk.
|
|
72
|
+
*/
|
|
73
|
+
export declare function settingsCandidates(env: {
|
|
74
|
+
home: string;
|
|
75
|
+
cwd: string;
|
|
76
|
+
platform: NodeJS.Platform;
|
|
77
|
+
programData?: string;
|
|
78
|
+
}): SettingsCandidate[];
|
|
79
|
+
/**
|
|
80
|
+
* Read what each settings file sets, of the variables that decide deferral.
|
|
81
|
+
*
|
|
82
|
+
* Every candidate comes back, present or not, because "this file is not on the
|
|
83
|
+
* machine" and "this file was never opened" are different answers to the
|
|
84
|
+
* question the report asks, and only one of them means the default stands.
|
|
85
|
+
*
|
|
86
|
+
* Only the three tool-search variables are picked out; the rest of an `env`
|
|
87
|
+
* block — which is where a person keeps their API keys — is left where it is.
|
|
88
|
+
* A file that exists but cannot be parsed is `unreadable`, never silently
|
|
89
|
+
* treated as setting nothing.
|
|
90
|
+
*/
|
|
91
|
+
export declare function loadSettingsSources(candidates: SettingsCandidate[]): ToolSearchSource[];
|
package/dist/audit/config.js
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import { existsSync, readFileSync } from 'node:fs';
|
|
20
20
|
import { join } from 'node:path';
|
|
21
|
+
import { TOOL_SEARCH_VARS, } from './deferral.js';
|
|
21
22
|
/**
|
|
22
23
|
* JSON with comments and trailing commas — VS Code's mcp.json allows both, and
|
|
23
24
|
* hand-edited Claude/Cursor configs often pick up a trailing comma too. String
|
|
@@ -177,3 +178,66 @@ export function loadConfigs(candidates, cwd) {
|
|
|
177
178
|
}
|
|
178
179
|
return out;
|
|
179
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Every settings file Claude Code takes environment variables from, highest
|
|
183
|
+
* precedence first.
|
|
184
|
+
*
|
|
185
|
+
* A client config says which servers there are; these say how the client
|
|
186
|
+
* behaves — including whether it defers their tool definitions. They are a
|
|
187
|
+
* different set of files from `configCandidates` above and are read for a
|
|
188
|
+
* different question, so they are listed separately rather than folded in.
|
|
189
|
+
*
|
|
190
|
+
* Order is Claude Code's documented settings precedence (enterprise managed
|
|
191
|
+
* policy, then project-local, then project, then user), read 2026-08-20. Paths
|
|
192
|
+
* in, candidates out — nothing here touches a disk.
|
|
193
|
+
*/
|
|
194
|
+
export function settingsCandidates(env) {
|
|
195
|
+
const { home, cwd, platform } = env;
|
|
196
|
+
const managed = platform === 'darwin'
|
|
197
|
+
? '/Library/Application Support/ClaudeCode/managed-settings.json'
|
|
198
|
+
: platform === 'win32'
|
|
199
|
+
? join(env.programData ?? 'C:\\ProgramData', 'ClaudeCode', 'managed-settings.json')
|
|
200
|
+
: '/etc/claude-code/managed-settings.json';
|
|
201
|
+
return [
|
|
202
|
+
{ scope: 'managed-settings', path: managed },
|
|
203
|
+
{ scope: 'local-settings', path: join(cwd, '.claude', 'settings.local.json') },
|
|
204
|
+
{ scope: 'project-settings', path: join(cwd, '.claude', 'settings.json') },
|
|
205
|
+
{ scope: 'user-settings', path: join(home, '.claude', 'settings.json') },
|
|
206
|
+
];
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Read what each settings file sets, of the variables that decide deferral.
|
|
210
|
+
*
|
|
211
|
+
* Every candidate comes back, present or not, because "this file is not on the
|
|
212
|
+
* machine" and "this file was never opened" are different answers to the
|
|
213
|
+
* question the report asks, and only one of them means the default stands.
|
|
214
|
+
*
|
|
215
|
+
* Only the three tool-search variables are picked out; the rest of an `env`
|
|
216
|
+
* block — which is where a person keeps their API keys — is left where it is.
|
|
217
|
+
* A file that exists but cannot be parsed is `unreadable`, never silently
|
|
218
|
+
* treated as setting nothing.
|
|
219
|
+
*/
|
|
220
|
+
export function loadSettingsSources(candidates) {
|
|
221
|
+
return candidates.map((c) => {
|
|
222
|
+
if (!existsSync(c.path))
|
|
223
|
+
return { scope: c.scope, source: c.path, state: 'absent', vars: {} };
|
|
224
|
+
try {
|
|
225
|
+
const doc = parseJsonc(readFileSync(c.path, 'utf8'));
|
|
226
|
+
if (!doc || typeof doc !== 'object' || Array.isArray(doc))
|
|
227
|
+
throw new Error('not a settings object');
|
|
228
|
+
const block = doc.env;
|
|
229
|
+
const vars = {};
|
|
230
|
+
if (block && typeof block === 'object' && !Array.isArray(block)) {
|
|
231
|
+
for (const name of TOOL_SEARCH_VARS) {
|
|
232
|
+
const v = block[name];
|
|
233
|
+
if (typeof v === 'string')
|
|
234
|
+
vars[name] = v;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return { scope: c.scope, source: c.path, state: 'read', vars };
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return { scope: c.scope, source: c.path, state: 'unreadable', vars: {} };
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|