@surfaces-ui/engine-mcp 0.0.0-stage → 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/sql.js ADDED
@@ -0,0 +1,123 @@
1
+ import { EngineError } from "@surfaces-ui/engine";
2
+ import { readRows, toolError } from "./parse.js";
3
+ const SQL_ARGUMENTS = ["sql", "query", "statement"];
4
+ /** Query tools of the servers this was written against, most specific first; a read-only form is preferred where a server has both. */
5
+ const KNOWN = ["base_readQuery", "execute_sql_read_only", "execute_sql_readonly", "execute_query", "execute_sql", "run_snowflake_query", "mysql_query", "read_query", "query", "run-sql"];
6
+ /**
7
+ * Picks the tool that runs SQL: one with a text argument named `sql`,
8
+ * `query` or `statement`. The argument is not required to be marked
9
+ * required, since several servers leave that out.
10
+ */
11
+ export function findQueryTool(tools, wanted) {
12
+ const argumentOf = (tool) => {
13
+ const properties = tool.inputSchema?.properties ?? {};
14
+ return wanted?.argument ?? SQL_ARGUMENTS.find((name) => name in properties);
15
+ };
16
+ if (wanted?.tool) {
17
+ const tool = tools.find((t) => t.name === wanted.tool);
18
+ if (!tool)
19
+ throw new EngineError("UNSUPPORTED", `The data server has no tool named ${wanted.tool}. It lists: ${tools.map((t) => t.name).join(", ") || "none"}.`);
20
+ return { tool: tool.name, argument: argumentOf(tool) ?? "sql" };
21
+ }
22
+ const candidates = tools.filter((tool) => argumentOf(tool) !== undefined);
23
+ const rank = (tool) => {
24
+ const known = KNOWN.indexOf(tool.name);
25
+ if (known !== -1)
26
+ return known;
27
+ if (/read[_-]?only/i.test(tool.name))
28
+ return KNOWN.length;
29
+ return /(sql|query)/i.test(tool.name) ? KNOWN.length + 1 : KNOWN.length + 2;
30
+ };
31
+ const best = candidates.sort((a, b) => rank(a) - rank(b))[0];
32
+ if (!best) {
33
+ throw new EngineError("UNSUPPORTED", `The data server lists no tool that takes SQL (an argument named sql, query or statement). Name one with "tool". It lists: ${tools.map((t) => t.name).join(", ") || "none"}.`);
34
+ }
35
+ return { tool: best.name, argument: argumentOf(best) };
36
+ }
37
+ /**
38
+ * Runs statements through a server's query tool, however the server is
39
+ * reached. `open` is called once, on first use, and again after it fails.
40
+ */
41
+ export function sqlOver(open, options = {}) {
42
+ let opened;
43
+ const ready = () => {
44
+ opened ??= (async () => {
45
+ let caller;
46
+ try {
47
+ caller = await open();
48
+ const tools = await caller.list();
49
+ return { caller, ...findQueryTool(tools, options), tools: tools.map((t) => t.name) };
50
+ }
51
+ catch (error) {
52
+ await caller?.close?.().catch(() => { });
53
+ opened = undefined;
54
+ if (error instanceof EngineError)
55
+ throw error;
56
+ // How a server is reached can hold a credential (a command line, an address), so the reason is not passed on.
57
+ throw new EngineError("ENGINE", "The data server could not be reached. Check that it is running and that its address or command is right.");
58
+ }
59
+ })();
60
+ return opened;
61
+ };
62
+ return {
63
+ async query(sql, queryOptions = {}) {
64
+ const { caller, tool, argument } = await ready();
65
+ let answer;
66
+ try {
67
+ answer = await caller.call(tool, { ...options.arguments, [argument]: sql }, { signal: queryOptions.signal });
68
+ }
69
+ catch (error) {
70
+ if (queryOptions.signal?.aborted)
71
+ throw new EngineError("CANCELLED", "The query was cancelled.");
72
+ if (error instanceof EngineError)
73
+ throw error;
74
+ // A server that reports a failed query as a protocol error: its message is the database's.
75
+ throw databaseError(error instanceof Error ? error.message : String(error?.message ?? error));
76
+ }
77
+ const failed = toolError(answer);
78
+ if (failed !== undefined)
79
+ throw databaseError(failed);
80
+ try {
81
+ return readRows(answer);
82
+ }
83
+ catch (error) {
84
+ throw new EngineError("UNSUPPORTED", error instanceof Error ? error.message : String(error));
85
+ }
86
+ },
87
+ async describe() {
88
+ const { caller, tool, argument, tools } = await ready();
89
+ return { tool, argument, tools, server: caller.server ?? "" };
90
+ },
91
+ async close() {
92
+ const current = opened;
93
+ opened = undefined;
94
+ if (current)
95
+ await current.then(({ caller }) => caller.close?.()).catch(() => { });
96
+ },
97
+ };
98
+ }
99
+ /** A database's error message as an EngineError with a code the UI understands. */
100
+ export function databaseError(message) {
101
+ // A driver's message can run on into a stack trace; the first line is the database's own.
102
+ const clean = (message.split("\n")[0] ?? "")
103
+ .replace(/^MCP error -?\d+:\s*/i, "")
104
+ .replace(/^Internal error:\s*/i, "")
105
+ .replace(/^\([\w.]+\)\s*/, "")
106
+ .replace(/^(Error|Binder Error|Catalog Error|Parser Error|Conversion Error):\s*/i, "")
107
+ .trim();
108
+ // The message is another server's text, so it is searched for plain words, never matched with a pattern that can backtrack over it.
109
+ const text = message.toLowerCase();
110
+ const has = (...words) => words.some((word) => text.includes(word));
111
+ const missing = has("does not exist", "not found", "doesn't exist");
112
+ const code = has("timed out", "timeout", "time out")
113
+ ? "TIMEOUT"
114
+ : has("syntax error", "parser error", "parse error", "[error 3706]")
115
+ ? "SYNTAX"
116
+ : has("unknown column", "referenced column", "[error 3810]") || (has("column") && missing)
117
+ ? "UNKNOWN_COLUMN"
118
+ : has("[error 3807]") || ((has("relation", "table", "object") || has("doesn't exist")) && missing)
119
+ ? "UNKNOWN_TABLE"
120
+ : "ENGINE";
121
+ return new EngineError(code, clean || "The data server reported an error.");
122
+ }
123
+ //# sourceMappingURL=sql.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sql.js","sourceRoot":"","sources":["../src/sql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAiC,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAwChF,MAAM,aAAa,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;AACpD,uIAAuI;AACvI,MAAM,KAAK,GAAG,CAAC,gBAAgB,EAAE,uBAAuB,EAAE,sBAAsB,EAAE,eAAe,EAAE,aAAa,EAAE,qBAAqB,EAAE,aAAa,EAAE,YAAY,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AAE1L;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,KAAmB,EAA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rF,IAAI,EAAE,CAAC;IACV,oIAAoI;IACpI,MAAM,IAAI,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IACnC,MAAM,GAAG,GAAG,CAAC,GAAG,KAAe,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9E,MAAM,OAAO,GAAG,GAAG,CAAC,gBAAgB,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC;IACpE,MAAM,IAAI,GAAG,GAAG,CAAC,WAAW,EAAE,SAAS,EAAE,UAAU,CAAC;QAClD,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,GAAG,CAAC,cAAc,EAAE,cAAc,EAAE,aAAa,EAAE,cAAc,CAAC;YAClE,CAAC,CAAC,QAAQ;YACV,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,mBAAmB,EAAE,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,OAAO,CAAC;gBACxF,CAAC,CAAC,gBAAgB;gBAClB,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,CAAC,IAAI,GAAG,CAAC,eAAe,CAAC,CAAC,IAAI,OAAO,CAAC;oBAChG,CAAC,CAAC,eAAe;oBACjB,CAAC,CAAC,QAAQ,CAAC;IACnB,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE,KAAK,IAAI,oCAAoC,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["import { EngineError } from \"@surfaces-ui/engine\";\nimport { type SqlRows, type ToolAnswer, readRows, toolError } from \"./parse.js\";\n\n/** What every way of running a query tool takes, whether through an MCP connection or a host that holds one. */\nexport interface QueryToolOptions {\n /** The server's tool that runs a query. Found by its shape when left out. */\n tool?: string;\n /** The tool's argument that takes the SQL. Found from the tool's schema when left out. */\n argument?: string;\n /** Other arguments sent with every query, such as a project id or a row limit. Never a credential. */\n arguments?: Record<string, unknown>;\n}\n\n/**\n * A server's tools as something else reaches them: an MCP connection this\n * process opened (`mcpSql`), or a host that already holds the connection and\n * calls tools for a page (a Claude artifact's connectors). `call` rejects\n * with the server's message when a tool fails.\n */\nexport interface ToolCaller {\n /** The server's tools; a tool's `inputSchema` may be missing where the host does not give it. */\n list(): Promise<ListedTool[]>;\n call(tool: string, input: Record<string, unknown>, options: { signal?: AbortSignal }): Promise<ToolAnswer>;\n /** The server's name, for `describe`. */\n server?: string;\n close?(): Promise<void>;\n}\n\n/** A database's MCP server, used for one thing: running a statement and reading its rows. */\nexport interface McpSql {\n query(sql: string, options?: { signal?: AbortSignal }): Promise<SqlRows>;\n /** The query tool in use and the other tools the server lists, once connected. */\n describe(): Promise<{ tool: string; argument: string; tools: string[]; server: string }>;\n close(): Promise<void>;\n}\n\nexport interface ListedTool {\n name: string;\n inputSchema?: { properties?: Record<string, { type?: unknown }> };\n}\n\nconst SQL_ARGUMENTS = [\"sql\", \"query\", \"statement\"];\n/** Query tools of the servers this was written against, most specific first; a read-only form is preferred where a server has both. */\nconst KNOWN = [\"base_readQuery\", \"execute_sql_read_only\", \"execute_sql_readonly\", \"execute_query\", \"execute_sql\", \"run_snowflake_query\", \"mysql_query\", \"read_query\", \"query\", \"run-sql\"];\n\n/**\n * Picks the tool that runs SQL: one with a text argument named `sql`,\n * `query` or `statement`. The argument is not required to be marked\n * required, since several servers leave that out.\n */\nexport function findQueryTool(tools: ListedTool[], wanted?: { tool?: string; argument?: string }): { tool: string; argument: string } {\n const argumentOf = (tool: ListedTool) => {\n const properties = tool.inputSchema?.properties ?? {};\n return wanted?.argument ?? SQL_ARGUMENTS.find((name) => name in properties);\n };\n if (wanted?.tool) {\n const tool = tools.find((t) => t.name === wanted.tool);\n if (!tool) throw new EngineError(\"UNSUPPORTED\", `The data server has no tool named ${wanted.tool}. It lists: ${tools.map((t) => t.name).join(\", \") || \"none\"}.`);\n return { tool: tool.name, argument: argumentOf(tool) ?? \"sql\" };\n }\n const candidates = tools.filter((tool) => argumentOf(tool) !== undefined);\n const rank = (tool: ListedTool) => {\n const known = KNOWN.indexOf(tool.name);\n if (known !== -1) return known;\n if (/read[_-]?only/i.test(tool.name)) return KNOWN.length;\n return /(sql|query)/i.test(tool.name) ? KNOWN.length + 1 : KNOWN.length + 2;\n };\n const best = candidates.sort((a, b) => rank(a) - rank(b))[0];\n if (!best) {\n throw new EngineError(\"UNSUPPORTED\", `The data server lists no tool that takes SQL (an argument named sql, query or statement). Name one with \"tool\". It lists: ${tools.map((t) => t.name).join(\", \") || \"none\"}.`);\n }\n return { tool: best.name, argument: argumentOf(best)! };\n}\n\n/**\n * Runs statements through a server's query tool, however the server is\n * reached. `open` is called once, on first use, and again after it fails.\n */\nexport function sqlOver(open: () => Promise<ToolCaller>, options: QueryToolOptions = {}): McpSql {\n let opened: Promise<{ caller: ToolCaller; tool: string; argument: string; tools: string[] }> | undefined;\n const ready = () => {\n opened ??= (async () => {\n let caller: ToolCaller | undefined;\n try {\n caller = await open();\n const tools = await caller.list();\n return { caller, ...findQueryTool(tools, options), tools: tools.map((t) => t.name) };\n } catch (error) {\n await caller?.close?.().catch(() => {});\n opened = undefined;\n if (error instanceof EngineError) throw error;\n // How a server is reached can hold a credential (a command line, an address), so the reason is not passed on.\n throw new EngineError(\"ENGINE\", \"The data server could not be reached. Check that it is running and that its address or command is right.\");\n }\n })();\n return opened;\n };\n return {\n async query(sql, queryOptions = {}) {\n const { caller, tool, argument } = await ready();\n let answer: ToolAnswer;\n try {\n answer = await caller.call(tool, { ...options.arguments, [argument]: sql }, { signal: queryOptions.signal });\n } catch (error) {\n if (queryOptions.signal?.aborted) throw new EngineError(\"CANCELLED\", \"The query was cancelled.\");\n if (error instanceof EngineError) throw error;\n // A server that reports a failed query as a protocol error: its message is the database's.\n throw databaseError(error instanceof Error ? error.message : String((error as { message?: unknown } | null)?.message ?? error));\n }\n const failed = toolError(answer);\n if (failed !== undefined) throw databaseError(failed);\n try {\n return readRows(answer);\n } catch (error) {\n throw new EngineError(\"UNSUPPORTED\", error instanceof Error ? error.message : String(error));\n }\n },\n async describe() {\n const { caller, tool, argument, tools } = await ready();\n return { tool, argument, tools, server: caller.server ?? \"\" };\n },\n async close() {\n const current = opened;\n opened = undefined;\n if (current) await current.then(({ caller }) => caller.close?.()).catch(() => {});\n },\n };\n}\n\n/** A database's error message as an EngineError with a code the UI understands. */\nexport function databaseError(message: string): EngineError {\n // A driver's message can run on into a stack trace; the first line is the database's own.\n const clean = (message.split(\"\\n\")[0] ?? \"\")\n .replace(/^MCP error -?\\d+:\\s*/i, \"\")\n .replace(/^Internal error:\\s*/i, \"\")\n .replace(/^\\([\\w.]+\\)\\s*/, \"\")\n .replace(/^(Error|Binder Error|Catalog Error|Parser Error|Conversion Error):\\s*/i, \"\")\n .trim();\n // The message is another server's text, so it is searched for plain words, never matched with a pattern that can backtrack over it.\n const text = message.toLowerCase();\n const has = (...words: string[]) => words.some((word) => text.includes(word));\n const missing = has(\"does not exist\", \"not found\", \"doesn't exist\");\n const code = has(\"timed out\", \"timeout\", \"time out\")\n ? \"TIMEOUT\"\n : has(\"syntax error\", \"parser error\", \"parse error\", \"[error 3706]\")\n ? \"SYNTAX\"\n : has(\"unknown column\", \"referenced column\", \"[error 3810]\") || (has(\"column\") && missing)\n ? \"UNKNOWN_COLUMN\"\n : has(\"[error 3807]\") || ((has(\"relation\", \"table\", \"object\") || has(\"doesn't exist\")) && missing)\n ? \"UNKNOWN_TABLE\"\n : \"ENGINE\";\n return new EngineError(code, clean || \"The data server reported an error.\");\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,68 @@
1
1
  {
2
2
  "name": "@surfaces-ui/engine-mcp",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.5.0",
4
+ "description": "A database's own MCP server as a Surfaces UI engine: sorts, filters and pages run as SQL through that server's query tool (Teradata, Postgres, MySQL, DuckDB)",
5
+ "keywords": [
6
+ "surfaces-ui",
7
+ "analytics",
8
+ "data",
9
+ "mcp",
10
+ "sql",
11
+ "teradata",
12
+ "postgres"
13
+ ],
14
+ "license": "Apache-2.0",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/teradata-labs/surfaces-ui.git",
18
+ "directory": "packages/engine-mcp"
19
+ },
20
+ "homepage": "https://github.com/teradata-labs/surfaces-ui#readme",
21
+ "bugs": {
22
+ "url": "https://github.com/teradata-labs/surfaces-ui/issues"
23
+ },
24
+ "type": "module",
25
+ "main": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "default": "./dist/index.js"
31
+ },
32
+ "./node": {
33
+ "types": "./dist/node.d.ts",
34
+ "default": "./dist/node.js"
35
+ },
36
+ "./hosted": {
37
+ "types": "./dist/hosted.d.ts",
38
+ "default": "./dist/hosted.js"
39
+ }
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "NOTICE"
44
+ ],
45
+ "dependencies": {
46
+ "@bufbuild/protobuf": "^2.17.0",
47
+ "@modelcontextprotocol/client": "^2.4.0",
48
+ "apache-arrow": "^21.2.0",
49
+ "@surfaces-ui/engine": "0.5.0",
50
+ "@surfaces-ui/engine-teradata": "0.5.0",
51
+ "@surfaces-ui/proto": "0.5.0",
52
+ "@surfaces-ui/sql": "0.5.0"
53
+ },
54
+ "devDependencies": {
55
+ "@modelcontextprotocol/server": "^2.4.0",
56
+ "@types/node": "^26.6.6",
57
+ "zod": "^4.6.7"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public",
61
+ "provenance": true
62
+ },
63
+ "scripts": {
64
+ "build": "tsc -p tsconfig.build.json",
65
+ "typecheck": "tsc -p tsconfig.json --noEmit",
66
+ "test": "vitest run --passWithNoTests"
67
+ }
6
68
  }