@statsmcp/core 0.4.0 → 0.4.1
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 +1 -0
- package/dist/index.js +89 -42
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -667,6 +667,18 @@ const DEFAULT_DATASETS_URL = "https://datasets.cbs.nl/odata/v1/CBS";
|
|
|
667
667
|
function isNotFound(err) {
|
|
668
668
|
return err instanceof PxwebError && NOT_FOUND_STATUSES$4.has(err.status);
|
|
669
669
|
}
|
|
670
|
+
function isTransient(err) {
|
|
671
|
+
if (!(err instanceof PxwebError)) return true;
|
|
672
|
+
return err.status === 429 || err.status >= 500;
|
|
673
|
+
}
|
|
674
|
+
function sameOriginLink(next, current) {
|
|
675
|
+
if (!next) return void 0;
|
|
676
|
+
try {
|
|
677
|
+
return new URL(next).origin === new URL(current).origin ? next : void 0;
|
|
678
|
+
} catch {
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
670
682
|
function notFound$5(tableId) {
|
|
671
683
|
return new PxwebError(`Table "${tableId}" was not found. Use search_tables to find a table id.`, 404);
|
|
672
684
|
}
|
|
@@ -682,7 +694,7 @@ function odataUrl(base, params) {
|
|
|
682
694
|
...params
|
|
683
695
|
}).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join("&")}`;
|
|
684
696
|
}
|
|
685
|
-
function checkTableId$
|
|
697
|
+
function checkTableId$4(tableId) {
|
|
686
698
|
if (!TABLE_ID_PATTERN$3.test(tableId)) throw new PxwebError(`Invalid table id "${tableId}". Use the id exactly as search_tables returns it.`, 400);
|
|
687
699
|
}
|
|
688
700
|
function selectedCodes$2(variable, selection) {
|
|
@@ -744,7 +756,7 @@ function createCbsODataClient(options) {
|
|
|
744
756
|
for (let page = 0; url && page < MAX_KEY_PAGES; page++) {
|
|
745
757
|
const data = await request(url, get, keysSchema, (parsed) => parsed, METADATA_TTL_SECONDS, `meta:${url}`);
|
|
746
758
|
keys.push(...data.value);
|
|
747
|
-
url = data["odata.nextLink"];
|
|
759
|
+
url = sameOriginLink(data["odata.nextLink"], firstUrl);
|
|
748
760
|
}
|
|
749
761
|
return keys;
|
|
750
762
|
}
|
|
@@ -789,7 +801,7 @@ function createCbsODataClient(options) {
|
|
|
789
801
|
for (let page = 0; url && page < MAX_KEY_PAGES; page++) {
|
|
790
802
|
const data = await request(url, get, v4CodesSchema, (parsed) => parsed, METADATA_TTL_SECONDS, `meta:${url}`);
|
|
791
803
|
codes.push(...data.value);
|
|
792
|
-
url = data["@odata.nextLink"];
|
|
804
|
+
url = sameOriginLink(data["@odata.nextLink"], firstUrl);
|
|
793
805
|
}
|
|
794
806
|
return codes;
|
|
795
807
|
}
|
|
@@ -838,7 +850,7 @@ function createCbsODataClient(options) {
|
|
|
838
850
|
};
|
|
839
851
|
}
|
|
840
852
|
async function table(tableId) {
|
|
841
|
-
checkTableId$
|
|
853
|
+
checkTableId$4(tableId);
|
|
842
854
|
try {
|
|
843
855
|
return await loadTable(tableId);
|
|
844
856
|
} catch (err) {
|
|
@@ -846,7 +858,7 @@ function createCbsODataClient(options) {
|
|
|
846
858
|
try {
|
|
847
859
|
return await loadV4Table(tableId);
|
|
848
860
|
} catch (v4Err) {
|
|
849
|
-
if (isNotFound(v4Err)) throw notFound$5(tableId);
|
|
861
|
+
if (isNotFound(v4Err)) throw isTransient(err) ? err : notFound$5(tableId);
|
|
850
862
|
throw v4Err;
|
|
851
863
|
}
|
|
852
864
|
}
|
|
@@ -934,7 +946,7 @@ function createCbsODataClient(options) {
|
|
|
934
946
|
};
|
|
935
947
|
}
|
|
936
948
|
// src/eurostat-client.ts
|
|
937
|
-
const CODE_PATTERN = /^[A-Za-z0-9_$.-]+$/;
|
|
949
|
+
const CODE_PATTERN = /^(?!\.{1,2}$)[A-Za-z0-9_$.-]+$/;
|
|
938
950
|
const CLOSED_TITLE = /\(\d{4}\s*-\s*\d{4}\)\s*$/;
|
|
939
951
|
const TIME_CODE$2 = "time";
|
|
940
952
|
const NOT_FOUND_STATUSES$3 = /* @__PURE__ */ new Set([400, 404]);
|
|
@@ -1154,13 +1166,15 @@ function createEurostatClient(options) {
|
|
|
1154
1166
|
isTime: false
|
|
1155
1167
|
}));
|
|
1156
1168
|
const periods = periodsBetween(entry.start, entry.end);
|
|
1169
|
+
const incomplete = stepOf(entry.end) === void 0 || !periodParts(entry.start) || !periodParts(entry.end);
|
|
1157
1170
|
variables.push({
|
|
1158
1171
|
code: TIME_CODE$2,
|
|
1159
1172
|
label: TIME_CODE$2,
|
|
1160
1173
|
values: periods,
|
|
1161
1174
|
valueLabels: periods,
|
|
1162
1175
|
eliminable: false,
|
|
1163
|
-
isTime: true
|
|
1176
|
+
isTime: true,
|
|
1177
|
+
...incomplete && { periodsIncomplete: true }
|
|
1164
1178
|
});
|
|
1165
1179
|
return {
|
|
1166
1180
|
title: entry.title,
|
|
@@ -1252,7 +1266,7 @@ function toTable(data) {
|
|
|
1252
1266
|
function notFound$3(tableId) {
|
|
1253
1267
|
return new PxwebError(`Table "${tableId}" was not found. Use search_tables to find a table id.`, 404);
|
|
1254
1268
|
}
|
|
1255
|
-
function checkTableId$
|
|
1269
|
+
function checkTableId$3(tableId) {
|
|
1256
1270
|
if (!MATRIX_PATTERN.test(tableId)) throw new PxwebError(`Invalid table id "${tableId}". Use the id exactly as search_tables returns it.`, 400);
|
|
1257
1271
|
}
|
|
1258
1272
|
function requestedCodes(values, selection, total) {
|
|
@@ -1274,7 +1288,7 @@ function createPxstatClient(options) {
|
|
|
1274
1288
|
currentYear: () => now().getUTCFullYear()
|
|
1275
1289
|
});
|
|
1276
1290
|
async function loadTable(lang, tableId) {
|
|
1277
|
-
checkTableId$
|
|
1291
|
+
checkTableId$3(tableId);
|
|
1278
1292
|
const url = `${root}/api.restful/PxStat.Data.Cube_API.ReadMetadata/${tableId}/JSON-stat/2.0/${encodeURIComponent(lang)}`;
|
|
1279
1293
|
try {
|
|
1280
1294
|
return await request(url, { method: "GET" }, metadataSchema$3, toTable, METADATA_TTL_SECONDS, `meta:${url}`);
|
|
@@ -1339,7 +1353,7 @@ const NOT_FOUND_STATUSES$2 = /* @__PURE__ */ new Set([400, 404]);
|
|
|
1339
1353
|
* dashes and diacritics; "." and ".." and URL syntax characters are refused.
|
|
1340
1354
|
*/
|
|
1341
1355
|
const SEGMENT_PATTERN = /^(?!\.{1,2}$)[^\s/\\?#%\p{Cc}\p{Cs}](?:[^/\\?#%\p{Cc}\p{Cs}]*[^\s/\\?#%\p{Cc}\p{Cs}])?$/u;
|
|
1342
|
-
const MAX_TABLE_ID_LENGTH = 400;
|
|
1356
|
+
const MAX_TABLE_ID_LENGTH$1 = 400;
|
|
1343
1357
|
const searchHitSchema = z.object({
|
|
1344
1358
|
id: z.string(),
|
|
1345
1359
|
path: z.string().optional(),
|
|
@@ -1371,7 +1385,7 @@ const metadataSchema$2 = z.object({
|
|
|
1371
1385
|
variables: z.array(variableSchema)
|
|
1372
1386
|
});
|
|
1373
1387
|
function isValidTableId(tableId) {
|
|
1374
|
-
return tableId.length <= MAX_TABLE_ID_LENGTH && tableId.split("/").every((segment) => SEGMENT_PATTERN.test(segment));
|
|
1388
|
+
return tableId.length <= MAX_TABLE_ID_LENGTH$1 && tableId.split("/").every((segment) => SEGMENT_PATTERN.test(segment));
|
|
1375
1389
|
}
|
|
1376
1390
|
const encodePath = (path) => path.split("/").map(encodeURIComponent).join("/");
|
|
1377
1391
|
function rootForLanguage(baseUrl, lang) {
|
|
@@ -1841,7 +1855,8 @@ function toTableStructure(structure, availability, lang) {
|
|
|
1841
1855
|
values: periods,
|
|
1842
1856
|
valueLabels: periods,
|
|
1843
1857
|
eliminable: false,
|
|
1844
|
-
isTime: true
|
|
1858
|
+
isTime: true,
|
|
1859
|
+
...periods.length === 0 && { periodsIncomplete: true }
|
|
1845
1860
|
});
|
|
1846
1861
|
return {
|
|
1847
1862
|
metadata: {
|
|
@@ -2149,7 +2164,7 @@ function unwrap(envelope) {
|
|
|
2149
2164
|
if (status !== 200 || !envelope.Data) throw new PxwebError(`SingStat returned ${status}: ${envelope.Message ?? "no data"}`, status === 200 ? 502 : status);
|
|
2150
2165
|
return envelope.Data;
|
|
2151
2166
|
}
|
|
2152
|
-
function checkTableId$
|
|
2167
|
+
function checkTableId$2(tableId) {
|
|
2153
2168
|
if (!TABLE_ID_PATTERN$1.test(tableId)) throw new PxwebError(`Invalid table id "${tableId}". Use the id exactly as search_tables returns it.`, 400);
|
|
2154
2169
|
}
|
|
2155
2170
|
function notFound$1(tableId) {
|
|
@@ -2221,11 +2236,11 @@ function createSingstatClient(options) {
|
|
|
2221
2236
|
return rankSearchHits([...inTitle, ...hits.filter((hit) => !titled.has(hit.tableId))], now().getUTCFullYear());
|
|
2222
2237
|
},
|
|
2223
2238
|
async getMetadata(_lang, tableId) {
|
|
2224
|
-
checkTableId$
|
|
2239
|
+
checkTableId$2(tableId);
|
|
2225
2240
|
return metadata(tableId);
|
|
2226
2241
|
},
|
|
2227
2242
|
async query(_lang, tableId, selections) {
|
|
2228
|
-
checkTableId$
|
|
2243
|
+
checkTableId$2(tableId);
|
|
2229
2244
|
const table = await metadata(tableId);
|
|
2230
2245
|
const byCode = new Map(selections.map((selection) => [selection.code, selection]));
|
|
2231
2246
|
const [seriesVariable, timeVariable] = table.variables;
|
|
@@ -2338,7 +2353,7 @@ function valueCodes(selection) {
|
|
|
2338
2353
|
if (filter === "all") return ["*"];
|
|
2339
2354
|
return values;
|
|
2340
2355
|
}
|
|
2341
|
-
function checkTableId(tableId) {
|
|
2356
|
+
function checkTableId$1(tableId) {
|
|
2342
2357
|
if (!TABLE_ID_PATTERN.test(tableId)) throw new PxwebError(`Invalid table id "${tableId}". Use the id exactly as search_tables returns it.`, 400);
|
|
2343
2358
|
}
|
|
2344
2359
|
function post$1(body) {
|
|
@@ -2377,7 +2392,7 @@ function createStatbankDkClient(options) {
|
|
|
2377
2392
|
return (await tableIndex(lang)).search(query, searchOptions);
|
|
2378
2393
|
},
|
|
2379
2394
|
async getMetadata(lang, tableId) {
|
|
2380
|
-
checkTableId(tableId);
|
|
2395
|
+
checkTableId$1(tableId);
|
|
2381
2396
|
const url = `${baseUrl}/tableinfo/${encodeURIComponent(tableId)}?${new URLSearchParams({ lang }).toString()}`;
|
|
2382
2397
|
try {
|
|
2383
2398
|
return await request(url, { method: "GET" }, tableInfoSchema, toTableMetadata, METADATA_TTL_SECONDS, `meta:${url}`);
|
|
@@ -2387,7 +2402,7 @@ function createStatbankDkClient(options) {
|
|
|
2387
2402
|
}
|
|
2388
2403
|
},
|
|
2389
2404
|
async query(lang, tableId, selections) {
|
|
2390
|
-
checkTableId(tableId);
|
|
2405
|
+
checkTableId$1(tableId);
|
|
2391
2406
|
const url = `${baseUrl}/data`;
|
|
2392
2407
|
const body = {
|
|
2393
2408
|
table: tableId,
|
|
@@ -2586,13 +2601,15 @@ function createStatcanClient(options) {
|
|
|
2586
2601
|
isTime: false
|
|
2587
2602
|
}));
|
|
2588
2603
|
const periods = periodsOf(listed.cubeStartDate ?? void 0, listed.cubeEndDate ?? void 0, frequency);
|
|
2604
|
+
const incomplete = !REGULAR_FREQUENCIES[frequency ?? ""] || !listed.cubeStartDate;
|
|
2589
2605
|
variables.push({
|
|
2590
2606
|
code: TIME_CODE,
|
|
2591
2607
|
label: french ? "Période de référence" : "Reference period",
|
|
2592
2608
|
values: periods,
|
|
2593
2609
|
valueLabels: periods,
|
|
2594
2610
|
eliminable: false,
|
|
2595
|
-
isTime: true
|
|
2611
|
+
isTime: true,
|
|
2612
|
+
...incomplete && { periodsIncomplete: true }
|
|
2596
2613
|
});
|
|
2597
2614
|
return {
|
|
2598
2615
|
metadata: {
|
|
@@ -2687,27 +2704,6 @@ function createStatcanClient(options) {
|
|
|
2687
2704
|
}
|
|
2688
2705
|
};
|
|
2689
2706
|
}
|
|
2690
|
-
// src/tools/tool-input.ts
|
|
2691
|
-
/** Tool input without the `$schema` URI: clients send it to the model on every turn, and it carries no meaning there. */
|
|
2692
|
-
function toolInput(shape) {
|
|
2693
|
-
const schema = z.object(shape);
|
|
2694
|
-
const standard = schema["~standard"];
|
|
2695
|
-
const { jsonSchema } = standard;
|
|
2696
|
-
schema["~standard"] = {
|
|
2697
|
-
...standard,
|
|
2698
|
-
jsonSchema: {
|
|
2699
|
-
input: (options) => {
|
|
2700
|
-
const { $schema: _uri, ...rest } = jsonSchema.input(options);
|
|
2701
|
-
return rest;
|
|
2702
|
-
},
|
|
2703
|
-
output: (options) => {
|
|
2704
|
-
const { $schema: _uri, ...rest } = jsonSchema.output(options);
|
|
2705
|
-
return rest;
|
|
2706
|
-
}
|
|
2707
|
-
}
|
|
2708
|
-
};
|
|
2709
|
-
return schema;
|
|
2710
|
-
}
|
|
2711
2707
|
// src/query-plan.ts
|
|
2712
2708
|
const DEFAULT_MAX_RESULT_CHARS = 3e4;
|
|
2713
2709
|
const VALUE_CHARS = 10;
|
|
@@ -2750,7 +2746,7 @@ function planQuery(metadata, inputs, maxCells, maxResultChars = DEFAULT_MAX_RESU
|
|
|
2750
2746
|
for (const variable of metadata.variables) {
|
|
2751
2747
|
const input = inputByCode.get(variable.code);
|
|
2752
2748
|
if (input?.latest !== void 0) {
|
|
2753
|
-
const count = Math.min(input.latest, variable.values.length);
|
|
2749
|
+
const count = variable.periodsIncomplete ? input.latest : Math.min(input.latest, variable.values.length);
|
|
2754
2750
|
selections.push({
|
|
2755
2751
|
code: variable.code,
|
|
2756
2752
|
selection: {
|
|
@@ -2809,6 +2805,50 @@ function planQuery(metadata, inputs, maxCells, maxResultChars = DEFAULT_MAX_RESU
|
|
|
2809
2805
|
estimatedChars
|
|
2810
2806
|
};
|
|
2811
2807
|
}
|
|
2808
|
+
const MAX_VALUES_PER_SELECTION = 5e3;
|
|
2809
|
+
function checkLength(name, value, max, hint) {
|
|
2810
|
+
if (value.length > max) throw new QueryPlanError(`${name} is too long (over ${max} characters). ${hint}`);
|
|
2811
|
+
}
|
|
2812
|
+
function checkTableId(tableId) {
|
|
2813
|
+
checkLength("tableId", tableId, 200, "Use the id exactly as search_tables returns it.");
|
|
2814
|
+
}
|
|
2815
|
+
function checkSearchInput(query) {
|
|
2816
|
+
checkLength("query", query, 200, "Use a few short keywords.");
|
|
2817
|
+
}
|
|
2818
|
+
function checkMetadataInput(tableId, valueFilter) {
|
|
2819
|
+
checkTableId(tableId);
|
|
2820
|
+
if (valueFilter !== void 0) checkLength("valueFilter", valueFilter, 100, "Use a short piece of a label.");
|
|
2821
|
+
}
|
|
2822
|
+
function checkQueryInput(tableId, selections) {
|
|
2823
|
+
checkTableId(tableId);
|
|
2824
|
+
if (selections.length > 30) throw new QueryPlanError(`Too many selections (over 30). Leave out variables you do not need.`);
|
|
2825
|
+
for (const selection of selections) {
|
|
2826
|
+
const values = selection.values ?? [];
|
|
2827
|
+
if (values.length > 5e3) throw new QueryPlanError(`Too many values for "${selection.code}" (over ${MAX_VALUES_PER_SELECTION}). Select fewer values or use latest.`);
|
|
2828
|
+
for (const value of values) checkLength("A value code", value, 200, "Use value codes exactly as get_table_metadata returns them.");
|
|
2829
|
+
}
|
|
2830
|
+
}
|
|
2831
|
+
// src/tools/tool-input.ts
|
|
2832
|
+
/** Tool input without the `$schema` URI: clients send it to the model on every turn, and it carries no meaning there. */
|
|
2833
|
+
function toolInput(shape) {
|
|
2834
|
+
const schema = z.object(shape);
|
|
2835
|
+
const standard = schema["~standard"];
|
|
2836
|
+
const { jsonSchema } = standard;
|
|
2837
|
+
schema["~standard"] = {
|
|
2838
|
+
...standard,
|
|
2839
|
+
jsonSchema: {
|
|
2840
|
+
input: (options) => {
|
|
2841
|
+
const { $schema: _uri, ...rest } = jsonSchema.input(options);
|
|
2842
|
+
return rest;
|
|
2843
|
+
},
|
|
2844
|
+
output: (options) => {
|
|
2845
|
+
const { $schema: _uri, ...rest } = jsonSchema.output(options);
|
|
2846
|
+
return rest;
|
|
2847
|
+
}
|
|
2848
|
+
}
|
|
2849
|
+
};
|
|
2850
|
+
return schema;
|
|
2851
|
+
}
|
|
2812
2852
|
// src/tools/tool-result.ts
|
|
2813
2853
|
function textResult(text) {
|
|
2814
2854
|
return { content: [{
|
|
@@ -2868,6 +2908,7 @@ const includeArchivedSchema = z.boolean().optional().describe("Also list discont
|
|
|
2868
2908
|
function searchTables(client, preset, args) {
|
|
2869
2909
|
const { query, lang, limit, includeArchived } = args;
|
|
2870
2910
|
return runTool(async () => {
|
|
2911
|
+
checkSearchInput(query);
|
|
2871
2912
|
const hits = await searchWithAliases(client, preset, lang, query, includeArchived);
|
|
2872
2913
|
if (!hits.length) {
|
|
2873
2914
|
const archived = includeArchived ? [] : await searchWithAliases(client, preset, lang, query, true);
|
|
@@ -2953,16 +2994,22 @@ function sourceLine(preset, language, id) {
|
|
|
2953
2994
|
}
|
|
2954
2995
|
function getTableMetadata(client, preset, args) {
|
|
2955
2996
|
return runTool(async () => {
|
|
2997
|
+
checkMetadataInput(args.tableId, args.valueFilter);
|
|
2956
2998
|
const metadata = await client.getMetadata(args.lang, args.tableId);
|
|
2957
2999
|
return textResult(`${formatMetadata(args.tableId, metadata, args.valueFilter)}\n\nOptional variables can be left out; the result then shows their total. Required variables left out return all their values.\n\n${sourceLine(preset, args.lang, args.tableId)}`);
|
|
2958
3000
|
});
|
|
2959
3001
|
}
|
|
2960
3002
|
function queryTable(client, preset, args) {
|
|
2961
3003
|
return runTool(async () => {
|
|
3004
|
+
checkQueryInput(args.tableId, args.selections);
|
|
2962
3005
|
const metadata = await client.getMetadata(args.lang, args.tableId);
|
|
2963
3006
|
const plan = planQuery(metadata, args.selections, preset.maxCells, preset.maxResultChars);
|
|
2964
3007
|
const table = await client.query(args.lang, args.tableId, plan.selections);
|
|
2965
|
-
|
|
3008
|
+
const csv = toCsv(table);
|
|
3009
|
+
const maxChars = preset.maxResultChars ?? 3e4;
|
|
3010
|
+
if (table.rows.length > preset.maxCells) throw new QueryPlanError(`The result has ${table.rows.length} rows, above the limit of ${preset.maxCells}. Select fewer values or use a smaller latest.`);
|
|
3011
|
+
if (csv.length > maxChars) throw new QueryPlanError(`The result has ${csv.length} characters, above the limit of ${maxChars}. Select fewer values or use a smaller latest.`);
|
|
3012
|
+
return textResult(`${`${table.label ?? metadata.title}. ${table.rows.length} rows.`}\n\n${csv}\n\n${sourceLine(preset, args.lang, args.tableId)}`);
|
|
2966
3013
|
});
|
|
2967
3014
|
}
|
|
2968
3015
|
function registerTableTools(server, client, preset) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statsmcp/core",
|
|
3
|
-
"version": "0.4.
|
|
4
|
-
"description": "MCP server toolkit for PxWeb
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "MCP server toolkit for statistics APIs: PxWeb v1 and v2, StatBank Denmark, CBS OData, PxStat, SDMX, Eurostat, Statistics Canada and SingStat",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Tadej Polajnar",
|
|
7
7
|
"homepage": "https://statsmcp.dev/",
|