@malloy-publisher/server 0.0.232 → 0.0.234
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/README.docker.md +1 -0
- package/dist/app/api-doc.yaml +269 -10
- package/dist/app/assets/{EnvironmentPage-DXEaZIPx.js → EnvironmentPage-DTZQ4Gxc.js} +1 -1
- package/dist/app/assets/{HomePage-kofsqpZt.js → HomePage-C5mlDPXK.js} +1 -1
- package/dist/app/assets/{LightMode-CNhIlIlJ.js → LightMode-DGNmhG0u.js} +1 -1
- package/dist/app/assets/{MainPage-Bgqo8jCy.js → MainPage-CVL_wmP4.js} +1 -1
- package/dist/app/assets/{MaterializationsPage-CgBlgGz2.js → MaterializationsPage-DmzMBCpy.js} +1 -1
- package/dist/app/assets/{ModelPage-B0TjoDtf.js → ModelPage-Dbvf4QbB.js} +1 -1
- package/dist/app/assets/{PackagePage-BL8vnFj1.js → PackagePage-DxdHc2Qs.js} +1 -1
- package/dist/app/assets/{RouteError-BzPby0X2.js → RouteError-OJdT4tCd.js} +1 -1
- package/dist/app/assets/{ThemeEditorPage-CTEP_9r3.js → ThemeEditorPage-Bk7s0KXY.js} +1 -1
- package/dist/app/assets/{WorkbookPage-BwM3BmKw.js → WorkbookPage-j_vCWdN3.js} +1 -1
- package/dist/app/assets/{core-CK68iv6w.es-CpRxXBt7.js → core-Rj_4rRnA.es-DoIfLxDJ.js} +1 -1
- package/dist/app/assets/{index-B33zGctF.js → index-B_jKMR35.js} +4 -4
- package/dist/app/assets/{index-CmkW1MiE.js → index-D-rDyK11.js} +1 -1
- package/dist/app/assets/{index-tXJXwdyj.js → index-DWIe_hK0.js} +1 -1
- package/dist/app/assets/{index-BkiWKaAF.js → index-hw-xn0X7.js} +1 -1
- package/dist/app/index.html +1 -1
- package/dist/package_load_worker.mjs +53 -3
- package/dist/server.mjs +20277 -925
- package/package.json +1 -1
- package/src/config.ts +35 -1
- package/src/controller/connection.controller.spec.ts +46 -0
- package/src/controller/connection.controller.ts +105 -2
- package/src/controller/materialization.controller.spec.ts +25 -0
- package/src/controller/materialization.controller.ts +60 -0
- package/src/controller/model.controller.ts +24 -0
- package/src/controller/query.controller.ts +83 -10
- package/src/json_utils.spec.ts +51 -0
- package/src/json_utils.ts +33 -0
- package/src/mcp/handler_utils.ts +10 -2
- package/src/mcp/query_envelope.spec.ts +229 -0
- package/src/mcp/query_envelope.ts +240 -0
- package/src/mcp/server.protocol.spec.ts +128 -16
- package/src/mcp/skills/build_skills_bundle.ts +94 -4
- package/src/mcp/skills/skills_bundle.json +1 -1
- package/src/mcp/skills/skills_bundle.spec.ts +113 -4
- package/src/mcp/tool_response.spec.ts +108 -0
- package/src/mcp/tool_response.ts +138 -0
- package/src/mcp/tools/compile_tool.spec.ts +112 -4
- package/src/mcp/tools/compile_tool.ts +61 -30
- package/src/mcp/tools/docs_search_tool.ts +6 -16
- package/src/mcp/tools/execute_query_tool.spec.ts +154 -3
- package/src/mcp/tools/execute_query_tool.ts +131 -155
- package/src/mcp/tools/get_context_tool.spec.ts +63 -3
- package/src/mcp/tools/get_context_tool.ts +43 -46
- package/src/mcp/tools/reload_package_tool.ts +3 -29
- package/src/mcp_config.spec.ts +919 -0
- package/src/mcp_config.ts +425 -0
- package/src/oom_guards.integration.spec.ts +11 -3
- package/src/package_load/package_load_pool.ts +2 -0
- package/src/package_load/package_load_worker.ts +17 -5
- package/src/package_load/protocol.ts +6 -0
- package/src/query_metadata_metrics.ts +49 -0
- package/src/server.ts +99 -3
- package/src/service/build_plan.spec.ts +125 -0
- package/src/service/build_plan.ts +108 -7
- package/src/service/compile_fragment_techniques.spec.ts +156 -0
- package/src/service/connection.spec.ts +371 -1
- package/src/service/connection.ts +77 -14
- package/src/service/connection_config.spec.ts +60 -0
- package/src/service/connection_config.ts +75 -0
- package/src/service/duckdb_instance_isolation.spec.ts +137 -0
- package/src/service/environment.ts +57 -3
- package/src/service/materialization_config_validation.spec.ts +99 -0
- package/src/service/materialization_config_validation.ts +120 -0
- package/src/service/materialization_schedule_surface.spec.ts +124 -0
- package/src/service/materialization_service.spec.ts +119 -0
- package/src/service/materialization_service.ts +186 -3
- package/src/service/materialization_test_fixtures.ts +86 -21
- package/src/service/model.spec.ts +45 -1
- package/src/service/model.ts +171 -23
- package/src/service/model_limits.spec.ts +28 -0
- package/src/service/model_limits.ts +21 -0
- package/src/service/package.ts +24 -1
- package/src/service/package_manifest.spec.ts +137 -4
- package/src/service/package_manifest.ts +140 -5
- package/src/service/persist_annotation_validation.spec.ts +12 -0
- package/src/service/persist_annotation_validation.ts +9 -4
- package/src/service/query_metadata.spec.ts +408 -0
- package/src/service/query_metadata.ts +492 -0
- package/src/service/query_metadata_identity.spec.ts +149 -0
- package/tests/integration/mcp/mcp_execute_query_tool.integration.spec.ts +37 -12
|
@@ -3,6 +3,7 @@ import { z } from "zod";
|
|
|
3
3
|
import lunr from "lunr";
|
|
4
4
|
import { EnvironmentStore } from "../../service/environment_store";
|
|
5
5
|
import { buildMalloyUri } from "../handler_utils";
|
|
6
|
+
import { jsonResource } from "../tool_response";
|
|
6
7
|
import { logger } from "../../logger";
|
|
7
8
|
import rawIndex from "./docs_search/malloy_docs_index.json";
|
|
8
9
|
|
|
@@ -88,6 +89,10 @@ const SEARCH_DOCS_DESCRIPTION = `Search the Malloy documentation by keyword and
|
|
|
88
89
|
- Before writing unfamiliar Malloy syntax (window functions, autobin, dialect-specific functions, rendering tags) or when a query fails with a syntax error you do not recognize.
|
|
89
90
|
- Do NOT use it to look up field or source names in a model; use malloy_getContext for that.
|
|
90
91
|
|
|
92
|
+
## Contract rules
|
|
93
|
+
- These are documentation pages, not model entities. Do not treat a doc title as a field or source name.
|
|
94
|
+
- The excerpt is only a hint; open the url for the full detail.
|
|
95
|
+
|
|
91
96
|
## Parameters
|
|
92
97
|
- query (required): keywords describing what you need.
|
|
93
98
|
- limit (optional): maximum results to return; default 8.
|
|
@@ -95,10 +100,6 @@ const SEARCH_DOCS_DESCRIPTION = `Search the Malloy documentation by keyword and
|
|
|
95
100
|
## Response
|
|
96
101
|
A JSON array of matches, each with title, url (a docs.malloydata.dev link), and a short excerpt, ordered by relevance. Empty array if nothing matches; broaden the keywords and retry.
|
|
97
102
|
|
|
98
|
-
## Contract rules
|
|
99
|
-
- These are documentation pages, not model entities. Do not treat a doc title as a field or source name.
|
|
100
|
-
- The excerpt is only a hint; open the url for the full detail.
|
|
101
|
-
|
|
102
103
|
## Worked example
|
|
103
104
|
{ "query": "window functions lag" }`;
|
|
104
105
|
|
|
@@ -121,18 +122,7 @@ export function registerDocsSearchTool(
|
|
|
121
122
|
|
|
122
123
|
const results = searchDocsIndex(query, max);
|
|
123
124
|
|
|
124
|
-
return {
|
|
125
|
-
content: [
|
|
126
|
-
{
|
|
127
|
-
type: "resource" as const,
|
|
128
|
-
resource: {
|
|
129
|
-
type: "application/json",
|
|
130
|
-
uri: buildMalloyUri({}, "docs-search"),
|
|
131
|
-
text: JSON.stringify(results),
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
],
|
|
135
|
-
};
|
|
125
|
+
return jsonResource(buildMalloyUri({}, "docs-search"), results);
|
|
136
126
|
},
|
|
137
127
|
);
|
|
138
128
|
}
|
|
@@ -2,6 +2,7 @@ import { describe, expect, it } from "bun:test";
|
|
|
2
2
|
import { MalloyError } from "@malloydata/malloy";
|
|
3
3
|
import { registerExecuteQueryTool } from "./execute_query_tool";
|
|
4
4
|
import type { EnvironmentStore } from "../../service/environment_store";
|
|
5
|
+
import type { ModelQueryMetadataInput } from "../../service/model";
|
|
5
6
|
import {
|
|
6
7
|
NotQueryableError,
|
|
7
8
|
QueryTimeoutError,
|
|
@@ -13,7 +14,11 @@ import {
|
|
|
13
14
|
// compile_tool.spec.ts and reload_package_tool.spec.ts.
|
|
14
15
|
type Handler = (params: Record<string, unknown>) => Promise<{
|
|
15
16
|
isError?: boolean;
|
|
16
|
-
content: Array<{
|
|
17
|
+
content: Array<{
|
|
18
|
+
type?: string;
|
|
19
|
+
text?: string;
|
|
20
|
+
resource?: { text: string };
|
|
21
|
+
}>;
|
|
17
22
|
}>;
|
|
18
23
|
|
|
19
24
|
function captureHandler(store: Partial<EnvironmentStore>): Handler {
|
|
@@ -28,8 +33,8 @@ function captureHandler(store: Partial<EnvironmentStore>): Handler {
|
|
|
28
33
|
return handler;
|
|
29
34
|
}
|
|
30
35
|
|
|
31
|
-
function parse(result: { content: Array<{ resource
|
|
32
|
-
return JSON.parse(result.content[0].resource
|
|
36
|
+
function parse(result: { content: Array<{ resource?: { text: string } }> }) {
|
|
37
|
+
return JSON.parse(result.content[0].resource!.text);
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
/**
|
|
@@ -60,6 +65,64 @@ function storeWhoseQueryThrows(error: unknown): Partial<EnvironmentStore> {
|
|
|
60
65
|
};
|
|
61
66
|
}
|
|
62
67
|
|
|
68
|
+
/**
|
|
69
|
+
* A store whose query SUCCEEDS, capturing the per-query metadata input the tool
|
|
70
|
+
* built. `connections` becomes the environment's connection config, so a test
|
|
71
|
+
* can pin what the connection layers resolved to.
|
|
72
|
+
*/
|
|
73
|
+
function storeCapturingMetadata(
|
|
74
|
+
connections: Record<
|
|
75
|
+
string,
|
|
76
|
+
{
|
|
77
|
+
queryMetadata?: Record<string, string>;
|
|
78
|
+
queryMetadataEnforced?: Record<string, string>;
|
|
79
|
+
}
|
|
80
|
+
> = {},
|
|
81
|
+
): {
|
|
82
|
+
store: Partial<EnvironmentStore>;
|
|
83
|
+
captured: () => ModelQueryMetadataInput | undefined;
|
|
84
|
+
} {
|
|
85
|
+
let captured: ModelQueryMetadataInput | undefined;
|
|
86
|
+
const store: Partial<EnvironmentStore> = {
|
|
87
|
+
getEnvironment: async () =>
|
|
88
|
+
({
|
|
89
|
+
assertCanAdmitQuery: () => undefined,
|
|
90
|
+
getApiConnection: (name: string) => {
|
|
91
|
+
const connection = connections[name];
|
|
92
|
+
if (!connection) throw new Error(`no connection ${name}`);
|
|
93
|
+
return connection;
|
|
94
|
+
},
|
|
95
|
+
getPackage: async () => ({
|
|
96
|
+
getModel: () => ({
|
|
97
|
+
getModelType: () => "model",
|
|
98
|
+
getModel: async () => ({}),
|
|
99
|
+
getQueryResults: async (
|
|
100
|
+
..._args: [
|
|
101
|
+
...unknown[],
|
|
102
|
+
ModelQueryMetadataInput | undefined,
|
|
103
|
+
]
|
|
104
|
+
) => {
|
|
105
|
+
// The 8th argument: sourceName, queryName, query,
|
|
106
|
+
// filterParams, bypassFilters, givens, abortSignal, input.
|
|
107
|
+
captured = _args[7] as ModelQueryMetadataInput | undefined;
|
|
108
|
+
return {
|
|
109
|
+
result: {
|
|
110
|
+
schema: { fields: [] },
|
|
111
|
+
connection_name: "warehouse",
|
|
112
|
+
},
|
|
113
|
+
compactResult: [{ c: 1 }],
|
|
114
|
+
rowLimit: 1000,
|
|
115
|
+
rowLimitSource: "server_default",
|
|
116
|
+
queryCorrelationId: "corr-1",
|
|
117
|
+
};
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
}),
|
|
121
|
+
}) as never,
|
|
122
|
+
};
|
|
123
|
+
return { store, captured: () => captured };
|
|
124
|
+
}
|
|
125
|
+
|
|
63
126
|
const args = {
|
|
64
127
|
environmentName: "env",
|
|
65
128
|
packageName: "pkg",
|
|
@@ -140,4 +203,92 @@ describe("malloy_executeQuery error classification", () => {
|
|
|
140
203
|
// one; echoing the name back would undo that.
|
|
141
204
|
expect(parsed.error).not.toContain("salaries");
|
|
142
205
|
});
|
|
206
|
+
|
|
207
|
+
it("also states the error in a text block", async () => {
|
|
208
|
+
// The structured payload rides in an embedded resource block. A client
|
|
209
|
+
// that renders only text blocks on an isError result shows nothing at
|
|
210
|
+
// all for it, which is how a real diagnostic surfaces to the agent as a
|
|
211
|
+
// bare "Unknown error". Every error must say it in plain text too.
|
|
212
|
+
const handler = captureHandler(
|
|
213
|
+
storeWhoseQueryThrows(new MalloyError("unexpected '@'", [])),
|
|
214
|
+
);
|
|
215
|
+
const result = await handler(args);
|
|
216
|
+
const parsed = parse(result);
|
|
217
|
+
|
|
218
|
+
const textBlock = result.content.find((b) => b.type === "text");
|
|
219
|
+
expect(textBlock).toBeDefined();
|
|
220
|
+
expect(textBlock!.text).toContain(parsed.error);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
describe("malloy_executeQuery per-query metadata", () => {
|
|
225
|
+
it("resolves the connection's enforced layer, which an agent must not be able to shed", async () => {
|
|
226
|
+
// The reason this path matters: `queryMetadataEnforced` is the property a
|
|
227
|
+
// host is billed or audited by, and MCP is this server's primary agent
|
|
228
|
+
// interface. A tool that omits the metadata input issues every one of its
|
|
229
|
+
// statements without the tenant label, on a connection whose config says
|
|
230
|
+
// it is applied to everything the connection sends.
|
|
231
|
+
const { store, captured } = storeCapturingMetadata({
|
|
232
|
+
warehouse: {
|
|
233
|
+
queryMetadata: { team: "finance" },
|
|
234
|
+
queryMetadataEnforced: { tenant: "acme" },
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
const handler = captureHandler(store);
|
|
238
|
+
await handler(args);
|
|
239
|
+
|
|
240
|
+
const layers = captured()?.connectionMetadata?.("warehouse");
|
|
241
|
+
expect(layers).toEqual({
|
|
242
|
+
default: { team: "finance" },
|
|
243
|
+
enforced: { tenant: "acme" },
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("passes the environment and mints a correlation id", async () => {
|
|
248
|
+
// Both arrive through the input object; neither is derivable inside Model.
|
|
249
|
+
const { store, captured } = storeCapturingMetadata();
|
|
250
|
+
const handler = captureHandler(store);
|
|
251
|
+
await handler(args);
|
|
252
|
+
|
|
253
|
+
expect(captured()?.environment).toBe("env");
|
|
254
|
+
expect(captured()?.correlationId).toMatch(/^[0-9a-f-]{36}$/);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("returns the id the statements carried, so an agent can find its query", async () => {
|
|
258
|
+
const { store } = storeCapturingMetadata();
|
|
259
|
+
const handler = captureHandler(store);
|
|
260
|
+
const parsed = parse(await handler(args));
|
|
261
|
+
expect(parsed._query_id).toBe("corr-1");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("fails open when the connection config cannot be read", async () => {
|
|
265
|
+
// A tag must never be the reason a query fails, so an unresolvable
|
|
266
|
+
// connection costs the layers rather than the statement.
|
|
267
|
+
const { store, captured } = storeCapturingMetadata();
|
|
268
|
+
const handler = captureHandler(store);
|
|
269
|
+
await handler(args);
|
|
270
|
+
expect(captured()?.connectionMetadata?.("missing")).toBeNull();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("builds the same input for a named view as for ad-hoc Malloy", async () => {
|
|
274
|
+
// Two call sites, one input: the enforced layer cannot depend on which
|
|
275
|
+
// shape of query the agent happened to send.
|
|
276
|
+
const { store, captured } = storeCapturingMetadata({
|
|
277
|
+
warehouse: { queryMetadataEnforced: { tenant: "acme" } },
|
|
278
|
+
});
|
|
279
|
+
const handler = captureHandler(store);
|
|
280
|
+
await handler({
|
|
281
|
+
environmentName: "env",
|
|
282
|
+
packageName: "pkg",
|
|
283
|
+
modelPath: "m.malloy",
|
|
284
|
+
sourceName: "orders",
|
|
285
|
+
queryName: "by_month",
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
expect(captured()?.environment).toBe("env");
|
|
289
|
+
expect(captured()?.connectionMetadata?.("warehouse")).toEqual({
|
|
290
|
+
default: undefined,
|
|
291
|
+
enforced: { tenant: "acme" },
|
|
292
|
+
});
|
|
293
|
+
});
|
|
143
294
|
});
|
|
@@ -16,6 +16,10 @@ import {
|
|
|
16
16
|
classifyToolError,
|
|
17
17
|
getModelForQuery,
|
|
18
18
|
} from "../handler_utils";
|
|
19
|
+
import { jsonResource, jsonToolError } from "../tool_response";
|
|
20
|
+
import { buildQueryEnvelope } from "../query_envelope";
|
|
21
|
+
import { mintCorrelationId } from "../../service/query_metadata";
|
|
22
|
+
import { bigIntReplacer } from "../../json_utils";
|
|
19
23
|
import { MCP_ERROR_MESSAGES } from "../mcp_constants";
|
|
20
24
|
|
|
21
25
|
/**
|
|
@@ -61,6 +65,27 @@ const executeQueryShape = {
|
|
|
61
65
|
),
|
|
62
66
|
};
|
|
63
67
|
|
|
68
|
+
const EXECUTE_QUERY_DESCRIPTION = `Run a Malloy query against a model and return the rows. Takes either ad-hoc Malloy in query, or a named view/query via queryName (with sourceName for a view).
|
|
69
|
+
|
|
70
|
+
## Contract rules
|
|
71
|
+
- Check _limit_hit before reporting any total, count, or "top N". True means the server's default row cap cut the result off and more rows exist, so the numbers in front of you are a partial set, not the answer. A query that set its own limit: or top: does not set it, and returning exactly that many rows is a complete answer to what was asked.
|
|
72
|
+
- Never sum or count the returned rows to state a total when _limit_hit or _rows_truncated is set. Aggregate in the query instead.
|
|
73
|
+
- _returned_rows: 0 with _rows_truncated set means one row was too large to send, NOT that nothing matched. Do not report it as an empty result.
|
|
74
|
+
- Use source, view, and field names exactly as malloy_getContext returned them.
|
|
75
|
+
|
|
76
|
+
## Response
|
|
77
|
+
A JSON object, the same shape Credible's execute_query returns, so a data app behaves the same authored locally and served in production:
|
|
78
|
+
- rows: flat objects keyed by column name, the shape an in-package data app receives.
|
|
79
|
+
- _meta: the Malloy metadata flat rows drop (schema with field types and render tags, annotations, connection_name, query_timezone).
|
|
80
|
+
- _query_row_limit: the cap pushed into the SQL, from the query's own limit: or the server default.
|
|
81
|
+
- _limit_source: "query" when the cap came from the query's own limit:/top:, "server_default" otherwise.
|
|
82
|
+
- _limit_hit: the row count equals that cap AND the cap was the server default.
|
|
83
|
+
- _rows_truncated / _total_rows / _returned_rows: present only when the payload cap dropped rows.
|
|
84
|
+
- _query_id: this query's id in the warehouse's own query history. Present only where enabled.
|
|
85
|
+
- warning, renderLogErrors: present only when they apply.
|
|
86
|
+
|
|
87
|
+
A query with no limit: of its own gets the server default, so a result landing exactly on _query_row_limit is almost never the whole table. Values above 2^53 are returned as JSON strings so their digits survive.`;
|
|
88
|
+
|
|
64
89
|
// Type inference is handled automatically by the MCP server based on the executeQueryShape
|
|
65
90
|
|
|
66
91
|
/**
|
|
@@ -72,7 +97,7 @@ export function registerExecuteQueryTool(
|
|
|
72
97
|
): void {
|
|
73
98
|
mcpServer.tool(
|
|
74
99
|
"malloy_executeQuery",
|
|
75
|
-
|
|
100
|
+
EXECUTE_QUERY_DESCRIPTION,
|
|
76
101
|
executeQueryShape,
|
|
77
102
|
/** Handles requests for the malloy_executeQuery tool */
|
|
78
103
|
async (params) => {
|
|
@@ -120,33 +145,14 @@ export function registerExecuteQueryTool(
|
|
|
120
145
|
|
|
121
146
|
// Handle errors during package/model access (e.g., not found, initial compilation)
|
|
122
147
|
if ("error" in modelResult) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
error: modelResult.error.message,
|
|
127
|
-
suggestions: modelResult.error.suggestions,
|
|
128
|
-
},
|
|
129
|
-
null,
|
|
130
|
-
2,
|
|
148
|
+
return jsonToolError(
|
|
149
|
+
"error://executeQuery/modelAccess",
|
|
150
|
+
modelResult.error,
|
|
131
151
|
);
|
|
132
|
-
return {
|
|
133
|
-
isError: true,
|
|
134
|
-
// Return as application/json nested inside a 'resource' type
|
|
135
|
-
content: [
|
|
136
|
-
{
|
|
137
|
-
type: "resource", // Use 'resource' type
|
|
138
|
-
resource: {
|
|
139
|
-
type: "application/json", // Actual content type
|
|
140
|
-
uri: "error://executeQuery/modelAccess", // Placeholder URI
|
|
141
|
-
text: errorJson,
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
],
|
|
145
|
-
};
|
|
146
152
|
}
|
|
147
153
|
|
|
148
154
|
// --- Execute Query ---
|
|
149
|
-
const { model } = modelResult;
|
|
155
|
+
const { model, environment } = modelResult;
|
|
150
156
|
logger.info(
|
|
151
157
|
`[MCP Tool executeQuery] Model found. Proceeding to execute query.`,
|
|
152
158
|
);
|
|
@@ -160,123 +166,112 @@ export function registerExecuteQueryTool(
|
|
|
160
166
|
let querySlot: QuerySlotHandle | null = null;
|
|
161
167
|
try {
|
|
162
168
|
querySlot = tryAcquireQuerySlot("mcp:executeQuery");
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
169
|
+
// Per-query metadata, built the same way the HTTP query controller
|
|
170
|
+
// builds it: MCP is a query boundary like any other, and a
|
|
171
|
+
// connection's enforced properties describe the deployment rather
|
|
172
|
+
// than the protocol a query arrived over.
|
|
173
|
+
const queryMetadataInput = {
|
|
174
|
+
environment: environmentName,
|
|
175
|
+
// Minted here because the envelope below returns it.
|
|
176
|
+
correlationId: mintCorrelationId(),
|
|
177
|
+
// The environment owns the connection configs, so the default
|
|
178
|
+
// and enforced layers are read here rather than from the model.
|
|
179
|
+
connectionMetadata: (connectionName: string) => {
|
|
180
|
+
try {
|
|
181
|
+
const connection =
|
|
182
|
+
environment.getApiConnection(connectionName);
|
|
183
|
+
return {
|
|
184
|
+
default: connection.queryMetadata,
|
|
185
|
+
enforced: connection.queryMetadataEnforced,
|
|
186
|
+
};
|
|
187
|
+
} catch (error) {
|
|
188
|
+
logger.debug(
|
|
189
|
+
"[MCP Tool executeQuery] No query-metadata layers for connection",
|
|
190
|
+
{ connectionName, error },
|
|
191
|
+
);
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
// The two call modes differ only in which arguments carry the
|
|
197
|
+
// query; everything after the run is identical, so they share one
|
|
198
|
+
// path rather than two copies that can drift.
|
|
199
|
+
const {
|
|
200
|
+
result,
|
|
201
|
+
compactResult,
|
|
202
|
+
rowLimit,
|
|
203
|
+
rowLimitSource,
|
|
204
|
+
queryCorrelationId,
|
|
205
|
+
} = await runWithQueryTimeout(
|
|
206
|
+
(abortSignal) =>
|
|
207
|
+
query
|
|
208
|
+
? model.getQueryResults(
|
|
209
|
+
undefined,
|
|
210
|
+
undefined,
|
|
211
|
+
query,
|
|
212
|
+
filterParams,
|
|
213
|
+
undefined,
|
|
214
|
+
givens as Record<string, GivenValue> | undefined,
|
|
215
|
+
abortSignal,
|
|
216
|
+
queryMetadataInput,
|
|
217
|
+
)
|
|
218
|
+
: model.getQueryResults(
|
|
219
|
+
sourceName,
|
|
220
|
+
queryName,
|
|
221
|
+
undefined,
|
|
222
|
+
filterParams,
|
|
223
|
+
undefined,
|
|
224
|
+
givens as Record<string, GivenValue> | undefined,
|
|
225
|
+
abortSignal,
|
|
226
|
+
queryMetadataInput,
|
|
227
|
+
),
|
|
228
|
+
getQueryTimeoutMs(),
|
|
229
|
+
);
|
|
215
230
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
undefined,
|
|
224
|
-
filterParams,
|
|
225
|
-
undefined,
|
|
226
|
-
givens as Record<string, GivenValue> | undefined,
|
|
227
|
-
abortSignal,
|
|
228
|
-
),
|
|
229
|
-
getQueryTimeoutMs(),
|
|
230
|
-
);
|
|
231
|
-
const { validateRenderTags } = await import(
|
|
232
|
-
"@malloydata/render-validator"
|
|
233
|
-
);
|
|
234
|
-
const renderLogs = validateRenderTags(result);
|
|
231
|
+
// Render-tag validation reads the FULL Malloy result: the tags live
|
|
232
|
+
// in its schema annotations, which the flat rows do not carry. It
|
|
233
|
+
// runs regardless of which shape is returned.
|
|
234
|
+
const { validateRenderTags } = await import(
|
|
235
|
+
"@malloydata/render-validator"
|
|
236
|
+
);
|
|
237
|
+
const renderLogs = validateRenderTags(result);
|
|
235
238
|
|
|
236
|
-
|
|
239
|
+
const resultUri = buildMalloyUri(
|
|
240
|
+
{
|
|
237
241
|
environment: environmentName,
|
|
238
242
|
package: packageName,
|
|
239
243
|
resourceType: "models" as const,
|
|
240
244
|
resourceName: modelPath,
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
const content = [
|
|
246
|
-
{
|
|
247
|
-
type: "resource" as const,
|
|
248
|
-
resource: {
|
|
249
|
-
type: "application/json",
|
|
250
|
-
uri: resultUri,
|
|
251
|
-
text: resultString,
|
|
252
|
-
},
|
|
253
|
-
},
|
|
254
|
-
];
|
|
245
|
+
},
|
|
246
|
+
"result",
|
|
247
|
+
);
|
|
255
248
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
],
|
|
266
|
-
};
|
|
267
|
-
}
|
|
249
|
+
const envelope = buildQueryEnvelope(
|
|
250
|
+
compactResult,
|
|
251
|
+
rowLimit,
|
|
252
|
+
result,
|
|
253
|
+
renderLogs.map((log) => log.message),
|
|
254
|
+
undefined,
|
|
255
|
+
rowLimitSource,
|
|
256
|
+
queryCorrelationId,
|
|
257
|
+
);
|
|
268
258
|
|
|
269
|
-
|
|
270
|
-
|
|
259
|
+
// A capped or truncated result, and a broken render tag, are the
|
|
260
|
+
// things an agent most needs to notice, so they are stated in text
|
|
261
|
+
// rather than left for a client that parses the payload.
|
|
262
|
+
const notes = [
|
|
263
|
+
envelope.warning,
|
|
264
|
+
envelope.renderLogErrors &&
|
|
265
|
+
`Render tag problems: ${envelope.renderLogErrors.join("; ")}`,
|
|
266
|
+
].filter(Boolean);
|
|
271
267
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
);
|
|
268
|
+
return jsonResource(resultUri, envelope, {
|
|
269
|
+
space: 2,
|
|
270
|
+
// BigInt reaches here: compactResult is raw driver output and
|
|
271
|
+
// DuckDB returns count() as one.
|
|
272
|
+
replacer: bigIntReplacer,
|
|
273
|
+
text: notes.length > 0 ? notes.join("\n\n") : undefined,
|
|
274
|
+
});
|
|
280
275
|
} catch (queryError) {
|
|
281
276
|
// Handle query execution errors (syntax errors, invalid queries, etc.)
|
|
282
277
|
logger.error(
|
|
@@ -306,29 +301,10 @@ export function registerExecuteQueryTool(
|
|
|
306
301
|
);
|
|
307
302
|
}
|
|
308
303
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
suggestions,
|
|
314
|
-
},
|
|
315
|
-
null,
|
|
316
|
-
2,
|
|
317
|
-
);
|
|
318
|
-
return {
|
|
319
|
-
isError: true,
|
|
320
|
-
// Return as application/json nested inside a 'resource' type
|
|
321
|
-
content: [
|
|
322
|
-
{
|
|
323
|
-
type: "resource", // Use 'resource' type
|
|
324
|
-
resource: {
|
|
325
|
-
type: "application/json", // Actual content type
|
|
326
|
-
uri: "error://executeQuery/queryExecution", // Placeholder URI
|
|
327
|
-
text: errorJson,
|
|
328
|
-
},
|
|
329
|
-
},
|
|
330
|
-
],
|
|
331
|
-
};
|
|
304
|
+
return jsonToolError("error://executeQuery/queryExecution", {
|
|
305
|
+
message: errorDetails.message,
|
|
306
|
+
suggestions,
|
|
307
|
+
});
|
|
332
308
|
} finally {
|
|
333
309
|
// Release on every exit path — success, error, or
|
|
334
310
|
// unreachable code-path throw. `release()` is idempotent
|