bharatlas-mcp 1.4.0 → 1.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/import-records.js +36 -0
- package/import-records.test.js +43 -0
- package/index.js +15 -5
- package/package.json +5 -1
- package/server.json +2 -2
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// import_points: give each record its client_id (the row's own id, scoped to
|
|
2
|
+
// the map) so re-running an import adds nothing and a corrected re-run updates
|
|
3
|
+
// rows in place. The server enforces it; this only picks the id out of the input.
|
|
4
|
+
|
|
5
|
+
const idText = (v) => {
|
|
6
|
+
if (typeof v === "number") return Number.isFinite(v) ? String(v) : undefined;
|
|
7
|
+
return typeof v === "string" && v.trim() !== "" ? v : undefined;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Where each record's client_id comes from, first match wins:
|
|
12
|
+
* 1. `client_id` already on the record;
|
|
13
|
+
* 2. with `clientIdField` (a non-empty string): that property;
|
|
14
|
+
* without it: the GeoJSON feature `id`, then an `id` property.
|
|
15
|
+
* An empty `clientIdField` turns the default off. Records with no usable id are
|
|
16
|
+
* sent without one (the server adds them again on a re-run).
|
|
17
|
+
*/
|
|
18
|
+
export function withClientIds(records, clientIdField) {
|
|
19
|
+
return records.map((r) => {
|
|
20
|
+
if (!r || typeof r !== "object" || r.client_id !== undefined) return r;
|
|
21
|
+
const props = r.properties && typeof r.properties === "object" ? r.properties : {};
|
|
22
|
+
const id = clientIdField === undefined || clientIdField === null
|
|
23
|
+
? idText(r.id) ?? idText(props.id)
|
|
24
|
+
: clientIdField === "" ? undefined : idText(props[clientIdField]);
|
|
25
|
+
return id === undefined ? r : { ...r, client_id: id };
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One line for the tool result: what the import did. */
|
|
30
|
+
export function importSummary(res, withoutId) {
|
|
31
|
+
const refused = res.errors?.length ?? 0;
|
|
32
|
+
return [
|
|
33
|
+
`${res.added ?? res.imported ?? 0} added, ${res.updated ?? 0} updated, ${res.unchanged ?? 0} unchanged of ${res.total} rows${refused ? `; ${refused} refused (see errors)` : ""}.`,
|
|
34
|
+
withoutId ? `${withoutId} row${withoutId === 1 ? "" : "s"} had no client_id, so re-running adds ${withoutId === 1 ? "it" : "them"} again.` : "",
|
|
35
|
+
].filter(Boolean).join(" ");
|
|
36
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Unit tests for import_points' record shaping (no network). Run: node --test
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { withClientIds, importSummary } from "./import-records.js";
|
|
5
|
+
|
|
6
|
+
const g = { type: "Point", coordinates: [77.59, 12.97] };
|
|
7
|
+
|
|
8
|
+
test("client_id_field names the property that carries each row's id", () => {
|
|
9
|
+
const out = withClientIds([{ geometry: g, properties: { code: 101, name: "A" } }], "code");
|
|
10
|
+
assert.equal(out[0].client_id, "101");
|
|
11
|
+
assert.deepEqual(out[0].properties, { code: 101, name: "A" });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("by default the feature id is the client id, then an `id` property", () => {
|
|
15
|
+
const out = withClientIds([
|
|
16
|
+
{ type: "Feature", id: "f-1", geometry: g, properties: { id: "p-1" } },
|
|
17
|
+
{ geometry: g, properties: { id: 7 } },
|
|
18
|
+
{ geometry: g, properties: { name: "no id" } },
|
|
19
|
+
]);
|
|
20
|
+
assert.deepEqual(out.map((r) => r.client_id), ["f-1", "7", undefined]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("a client_id already on the record wins; an empty client_id_field turns the default off", () => {
|
|
24
|
+
assert.equal(withClientIds([{ client_id: "mine", id: "f", geometry: g, properties: {} }], "id")[0].client_id, "mine");
|
|
25
|
+
assert.equal(withClientIds([{ id: "f", geometry: g, properties: { id: "p" } }], "")[0].client_id, undefined);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("blank and non-scalar ids are left out (the server would refuse them)", () => {
|
|
29
|
+
const out = withClientIds([
|
|
30
|
+
{ geometry: g, properties: { code: " " } },
|
|
31
|
+
{ geometry: g, properties: { code: { a: 1 } } },
|
|
32
|
+
{ geometry: g, properties: { code: Number.NaN } },
|
|
33
|
+
], "code");
|
|
34
|
+
assert.deepEqual(out.map((r) => r.client_id), [undefined, undefined, undefined]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("the summary names added, updated and unchanged", () => {
|
|
38
|
+
assert.equal(
|
|
39
|
+
importSummary({ added: 1, updated: 2, unchanged: 3, total: 7, errors: [{ index: 0, error: "x" }] }, 1),
|
|
40
|
+
"1 added, 2 updated, 3 unchanged of 7 rows; 1 refused (see errors). 1 row had no client_id, so re-running adds it again.",
|
|
41
|
+
);
|
|
42
|
+
assert.equal(importSummary({ added: 3, updated: 0, unchanged: 0, total: 3, errors: [] }, 0), "3 added, 0 updated, 0 unchanged of 3 rows.");
|
|
43
|
+
});
|
package/index.js
CHANGED
|
@@ -11,6 +11,7 @@ import { readFileSync, writeFileSync, existsSync } from "node:fs";
|
|
|
11
11
|
import { homedir } from "node:os";
|
|
12
12
|
import { join } from "node:path";
|
|
13
13
|
import { INSTRUCTIONS_CORE, READ_TOOLS, readToolRequest, shapeReadResult, toSearchParams } from "./read-tools.js";
|
|
14
|
+
import { withClientIds, importSummary } from "./import-records.js";
|
|
14
15
|
|
|
15
16
|
const API = process.env.BHARATLAS_API || "https://bharatlas.com/api/v1";
|
|
16
17
|
|
|
@@ -93,14 +94,20 @@ const TOOLS = [
|
|
|
93
94
|
name: "import_points",
|
|
94
95
|
description:
|
|
95
96
|
"Bulk-import points you already hold into a collect map (needs the admin link). NOT for synthetic data. " +
|
|
96
|
-
"Requires a named source + rights confirmation; each row lands with that source as its provenance."
|
|
97
|
+
"Requires a named source + rights confirmation; each row lands with that source as its provenance. " +
|
|
98
|
+
"Re-running is safe for rows with a client id (the row's own stable id, unique within the map, up to 128 characters): " +
|
|
99
|
+
"a row already imported with that id is updated in place if its data changed and otherwise left alone, so the same import twice adds nothing. " +
|
|
100
|
+
"The client id is the record's `client_id`, else the property named by client_id_field, else (by default) the GeoJSON feature `id`, then an `id` property. " +
|
|
101
|
+
"Rows without one are added again on every run. The result says how many were added, updated and unchanged (per batch of 100 in `batches`). " +
|
|
102
|
+
"Up to 5000 records per call.",
|
|
97
103
|
inputSchema: {
|
|
98
104
|
type: "object",
|
|
99
105
|
properties: {
|
|
100
106
|
id: { type: "string" },
|
|
101
|
-
records: { type: "array", items: { type: "object" }, description: "GeoJSON-like {geometry, properties}" },
|
|
107
|
+
records: { type: "array", items: { type: "object" }, description: "GeoJSON-like {geometry, properties}, optionally with client_id" },
|
|
102
108
|
source: { type: "string" },
|
|
103
109
|
rights_confirmed: { type: "boolean", description: "you have the right to publish this under the map's licence" },
|
|
110
|
+
client_id_field: { type: "string", description: "property holding each row's stable id (e.g. \"code\"); default: feature id, then an `id` property; \"\" for none" },
|
|
104
111
|
},
|
|
105
112
|
required: ["id", "records", "source", "rights_confirmed"],
|
|
106
113
|
},
|
|
@@ -287,11 +294,14 @@ async function handleTool(name, args) {
|
|
|
287
294
|
body: { status: args.status, reason: args.reason },
|
|
288
295
|
});
|
|
289
296
|
|
|
290
|
-
case "import_points":
|
|
291
|
-
|
|
297
|
+
case "import_points": {
|
|
298
|
+
const records = withClientIds(Array.isArray(args.records) ? args.records : [], args.client_id_field);
|
|
299
|
+
const res = await collectApi("POST", `/collections/${args.id}/import`, {
|
|
292
300
|
token: tokenFor(args.id, "admin"),
|
|
293
|
-
body: { records
|
|
301
|
+
body: { records, source: args.source, rights_confirmed: !!args.rights_confirmed },
|
|
294
302
|
});
|
|
303
|
+
return { summary: importSummary(res, records.filter((r) => r?.client_id === undefined).length), ...res };
|
|
304
|
+
}
|
|
295
305
|
|
|
296
306
|
case "edit_map": {
|
|
297
307
|
const { id, ...patch } = args;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bharatlas-mcp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "MCP server for India's open geo data. Query layers, locate points, find nearby features, filter and group data. Also authors collect maps: register a share link, moderate contributions, and publish to the catalog.",
|
|
5
5
|
"keywords": ["mcp", "india", "geo", "geospatial", "gis", "boundaries", "parquet", "pmtiles", "llm", "claude", "bharatlas", "collect"],
|
|
6
6
|
"repository": { "type": "git", "url": "git+https://github.com/urbanmorph/geodata.git", "directory": "mcp" },
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
"mcpName": "io.github.sathya-sankaran/bharatlas-mcp",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "index.js",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "node --test import-records.test.js",
|
|
13
|
+
"test:live": "node test.js"
|
|
14
|
+
},
|
|
11
15
|
"bin": {
|
|
12
16
|
"bharatlas-mcp": "index.js"
|
|
13
17
|
},
|
package/server.json
CHANGED