bcv-today 0.0.0-stage → 1.0.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.
@@ -0,0 +1,838 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ ANY_CURRENCIES,
4
+ Api,
5
+ CURRENCIES,
6
+ MAX_HISTORY_DAYS,
7
+ NotFound,
8
+ SOURCE,
9
+ TOOL_HINTS,
10
+ TTLCache,
11
+ UNIT,
12
+ UpstreamError,
13
+ VERSION,
14
+ carryForwardNote,
15
+ checkDay,
16
+ conversionRates,
17
+ freshness,
18
+ historyWindow,
19
+ invalidDateMessage,
20
+ isIsoDate,
21
+ isProblem,
22
+ notPublishedMessage,
23
+ pickRates,
24
+ rangeOrderMessage,
25
+ round,
26
+ sameCurrencyMessage
27
+ } from "./chunk-2JWU4TEK.js";
28
+
29
+ // src/tools.ts
30
+ function ok(summary, data) {
31
+ return { ok: true, summary, data };
32
+ }
33
+ function fail(message) {
34
+ return { ok: false, message };
35
+ }
36
+ function upstreamFailure(err) {
37
+ if (err instanceof UpstreamError || err instanceof NotFound)
38
+ return { ok: false, message: err.message, upstream: true };
39
+ return { ok: false, message: `Unexpected error: ${err?.message ?? String(err)}`, upstream: true };
40
+ }
41
+ function fmt(n, digits = 4) {
42
+ return n.toLocaleString("en-US", { maximumFractionDigits: digits, minimumFractionDigits: 2 });
43
+ }
44
+ async function loadDay(data, date) {
45
+ const bad = checkDay(date);
46
+ if (bad) return fail(bad.message);
47
+ try {
48
+ return await data.day(date);
49
+ } catch (err) {
50
+ if (err instanceof NotFound) {
51
+ let latest = "";
52
+ try {
53
+ latest = (await data.latest()).date;
54
+ } catch {
55
+ }
56
+ return fail(notPublishedMessage(date, latest || void 0, TOOL_HINTS));
57
+ }
58
+ throw err;
59
+ }
60
+ }
61
+ function isOutcome(v) {
62
+ return "ok" in v && typeof v.ok === "boolean";
63
+ }
64
+ async function latestRates(data) {
65
+ try {
66
+ const e = await data.latest();
67
+ const { rates } = pickRates(e, e.date);
68
+ const out = {
69
+ rates,
70
+ effective_date: e.effective_date ?? e.date,
71
+ date: e.date,
72
+ updated_at: e.updated_at ?? "",
73
+ unit: UNIT,
74
+ source: SOURCE
75
+ };
76
+ const line = Object.entries(rates).map(([c, v]) => `${c} ${fmt(v)}`).join(", ");
77
+ return ok(`Official BCV rates (effective ${out.effective_date}, Bs. per unit): ${line}.`, out);
78
+ } catch (err) {
79
+ return upstreamFailure(err);
80
+ }
81
+ }
82
+ async function rateByDate(data, { date, currency }) {
83
+ try {
84
+ const e = await loadDay(data, date);
85
+ if (isOutcome(e)) return e;
86
+ const picked = pickRates(e, date, currency);
87
+ if (isProblem(picked)) return fail(picked.message);
88
+ const { rates } = picked;
89
+ const effective = e.effective_date ?? e.date;
90
+ const note = carryForwardNote(date, effective);
91
+ const out = {
92
+ date,
93
+ effective_date: effective,
94
+ carried_forward: Boolean(note),
95
+ ...note ? { note } : {},
96
+ rates,
97
+ ...e.updated_at ? { updated_at: e.updated_at } : {},
98
+ unit: UNIT,
99
+ source: SOURCE
100
+ };
101
+ const line = Object.entries(rates).map(([c, v]) => `${c} ${fmt(v)}`).join(", ");
102
+ return ok(`BCV rate in effect on ${date} (Bs. per unit): ${line}.${note ? ` ${note}` : ""}`, out);
103
+ } catch (err) {
104
+ return upstreamFailure(err);
105
+ }
106
+ }
107
+ async function history(data, { currency, days, from, to }) {
108
+ for (const [label, v] of [
109
+ ["from", from],
110
+ ["to", to]
111
+ ]) {
112
+ if (v !== void 0 && !isIsoDate(v)) return fail(invalidDateMessage(label, v, false));
113
+ }
114
+ if (from && to && from > to) return fail(rangeOrderMessage("from", from, "to", to));
115
+ try {
116
+ const all = await data.history();
117
+ const w = historyWindow(all, currency, { days, from, to }, TOOL_HINTS);
118
+ if (isProblem(w)) return fail(w.message);
119
+ const { series, first, last, min, max, notes } = w;
120
+ const change = last.rate - first.rate;
121
+ const changePct = change / first.rate * 100;
122
+ const out = {
123
+ currency,
124
+ from: first.date,
125
+ to: last.date,
126
+ points: series.length,
127
+ first: { date: first.date, rate: first.rate },
128
+ last: { date: last.date, rate: last.rate },
129
+ min: { date: min.date, rate: min.rate },
130
+ max: { date: max.date, rate: max.rate },
131
+ change: round(change),
132
+ change_pct: round(changePct, 4),
133
+ series,
134
+ ...notes.length ? { note: notes.join(" ") } : {},
135
+ unit: UNIT,
136
+ source: SOURCE
137
+ };
138
+ const sign = change >= 0 ? "+" : "";
139
+ return ok(
140
+ `${currency}/VES from ${first.date} to ${last.date} (${series.length} days): ${fmt(first.rate)} \u2192 ${fmt(last.rate)} (${sign}${changePct.toFixed(2)}%). Min ${fmt(min.rate)} on ${min.date}, max ${fmt(max.rate)} on ${max.date}.` + (notes.length ? ` ${notes.join(" ")}` : ""),
141
+ out
142
+ );
143
+ } catch (err) {
144
+ return upstreamFailure(err);
145
+ }
146
+ }
147
+ async function convert(data, { amount, from, to, date }) {
148
+ if (from === to) return fail(sameCurrencyMessage(from));
149
+ try {
150
+ let e;
151
+ if (date) {
152
+ const r = await loadDay(data, date);
153
+ if (isOutcome(r)) return r;
154
+ e = r;
155
+ } else {
156
+ e = await data.latest();
157
+ }
158
+ const day = date ?? e.date;
159
+ const picked = conversionRates(e, from, to, day);
160
+ if (isProblem(picked)) return fail(picked.message);
161
+ const { vesPerFrom: rf, vesPerTo: rt, ratesUsed } = picked;
162
+ const rate = rf / rt;
163
+ const result = amount * rate;
164
+ const effective = e.effective_date ?? e.date;
165
+ const note = date ? carryForwardNote(date, effective) : void 0;
166
+ const out = {
167
+ amount,
168
+ from,
169
+ to,
170
+ result: round(result, 8),
171
+ rate: round(rate, 10),
172
+ rates_used: ratesUsed,
173
+ date: day,
174
+ effective_date: effective,
175
+ ...note ? { note } : {},
176
+ source: SOURCE
177
+ };
178
+ return ok(
179
+ `${fmt(amount, 2)} ${from} = ${fmt(result, 2)} ${to} at the official BCV rate (effective ${effective}; 1 ${from} = ${fmt(rate, 6)} ${to}).` + (note ? ` ${note}` : ""),
180
+ out
181
+ );
182
+ } catch (err) {
183
+ return upstreamFailure(err);
184
+ }
185
+ }
186
+ async function status(data) {
187
+ try {
188
+ const s = await data.status();
189
+ const { ageHours, stale } = freshness(s);
190
+ const out = {
191
+ status: s.status ?? "unknown",
192
+ updated_at: s.updated_at ?? "",
193
+ ...s.generated_at ? { generated_at: s.generated_at } : {},
194
+ ...s.date ? { date: s.date } : {},
195
+ ...s.effective_date ? { effective_date: s.effective_date } : {},
196
+ ...ageHours !== void 0 ? { age_hours: ageHours } : {},
197
+ stale,
198
+ supported_currencies: s.supported_currencies ?? [...CURRENCIES],
199
+ ...s.currencies ? { currencies: s.currencies } : {},
200
+ ...s.timezone ? { timezone: s.timezone } : {},
201
+ ...s.api_version ? { api_version: s.api_version } : {}
202
+ };
203
+ return ok(
204
+ `Status ${out.status}${stale ? " (STALE)" : ""}: last capture ${out.updated_at}` + (ageHours !== void 0 ? ` (${ageHours} h ago)` : "") + `, effective date ${s.effective_date ?? "unknown"}.`,
205
+ out
206
+ );
207
+ } catch (err) {
208
+ return upstreamFailure(err);
209
+ }
210
+ }
211
+
212
+ // src/mcp/meta.ts
213
+ var SERVER_NAME = "bcv-today";
214
+ var SERVER_TITLE = "BCV Today";
215
+ var SERVER_WEBSITE = "https://bcv.today";
216
+ var INSTRUCTIONS = [
217
+ "BCV Today: official exchange rates published by Banco Central de Venezuela (BCV).",
218
+ "Rates are bolivars (VES) per one unit of USD, EUR, CNY, TRY or RUB.",
219
+ "effective_date is the official BCV validity date ('fecha valor'); updated_at is the UTC capture time.",
220
+ "BCV does not publish on weekends and holidays: the last published rate stays in effect, so effective_date may be earlier than the requested date.",
221
+ "Data is free, public and read-only. Unofficial project, not affiliated with the bank. Docs: https://bcv.today/llms.txt"
222
+ ].join(" ");
223
+ var READ_ONLY = {
224
+ readOnlyHint: true,
225
+ destructiveHint: false,
226
+ idempotentHint: true,
227
+ openWorldHint: false
228
+ };
229
+ var TOOLS = {
230
+ get_latest_rates: {
231
+ title: "Latest official BCV rates",
232
+ description: "Official BCV exchange rates currently in effect for USD, EUR, CNY, TRY and RUB, in bolivars (VES) per unit, with effective date and capture time."
233
+ },
234
+ get_rate_by_date: {
235
+ title: "BCV rate on a date",
236
+ description: "Official BCV rate(s) in effect on a calendar day (YYYY-MM-DD, from 2021-08-02). Weekends and holidays carry forward the last published rate; the response explains it. Optionally restrict to one currency."
237
+ },
238
+ get_history: {
239
+ title: "BCV rate history",
240
+ description: "Daily official BCV rate series for one currency, either the last N calendar days (days, default 30, max 1830) or a from/to date range, with min, max and percentage change."
241
+ },
242
+ convert: {
243
+ title: "Convert currency at the BCV rate",
244
+ description: "Convert an amount between VES, USD, EUR, CNY, TRY and RUB using the official BCV rate currently in effect, or the rate in effect on a given date (YYYY-MM-DD)."
245
+ },
246
+ get_status: {
247
+ title: "BCV Today data freshness",
248
+ description: "Freshness of the published BCV data: status, last capture time, effective date, age in hours and supported currencies."
249
+ }
250
+ };
251
+ var ARGS = {
252
+ dateFormat: "Use the YYYY-MM-DD format, e.g. 2026-01-02.",
253
+ get_rate_by_date: {
254
+ date: "Calendar day, YYYY-MM-DD (e.g. 2026-01-02).",
255
+ currency: "Only return this currency. Omit for all available."
256
+ },
257
+ get_history: {
258
+ currency: "Currency to chart against the bolivar.",
259
+ days: "Last N calendar days ending on the latest published date (default 30). Ignored when from/to is given.",
260
+ from: "Range start, YYYY-MM-DD (inclusive).",
261
+ to: "Range end, YYYY-MM-DD (inclusive). Defaults to the latest date."
262
+ },
263
+ convert: {
264
+ amount: "Amount to convert, e.g. 100.",
265
+ from: "Source currency code.",
266
+ to: "Target currency code.",
267
+ date: "Use the rate in effect on this day (YYYY-MM-DD). Omit for the latest rate."
268
+ },
269
+ convertRate: "Units of `to` per 1 unit of `from`."
270
+ };
271
+ var DOC_RESOURCES = [
272
+ {
273
+ name: "llms-txt",
274
+ path: "/llms.txt",
275
+ mimeType: "text/plain",
276
+ title: "BCV Today llms.txt",
277
+ description: "Agent index: API endpoints, data notes and discovery documents."
278
+ },
279
+ {
280
+ name: "openapi",
281
+ path: "/openapi.json",
282
+ mimeType: "application/json",
283
+ title: "BCV Today OpenAPI",
284
+ description: "OpenAPI 3.1 description of the static JSON API."
285
+ }
286
+ ];
287
+
288
+ // src/mcp/schemas.ts
289
+ var INPUT_SCHEMAS = {
290
+ get_latest_rates: {
291
+ type: "object",
292
+ properties: {},
293
+ $schema: "http://json-schema.org/draft-07/schema#"
294
+ },
295
+ get_rate_by_date: {
296
+ type: "object",
297
+ properties: {
298
+ date: {
299
+ type: "string",
300
+ pattern: "^\\d{4}-\\d{2}-\\d{2}$",
301
+ description: "Calendar day, YYYY-MM-DD (e.g. 2026-01-02)."
302
+ },
303
+ currency: {
304
+ description: "Only return this currency. Omit for all available.",
305
+ type: "string",
306
+ enum: ["USD", "EUR", "CNY", "TRY", "RUB"]
307
+ }
308
+ },
309
+ required: ["date"],
310
+ $schema: "http://json-schema.org/draft-07/schema#"
311
+ },
312
+ get_history: {
313
+ type: "object",
314
+ properties: {
315
+ currency: {
316
+ type: "string",
317
+ enum: ["USD", "EUR", "CNY", "TRY", "RUB"],
318
+ description: "Currency to chart against the bolivar."
319
+ },
320
+ days: {
321
+ description: "Last N calendar days ending on the latest published date (default 30). Ignored when from/to is given.",
322
+ type: "integer",
323
+ minimum: 1,
324
+ maximum: 1830
325
+ },
326
+ from: {
327
+ description: "Range start, YYYY-MM-DD (inclusive).",
328
+ type: "string",
329
+ pattern: "^\\d{4}-\\d{2}-\\d{2}$"
330
+ },
331
+ to: {
332
+ description: "Range end, YYYY-MM-DD (inclusive). Defaults to the latest date.",
333
+ type: "string",
334
+ pattern: "^\\d{4}-\\d{2}-\\d{2}$"
335
+ }
336
+ },
337
+ required: ["currency"],
338
+ $schema: "http://json-schema.org/draft-07/schema#"
339
+ },
340
+ convert: {
341
+ type: "object",
342
+ properties: {
343
+ amount: {
344
+ type: "number",
345
+ minimum: 0,
346
+ maximum: 1e15,
347
+ description: "Amount to convert, e.g. 100."
348
+ },
349
+ from: {
350
+ type: "string",
351
+ enum: ["VES", "USD", "EUR", "CNY", "TRY", "RUB"],
352
+ description: "Source currency code."
353
+ },
354
+ to: {
355
+ type: "string",
356
+ enum: ["VES", "USD", "EUR", "CNY", "TRY", "RUB"],
357
+ description: "Target currency code."
358
+ },
359
+ date: {
360
+ description: "Use the rate in effect on this day (YYYY-MM-DD). Omit for the latest rate.",
361
+ type: "string",
362
+ pattern: "^\\d{4}-\\d{2}-\\d{2}$"
363
+ }
364
+ },
365
+ required: ["amount", "from", "to"],
366
+ $schema: "http://json-schema.org/draft-07/schema#"
367
+ },
368
+ get_status: {
369
+ type: "object",
370
+ properties: {},
371
+ $schema: "http://json-schema.org/draft-07/schema#"
372
+ }
373
+ };
374
+ var OUTPUT_SCHEMAS = {
375
+ get_latest_rates: {
376
+ type: "object",
377
+ properties: {
378
+ rates: {
379
+ type: "object",
380
+ propertyNames: {
381
+ type: "string"
382
+ },
383
+ additionalProperties: {
384
+ type: "number"
385
+ },
386
+ description: "VES (bolivars) per 1 unit of the currency"
387
+ },
388
+ effective_date: {
389
+ type: "string"
390
+ },
391
+ date: {
392
+ type: "string"
393
+ },
394
+ updated_at: {
395
+ type: "string"
396
+ },
397
+ unit: {
398
+ type: "string"
399
+ },
400
+ source: {
401
+ type: "string"
402
+ }
403
+ },
404
+ required: ["rates", "effective_date", "date", "updated_at", "unit", "source"],
405
+ $schema: "http://json-schema.org/draft-07/schema#",
406
+ additionalProperties: false
407
+ },
408
+ get_rate_by_date: {
409
+ type: "object",
410
+ properties: {
411
+ date: {
412
+ type: "string"
413
+ },
414
+ effective_date: {
415
+ type: "string"
416
+ },
417
+ carried_forward: {
418
+ type: "boolean"
419
+ },
420
+ note: {
421
+ type: "string"
422
+ },
423
+ rates: {
424
+ type: "object",
425
+ propertyNames: {
426
+ type: "string"
427
+ },
428
+ additionalProperties: {
429
+ type: "number"
430
+ },
431
+ description: "VES (bolivars) per 1 unit of the currency"
432
+ },
433
+ updated_at: {
434
+ type: "string"
435
+ },
436
+ unit: {
437
+ type: "string"
438
+ },
439
+ source: {
440
+ type: "string"
441
+ }
442
+ },
443
+ required: ["date", "effective_date", "carried_forward", "rates", "unit", "source"],
444
+ $schema: "http://json-schema.org/draft-07/schema#",
445
+ additionalProperties: false
446
+ },
447
+ get_history: {
448
+ type: "object",
449
+ properties: {
450
+ currency: {
451
+ type: "string"
452
+ },
453
+ from: {
454
+ type: "string"
455
+ },
456
+ to: {
457
+ type: "string"
458
+ },
459
+ points: {
460
+ type: "number"
461
+ },
462
+ first: {
463
+ type: "object",
464
+ properties: {
465
+ date: {
466
+ type: "string"
467
+ },
468
+ rate: {
469
+ type: "number"
470
+ }
471
+ },
472
+ required: ["date", "rate"],
473
+ additionalProperties: false
474
+ },
475
+ last: {
476
+ type: "object",
477
+ properties: {
478
+ date: {
479
+ type: "string"
480
+ },
481
+ rate: {
482
+ type: "number"
483
+ }
484
+ },
485
+ required: ["date", "rate"],
486
+ additionalProperties: false
487
+ },
488
+ min: {
489
+ type: "object",
490
+ properties: {
491
+ date: {
492
+ type: "string"
493
+ },
494
+ rate: {
495
+ type: "number"
496
+ }
497
+ },
498
+ required: ["date", "rate"],
499
+ additionalProperties: false
500
+ },
501
+ max: {
502
+ type: "object",
503
+ properties: {
504
+ date: {
505
+ type: "string"
506
+ },
507
+ rate: {
508
+ type: "number"
509
+ }
510
+ },
511
+ required: ["date", "rate"],
512
+ additionalProperties: false
513
+ },
514
+ change: {
515
+ type: "number"
516
+ },
517
+ change_pct: {
518
+ type: "number"
519
+ },
520
+ series: {
521
+ type: "array",
522
+ items: {
523
+ type: "object",
524
+ properties: {
525
+ date: {
526
+ type: "string"
527
+ },
528
+ effective_date: {
529
+ type: "string"
530
+ },
531
+ rate: {
532
+ type: "number"
533
+ }
534
+ },
535
+ required: ["date", "effective_date", "rate"],
536
+ additionalProperties: false
537
+ }
538
+ },
539
+ note: {
540
+ type: "string"
541
+ },
542
+ unit: {
543
+ type: "string"
544
+ },
545
+ source: {
546
+ type: "string"
547
+ }
548
+ },
549
+ required: [
550
+ "currency",
551
+ "from",
552
+ "to",
553
+ "points",
554
+ "first",
555
+ "last",
556
+ "min",
557
+ "max",
558
+ "change",
559
+ "change_pct",
560
+ "series",
561
+ "unit",
562
+ "source"
563
+ ],
564
+ $schema: "http://json-schema.org/draft-07/schema#",
565
+ additionalProperties: false
566
+ },
567
+ convert: {
568
+ type: "object",
569
+ properties: {
570
+ amount: {
571
+ type: "number"
572
+ },
573
+ from: {
574
+ type: "string"
575
+ },
576
+ to: {
577
+ type: "string"
578
+ },
579
+ result: {
580
+ type: "number"
581
+ },
582
+ rate: {
583
+ type: "number",
584
+ description: "Units of `to` per 1 unit of `from`."
585
+ },
586
+ rates_used: {
587
+ type: "object",
588
+ propertyNames: {
589
+ type: "string"
590
+ },
591
+ additionalProperties: {
592
+ type: "number"
593
+ },
594
+ description: "VES (bolivars) per 1 unit of the currency"
595
+ },
596
+ date: {
597
+ type: "string"
598
+ },
599
+ effective_date: {
600
+ type: "string"
601
+ },
602
+ note: {
603
+ type: "string"
604
+ },
605
+ source: {
606
+ type: "string"
607
+ }
608
+ },
609
+ required: ["amount", "from", "to", "result", "rate", "rates_used", "date", "effective_date", "source"],
610
+ $schema: "http://json-schema.org/draft-07/schema#",
611
+ additionalProperties: false
612
+ },
613
+ get_status: {
614
+ type: "object",
615
+ properties: {
616
+ status: {
617
+ type: "string"
618
+ },
619
+ updated_at: {
620
+ type: "string"
621
+ },
622
+ generated_at: {
623
+ type: "string"
624
+ },
625
+ date: {
626
+ type: "string"
627
+ },
628
+ effective_date: {
629
+ type: "string"
630
+ },
631
+ age_hours: {
632
+ type: "number"
633
+ },
634
+ stale: {
635
+ type: "boolean"
636
+ },
637
+ supported_currencies: {
638
+ type: "array",
639
+ items: {
640
+ type: "string"
641
+ }
642
+ },
643
+ currencies: {
644
+ type: "object",
645
+ propertyNames: {
646
+ type: "string"
647
+ },
648
+ additionalProperties: {
649
+ type: "boolean"
650
+ }
651
+ },
652
+ timezone: {
653
+ type: "string"
654
+ },
655
+ api_version: {
656
+ type: "string"
657
+ }
658
+ },
659
+ required: ["status", "updated_at", "stale", "supported_currencies"],
660
+ $schema: "http://json-schema.org/draft-07/schema#",
661
+ additionalProperties: false
662
+ }
663
+ };
664
+ var EXECUTION = {
665
+ get_latest_rates: {
666
+ taskSupport: "forbidden"
667
+ },
668
+ get_rate_by_date: {
669
+ taskSupport: "forbidden"
670
+ },
671
+ get_history: {
672
+ taskSupport: "forbidden"
673
+ },
674
+ convert: {
675
+ taskSupport: "forbidden"
676
+ },
677
+ get_status: {
678
+ taskSupport: "forbidden"
679
+ }
680
+ };
681
+
682
+ // src/mcp/stdio.ts
683
+ var SDK_PACKAGE = "@modelcontextprotocol/sdk";
684
+ var MissingSdk = class extends Error {
685
+ name = "MissingSdk";
686
+ };
687
+ var ISO = /^\d{4}-\d{2}-\d{2}$/;
688
+ function toolList() {
689
+ return Object.keys(TOOLS).map((name) => ({
690
+ name,
691
+ title: TOOLS[name].title,
692
+ description: TOOLS[name].description,
693
+ inputSchema: INPUT_SCHEMAS[name],
694
+ outputSchema: OUTPUT_SCHEMAS[name],
695
+ annotations: { title: TOOLS[name].title, ...READ_ONLY },
696
+ ...EXECUTION[name] ? { execution: EXECUTION[name] } : {}
697
+ }));
698
+ }
699
+ function validateArgs(name, args) {
700
+ const issues = [];
701
+ const date = (key, required) => {
702
+ const v = args[key];
703
+ if (v === void 0 && !required) return;
704
+ if (typeof v !== "string") issues.push(`${key}: expected a string`);
705
+ else if (!ISO.test(v)) issues.push(`${key}: ${ARGS.dateFormat}`);
706
+ };
707
+ const oneOf = (key, values, required) => {
708
+ const v = args[key];
709
+ if (v === void 0 && !required) return;
710
+ if (typeof v !== "string" || !values.includes(v)) issues.push(`${key}: expected one of ${values.join(", ")}`);
711
+ };
712
+ switch (name) {
713
+ case "get_rate_by_date":
714
+ date("date", true);
715
+ oneOf("currency", CURRENCIES, false);
716
+ break;
717
+ case "get_history": {
718
+ oneOf("currency", CURRENCIES, true);
719
+ const d = args.days;
720
+ if (d !== void 0 && (typeof d !== "number" || !Number.isInteger(d) || d < 1 || d > MAX_HISTORY_DAYS)) {
721
+ issues.push(`days: expected a whole number from 1 to ${MAX_HISTORY_DAYS}`);
722
+ }
723
+ date("from", false);
724
+ date("to", false);
725
+ break;
726
+ }
727
+ case "convert": {
728
+ const a = args.amount;
729
+ if (typeof a !== "number" || !Number.isFinite(a) || a < 0 || a > 1e15) {
730
+ issues.push("amount: expected a finite number from 0 to 1e15");
731
+ }
732
+ oneOf("from", ANY_CURRENCIES, true);
733
+ oneOf("to", ANY_CURRENCIES, true);
734
+ date("date", false);
735
+ break;
736
+ }
737
+ default:
738
+ break;
739
+ }
740
+ return issues;
741
+ }
742
+ function runTool(api, name, args) {
743
+ switch (name) {
744
+ case "get_latest_rates":
745
+ return latestRates(api);
746
+ case "get_rate_by_date":
747
+ return rateByDate(api, args);
748
+ case "get_history":
749
+ return history(api, args);
750
+ case "convert":
751
+ return convert(api, args);
752
+ default:
753
+ return status(api);
754
+ }
755
+ }
756
+ function toResult(r) {
757
+ if (!r.ok) return { content: [{ type: "text", text: r.message }], isError: true };
758
+ return {
759
+ content: [{ type: "text", text: `${r.summary}
760
+
761
+ ${JSON.stringify(r.data, null, 2)}` }],
762
+ structuredContent: r.data
763
+ };
764
+ }
765
+ async function loadSdk() {
766
+ try {
767
+ const [server, stdio, types] = await Promise.all([
768
+ import("@modelcontextprotocol/sdk/server/index.js"),
769
+ import("@modelcontextprotocol/sdk/server/stdio.js"),
770
+ import("@modelcontextprotocol/sdk/types.js")
771
+ ]);
772
+ return { Server: server.Server, StdioServerTransport: stdio.StdioServerTransport, types };
773
+ } catch (err) {
774
+ const code = err.code;
775
+ if (code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") {
776
+ throw new MissingSdk(
777
+ `"bcv-today mcp" needs the optional peer dependency ${SDK_PACKAGE}. Install it next to bcv-today (npm install ${SDK_PACKAGE}) or run: npx -y -p bcv-today -p ${SDK_PACKAGE} bcv-today mcp`
778
+ );
779
+ }
780
+ throw err;
781
+ }
782
+ }
783
+ async function runStdioServer(opts = {}) {
784
+ const { Server, StdioServerTransport, types } = await loadSdk();
785
+ const api = new Api({
786
+ baseUrl: opts.baseUrl,
787
+ timeoutMs: opts.timeoutMs ?? 1e4,
788
+ cache: new TTLCache(3e5),
789
+ userAgent: `bcv-today-js/${VERSION} (+https://bcv.today) mcp-stdio`
790
+ });
791
+ const server = new Server(
792
+ { name: SERVER_NAME, title: SERVER_TITLE, version: VERSION, websiteUrl: SERVER_WEBSITE },
793
+ { instructions: INSTRUCTIONS, capabilities: { tools: { listChanged: false }, resources: { listChanged: false } } }
794
+ );
795
+ const tools = toolList();
796
+ server.setRequestHandler(types.ListToolsRequestSchema, async () => ({ tools }));
797
+ server.setRequestHandler(types.CallToolRequestSchema, async (req) => {
798
+ const name = req.params.name;
799
+ if (!(name in TOOLS)) {
800
+ return { content: [{ type: "text", text: `Tool ${req.params.name} not found` }], isError: true };
801
+ }
802
+ const args = req.params.arguments ?? {};
803
+ const issues = validateArgs(name, args);
804
+ if (issues.length) {
805
+ return {
806
+ content: [
807
+ { type: "text", text: `Input validation error: Invalid arguments for tool ${name}: ${issues.join("; ")}` }
808
+ ],
809
+ isError: true
810
+ };
811
+ }
812
+ return toResult(await runTool(api, name, args));
813
+ });
814
+ server.setRequestHandler(types.ListResourcesRequestSchema, async () => ({
815
+ resources: DOC_RESOURCES.map((d) => ({
816
+ uri: `https://bcv.today${d.path}`,
817
+ name: d.name,
818
+ title: d.title,
819
+ description: d.description,
820
+ mimeType: d.mimeType
821
+ }))
822
+ }));
823
+ server.setRequestHandler(types.ReadResourceRequestSchema, async (req) => {
824
+ const d = DOC_RESOURCES.find((r) => `https://bcv.today${r.path}` === req.params.uri);
825
+ if (!d) throw new Error(`Resource ${req.params.uri} not found`);
826
+ return { contents: [{ uri: req.params.uri, mimeType: d.mimeType, text: await api.text(d.path, d.mimeType) }] };
827
+ });
828
+ await server.connect(new StdioServerTransport());
829
+ }
830
+ export {
831
+ MissingSdk,
832
+ SDK_PACKAGE,
833
+ runStdioServer,
834
+ runTool,
835
+ toResult,
836
+ toolList,
837
+ validateArgs
838
+ };