@casoon/opengrid-mcp 0.14.0 → 0.16.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@casoon/opengrid-mcp",
3
- "version": "0.14.0",
3
+ "version": "0.16.0",
4
4
  "description": "opengrid as an MCP App: the grid in a chat client, driven by keyboard and by the model through one view",
5
5
  "type": "module",
6
6
  "license": "MIT OR Apache-2.0",
@@ -20,11 +20,11 @@
20
20
  "LICENSE-APACHE"
21
21
  ],
22
22
  "dependencies": {
23
- "@modelcontextprotocol/client": "^2.2.0",
24
- "@modelcontextprotocol/ext-apps": "^2.0.3",
25
- "@modelcontextprotocol/server": "^2.2.0",
26
- "zod": "^4.6.5",
27
- "@casoon/opengrid": "0.14.0"
23
+ "@modelcontextprotocol/client": "^2.4.0",
24
+ "@modelcontextprotocol/ext-apps": "^2.0.4",
25
+ "@modelcontextprotocol/server": "^2.4.0",
26
+ "zod": "^4.6.7",
27
+ "@casoon/opengrid": "0.16.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "esbuild": "0.28.2"
package/src/config.mjs CHANGED
@@ -16,6 +16,15 @@ import { readFileSync } from "node:fs";
16
16
  import { dirname, resolve } from "node:path";
17
17
  import { z } from "zod";
18
18
 
19
+ // A bearer token must not travel in clear text over a network (#242).
20
+ const LOOPBACK = new Set(["localhost", "127.0.0.1", "[::1]"]);
21
+ function isSafeUrl(value) {
22
+ // Not a URL at all: `.url()` says so, and a refinement still runs after it.
23
+ if (!URL.canParse(value)) return true;
24
+ const url = new URL(value);
25
+ return url.protocol === "https:" || (url.protocol === "http:" && LOOPBACK.has(url.hostname));
26
+ }
27
+
19
28
  const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
20
29
 
21
30
  const File = z
@@ -34,7 +43,7 @@ const OnServer = z
34
43
  .object({
35
44
  server: z
36
45
  .object({
37
- url: z.string().url(),
46
+ url: z.string().url().refine(isSafeUrl, { message: "https, or http only for localhost, 127.0.0.1 or [::1]" }),
38
47
  source: z.string().regex(NAME).optional(),
39
48
  tokenEnv: z.string().min(1).optional(),
40
49
  token: z.string().min(1).optional(),
package/src/sessions.mjs CHANGED
@@ -4,8 +4,13 @@
4
4
  // the model's when it next syncs (E40).
5
5
  import { randomUUID } from "node:crypto";
6
6
 
7
+ /** How many sessions the server keeps; the least recently used goes first (#242). */
8
+ export const MAX_SESSIONS = 100;
9
+
7
10
  export class Sessions {
8
- constructor() {
11
+ constructor(max = MAX_SESSIONS) {
12
+ this.max = max;
13
+ // A Map iterates in insertion order: the first key is the least recently used.
9
14
  this.sessions = new Map();
10
15
  }
11
16
 
@@ -26,11 +31,19 @@ export class Sessions {
26
31
  modelRevision: 1,
27
32
  };
28
33
  this.sessions.set(session.id, session);
34
+ while (this.sessions.size > this.max) {
35
+ this.sessions.delete(this.sessions.keys().next().value);
36
+ }
29
37
  return session;
30
38
  }
31
39
 
32
40
  get(id) {
33
- return this.sessions.get(id);
41
+ const session = this.sessions.get(id);
42
+ if (session) {
43
+ this.sessions.delete(id);
44
+ this.sessions.set(id, session);
45
+ }
46
+ return session;
34
47
  }
35
48
 
36
49
  /** The model's view: the selection goes with the old one, as in the grid. */
package/src/tools.mjs CHANGED
@@ -15,9 +15,59 @@ const VIEW_HTML = fileURLToPath(new URL("../dist/view.html", import.meta.url));
15
15
  export const SELECTION_LIMIT = 100;
16
16
  export const SELECTION_MAX = 500;
17
17
 
18
- const View = z.record(z.string(), z.unknown()).describe(
19
- "A grid view, exactly the JSON of opengrid's get_view: sort, filters, columns, density, group, expanded, aggregates, filter_row, facets. Fields left out keep their defaults.",
20
- );
18
+ /** The largest `limit` the grid's provider answers: a window, never a source (#242). */
19
+ export const QUERY_LIMIT_MAX = 500;
20
+
21
+ // ViewInput of loader.d.ts: any part of a view. Unknown keys pass through, so
22
+ // that the engine's own refusal (which names the key) is what the model reads.
23
+ const Value = z.union([z.string(), z.number(), z.boolean(), z.null()]);
24
+ const View = z
25
+ .looseObject({
26
+ sort: z.array(z.object({ field: z.string(), direction: z.enum(["asc", "desc"]) })).optional(),
27
+ filters: z
28
+ .array(
29
+ z.object({
30
+ column: z.string(),
31
+ op: z.enum([
32
+ "eq", "ne", "gt", "gte", "lt", "lte", "contains", "starts_with",
33
+ "is_null", "is_not_null", "has", "has_all", "is_empty",
34
+ ]),
35
+ value: z.string(),
36
+ }),
37
+ )
38
+ .optional(),
39
+ columns: z
40
+ .object({
41
+ order: z.array(z.string()).optional(),
42
+ hidden: z.array(z.string()).optional(),
43
+ widths: z.record(z.string(), z.number()).optional(),
44
+ pinned: z.record(z.string(), z.enum(["start", "end", "none"])).optional(),
45
+ })
46
+ .optional(),
47
+ density: z.enum(["compact", "normal", "comfortable"]).optional(),
48
+ group: z.array(z.string()).optional(),
49
+ expanded: z.array(z.array(Value)).optional(),
50
+ aggregates: z.record(z.string(), z.enum(["sum", "avg", "count", "min", "max", "range"])).optional(),
51
+ filterRow: z.boolean().optional(),
52
+ facets: z
53
+ .record(
54
+ z.string(),
55
+ z.union([
56
+ z.object({ values: z.array(Value) }),
57
+ z.object({ min: z.string(), max: z.string() }),
58
+ z.object({ from: z.string(), to: z.string() }),
59
+ ]),
60
+ )
61
+ .optional(),
62
+ // The nested filter: the wire's filter with `and`, `or` and conditions.
63
+ // An object here, and no more: the engine's own refusal names what it
64
+ // does not take (`not`, `in`, an unknown column), and a recursive schema
65
+ // is more than some hosts read.
66
+ where: z.record(z.string(), z.unknown()).optional().describe(
67
+ 'A filter with "or" across columns, and-ed onto `filters`: {"or":[{"field":"country","op":"eq","value":"DE"},{"field":"amount","op":"gte","value":1000}]}. Values are typed as the column is.',
68
+ ),
69
+ })
70
+ .describe("A grid view, the JSON of opengrid's get_view. Fields left out keep their defaults.");
21
71
 
22
72
  function answer(structured, text) {
23
73
  return { structuredContent: structured, content: [{ type: "text", text: text ?? JSON.stringify(structured) }] };
@@ -218,7 +268,10 @@ export function registerTools(server, data, sessions) {
218
268
  if (query.source !== session.source) {
219
269
  throw new DataError("validation", `the session's source is ${session.source}`, "query.source");
220
270
  }
221
- return { content: [{ type: "text", text: await data.execute(query) }] };
271
+ // The tool is for the grid, but a host that ignores `visibility` shows it
272
+ // to the model: a window is all it may take, never a source in pieces.
273
+ const limit = Math.min(Number.isInteger(query.limit) ? query.limit : QUERY_LIMIT_MAX, QUERY_LIMIT_MAX);
274
+ return { content: [{ type: "text", text: await data.execute({ ...query, limit }) }] };
222
275
  }),
223
276
  );
224
277