@malloy-publisher/server 0.0.233 → 0.0.235

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.
Files changed (62) hide show
  1. package/README.docker.md +1 -0
  2. package/dist/app/api-doc.yaml +208 -0
  3. package/dist/app/assets/{EnvironmentPage-DutP7T8h.js → EnvironmentPage-BsAnavYN.js} +1 -1
  4. package/dist/app/assets/{HomePage-BcxDrBfl.js → HomePage-CADE138j.js} +1 -1
  5. package/dist/app/assets/{LightMode-BJukGxgz.js → LightMode-Cfh7KzN8.js} +1 -1
  6. package/dist/app/assets/{MainPage-DXbwlMeF.js → MainPage-CO3pRlnV.js} +2 -2
  7. package/dist/app/assets/{MaterializationsPage-BBQksmTU.js → MaterializationsPage-p9YjkRXZ.js} +1 -1
  8. package/dist/app/assets/{ModelPage-C6tK51uU.js → ModelPage-C1OSTv-x.js} +1 -1
  9. package/dist/app/assets/{PackagePage-Bo3cwwZE.js → PackagePage-e4kN75YR.js} +1 -1
  10. package/dist/app/assets/{RouteError-BufkcAKE.js → RouteError-CzbfOkng.js} +1 -1
  11. package/dist/app/assets/{ThemeEditorPage-DICvvKpa.js → ThemeEditorPage-CciagFTq.js} +1 -1
  12. package/dist/app/assets/{WorkbookPage-Dkwt75Nj.js → WorkbookPage-DNWmkCXa.js} +1 -1
  13. package/dist/app/assets/{core-C0nunIQT.es-DlMLKZBK.js → core-Rj_4rRnA.es-BZyvITuO.js} +1 -1
  14. package/dist/app/assets/{index-CmEVVe-8.js → index-CH2AcDzc.js} +4 -4
  15. package/dist/app/assets/{index-qnhU9CGo.js → index-DQa463gC.js} +2 -2
  16. package/dist/app/assets/{index-Cs4WVm2z.js → index-DQpV7MyA.js} +1 -1
  17. package/dist/app/assets/{index-BabP-V-S.js → index-DzaYbhnD.js} +1 -1
  18. package/dist/app/assets/{index-BusxL5Pt.js → index-VBbcc8s6.js} +1 -1
  19. package/dist/app/index.html +1 -1
  20. package/dist/package_load_worker.mjs +53 -3
  21. package/dist/server.mjs +835 -97
  22. package/package.json +12 -12
  23. package/src/config.ts +35 -1
  24. package/src/controller/connection.controller.spec.ts +46 -0
  25. package/src/controller/connection.controller.ts +105 -2
  26. package/src/controller/materialization.controller.spec.ts +25 -0
  27. package/src/controller/materialization.controller.ts +60 -0
  28. package/src/controller/model.controller.ts +24 -0
  29. package/src/controller/query.controller.ts +83 -15
  30. package/src/mcp/handler_utils.ts +10 -2
  31. package/src/mcp/query_envelope.ts +10 -0
  32. package/src/mcp/skills/skills_bundle.json +1 -1
  33. package/src/mcp/tools/execute_query_tool.spec.ts +131 -0
  34. package/src/mcp/tools/execute_query_tool.ts +62 -25
  35. package/src/mcp_config.spec.ts +919 -0
  36. package/src/mcp_config.ts +425 -0
  37. package/src/oom_guards.integration.spec.ts +11 -3
  38. package/src/package_load/package_load_pool.ts +2 -0
  39. package/src/package_load/package_load_worker.ts +17 -5
  40. package/src/package_load/protocol.ts +6 -0
  41. package/src/query_metadata_metrics.ts +49 -0
  42. package/src/server.ts +99 -3
  43. package/src/service/build_plan.spec.ts +125 -0
  44. package/src/service/build_plan.ts +108 -7
  45. package/src/service/connection_config.ts +49 -0
  46. package/src/service/environment.ts +57 -3
  47. package/src/service/materialization_config_validation.spec.ts +99 -0
  48. package/src/service/materialization_config_validation.ts +120 -0
  49. package/src/service/materialization_schedule_surface.spec.ts +124 -0
  50. package/src/service/materialization_service.spec.ts +119 -0
  51. package/src/service/materialization_service.ts +186 -3
  52. package/src/service/materialization_test_fixtures.ts +86 -21
  53. package/src/service/model.spec.ts +45 -1
  54. package/src/service/model.ts +145 -19
  55. package/src/service/package.ts +24 -1
  56. package/src/service/package_manifest.spec.ts +137 -4
  57. package/src/service/package_manifest.ts +140 -5
  58. package/src/service/persist_annotation_validation.spec.ts +12 -0
  59. package/src/service/persist_annotation_validation.ts +9 -4
  60. package/src/service/query_metadata.spec.ts +408 -0
  61. package/src/service/query_metadata.ts +492 -0
  62. package/src/service/query_metadata_identity.spec.ts +149 -0
@@ -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,
@@ -64,6 +65,64 @@ function storeWhoseQueryThrows(error: unknown): Partial<EnvironmentStore> {
64
65
  };
65
66
  }
66
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
+
67
126
  const args = {
68
127
  environmentName: "env",
69
128
  packageName: "pkg",
@@ -161,3 +220,75 @@ describe("malloy_executeQuery error classification", () => {
161
220
  expect(textBlock!.text).toContain(parsed.error);
162
221
  });
163
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
+ });
294
+ });
@@ -18,6 +18,7 @@ import {
18
18
  } from "../handler_utils";
19
19
  import { jsonResource, jsonToolError } from "../tool_response";
20
20
  import { buildQueryEnvelope } from "../query_envelope";
21
+ import { mintCorrelationId } from "../../service/query_metadata";
21
22
  import { bigIntReplacer } from "../../json_utils";
22
23
  import { MCP_ERROR_MESSAGES } from "../mcp_constants";
23
24
 
@@ -80,6 +81,7 @@ A JSON object, the same shape Credible's execute_query returns, so a data app be
80
81
  - _limit_source: "query" when the cap came from the query's own limit:/top:, "server_default" otherwise.
81
82
  - _limit_hit: the row count equals that cap AND the cap was the server default.
82
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.
83
85
  - warning, renderLogErrors: present only when they apply.
84
86
 
85
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.`;
@@ -150,7 +152,7 @@ export function registerExecuteQueryTool(
150
152
  }
151
153
 
152
154
  // --- Execute Query ---
153
- const { model } = modelResult;
155
+ const { model, environment } = modelResult;
154
156
  logger.info(
155
157
  `[MCP Tool executeQuery] Model found. Proceeding to execute query.`,
156
158
  );
@@ -164,33 +166,67 @@ export function registerExecuteQueryTool(
164
166
  let querySlot: QuerySlotHandle | null = null;
165
167
  try {
166
168
  querySlot = tryAcquireQuerySlot("mcp:executeQuery");
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
+ };
167
196
  // The two call modes differ only in which arguments carry the
168
197
  // query; everything after the run is identical, so they share one
169
198
  // path rather than two copies that can drift.
170
- const { result, compactResult, rowLimit, rowLimitSource } =
171
- await runWithQueryTimeout(
172
- (abortSignal) =>
173
- query
174
- ? model.getQueryResults(
175
- undefined,
176
- undefined,
177
- query,
178
- filterParams,
179
- undefined,
180
- givens as Record<string, GivenValue> | undefined,
181
- abortSignal,
182
- )
183
- : model.getQueryResults(
184
- sourceName,
185
- queryName,
186
- undefined,
187
- filterParams,
188
- undefined,
189
- givens as Record<string, GivenValue> | undefined,
190
- abortSignal,
191
- ),
192
- getQueryTimeoutMs(),
193
- );
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
+ );
194
230
 
195
231
  // Render-tag validation reads the FULL Malloy result: the tags live
196
232
  // in its schema annotations, which the flat rows do not carry. It
@@ -217,6 +253,7 @@ export function registerExecuteQueryTool(
217
253
  renderLogs.map((log) => log.message),
218
254
  undefined,
219
255
  rowLimitSource,
256
+ queryCorrelationId,
220
257
  );
221
258
 
222
259
  // A capped or truncated result, and a broken render tag, are the