mcp-context-cost 0.4.0 → 0.6.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.
Files changed (41) hide show
  1. package/README.md +101 -33
  2. package/dist/audit/audit.d.ts +48 -0
  3. package/dist/audit/audit.js +392 -7
  4. package/dist/audit/config.d.ts +46 -0
  5. package/dist/audit/config.js +86 -2
  6. package/dist/audit/deferral.d.ts +366 -0
  7. package/dist/audit/deferral.js +403 -0
  8. package/dist/audit/run.d.ts +22 -0
  9. package/dist/audit/run.js +17 -1
  10. package/dist/cli.js +11 -0
  11. package/dist/core/adoption.d.ts +226 -0
  12. package/dist/core/adoption.js +432 -0
  13. package/dist/core/canonical.d.ts +6 -0
  14. package/dist/core/canonical.js +3 -0
  15. package/dist/core/index.d.ts +1 -0
  16. package/dist/core/index.js +1 -0
  17. package/dist/core/session-start.d.ts +102 -0
  18. package/dist/core/session-start.js +186 -0
  19. package/dist/core/types.d.ts +8 -0
  20. package/dist/sweep/client.d.ts +6 -1
  21. package/dist/sweep/client.js +1 -0
  22. package/dist/sweep/dashboard.d.ts +10 -0
  23. package/dist/sweep/dashboard.js +32 -16
  24. package/dist/sweep/docker.d.ts +23 -0
  25. package/dist/sweep/docker.js +16 -12
  26. package/dist/sweep/harness-guard.d.ts +57 -0
  27. package/dist/sweep/harness-guard.js +144 -0
  28. package/dist/sweep/history.d.ts +33 -1
  29. package/dist/sweep/history.js +60 -5
  30. package/dist/sweep/regen.js +6 -1
  31. package/dist/sweep/report.d.ts +16 -0
  32. package/dist/sweep/report.js +79 -5
  33. package/dist/sweep/run.d.ts +41 -0
  34. package/dist/sweep/run.js +129 -36
  35. package/dist/sweep/server-pages.js +31 -6
  36. package/dist/sweep/session-start.d.ts +3 -0
  37. package/dist/sweep/session-start.js +103 -0
  38. package/dist/sweep/shard.d.ts +41 -0
  39. package/dist/sweep/shard.js +58 -0
  40. package/dist/sweep/sweep-all.js +56 -2
  41. package/package.json +3 -1
@@ -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,15 +166,29 @@ 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 results = [];
169
+ const emptyConfigs = [];
170
+ const built = [];
171
+ // Across every config at once: a twin in one client's file is measured for
172
+ // the other client's entry just the same.
173
+ const collapsed = collapsedKeys(configs);
174
+ const shared = new Map();
78
175
  for (const cfg of configs) {
79
176
  if (cfg.error) {
80
177
  problems.push(`${cfg.source}: ${cfg.error}`);
81
178
  continue;
82
179
  }
180
+ // Parsed, and declares nothing. Not a problem and not a report line: it is
181
+ // recorded as itself, so a reader is told what this machine actually has.
182
+ if (cfg.declaresNothing || cfg.servers.length === 0) {
183
+ emptyConfigs.push({ client: cfg.client, source: cfg.source });
184
+ continue;
185
+ }
83
186
  const ok = [];
84
187
  const skipped = [];
85
188
  const tools = [];
189
+ // Counted only for servers that put a number into the total: a twin that
190
+ // failed to launch is already a floor, and adds nothing to a sum.
191
+ let sharedHere = 0;
86
192
  for (const s of cfg.servers) {
87
193
  const base = {
88
194
  name: s.name,
@@ -118,6 +224,8 @@ export function buildReport(configs, measured, opts = {}) {
118
224
  });
119
225
  continue;
120
226
  }
227
+ if (collapsed.has(serverKey(s)))
228
+ sharedHere++;
121
229
  const divRow = opts.divergence?.servers[s.name];
122
230
  ok.push({
123
231
  ...base,
@@ -138,7 +246,7 @@ export function buildReport(configs, measured, opts = {}) {
138
246
  for (const s of ok)
139
247
  s.share = totalTokens > 0 ? (s.tokens ?? 0) / totalTokens : 0;
140
248
  tools.sort((a, b) => b.tokens - a.tokens);
141
- results.push({
249
+ const result = {
142
250
  client: cfg.client,
143
251
  source: cfg.source,
144
252
  totalTokens,
@@ -153,15 +261,19 @@ export function buildReport(configs, measured, opts = {}) {
153
261
  skipped,
154
262
  heaviestTools: tools.slice(0, 5),
155
263
  trimAdvice: buildTrimAdvice(tools, totalTokens),
156
- });
264
+ };
265
+ built.push(result);
266
+ shared.set(result, sharedHere);
157
267
  }
158
- results.sort((a, b) => b.totalTokens - a.totalTokens);
268
+ built.sort((a, b) => b.totalTokens - a.totalTokens);
269
+ const results = attachDeferral(built, contextWindow, { ...opts, shared });
159
270
  const report = {
160
271
  methodologyVersion: METHODOLOGY_VERSION,
161
272
  encoding: 'o200k_base',
162
273
  generatedAt: opts.generatedAt ?? new Date().toISOString(),
163
274
  contextWindow,
164
275
  configs: results,
276
+ emptyConfigs,
165
277
  problems,
166
278
  };
167
279
  if (opts.divergence) {
@@ -184,11 +296,276 @@ export function buildReport(configs, measured, opts = {}) {
184
296
  }
185
297
  const n = (x) => x.toLocaleString('en-US');
186
298
  const pct = (x) => `${(x * 100).toFixed(1)}%`;
299
+ /** The measurement itself — unconditional, and separate from any claim about who pays it. */
300
+ function measurementLine(cfg, contextWindow) {
301
+ return (` ${n(cfg.totalTokens)} tokens of tool schemas — ${pct(cfg.contextShare)} of a ` +
302
+ `${n(contextWindow)}-token context window.`);
303
+ }
304
+ /** How a place is named in a sentence; the shell has a path-shaped stand-in in JSON. */
305
+ function sourceName(source) {
306
+ return source === SHELL_SOURCE ? 'this shell' : source;
307
+ }
308
+ /** "ENABLE_TOOL_SEARCH=auto on this machine" / "unset here, the documented default". */
309
+ function settingPhrase(d) {
310
+ const s = d.setting;
311
+ if (!s || !s.variable)
312
+ return 'by default';
313
+ // Which place it was read in is not spelled here: a settings path is longer
314
+ // than this report's line, and both cases — the value and the silence — are
315
+ // answered by the list `postureSourceLines` prints directly underneath, which
316
+ // marks the place that decided it.
317
+ if (!s.readFromMachine)
318
+ return `${s.variable} is unset here, which is the documented default`;
319
+ // A base URL is reported by hostname only (see ToolSearchSetting.value), so it
320
+ // is phrased as where the variable points and never as what it equals.
321
+ if (s.variable === 'ANTHROPIC_BASE_URL')
322
+ return `${s.variable} points at ${s.value} on this machine`;
323
+ return `${s.variable}=${s.value} on this machine`;
324
+ }
325
+ /**
326
+ * Which places this posture was read from, and what each one held.
327
+ *
328
+ * The sentence above this list states a verdict about tokens on somebody's
329
+ * machine, and the commonest of those verdicts — the documented default — is
330
+ * an argument from silence. Silence is only evidence across the places that
331
+ * were opened, so they are named: a reader who sets `ENABLE_TOOL_SEARCH` in a
332
+ * file this audit did not read can see that from the report instead of
333
+ * believing a default that does not apply to them.
334
+ */
335
+ function postureSourceLines(d) {
336
+ const recs = d.setting?.sources ?? [];
337
+ if (recs.length === 0)
338
+ return [];
339
+ const lines = [
340
+ ' Where this was read — Claude Code takes these variables from the shell it',
341
+ ' starts in and from the env block of its own settings files:',
342
+ ];
343
+ let absent = 0;
344
+ for (const r of recs) {
345
+ if (r.state === 'absent') {
346
+ absent++;
347
+ continue;
348
+ }
349
+ // A place that sets one of them to something unreadable is neither a place
350
+ // that sets it nor a place that sets none of them, and printing it as the
351
+ // second is the silence this report would then argue from.
352
+ const unreadableVars = r.unreadable?.length
353
+ ? `sets ${r.unreadable.join(', ')} to a value this cannot read`
354
+ : '';
355
+ const held = r.state === 'unreadable'
356
+ ? 'could not be read — what it sets is unknown'
357
+ : [r.sets.length ? `sets ${r.sets.join(', ')}` : '', unreadableVars].filter(Boolean).join(', and ') ||
358
+ 'sets none of them';
359
+ // Which place the verdict came out of, said once rather than left to a
360
+ // reader to work out from two lists.
361
+ const decided = d.setting?.source === r.source ? ', which decided this' : '';
362
+ lines.push(` ${sourceName(r.source)} — ${held}${decided}`);
363
+ }
364
+ if (absent > 0) {
365
+ lines.push(` ${absent} other settings file(s) it reads are not on this machine`);
366
+ }
367
+ if (!recs.some((r) => r.scope !== 'shell')) {
368
+ lines.push(" its settings files were NOT read here, so what they set is unknown");
369
+ }
370
+ return lines;
371
+ }
372
+ /**
373
+ * The total the lines around this one are about was summed from a measurement
374
+ * that stood for more than one entry, so it is not a number to reason from.
375
+ *
376
+ * Printed in EVERY mode, not only the one that weighs the total against a
377
+ * threshold. The other modes state what these tokens cost just as plainly —
378
+ * `loads-upfront` says every request carries them, and there the number IS the
379
+ * whole cost claim — so a total this report will not stand behind must not be
380
+ * left uncaveated in any of them. `evaluateDeferral` additionally withholds
381
+ * `clientTokens` and `crosses`, which only threshold mode derives; the other
382
+ * modes derive nothing from the total, so this paragraph is the whole of the
383
+ * rule there.
384
+ */
385
+ function sharedMeasurementLines(d, skippedNames, consequence) {
386
+ const lines = [
387
+ ` How big this stack is cannot be said here: ${d.sharedMeasurements} of the servers above run`,
388
+ ' the same command as another entry and differ only in the environment they',
389
+ ' are given, so one launch was measured and its number counted for each of',
390
+ ' them. Environment decides what a server serves, so that sum can be wrong in',
391
+ ...consequence,
392
+ ];
393
+ if (d.isFloor) {
394
+ lines.push(` ${skippedNames} server(s) here also produced no number — see "not measured" above.`);
395
+ }
396
+ return lines;
397
+ }
398
+ /** Where a threshold is in play, the unknown size is the whole verdict. */
399
+ const SIDE_UNKNOWN = [
400
+ ' either direction — which side of the threshold this stack falls on cannot be',
401
+ ' read off it. Measurements here are keyed by command line alone; nothing is',
402
+ ' wrong with the config.',
403
+ ];
404
+ /** Where none is, the size is simply not a figure to quote. */
405
+ const SIZE_UNKNOWN = [
406
+ ' either direction — how many tokens this stack costs cannot be read off it.',
407
+ ' Measurements here are keyed by command line alone; nothing is wrong with',
408
+ ' the config.',
409
+ ];
410
+ /**
411
+ * Whether the tokens above are paid up front, and — only where the client
412
+ * decides that by a threshold — which side of it this stack is on.
413
+ *
414
+ * Two things this must not do, both of which an earlier version did. It must
415
+ * not present the threshold as what decides the default case: Claude Code's
416
+ * default defers every MCP tool definition unconditionally, so a stack of any
417
+ * size is deferred and telling its owner they pay those tokens is wrong in the
418
+ * commonest case there is. And where a threshold does apply, it must not
419
+ * compare the audit's wire count against it as though they were the same unit;
420
+ * they differ by a factor this repository publishes and cannot narrow, so the
421
+ * answer is a range and sometimes an admission.
422
+ */
423
+ function deferralLines(d, skippedNames) {
424
+ const lines = [];
425
+ if (d.sources.length > 1) {
426
+ lines.push(` These ${d.sources.length} config files are read into one ${d.client} session:`);
427
+ for (const s of d.sources)
428
+ lines.push(` ${s}`);
429
+ lines.push(' so they face the question below together, as their sum.');
430
+ }
431
+ if (d.mode === 'client-unknown') {
432
+ lines.push(' Which client reads this config is not known here, so whether it defers');
433
+ lines.push(' tool definitions by default is not known either. Read as loaded up front.');
434
+ if (d.sharedMeasurements > 0)
435
+ lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
436
+ return lines;
437
+ }
438
+ if (d.mode === 'no-deferral-on-record') {
439
+ lines.push(` No default deferral is on record for ${d.client}, so every request`);
440
+ lines.push(' carries these tokens before you type anything — an absence of a record');
441
+ lines.push(' about the client, not a measurement of it.');
442
+ if (d.sharedMeasurements > 0)
443
+ lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
444
+ return lines;
445
+ }
446
+ if (d.mode === 'setting-unrecognized') {
447
+ lines.push(` ${d.setting?.variable} is set to "${d.setting?.value}" on this machine, which is not`);
448
+ lines.push(' one of the values Claude Code documents (unset, true, false, auto, auto:N).');
449
+ lines.push(' Whether these tokens are deferred cannot be said from it.');
450
+ lines.push(...postureSourceLines(d));
451
+ if (d.sharedMeasurements > 0)
452
+ lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
453
+ return lines;
454
+ }
455
+ if (d.mode === 'setting-unresolved') {
456
+ if (d.setting?.unresolved === 'sources-disagree') {
457
+ lines.push(` ${d.setting.variable} is set to different values by more than one place this`);
458
+ lines.push(' machine reads it from, and which one Claude Code takes is not on record');
459
+ lines.push(' here. Whether these tokens are deferred cannot be said from them.');
460
+ }
461
+ else if (d.setting?.unresolved === 'value-unreadable') {
462
+ lines.push(` ${d.setting.variable} is set by a settings file Claude Code reads, to`);
463
+ lines.push(' something that is not a value this can read — an env block holds it as a');
464
+ lines.push(' JSON boolean, a number or null rather than a string. It is set there, and');
465
+ lines.push(' what it is set to is unknown, so whether these tokens are deferred cannot');
466
+ lines.push(' be said from it.');
467
+ }
468
+ else {
469
+ lines.push(' A settings file Claude Code reads exists here and could not be read, so');
470
+ lines.push(' what it sets is unknown — and it can set the variable that decides this.');
471
+ lines.push(' Whether these tokens are deferred cannot be said from them.');
472
+ }
473
+ lines.push(...postureSourceLines(d));
474
+ if (d.sharedMeasurements > 0)
475
+ lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
476
+ return lines;
477
+ }
478
+ if (d.mode === 'loads-upfront') {
479
+ lines.push(` ${d.client} loads every tool definition up front here: ${d.mechanism} is off`);
480
+ lines.push(` because ${settingPhrase(d)}. Every request carries these`);
481
+ lines.push(' tokens before you type anything.');
482
+ lines.push(...postureSourceLines(d));
483
+ if (d.sharedMeasurements > 0)
484
+ lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
485
+ return lines;
486
+ }
487
+ if (d.mode === 'defers-all') {
488
+ lines.push(` ${d.client} defers every MCP tool definition (${d.mechanism}), with no threshold —`);
489
+ lines.push(` ${settingPhrase(d)}. These tokens are NOT loaded`);
490
+ lines.push(' up front at any size; they load when the model reaches for a tool. Size');
491
+ lines.push(' decides nothing here, so none of the arithmetic above changes the answer.');
492
+ lines.push(...postureSourceLines(d));
493
+ if (d.sharedMeasurements > 0)
494
+ lines.push(...sharedMeasurementLines(d, skippedNames, SIZE_UNKNOWN));
495
+ lines.push(' The full number is paid where deferral does not apply:');
496
+ for (const e of d.exceptions)
497
+ lines.push(` ${e}`);
498
+ return lines;
499
+ }
500
+ // Threshold mode: the only mode where how big this stack is matters at all.
501
+ const t = d.thresholdTokens ?? 0;
502
+ lines.push(` ${d.client} defers tool definitions above a threshold here (${d.mechanism}):`);
503
+ lines.push(` ${settingPhrase(d)}, so deferral activates once the`);
504
+ lines.push(` definitions reach ${n(t)} tokens — ${pct(d.thresholdShare ?? 0)} of the context window.`);
505
+ lines.push(...postureSourceLines(d));
506
+ const c = d.clientTokens;
507
+ if (!c) {
508
+ // No total was established, so there is no side to be on and no number to
509
+ // say it with. Both are withheld together: printing the sum here and only
510
+ // withholding the verdict would leave a figure a reader would compare
511
+ // against the threshold themselves.
512
+ lines.push(...sharedMeasurementLines(d, skippedNames, SIDE_UNKNOWN));
513
+ return lines;
514
+ }
515
+ const total = d.isFloor ? `at least ${n(d.wireTokens)}` : n(d.wireTokens);
516
+ if (c.estimated === 0) {
517
+ lines.push(` This stack is ${total} tokens on the wire, and ${n(c.low)} by the published`);
518
+ lines.push(` Anthropic counts for all ${c.exact} measured server(s) — which is the side the`);
519
+ lines.push(' threshold is counted on.');
520
+ }
521
+ else {
522
+ lines.push(` This stack is ${total} tokens on the wire. The threshold is counted in what`);
523
+ lines.push(` the client sends to the API, which is a different number: across ${d.ratio.servers}`);
524
+ lines.push(` servers in ${d.ratio.source} the two differ by ${ratioBand(d)}, putting this stack`);
525
+ lines.push(` between ${n(c.low)} and ${n(c.high)} tokens on that side` +
526
+ (c.exact > 0 ? `, with ${c.exact} of them taken from published counts.` : '.'));
527
+ }
528
+ if (d.crosses === true) {
529
+ lines.push(` That is at or above the threshold — over by ${n(d.distanceTokens.low)} at the low end —`);
530
+ lines.push(' so these tokens are NOT loaded up front. The full number is still paid');
531
+ lines.push(' where deferral does not apply:');
532
+ for (const e of d.exceptions)
533
+ lines.push(` ${e}`);
534
+ }
535
+ else if (d.crosses === false) {
536
+ lines.push(` That is below the threshold — under by ${n(-d.distanceTokens.high)} at the high end —`);
537
+ lines.push(' so deferral does not activate and every request carries these tokens');
538
+ lines.push(' before you type anything.');
539
+ }
540
+ else {
541
+ lines.push(' Which side this stack falls on cannot be said:');
542
+ if (c.low < t && c.high >= t) {
543
+ lines.push(` that range straddles the ${n(t)}-token threshold`);
544
+ }
545
+ if (d.isFloor) {
546
+ lines.push(` ${skippedNames} server(s) here produced no number, and what they serve`);
547
+ lines.push(' counts toward it too — see "not measured" above');
548
+ }
549
+ if (c.estimated > 0) {
550
+ lines.push(' run with --claude to replace the estimate with published Anthropic');
551
+ lines.push(' counts wherever a capture still matches');
552
+ }
553
+ }
554
+ return lines;
555
+ }
556
+ /** "0.20×–1.92×", from whichever band the verdict was actually computed against. */
557
+ function ratioBand(d) {
558
+ const r = d.ratio ?? PUBLISHED_WIRE_TO_CLIENT_RATIO;
559
+ return `${r.low.toFixed(2)}×–${r.high.toFixed(2)}×`;
560
+ }
187
561
  /** Human output. JSON output is the report object itself. */
188
562
  export function formatReport(report) {
189
563
  const lines = [];
190
564
  lines.push(`mcp-context-cost audit · methodology ${report.methodologyVersion} · ${report.encoding} · context window ${n(report.contextWindow)}`);
191
565
  const showClaude = !!report.claudeDivergence;
566
+ // Configs one session loads together share a verdict object; it answers for
567
+ // all of them at once, so it is printed under the first one and not repeated.
568
+ const verdictPrinted = new Set();
192
569
  for (const cfg of report.configs) {
193
570
  lines.push('');
194
571
  lines.push(`${cfg.client} ${cfg.source}`);
@@ -213,8 +590,15 @@ export function formatReport(report) {
213
590
  lines.push(` ${'─'.repeat(w.name + w.tools + w.tokens + 14 + (showClaude ? w.claude + 2 : 0))}`);
214
591
  lines.push(line('total', String(cfg.toolCount), n(cfg.totalTokens), '', ''));
215
592
  lines.push('');
216
- lines.push(` Every request in this client carries ${n(cfg.totalTokens)} tokens of tool schemas — ` +
217
- `${pct(cfg.contextShare)} of a ${n(report.contextWindow)}-token context window, before you type anything.`);
593
+ lines.push(measurementLine(cfg, report.contextWindow));
594
+ if (!verdictPrinted.has(cfg.deferral)) {
595
+ verdictPrinted.add(cfg.deferral);
596
+ const skippedInScope = report.configs
597
+ .filter((c) => c.deferral === cfg.deferral)
598
+ .reduce((a, c) => a + c.skipped.length, 0);
599
+ for (const line of deferralLines(cfg.deferral, skippedInScope))
600
+ lines.push(line);
601
+ }
218
602
  if (cfg.heaviestTools.length) {
219
603
  lines.push('');
220
604
  lines.push(' heaviest tools');
@@ -300,7 +684,8 @@ export function formatReport(report) {
300
684
  }
301
685
  lines.push('');
302
686
  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('differs per provider: measured ratios run 0.34×–1.92× on Anthropic requests. See docs/METHODOLOGY.md §claude-divergence.');
687
+ lines.push(`differs per provider: measured ratios run ${PUBLISHED_WIRE_TO_CLIENT_RATIO.low.toFixed(2)}×–` +
688
+ `${PUBLISHED_WIRE_TO_CLIENT_RATIO.high.toFixed(2)}× on Anthropic requests. See docs/METHODOLOGY.md §claude-divergence.`);
304
689
  return lines.map((l) => l.replace(/\s+$/, '')).join('\n');
305
690
  }
306
691
  /** Top-level tool list across every config — used by nothing yet, handy for --json consumers. */
@@ -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', ...). */
@@ -48,6 +49,51 @@ export interface LoadedConfig {
48
49
  servers: ConfiguredServer[];
49
50
  /** Set when the file exists but could not be read/parsed. */
50
51
  error?: string;
52
+ /**
53
+ * Set when the file was read and parsed cleanly and declares no servers at
54
+ * all. Such a config has nothing to total, but "this client declares nothing"
55
+ * and "no client is installed here" are different facts about a machine and
56
+ * only one of them is about the client, so the second is not reported as the
57
+ * first.
58
+ */
59
+ declaresNothing?: true;
51
60
  }
52
61
  /** Read + parse the candidates that exist. Unreadable files are reported, not thrown. */
53
62
  export declare function loadConfigs(candidates: ConfigCandidate[], cwd: string): LoadedConfig[];
63
+ /** One file Claude Code reads its `env` block from. */
64
+ export interface SettingsCandidate {
65
+ scope: ToolSearchScope;
66
+ path: string;
67
+ }
68
+ /**
69
+ * Every settings file Claude Code takes environment variables from, highest
70
+ * precedence first.
71
+ *
72
+ * A client config says which servers there are; these say how the client
73
+ * behaves — including whether it defers their tool definitions. They are a
74
+ * different set of files from `configCandidates` above and are read for a
75
+ * different question, so they are listed separately rather than folded in.
76
+ *
77
+ * Order is Claude Code's documented settings precedence (enterprise managed
78
+ * policy, then project-local, then project, then user), read 2026-08-20. Paths
79
+ * in, candidates out — nothing here touches a disk.
80
+ */
81
+ export declare function settingsCandidates(env: {
82
+ home: string;
83
+ cwd: string;
84
+ platform: NodeJS.Platform;
85
+ programData?: string;
86
+ }): SettingsCandidate[];
87
+ /**
88
+ * Read what each settings file sets, of the variables that decide deferral.
89
+ *
90
+ * Every candidate comes back, present or not, because "this file is not on the
91
+ * machine" and "this file was never opened" are different answers to the
92
+ * question the report asks, and only one of them means the default stands.
93
+ *
94
+ * Only the three tool-search variables are picked out; the rest of an `env`
95
+ * block — which is where a person keeps their API keys — is left where it is.
96
+ * A file that exists but cannot be parsed is `unreadable`, never silently
97
+ * treated as setting nothing.
98
+ */
99
+ export declare function loadSettingsSources(candidates: SettingsCandidate[]): ToolSearchSource[];
@@ -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
@@ -166,9 +167,13 @@ export function loadConfigs(candidates, cwd) {
166
167
  const doc = parseJsonc(readFileSync(c.path, 'utf8'));
167
168
  const servers = extractServers(doc, { client: c.client, source: c.path, cwd });
168
169
  // A config with no MCP block at all (e.g. a ~/.claude.json holding only
169
- // session history) is not worth a line in the report.
170
- if (servers.length === 0)
170
+ // session history) is not worth a line in the report — it has no total.
171
+ // It is still worth carrying: it is the evidence that a client is on this
172
+ // machine, which is what tells an empty client apart from no client.
173
+ if (servers.length === 0) {
174
+ out.push({ client: c.client, source: c.path, servers: [], declaresNothing: true });
171
175
  continue;
176
+ }
172
177
  out.push({ client: c.client, source: c.path, servers });
173
178
  }
174
179
  catch (e) {
@@ -177,3 +182,82 @@ export function loadConfigs(candidates, cwd) {
177
182
  }
178
183
  return out;
179
184
  }
185
+ /**
186
+ * Every settings file Claude Code takes environment variables from, highest
187
+ * precedence first.
188
+ *
189
+ * A client config says which servers there are; these say how the client
190
+ * behaves — including whether it defers their tool definitions. They are a
191
+ * different set of files from `configCandidates` above and are read for a
192
+ * different question, so they are listed separately rather than folded in.
193
+ *
194
+ * Order is Claude Code's documented settings precedence (enterprise managed
195
+ * policy, then project-local, then project, then user), read 2026-08-20. Paths
196
+ * in, candidates out — nothing here touches a disk.
197
+ */
198
+ export function settingsCandidates(env) {
199
+ const { home, cwd, platform } = env;
200
+ const managed = platform === 'darwin'
201
+ ? '/Library/Application Support/ClaudeCode/managed-settings.json'
202
+ : platform === 'win32'
203
+ ? join(env.programData ?? 'C:\\ProgramData', 'ClaudeCode', 'managed-settings.json')
204
+ : '/etc/claude-code/managed-settings.json';
205
+ return [
206
+ { scope: 'managed-settings', path: managed },
207
+ { scope: 'local-settings', path: join(cwd, '.claude', 'settings.local.json') },
208
+ { scope: 'project-settings', path: join(cwd, '.claude', 'settings.json') },
209
+ { scope: 'user-settings', path: join(home, '.claude', 'settings.json') },
210
+ ];
211
+ }
212
+ /**
213
+ * Read what each settings file sets, of the variables that decide deferral.
214
+ *
215
+ * Every candidate comes back, present or not, because "this file is not on the
216
+ * machine" and "this file was never opened" are different answers to the
217
+ * question the report asks, and only one of them means the default stands.
218
+ *
219
+ * Only the three tool-search variables are picked out; the rest of an `env`
220
+ * block — which is where a person keeps their API keys — is left where it is.
221
+ * A file that exists but cannot be parsed is `unreadable`, never silently
222
+ * treated as setting nothing.
223
+ */
224
+ export function loadSettingsSources(candidates) {
225
+ return candidates.map((c) => {
226
+ if (!existsSync(c.path))
227
+ return { scope: c.scope, source: c.path, state: 'absent', vars: {} };
228
+ try {
229
+ const doc = parseJsonc(readFileSync(c.path, 'utf8'));
230
+ if (!doc || typeof doc !== 'object' || Array.isArray(doc))
231
+ throw new Error('not a settings object');
232
+ const block = doc.env;
233
+ const vars = {};
234
+ const unreadable = [];
235
+ if (block && typeof block === 'object' && !Array.isArray(block)) {
236
+ for (const name of TOOL_SEARCH_VARS) {
237
+ if (!(name in block))
238
+ continue;
239
+ const v = block[name];
240
+ if (typeof v === 'string')
241
+ vars[name] = v;
242
+ // The variable is set in this file, to something that is not a value
243
+ // this can read — `"ENABLE_TOOL_SEARCH": false`, the JSON boolean, is
244
+ // the one people write. Dropping it here made the file look like a
245
+ // file that sets nothing, and the report then stated the documented
246
+ // default at a machine whose settings say otherwise.
247
+ else
248
+ unreadable.push(name);
249
+ }
250
+ }
251
+ return {
252
+ scope: c.scope,
253
+ source: c.path,
254
+ state: 'read',
255
+ vars,
256
+ ...(unreadable.length ? { unreadable } : {}),
257
+ };
258
+ }
259
+ catch {
260
+ return { scope: c.scope, source: c.path, state: 'unreadable', vars: {} };
261
+ }
262
+ });
263
+ }