@statsmcp/core 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { a as UrlSnapshotReaderOptions, c as readSnapshot, d as writeSnapshot, i as SnapshotReader, l as snapshotKey, n as Snapshot, o as bucketSnapshotReader, r as SnapshotBucket, s as firstSnapshot, t as SNAPSHOT_VERSION, u as urlSnapshotReader } from "./snapshot-DwtIVsqx.js";
1
+ import { a as UrlSnapshotReaderOptions, c as readSnapshot, d as writeSnapshot, i as SnapshotReader, l as snapshotKey, n as Snapshot, o as bucketSnapshotReader, r as SnapshotBucket, s as firstSnapshot, t as SNAPSHOT_VERSION, u as urlSnapshotReader } from "./snapshot-BCJSa_1e.js";
2
2
  import { McpServer } from "@modelcontextprotocol/server";
3
3
  import { z } from "zod";
4
+ import { AsPlainObject } from "minisearch";
4
5
  interface Cache {
5
6
  get(key: string): Promise<string | undefined>;
6
7
  set(key: string, value: string, ttlSeconds: number): Promise<void>;
@@ -81,6 +82,8 @@ interface TableVariable {
81
82
  eliminable: boolean;
82
83
  isTime: boolean;
83
84
  periodsIncomplete?: boolean;
85
+ /** The API's "top" filter does not pick the newest periods, so `latest` selects the newest codes. */
86
+ latestByCode?: boolean;
84
87
  }
85
88
  interface TableMetadata {
86
89
  title: string;
@@ -115,6 +118,8 @@ interface PxwebClientOptions {
115
118
  timeoutMs?: number;
116
119
  /** Clock for search ranking; defaults to the system time. */
117
120
  now?: () => Date;
121
+ /** Time variables that the API does not flag with `time: true`. */
122
+ timeVariables?: readonly string[];
118
123
  /** Builds the table id from a search hit; defaults to `path/id`. */
119
124
  tableIdFromHit?: (hit: SearchHit) => string;
120
125
  /** True when the office keeps the table in its archive, for example by its path. */
@@ -147,15 +152,15 @@ interface PxwebPreset {
147
152
  * (the language is a parameter), use the API table ids as they are and ignore
148
153
  * `database` and `tableIdFromHit`.
149
154
  */
150
- api?: 'pxweb-v1' | 'pxweb-v2' | 'statbank-dk' | 'cbs-odata' | 'eurostat' | 'statcan-wds' | 'singstat' | 'cso-pxstat' | 'sdmx';
155
+ api?: 'pxweb-v1' | 'pxweb-v2' | 'statbank-dk' | 'cbs-odata' | 'eurostat' | 'statcan-wds' | 'singstat' | 'cso-pxstat' | 'ine-es' | 'ine-pt' | 'insee' | 'ons' | 'statbel' | 'czso' | 'ibge' | 'osp' | 'sdmx';
151
156
  /** SDMX agency id, for example "ABS". Required when `api` is "sdmx". */
152
157
  sdmxAgency?: string;
153
158
  /**
154
159
  * Search strategy for APIs that offer both: "api" uses the API's own search,
155
160
  * "index" lists every table and searches locally (better on the golden sets).
156
161
  * PxWeb v2 lists the tables itself. PxWeb v1 reads the crawled snapshot from the
157
- * server's `tableSnapshots` option. PxStat ("cso-pxstat") always reads the
158
- * snapshot. Other API types always search an index.
162
+ * server's `tableSnapshots` option. PxStat ("cso-pxstat"), the INE clients
163
+ * ("ine-es", "ine-pt") and INSEE ("insee") always read the snapshot. ONS ("ons") always uses the API search. Other API types always search an index.
159
164
  */
160
165
  search?: 'api' | 'index';
161
166
  /**
@@ -164,6 +169,8 @@ interface PxwebPreset {
164
169
  * A database that fails is left out of the results; search fails only if all fail.
165
170
  */
166
171
  database?: string | readonly [string, ...string[]];
172
+ /** pxweb-v1: time variables that the API does not flag with `time: true`. */
173
+ timeVariables?: readonly string[];
167
174
  languages: readonly [string, ...string[]];
168
175
  /** Appended to every tool result. Most statistics offices require it. */
169
176
  attribution: (lang: string) => string;
@@ -191,6 +198,7 @@ interface PresetServerOptions {
191
198
  }
192
199
  export declare function createPresetClient(preset: PxwebPreset, options: Pick<PresetServerOptions, 'cache' | 'fetch' | 'tableSnapshots'> & {
193
200
  userAgent: string;
201
+ timeoutMs?: number;
194
202
  }): PxwebClient;
195
203
  export declare function createPresetServer(preset: PxwebPreset, options?: PresetServerOptions): McpServer;
196
204
  interface SelectionInput {
@@ -234,6 +242,19 @@ interface TableIndexOptions {
234
242
  */
235
243
  currentYear?: number;
236
244
  }
245
+ /** Bump when the index fields or the MiniSearch version change; term processing is checked by its fingerprint. */
246
+ export declare const SEARCH_INDEX_VERSION = 1;
247
+ /** A search index built ahead of time, for example by the crawler. */
248
+ interface PrebuiltSearchIndex {
249
+ version: number;
250
+ /** How the language's terms were processed; a change to normalization or stop words rebuilds. */
251
+ terms?: string;
252
+ index: AsPlainObject;
253
+ }
254
+ /** The prebuilt index stored in a snapshot, if it has the expected shape. */
255
+ export declare function asPrebuiltIndex(value: unknown): PrebuiltSearchIndex | undefined;
256
+ /** Builds the search index of a table list, to store with the list. */
257
+ export declare function buildSearchIndex(docs: readonly TableDoc[], lang: string): PrebuiltSearchIndex;
237
258
  /**
238
259
  * Keyword search over a source's table catalogue, for APIs whose own search is
239
260
  * missing, broken or weak. It fuses two rankings: BM25 over titles (three times)
@@ -244,6 +265,8 @@ export declare function createTableIndex(docs: readonly TableDoc[], lang: string
244
265
  interface TableListOptions {
245
266
  /** Tables to use when the live table list fails, for example a crawled snapshot. */
246
267
  listFallback?: (lang: string) => Promise<readonly TableDoc[] | undefined>;
268
+ /** Tables to use before the live table list, for example a crawled snapshot. */
269
+ crawledList?: (lang: string) => Promise<readonly TableDoc[] | undefined>;
247
270
  }
248
271
  interface TableIndexLoaderOptions {
249
272
  /** How long a built index stays fresh. */
@@ -273,6 +296,202 @@ interface PxstatClientOptions extends HttpOptions {
273
296
  now?: () => Date;
274
297
  }
275
298
  export declare function createPxstatClient(options: PxstatClientOptions): PxwebClient;
299
+ interface IneEsClientOptions extends HttpOptions {
300
+ /** Tempus3 JSON root, for example "https://servicios.ine.es/wstempus/js". */
301
+ baseUrl: string;
302
+ /** Full table list (the crawled snapshot); Tempus3 has no search. */
303
+ tableList?: (lang: string) => Promise<readonly TableDoc[]>;
304
+ /** Clock for search ranking; defaults to the system time. */
305
+ now?: () => Date;
306
+ }
307
+ export declare function createIneEsClient(options: IneEsClientOptions): Required<PxwebClient>;
308
+ interface InePtClientOptions extends HttpOptions {
309
+ /** Root of the INE web services, for example "https://www.ine.pt/ine". */
310
+ baseUrl: string;
311
+ /** Full table list (the crawled snapshot); INE has no search. */
312
+ tableList?: (lang: string) => Promise<readonly TableDoc[]>;
313
+ /** Refuses larger queries before the request. Default 1,000,000 (the API limit). */
314
+ maxCells?: number;
315
+ /** Clock for search ranking; defaults to the system time. */
316
+ now?: () => Date;
317
+ }
318
+ export declare function createInePtClient(options: InePtClientOptions): PxwebClient & Required<Pick<PxwebClient, 'listTables'>>;
319
+ interface InseeClientOptions extends HttpOptions {
320
+ /** Melodi root, for example "https://api.insee.fr/melodi". */
321
+ baseUrl: string;
322
+ /** Full dataset list (the crawled snapshot); Melodi has no search. */
323
+ tableList?: (lang: string) => Promise<readonly TableDoc[]>;
324
+ /** Upper limit of observations in one data request. */
325
+ maxCells?: number;
326
+ /** Clock for search ranking; defaults to the system time. */
327
+ now?: () => Date;
328
+ }
329
+ export declare function createInseeClient(options: InseeClientOptions): Required<PxwebClient>;
330
+ interface CzsoClientOptions extends HttpOptions, TableListOptions {
331
+ /** DataStat API root, for example "https://data.csu.gov.cz/api". */
332
+ baseUrl: string;
333
+ /** Upper limit of cells in one data request. */
334
+ maxCells?: number;
335
+ /** Clock for search ranking; defaults to the system time. */
336
+ now?: () => Date;
337
+ }
338
+ /**
339
+ * Client for DataStat, the CZSO (Czech Statistical Office) API, in Czech and English.
340
+ *
341
+ * - Table id: the dataset code (`kod`, for example "CEN0101H").
342
+ * - Search: a local index over the live catalogue (`katalog/v1/sady`, per language) and
343
+ * the content list (`dotaz/v1/metadata/sady/seznam`, for the data update date and
344
+ * last period), both cached for an hour. No crawled snapshot is needed; `listFallback`
345
+ * is used when the live catalogue fails.
346
+ * - Metadata: the dataset detail gives the indicators and the dimension variants. A
347
+ * dataset can have several variants of one dimension type (years, quarters, or years
348
+ * and quarters; regions or districts), and a query can use only one, so each type
349
+ * exposes its default variant, else the one with the most levels, then the most items.
350
+ * Items come from the variant's code list; time items are cut to the data range of the
351
+ * dataset content metadata and ordered by the code list order. A dimension with a
352
+ * total item is eliminable: DataStat returns the total when it is left out.
353
+ * - Query: one GET custom selection (`data/sady/{kod}/vlastni`, JSON-stat 2.0) with one
354
+ * parameter per selected dimension. `latest` gives the newest periods of each time level.
355
+ * The cell limit is checked before the request. Predefined selections (`data/vybery`)
356
+ * are not used: they cover one variant each and cost the same single request.
357
+ * - Caching: catalogue and code lists in the shared cache; the parsed table in memory for
358
+ * an hour, so new periods appear soon after a release.
359
+ */
360
+ export declare function createCzsoClient(options: CzsoClientOptions): Required<PxwebClient>;
361
+ interface IbgeClientOptions extends HttpOptions, TableListOptions {
362
+ /** Aggregates API root, for example "https://servicodados.ibge.gov.br/api/v3/agregados". */
363
+ baseUrl: string;
364
+ /** SIDRA values API root; defaults to "https://apisidra.ibge.gov.br/values". */
365
+ sidraUrl?: string;
366
+ /** Upper limit of cells in one data request. */
367
+ maxCells?: number;
368
+ /** Clock for search ranking; defaults to the system time. */
369
+ now?: () => Date;
370
+ }
371
+ /**
372
+ * Client for IBGE (Instituto Brasileiro de Geografia e Estatística), in Portuguese.
373
+ *
374
+ * - Table id: the aggregate id, which is also the SIDRA table number (for example "1737").
375
+ * - Search: a local index over the aggregates catalogue (`/agregados`, one 2 MB call
376
+ * cached for an hour); the survey group is the path. The catalogue has no dates, so
377
+ * there is no recency ranking. Aggregates are marked discontinued when the title
378
+ * says "série encerrada" (or "descontinuada", "suspensa"), ends with a closed range
379
+ * ("… até dezembro/2019)") or the survey ended long ago (`DISCONTINUED_GROUPS`).
380
+ * - Metadata: `/agregados/{id}/metadados`, `/periodos` and `/localidades/{levels}`.
381
+ * Variables: `variavel` (the IBGE variables, with the unit in the label), one `c{id}`
382
+ * per classification (eliminable when IBGE sums it, which gives the total category
383
+ * when it is left out), `localidade` (codes "N1:1", "N3:35", "N6:3550308"; levels
384
+ * Brazil, regions, states and municipalities; eliminable when Brazil is offered) and
385
+ * `periodo` (ordered oldest first).
386
+ * - Query: one SIDRA values request (`/t/{id}/n…/v/…/p/…/c…`), because SIDRA answers a
387
+ * refused selection with HTTP 400 and a message, where the aggregates API answers 500.
388
+ * The cell limit is checked first. SIDRA orders the columns as the request does
389
+ * (locality, variable, period, then classifications) and adds the left-out
390
+ * classifications after them, so only the requested ones become columns.
391
+ * - Not found: the aggregates API answers HTTP 500 for an unknown id, so a failed
392
+ * metadata call is checked against the catalogue.
393
+ * - Caching: catalogue, metadata and localities in the shared cache; the parsed table in
394
+ * memory for an hour.
395
+ */
396
+ export declare function createIbgeClient(options: IbgeClientOptions): Required<PxwebClient>;
397
+ interface OspClientOptions extends HttpOptions, TableListOptions {
398
+ /** SDMX-ML root, for example "https://osp-rs.stat.gov.lt/ords/ipospp/ospp/rest_xml". */
399
+ baseUrl: string;
400
+ /** Upper limit of cells in one result. */
401
+ maxCells?: number;
402
+ /** Upper limit of observations downloaded for one result (every code of every dimension). */
403
+ maxDownloadObservations?: number;
404
+ /** Clock for search ranking; defaults to the system time. */
405
+ now?: () => Date;
406
+ }
407
+ /**
408
+ * Client for the Official Statistics Portal (OSP) of Statistics Lithuania (State
409
+ * Data Agency), an SDMX 2.1 REST service that answers only SDMX-ML, in Lithuanian
410
+ * and English (every message carries both).
411
+ *
412
+ * - Table id: the dataflow id, for example "S7R121_M2020103_1". One indicator
413
+ * ("S7R121") has one dataflow per breakdown, so the title adds the dimensions
414
+ * and the period range from the dataflow description.
415
+ * - Search: a local index over the full dataflow list (`dataflow/`, 7 MB, one call
416
+ * for both languages). The parsed list is kept in memory for an hour; it is too
417
+ * large for the shared cache. The description gives the update date, the last
418
+ * period and "not renewed" (discontinued) dataflows.
419
+ * - Metadata: `dataflow/LSD/{id}` and `datastructure/LSD/{dsd}` (code lists and
420
+ * concept names). Code lists come unordered and are sorted by code. A dimension is
421
+ * eliminable only when exactly one code is labelled as a total ("Total by age",
422
+ * "Republic of Lithuania", "Males and females" …); left out, it is filtered to that
423
+ * code. The structure lists no periods, so they are expanded from the range in the
424
+ * dataflow description.
425
+ * - Query: OSP supports only `startPeriod` and `endPeriod` (no key, no
426
+ * `lastNObservations`, so URLs stay far below its 1,000-character limit). Each
427
+ * run of adjacent chosen periods is one request (more than four runs become one
428
+ * span) with every code of every dimension; the message is streamed and filtered
429
+ * here, never held whole. `latest` picks the newest listed periods. The cell
430
+ * limit applies to the result, and a separate limit (60,000 observations, about
431
+ * 20 s) to the download. Errors and "no data" come as HTTP 200 with a footer.
432
+ * - Caching: dataflow and structure messages in the shared cache; the parsed table
433
+ * in memory for an hour (finished values only); the filtered result in the shared
434
+ * cache for an hour. Titles of results carry the data update and retrieval dates.
435
+ */
436
+ export declare function createOspClient(options: OspClientOptions): Required<PxwebClient>;
437
+ interface OnsClientOptions extends HttpOptions {
438
+ /** Search API root, for example "https://api.beta.ons.gov.uk/v1". */
439
+ baseUrl: string;
440
+ /** Upper limit of rows in one query result. */
441
+ maxCells?: number;
442
+ /** Clock for search ranking; defaults to the system time. */
443
+ now?: () => Date;
444
+ }
445
+ /** The series page after this process resolved the id, otherwise an ONS search for its CDID. */
446
+ export declare function onsTableUrl(tableId: string): string;
447
+ /**
448
+ * ONS time series client. One table is one time series, with the id
449
+ * `<DATASET>/<CDID>` in upper case, for example "MM23/D7G7" (CPI annual rate).
450
+ * Lower case input is accepted.
451
+ *
452
+ * - Search uses the live search API (`/search?content_type=timeseries`); there is
453
+ * no crawled index. Hits keep the API order, adjusted for release recency.
454
+ * - An id resolves to its page uri through a search for the CDID; the hit must
455
+ * match both the CDID and the dataset. The requester cache keeps the answer.
456
+ * - Metadata and data come from one request, `https://www.ons.gov.uk/<uri>/data`.
457
+ * The variables are "frequency" (A, Q, M; only those that the series has) and
458
+ * "period" (codes "2025", "2026-Q3", "2026-10"; labels as ONS gives them, for
459
+ * example "2026 JUL-SEP" for a rolling quarter). The title gives the dataset,
460
+ * the unit, the release date and the next release.
461
+ * - `latest` on "period" gives the newest N periods of each selected frequency.
462
+ * - The beta dataset API (`/v1/datasets`) is not used: it is out of date.
463
+ */
464
+ export declare function createOnsClient(options: OnsClientOptions): PxwebClient;
465
+ interface StatbelClientOptions extends HttpOptions, TableListOptions {
466
+ /** be.STAT API root, for example "https://bestat.statbel.fgov.be/bestat/api". */
467
+ baseUrl: string;
468
+ /** Upper limit of cells (rows × indicators) in one result. */
469
+ maxCells?: number;
470
+ /** Clock for search ranking; defaults to the system time. */
471
+ now?: () => Date;
472
+ }
473
+ /**
474
+ * Client for be.STAT, the open data API of Statbel (Belgium).
475
+ *
476
+ * - Table id = be.STAT view id (a UUID). A view exists in one language only, so the
477
+ * same table in another language has another id. Labels follow the view's language.
478
+ * - Search: a local index over `GET /views/` (all 1,377 views in one call) joined with
479
+ * `GET /datasources/` for the subject description (path) and the last data update.
480
+ * Both lists are one call each and cached for an hour, so no crawled snapshot is
481
+ * needed; `listFallback` can still supply one. The list is in no useful order, so
482
+ * the catalogue order is the last data update, newest first: an old copy of a view
483
+ * (same title, a data source that stopped) then ranks below the current one.
484
+ * "Inapplicable" views answer with an empty body and are marked discontinued.
485
+ * - Metadata and query read `GET /views/{id}/result/JSON`, a flat list of facts.
486
+ * String columns are dimensions, number columns are indicators (one variable).
487
+ * Value codes are the trimmed labels; a null cell is the total over that column
488
+ * (code "_T"), so a variable with totals is optional and a left-out optional
489
+ * variable keeps only its total rows when there are any. Time columns (Year, Month,
490
+ * Quarter, Semester in en/nl/fr/de) merge into one "period" variable with codes
491
+ * such as "2026", "2026M10", "2026Q2" and "2026S1".
492
+ * - The query filters the cached facts locally and returns one row per indicator.
493
+ */
494
+ export declare function createStatbelClient(options: StatbelClientOptions): Required<PxwebClient>;
276
495
  interface SingstatClientOptions extends HttpOptions {
277
496
  /** Table Builder API root, for example "https://tablebuilder.singstat.gov.sg/api/table". */
278
497
  baseUrl: string;
@@ -313,6 +532,8 @@ interface SdmxClientOptions extends HttpOptions, TableListOptions {
313
532
  baseUrl: string;
314
533
  /** Agency of the dataflows, for example "ABS". */
315
534
  agency: string;
535
+ /** Sent as Accept-Language, so names come back in each of these languages. */
536
+ languages?: readonly string[];
316
537
  /** Clock for search ranking; defaults to the system time. */
317
538
  now?: () => Date;
318
539
  }
@@ -374,4 +595,4 @@ interface PxwebV2ClientOptions extends HttpOptions, TableListOptions {
374
595
  }
375
596
  /** Client for the PxWeb API v2, with the same interface as the v1 client. */
376
597
  export declare function createPxwebV2Client(options: PxwebV2ClientOptions): PxwebClient;
377
- export { type ApiBaseUrl, type Cache, type CbsODataClientOptions, type EurostatClientOptions, type FlatTable, type JsonStatDataset, type MemoryCacheOptions, type MultiServerEntry, type MultiServerOptions, type PresetServerOptions, type PxstatClientOptions, type PxwebClient, type PxwebClientOptions, type PxwebPreset, type PxwebSelection, type PxwebV2ClientOptions, type QueryPlan, SNAPSHOT_VERSION, type SdmxClientOptions, type SearchAlias, type SearchHit, type SelectionInput, type SingstatClientOptions, type Snapshot, type SnapshotBucket, type SnapshotReader, type StatbankDkClientOptions, type StatcanClientOptions, type TableDoc, type TableIndex, type TableIndexLoaderOptions, type TableIndexOptions, type TableIndexSearchOptions, type TableListOptions, type TableMetadata, type TableSearchHit, type TableSearchOptions, type TableVariable, type UrlSnapshotReaderOptions, bucketSnapshotReader, firstSnapshot, readSnapshot, snapshotKey, urlSnapshotReader, writeSnapshot };
598
+ export { type ApiBaseUrl, type Cache, type CbsODataClientOptions, type CzsoClientOptions, type EurostatClientOptions, type FlatTable, type IbgeClientOptions, type IneEsClientOptions, type InePtClientOptions, type InseeClientOptions, type JsonStatDataset, type MemoryCacheOptions, type MultiServerEntry, type MultiServerOptions, type OnsClientOptions, type OspClientOptions, type PrebuiltSearchIndex, type PresetServerOptions, type PxstatClientOptions, type PxwebClient, type PxwebClientOptions, type PxwebPreset, type PxwebSelection, type PxwebV2ClientOptions, type QueryPlan, SNAPSHOT_VERSION, type SdmxClientOptions, type SearchAlias, type SearchHit, type SelectionInput, type SingstatClientOptions, type Snapshot, type SnapshotBucket, type SnapshotReader, type StatbankDkClientOptions, type StatbelClientOptions, type StatcanClientOptions, type TableDoc, type TableIndex, type TableIndexLoaderOptions, type TableIndexOptions, type TableIndexSearchOptions, type TableListOptions, type TableMetadata, type TableSearchHit, type TableSearchOptions, type TableVariable, type UrlSnapshotReaderOptions, bucketSnapshotReader, firstSnapshot, readSnapshot, snapshotKey, urlSnapshotReader, writeSnapshot };