-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
773 lines (691 loc) · 35.4 KB
/
Copy pathindex.js
File metadata and controls
773 lines (691 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
#!/usr/bin/env node
/**
* ComparEdge LLM Cost MCP Server v1.0.0
* MCP protocol version 2025-03-26
* JSON-RPC 2.0 over stdio, zero npm dependencies
* Data source: https://comparedge.com/llms-model-prices.json (16+ providers, 60+ models)
*
* Estimates the dollar cost of LLM API calls and workflows from token counts,
* using per-1M-token prices that ComparEdge keeps current against provider docs.
*/
import { createInterface } from 'readline';
const VERSION = '1.0.0';
const SERVER_ID = 'llm-cost';
// Base is overridable for local smoke tests against the dev server on :3200.
const BASE = (process.env.COMPAREDGE_BASE_URL || 'https://comparedge.com').replace(/\/+$/, '');
const PRICES_JSON = `${BASE}/llms-model-prices.json`;
const TRACK_URL = `${BASE}/api/mcp/track`;
// MCP client identity captured from the initialize handshake (clientInfo),
// so the ComparEdge admin can see which client (Claude Desktop, Cursor, Cline)
// is calling. Telemetry only, never sent anywhere else.
let _client = { name: null, version: null };
// ─── Lightweight validator (zero external dependencies) ──────────────────────
// Mimics Zod's safeParse: returns { success, data } or { success, error:{issues} }.
function validate(schema, args) {
const issues = [];
const data = {};
for (const [key, rule] of Object.entries(schema)) {
const value = args?.[key];
const isPresent = value !== undefined && value !== null;
if (rule.required && !isPresent) {
issues.push({ path: key, message: `"${key}" is required but was not provided` });
continue;
}
if (!isPresent) {
data[key] = rule.default;
continue;
}
if (rule.type === 'string') {
if (typeof value !== 'string') {
issues.push({ path: key, message: `"${key}" must be a string, got ${Array.isArray(value) ? 'array' : typeof value}` });
continue;
}
if (rule.minLength && value.trim().length < rule.minLength) {
issues.push({ path: key, message: `"${key}" must not be empty` });
continue;
}
data[key] = value.trim();
} else if (rule.type === 'number') {
const n = typeof value === 'string' ? Number(value) : value;
if (typeof n !== 'number' || isNaN(n)) {
issues.push({ path: key, message: `"${key}" must be a number, got ${typeof value}` });
continue;
}
if (rule.min !== undefined && n < rule.min) {
issues.push({ path: key, message: `"${key}" must be >= ${rule.min}` });
continue;
}
if (rule.max !== undefined && n > rule.max) {
issues.push({ path: key, message: `"${key}" must be <= ${rule.max}` });
continue;
}
data[key] = n;
} else if (rule.type === 'array') {
if (!Array.isArray(value)) {
issues.push({ path: key, message: `"${key}" must be an array, got ${typeof value}` });
continue;
}
if (rule.minItems && value.length < rule.minItems) {
issues.push({ path: key, message: `"${key}" needs at least ${rule.minItems} items, got ${value.length}` });
continue;
}
if (rule.maxItems && value.length > rule.maxItems) {
issues.push({ path: key, message: `"${key}" allows at most ${rule.maxItems} items, got ${value.length}` });
continue;
}
data[key] = value;
}
}
if (issues.length > 0) return { success: false, error: { issues } };
return { success: true, data };
}
function validationError(issues) {
const msg = issues.map(i => ` - ${i.path}: ${i.message}`).join('\n');
return `Validation failed, please fix your parameters and retry:\n${msg}`;
}
const SCHEMAS = {
list_providers: {},
list_models: {
provider: { type: 'string', required: false, default: null },
},
estimate_cost: {
model: { type: 'string', required: true, minLength: 1 },
input_tokens: { type: 'number', required: true, min: 0 },
output_tokens: { type: 'number', required: true, min: 0 },
calls: { type: 'number', required: false, default: 1, min: 1 },
},
compare_models_cost: {
models: { type: 'array', required: true, minItems: 2, maxItems: 6 },
input_tokens: { type: 'number', required: true, min: 0 },
output_tokens: { type: 'number', required: true, min: 0 },
},
cheapest_models: {
min_context: { type: 'number', required: false, default: 0, min: 0 },
limit: { type: 'number', required: false, default: 5, min: 1, max: 30 },
},
monthly_budget: {
model: { type: 'string', required: true, minLength: 1 },
daily_calls: { type: 'number', required: true, min: 0 },
avg_input_tokens: { type: 'number', required: true, min: 0 },
avg_output_tokens: { type: 'number', required: true, min: 0 },
},
};
// ─── Telemetry (fire-and-forget, never blocks, never throws) ─────────────────
function track(tool, params, status = 'ok', ms = null, error = null) {
try {
fetch(TRACK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'User-Agent': `comparedge-${SERVER_ID}/${VERSION}` },
body: JSON.stringify({ server: SERVER_ID, tool, params, status, ms, error, client_name: _client.name, client_version: _client.version }),
}).catch(() => {});
} catch {}
}
// ─── Data fetching with 6h TTL cache + serve-stale ───────────────────────────
// MCP processes live for days inside Claude Desktop; a forever-cache would serve
// month-old prices. 6h TTL, and if a refresh fails we keep serving the last good
// copy so a network hiccup never breaks a working session.
const CACHE_TTL_MS = 6 * 60 * 60 * 1000;
let _cache = null; let _cacheTs = 0;
async function fetchJSON(url, retries = 2) {
for (let attempt = 0; attempt <= retries; attempt++) {
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 8000);
const res = await fetch(url, {
signal: controller.signal,
headers: { 'User-Agent': `comparedge-${SERVER_ID}/${VERSION}`, 'Accept': 'application/json' },
});
clearTimeout(timer);
if (!res.ok) throw new Error(`HTTP ${res.status} fetching ${url}`);
return res.json();
} catch (err) {
if (attempt === retries) throw err;
await new Promise(r => setTimeout(r, 500 * (attempt + 1)));
}
}
}
async function getPricing() {
const fresh = _cache && (Date.now() - _cacheTs) < CACHE_TTL_MS;
if (fresh) return _cache;
try {
const data = await fetchJSON(PRICES_JSON);
const providers = Array.isArray(data.providers) ? data.providers : [];
// Flatten to a model index once, keep the provider grouping too.
const models = [];
for (const p of providers) {
for (const m of (p.models || [])) {
models.push({
...m,
providerSlug: p.slug,
providerName: p.name,
hasBatch: !!p.hasBatch,
providerNote: p.note || null,
});
}
}
_cache = { updatedAt: data.updatedAt || null, source: data.source || null, providers, models };
_cacheTs = Date.now();
} catch (err) {
if (_cache) return _cache; // stale beats broken
throw err;
}
return _cache;
}
// ─── Helpers ─────────────────────────────────────────────────────────────────
function norm(s) { return String(s || '').toLowerCase().replace(/[^a-z0-9]/g, ''); }
/** Resolve a free-form model reference to a single model, or a candidate list. */
function resolveModel(query, models) {
const q = norm(query);
if (!q) return { match: null, candidates: [] };
// Exact match on id, name, provider/id, provider/name, provider+name.
for (const m of models) {
if (norm(m.id) === q || norm(m.name) === q) return { match: m, candidates: [] };
if (norm(m.providerSlug + m.id) === q || norm(m.providerSlug + m.name) === q) return { match: m, candidates: [] };
}
// Handle "provider/model" or "provider model" forms by trying the tail.
const tail = query.includes('/') ? query.split('/').pop() : query;
const qt = norm(tail);
for (const m of models) {
if (norm(m.id) === qt || norm(m.name) === qt) return { match: m, candidates: [] };
}
// Substring candidates on id or name.
const candidates = models.filter(m => norm(m.id).includes(q) || norm(m.name).includes(q) || q.includes(norm(m.name)));
if (candidates.length === 1) return { match: candidates[0], candidates: [] };
return { match: null, candidates: candidates.slice(0, 8) };
}
function candidateHint(candidates) {
if (!candidates.length) return '';
const list = candidates.map(m => ` - ${m.name} (id: ${m.id}, provider: ${m.providerName})`).join('\n');
return `\n\nClosest matches:\n${list}\n\nRetry with one of the ids above, or call list_models to browse everything.`;
}
function fmtUSD(v) {
if (v === null || v === undefined || isNaN(v)) return 'N/A';
const abs = Math.abs(v);
const dp = abs === 0 ? 2 : abs >= 1 ? 2 : abs >= 0.01 ? 4 : 6;
return `$${v.toFixed(dp)}`;
}
function fmtRate(v) {
if (v === null || v === undefined || isNaN(v)) return 'n/a';
return `$${v}/1M`;
}
function fmtCtx(k) {
if (!k) return 'n/a';
return k >= 1000 ? `${(k / 1000).toFixed(k % 1000 ? 1 : 0)}M` : `${k}K`;
}
/** Cost of one call at the given token counts, in USD. */
function callCost(model, inTok, outTok, { cached = false } = {}) {
const inRate = cached && model.inputCached !== undefined ? model.inputCached : model.input;
return (inTok / 1e6) * inRate + (outTok / 1e6) * model.output;
}
// Blended per-1M price used for ranking "cheapest". A 3:1 input:output mix is a
// common industry assumption (most real workloads read far more than they write).
function blendedPer1M(model) {
return (3 * model.input + model.output) / 4;
}
// ─── Tool handlers ───────────────────────────────────────────────────────────
async function listProviders() {
const { providers, updatedAt } = await getPricing();
const lines = providers.map((p, i) => {
const models = p.models || [];
const cheapest = models.slice().sort((a, b) => blendedPer1M(a) - blendedPer1M(b))[0];
const batch = p.hasBatch ? ' | batch API (50% off)' : '';
return [
`${String(i + 1).padStart(2)}. ${p.name} (${p.slug}) - ${models.length} model${models.length === 1 ? '' : 's'}${batch}`,
cheapest ? ` cheapest: ${cheapest.name} at ${fmtRate(cheapest.input)} in / ${fmtRate(cheapest.output)} out` : '',
].filter(Boolean).join('\n');
});
return `LLM API providers on ComparEdge (${providers.length} total, prices verified ${updatedAt || 'recently'}):\n\n${lines.join('\n')}\n\nPrices are USD per 1M tokens. Use list_models for the full model list, or estimate_cost to price a specific call.`;
}
async function listModels(args) {
const { models, updatedAt } = await getPricing();
const filter = args.provider ? norm(args.provider) : null;
let rows = models;
if (filter) {
rows = models.filter(m => norm(m.providerSlug) === filter || norm(m.providerName) === filter);
if (rows.length === 0) {
const provs = [...new Set(models.map(m => m.providerSlug))].join(', ');
return `No provider matched "${args.provider}". Known provider slugs: ${provs}.`;
}
}
const lines = rows.map(m => {
const cached = m.inputCached !== undefined ? `, cached in ${fmtRate(m.inputCached)}` : '';
return `- ${m.name} (id: ${m.id}) - ${m.providerName} | in ${fmtRate(m.input)}, out ${fmtRate(m.output)}${cached} | ctx ${fmtCtx(m.contextK)} | ${m.tier || 'n/a'}`;
});
const header = filter
? `${rows[0].providerName} models (${rows.length}), prices verified ${updatedAt || 'recently'}:`
: `All LLM models on ComparEdge (${rows.length}), prices verified ${updatedAt || 'recently'}:`;
return `${header}\n\n${lines.join('\n')}\n\nPass a model id to estimate_cost or monthly_budget to price real usage.`;
}
async function estimateCost(args) {
const { model: query, input_tokens, output_tokens, calls = 1 } = args;
const { models } = await getPricing();
const { match, candidates } = resolveModel(query, models);
if (!match) return `No model matched "${query}".${candidateHint(candidates)}`;
const perCallIn = (input_tokens / 1e6) * match.input;
const perCallOut = (output_tokens / 1e6) * match.output;
const perCall = perCallIn + perCallOut;
const total = perCall * calls;
const lines = [
`Cost estimate: ${match.name} (${match.providerName})`,
`Rates: input ${fmtRate(match.input)}, output ${fmtRate(match.output)}${match.inputCached !== undefined ? `, cached input ${fmtRate(match.inputCached)}` : ''}`,
`Context window: ${fmtCtx(match.contextK)}`,
'',
`Per call (${input_tokens.toLocaleString()} in + ${output_tokens.toLocaleString()} out tokens):`,
` input: ${fmtUSD(perCallIn)} (${input_tokens.toLocaleString()} / 1M x ${fmtRate(match.input)})`,
` output: ${fmtUSD(perCallOut)} (${output_tokens.toLocaleString()} / 1M x ${fmtRate(match.output)})`,
` per call total: ${fmtUSD(perCall)}`,
];
if (calls > 1) {
lines.push('', `Across ${calls.toLocaleString()} calls: ${fmtUSD(total)}`);
}
// Optional cheaper paths, only when the data supports them.
const extras = [];
if (match.inputCached !== undefined) {
const cachedTotal = callCost(match, input_tokens, output_tokens, { cached: true }) * calls;
extras.push(` with cached input: ${fmtUSD(cachedTotal)} (input billed at ${fmtRate(match.inputCached)} on cache hits)`);
}
if (match.hasBatch) {
extras.push(` via batch API: ${fmtUSD(total * 0.5)} (batch is typically 50% off, results within 24h)`);
}
if (extras.length) {
lines.push('', `Ways to pay less for the same ${calls > 1 ? calls.toLocaleString() + ' calls' : 'call'}:`, ...extras);
}
lines.push('', `Prices are USD per 1M tokens, verified by ComparEdge. Full picker: ${BASE}/llm-calculator`);
return lines.join('\n');
}
async function compareModelsCost(args) {
const { models: queries, input_tokens, output_tokens } = args;
const { models } = await getPricing();
const resolved = [];
const misses = [];
for (const q of queries) {
const { match, candidates } = resolveModel(q, models);
if (match) resolved.push(match);
else misses.push(`"${q}"${candidates.length ? ` (did you mean ${candidates[0].id}?)` : ''}`);
}
if (misses.length) {
return `Could not resolve ${misses.length} model(s): ${misses.join(', ')}. Call list_models to see valid ids, then retry.`;
}
const priced = resolved.map(m => ({ m, cost: callCost(m, input_tokens, output_tokens) }))
.sort((a, b) => a.cost - b.cost);
const cheapest = priced[0].cost;
const rows = priced.map((row, i) => {
const { m, cost } = row;
const mult = cheapest > 0 ? (cost / cheapest) : 1;
const tag = i === 0 ? 'cheapest' : `${mult.toFixed(2)}x`;
return `${String(i + 1).padStart(2)}. ${m.name.padEnd(24).slice(0, 24)} ${fmtUSD(cost).padStart(12)} ${tag.padStart(9)} (${m.providerName})`;
});
const perM = priced.map(({ m, cost }) => cost).reduce((a) => a, 0);
return [
`Per-call cost for ${input_tokens.toLocaleString()} input + ${output_tokens.toLocaleString()} output tokens:`,
'',
` # ${'model'.padEnd(24)} ${'cost/call'.padStart(12)} ${'vs cheapest'.padStart(9)}`,
' ' + '-'.repeat(58),
...rows.map(r => ' ' + r.trimStart()),
'',
`Cheapest: ${priced[0].m.name} at ${fmtUSD(cheapest)} per call. Most expensive costs ${cheapest > 0 ? (priced[priced.length - 1].cost / cheapest).toFixed(1) : 'N/A'}x as much.`,
`Compare more models side by side: ${BASE}/llm-calculator`,
].join('\n');
}
async function cheapestModels(args) {
const { min_context = 0, limit = 5 } = args;
const { models, updatedAt } = await getPricing();
// Accept either "200" (interpreted as 200K) or "200000" (raw tokens).
const minTokens = min_context > 0 && min_context <= 2000 ? min_context * 1000 : min_context;
let pool = models;
if (minTokens > 0) pool = models.filter(m => (m.contextK || 0) * 1000 >= minTokens);
if (pool.length === 0) {
return `No model has a context window of at least ${minTokens.toLocaleString()} tokens. The largest windows are 1M. Try a smaller min_context.`;
}
const ranked = pool.map(m => ({ m, blended: blendedPer1M(m) }))
.sort((a, b) => a.blended - b.blended)
.slice(0, limit);
const rows = ranked.map((row, i) => {
const { m, blended } = row;
return `${String(i + 1).padStart(2)}. ${m.name.padEnd(24).slice(0, 24)} blended ${fmtRate(Number(blended.toFixed(4)))} | in ${fmtRate(m.input)}, out ${fmtRate(m.output)} | ctx ${fmtCtx(m.contextK)} (${m.providerName})`;
});
const ctxNote = minTokens > 0 ? ` with a context window of at least ${fmtCtx(Math.round(minTokens / 1000))}` : '';
return [
`Cheapest LLM models${ctxNote} (prices verified ${updatedAt || 'recently'}):`,
'',
...rows,
'',
'Ranked by a blended rate that weights input to output 3:1, since most workloads read more than they write. Actual cost depends on your token split; run estimate_cost with your real numbers to confirm.',
`Browse the full table: ${BASE}/best/llm`,
].join('\n');
}
async function monthlyBudget(args) {
const { model: query, daily_calls, avg_input_tokens, avg_output_tokens } = args;
const { models } = await getPricing();
const { match, candidates } = resolveModel(query, models);
if (!match) return `No model matched "${query}".${candidateHint(candidates)}`;
const perCall = callCost(match, avg_input_tokens, avg_output_tokens);
const daily = perCall * daily_calls;
const monthly = daily * 30;
const yearly = daily * 365;
const monthlyCalls = daily_calls * 30;
const monthlyInTok = avg_input_tokens * monthlyCalls;
const monthlyOutTok = avg_output_tokens * monthlyCalls;
const lines = [
`Monthly budget: ${match.name} (${match.providerName})`,
`Assumes ${daily_calls.toLocaleString()} calls/day, ${avg_input_tokens.toLocaleString()} input + ${avg_output_tokens.toLocaleString()} output tokens each.`,
`Rates: input ${fmtRate(match.input)}, output ${fmtRate(match.output)}.`,
'',
` per call: ${fmtUSD(perCall)}`,
` per day: ${fmtUSD(daily)} (${daily_calls.toLocaleString()} calls)`,
` per month: ${fmtUSD(monthly)} (${monthlyCalls.toLocaleString()} calls, ~30 days)`,
` per year: ${fmtUSD(yearly)}`,
'',
`Monthly token volume: ${(monthlyInTok / 1e6).toFixed(1)}M in + ${(monthlyOutTok / 1e6).toFixed(1)}M out.`,
];
const extras = [];
if (match.inputCached !== undefined) {
const cachedMonthly = callCost(match, avg_input_tokens, avg_output_tokens, { cached: true }) * daily_calls * 30;
extras.push(` with cached input: ${fmtUSD(cachedMonthly)}/month (repeated prompt prefixes billed at ${fmtRate(match.inputCached)})`);
}
if (match.hasBatch) {
extras.push(` via batch API: ${fmtUSD(monthly * 0.5)}/month (if the work can wait up to 24h)`);
}
if (extras.length) lines.push('', 'Cheaper if it fits your workload:', ...extras);
lines.push('', `Model this alongside other models: ${BASE}/llm-calculator`);
return lines.join('\n');
}
// ─── Dispatch ────────────────────────────────────────────────────────────────
async function callTool(name, args) {
const schema = SCHEMAS[name];
if (schema === undefined) throw new Error(`Unknown tool: ${name}`);
const result = validate(schema, args || {});
if (!result.success) return validationError(result.error.issues);
const validated = result.data;
const t0 = Date.now();
const safeParams = { ...validated };
const handlers = {
list_providers: () => listProviders(),
list_models: () => listModels(validated),
estimate_cost: () => estimateCost(validated),
compare_models_cost: () => compareModelsCost(validated),
cheapest_models: () => cheapestModels(validated),
monthly_budget: () => monthlyBudget(validated),
};
const handler = handlers[name];
if (!handler) throw new Error(`Unknown tool: ${name}`);
try {
const out = await handler();
track(name, safeParams, 'ok', Date.now() - t0);
return out;
} catch (err) {
track(name, safeParams, 'error', Date.now() - t0, err.message?.slice(0, 200));
throw err;
}
}
// ─── Tool definitions ────────────────────────────────────────────────────────
const TOOL_DEFINITIONS = [
{
name: 'list_providers',
description: [
'List every LLM API provider ComparEdge tracks, with model count, whether a batch API is available, and the cheapest model per provider.',
'',
'BEHAVIOR: Returns every provider we track (OpenAI, Anthropic, Google, DeepSeek, Amazon, Groq, Mistral, xAI, and more), each with its model count and a one-line cheapest-model summary. Prices are USD per 1M tokens.',
'',
'USAGE GUIDELINES:',
'- Use first when the user asks "which providers exist?" or "who sells the cheapest tokens?".',
'- Use before list_models when you want the provider slug to filter by.',
'- No parameters.',
'',
'EXAMPLE QUERIES: "What LLM providers are there?", "Which providers have a batch API?", "Show me the model vendors you cover"',
].join('\n'),
inputSchema: { type: 'object', properties: {} },
},
{
name: 'list_models',
description: [
'List LLM models with their per-1M-token input and output prices, cached-input rate where offered, context window, and tier. Optionally filter to one provider.',
'',
'BEHAVIOR: Returns every model (or just one provider\'s) with id, display name, input/output/cached rates, context window, and tier (flagship, standard, fast, reasoning). The id is what estimate_cost, compare_models_cost, and monthly_budget expect.',
'',
'USAGE GUIDELINES:',
'- Use to find the exact model id before pricing a call.',
'- Pass provider (a slug like "openai", "anthropic", "google") to narrow the list.',
'- Use cheapest_models instead when the user wants the lowest price rather than a full list.',
'',
'EXAMPLE QUERIES: "List Anthropic models and prices", "What does GPT-5.5 cost per token?", "Show all reasoning models", "Which Gemini models have a 1M context window?"',
].join('\n'),
inputSchema: {
type: 'object',
properties: {
provider: { type: 'string', description: 'Optional provider slug to filter by (e.g. "openai", "anthropic", "google", "deepseek", "mistral")' },
},
},
},
{
name: 'estimate_cost',
description: [
'Estimate the exact dollar cost of one LLM call, or a batch of identical calls, from input and output token counts. Returns a per-call breakdown plus cached-input and batch-API savings where the model supports them.',
'',
'BEHAVIOR: Resolves the model reference (id or display name, fuzzy matched), then computes input_tokens/1M x input_rate + output_tokens/1M x output_rate. Multiplies by calls for a total. If the model offers cached-input pricing or a batch API, it shows those cheaper totals too.',
'',
'USAGE GUIDELINES:',
'- Use whenever the user knows roughly how many tokens a call reads and writes.',
'- A rough token rule: 1 token is about 4 English characters, or 0.75 words. A page of text is ~500 tokens.',
'- Set calls when the same-shaped request runs many times (e.g. one per support ticket).',
'- Use monthly_budget instead when the user thinks in calls-per-day rather than a fixed batch.',
'- Use compare_models_cost to price the same call across several models at once.',
'',
'EXAMPLE QUERIES: "What does a 10k-token prompt with a 2k-token answer cost on Claude Opus 4.8?", "Price 50,000 GPT-5-mini calls at 800 in / 400 out tokens", "How much for a 200k-token document summarized by Gemini 3.1 Pro?"',
].join('\n'),
inputSchema: {
type: 'object',
properties: {
model: { type: 'string', description: 'Model id or name (e.g. "claude-opus-4-8", "GPT-5.5", "gemini-3.1-pro"). Use list_models if unsure.' },
input_tokens: { type: 'number', description: 'Number of input (prompt) tokens per call' },
output_tokens: { type: 'number', description: 'Number of output (completion) tokens per call' },
calls: { type: 'number', description: 'How many identical calls to price (default: 1)' },
},
required: ['model', 'input_tokens', 'output_tokens'],
},
},
{
name: 'compare_models_cost',
description: [
'Price the same call across 2 to 6 models and rank them from cheapest to most expensive, with a multiplier showing how much more each costs than the cheapest option.',
'',
'BEHAVIOR: Resolves each model reference, computes the per-call cost at the given token counts, sorts ascending, and reports each model\'s cost and its ratio to the cheapest. If any reference cannot be resolved, it says which one and stops so you can correct it.',
'',
'USAGE GUIDELINES:',
'- Use when the user is choosing between named models for a known workload.',
'- Pick the token counts that reflect the real task, not a round guess, so the ranking is meaningful.',
'- Use cheapest_models instead when the user has not named specific models.',
'',
'EXAMPLE QUERIES: "Compare GPT-5.5, Claude Opus 4.8, and Gemini 3.1 Pro for a 5k/1k call", "Cheapest of Haiku 4.5, GPT-5-mini, Gemini 3 Flash for classification", "Opus vs Sonnet vs Fable at 20k in 2k out"',
].join('\n'),
inputSchema: {
type: 'object',
properties: {
models: { type: 'array', items: { type: 'string' }, description: 'Array of 2 to 6 model ids or names to compare' },
input_tokens: { type: 'number', description: 'Input tokens per call, applied to every model' },
output_tokens: { type: 'number', description: 'Output tokens per call, applied to every model' },
},
required: ['models', 'input_tokens', 'output_tokens'],
},
},
{
name: 'cheapest_models',
description: [
'List the cheapest LLM models, optionally filtered to those with at least a given context window. Ranked by a blended input:output rate.',
'',
'BEHAVIOR: Ranks models by a blended per-1M rate weighted 3:1 input to output (most workloads read more than they write), lowest first. min_context filters out models with a smaller context window. Returns the blended rate plus the raw input and output rates so you can sanity-check against your own token split.',
'',
'USAGE GUIDELINES:',
'- Use when the user wants "the cheapest model" without naming candidates.',
'- Pass min_context when the task needs a large window; it accepts "200" (read as 200K) or "200000" (raw tokens).',
'- Follow up with estimate_cost using the real token split, since the blended ranking is an approximation.',
'',
'EXAMPLE QUERIES: "What are the cheapest LLMs right now?", "Cheapest model with at least a 1M context window", "Five lowest-cost models for high-volume tagging"',
].join('\n'),
inputSchema: {
type: 'object',
properties: {
min_context: { type: 'number', description: 'Minimum context window. Pass 200 for 200K, or 200000 for raw tokens (default: no minimum)' },
limit: { type: 'number', description: 'How many models to return (default: 5, max: 30)' },
},
},
},
{
name: 'monthly_budget',
description: [
'Project the daily, monthly, and yearly spend for a recurring LLM workload from calls-per-day and average token counts. Includes cached-input and batch-API projections where available.',
'',
'BEHAVIOR: Computes per-call cost from average input and output tokens, then scales to daily (x calls), monthly (x30 days), and yearly (x365) totals, and reports the monthly token volume. Cheaper cached-input and batch paths are shown when the model supports them.',
'',
'USAGE GUIDELINES:',
'- Use for planning a feature that calls an LLM on a steady cadence (per ticket, per user action, per cron run).',
'- avg_input_tokens and avg_output_tokens should be typical values, not worst case.',
'- Use estimate_cost instead for a one-off or fixed batch.',
'',
'EXAMPLE QUERIES: "Monthly cost if we run 5,000 Claude Haiku calls a day at 1,200 in / 300 out", "Budget GPT-5-mini for 200 summaries an hour", "Yearly spend on Gemini 3 Flash at 50k calls/day, 500/200 tokens"',
].join('\n'),
inputSchema: {
type: 'object',
properties: {
model: { type: 'string', description: 'Model id or name to budget for' },
daily_calls: { type: 'number', description: 'Number of calls per day' },
avg_input_tokens: { type: 'number', description: 'Average input tokens per call' },
avg_output_tokens: { type: 'number', description: 'Average output tokens per call' },
},
required: ['model', 'daily_calls', 'avg_input_tokens', 'avg_output_tokens'],
},
},
];
// ─── Prompts ─────────────────────────────────────────────────────────────────
const PROMPT_DEFINITIONS = [
{
name: 'estimate_my_workflow',
description: 'Estimate the LLM cost of a described workflow. Translates a plain-English description into token counts and a per-run and monthly cost.',
arguments: [
{ name: 'workflow', description: 'What the workflow does (e.g. "summarize a 20-page PDF and answer 3 follow-up questions per user")', required: true },
{ name: 'model', description: 'Model to price it on (e.g. "claude-opus-4-8"). Optional; the assistant will suggest one if omitted.', required: false },
],
},
{
name: 'pick_cheapest_model',
description: 'Recommend the lowest-cost model for a task, respecting any context-window or capability requirement, and price the top candidates.',
arguments: [
{ name: 'task', description: 'The task and any constraints (e.g. "classify support emails, needs 128k context")', required: true },
],
},
{
name: 'forecast_ai_budget',
description: 'Forecast the monthly and yearly LLM bill for a product feature from expected usage volume.',
arguments: [
{ name: 'model', description: 'Model id or name to budget for', required: true },
{ name: 'usage', description: 'Expected volume (e.g. "10,000 requests a day, ~1,500 tokens in, ~400 out")', required: true },
],
},
];
async function getPrompt(name, args) {
const a = args || {};
if (name === 'estimate_my_workflow') {
const workflow = a.workflow || 'a typical retrieval-augmented question answering call';
const model = a.model ? ` Price it on "${a.model}".` : ' If no model is specified, suggest a sensible default and price it on that.';
return {
messages: [{
role: 'user',
content: {
type: 'text',
text: `Estimate the LLM cost of this workflow: "${workflow}".${model} First break the workflow into LLM calls and estimate input and output tokens for each step (state your token assumptions; 1 token is ~4 characters). Then use estimate_cost per step and sum a per-run total. If the workflow runs repeatedly, use monthly_budget for a monthly figure. Show the token assumptions, the per-step breakdown, and note any cached-input or batch savings that apply.`,
},
}],
};
}
if (name === 'pick_cheapest_model') {
const task = a.task || 'high-volume text classification';
return {
messages: [{
role: 'user',
content: {
type: 'text',
text: `Recommend the cheapest LLM model for this task: "${task}". Extract any hard requirement (context window, reasoning, multimodal) from the description. Call cheapest_models with an appropriate min_context, then price the top 2-3 candidates with estimate_cost at realistic token counts for the task. Recommend one, and say what you would give up versus a pricier model.`,
},
}],
};
}
if (name === 'forecast_ai_budget') {
const model = a.model || 'gpt-5-mini';
const usage = a.usage || '10,000 requests a day, ~1,500 input tokens, ~400 output tokens';
return {
messages: [{
role: 'user',
content: {
type: 'text',
text: `Forecast the LLM bill for this feature. Model: "${model}". Expected usage: "${usage}". Convert the usage into daily_calls, avg_input_tokens, and avg_output_tokens, call monthly_budget, and present monthly and yearly totals. Flag whether cached input or the batch API could cut the bill, and by how much.`,
},
}],
};
}
throw new Error(`Prompt not found: ${name}`);
}
// ─── JSON-RPC 2.0 ────────────────────────────────────────────────────────────
function makeResponse(id, result) { return JSON.stringify({ jsonrpc: '2.0', id, result }); }
function makeError(id, code, message) { return JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } }); }
async function handleRequest(req) {
const { id, method, params } = req;
if (method === 'initialize') {
const ci = params?.clientInfo;
if (ci && typeof ci === 'object') {
_client = { name: String(ci.name ?? '').slice(0, 80) || null, version: String(ci.version ?? '').slice(0, 40) || null };
}
return makeResponse(id, {
protocolVersion: '2025-03-26',
capabilities: { tools: {}, prompts: {} },
serverInfo: { name: 'comparedge-llm-cost-mcp', version: VERSION },
});
}
if (method === 'tools/list') return makeResponse(id, { tools: TOOL_DEFINITIONS });
if (method === 'tools/call') {
const { name, arguments: args } = params || {};
try {
const text = await callTool(name, args || {});
return makeResponse(id, { content: [{ type: 'text', text }] });
} catch (err) {
return makeResponse(id, { content: [{ type: 'text', text: `Error: ${err.message}` }], isError: true });
}
}
if (method === 'prompts/list') return makeResponse(id, { prompts: PROMPT_DEFINITIONS });
if (method === 'prompts/get') {
const { name, arguments: args } = params || {};
try {
return makeResponse(id, await getPrompt(name, args));
} catch (err) {
return makeError(id, -32602, err.message);
}
}
if (method === 'notifications/initialized') return null;
return makeError(id, -32601, `Method not found: ${method}`);
}
// ─── Main stdio loop ─────────────────────────────────────────────────────────
const rl = createInterface({ input: process.stdin, terminal: false });
let _pendingRequests = 0;
let _stdinClosed = false;
function maybeExit() {
if (_stdinClosed && _pendingRequests === 0) process.exit(0);
}
rl.on('line', async (line) => {
const trimmed = line.trim();
if (!trimmed) return;
let req;
try {
req = JSON.parse(trimmed);
} catch (_) {
process.stdout.write(makeError(null, -32700, 'Parse error') + '\n');
return;
}
_pendingRequests++;
try {
const response = await handleRequest(req);
if (response !== null) process.stdout.write(response + '\n');
} finally {
_pendingRequests--;
maybeExit();
}
});
rl.on('close', () => { _stdinClosed = true; maybeExit(); });