@malloy-publisher/server 0.0.231 → 0.0.233
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 +4 -0
- package/dist/app/api-doc.yaml +135 -13
- package/dist/app/assets/{EnvironmentPage-wa_EPkwK.js → EnvironmentPage-DutP7T8h.js} +1 -1
- package/dist/app/assets/{HomePage-jnCrupQp.js → HomePage-BcxDrBfl.js} +1 -1
- package/dist/app/assets/{LightMode-DYbwNULZ.js → LightMode-BJukGxgz.js} +1 -1
- package/dist/app/assets/{MainPage-CuJLrPNI.js → MainPage-DXbwlMeF.js} +2 -2
- package/dist/app/assets/{MaterializationsPage-D_67x2ee.js → MaterializationsPage-BBQksmTU.js} +1 -1
- package/dist/app/assets/{ModelPage-D5JtAWqR.js → ModelPage-C6tK51uU.js} +1 -1
- package/dist/app/assets/{PackagePage-BRwtqUSG.js → PackagePage-Bo3cwwZE.js} +1 -1
- package/dist/app/assets/{RouteError-CBNNrnSD.js → RouteError-BufkcAKE.js} +1 -1
- package/dist/app/assets/{ThemeEditorPage-CTCeBneA.js → ThemeEditorPage-DICvvKpa.js} +1 -1
- package/dist/app/assets/{WorkbookPage-SN6f1RBm.js → WorkbookPage-Dkwt75Nj.js} +1 -1
- package/dist/app/assets/{core-Dp3q5Ieu.es-CD5FvM2s.js → core-C0nunIQT.es-DlMLKZBK.js} +1 -1
- package/dist/app/assets/{index-DU4r7GdU.js → index-BabP-V-S.js} +346 -321
- package/dist/app/assets/{index-BLCx1EdC.js → index-BusxL5Pt.js} +1 -1
- package/dist/app/assets/{index-C_tJstcx.js → index-CmEVVe-8.js} +15 -15
- package/dist/app/assets/{index-CfmBVB6M.js → index-Cs4WVm2z.js} +1 -1
- package/dist/app/assets/{index-B3Nn8Vm2.js → index-qnhU9CGo.js} +266 -265
- package/dist/app/index.html +1 -1
- package/dist/instrumentation.mjs +2 -0
- package/dist/package_load_worker.mjs +11 -1
- package/dist/server.mjs +22627 -1260
- package/package.json +12 -12
- package/scripts/bake-duckdb-extensions.js +4 -1
- package/src/config.spec.ts +39 -0
- package/src/config.ts +135 -0
- package/src/controller/materialization.controller.spec.ts +62 -0
- package/src/controller/materialization.controller.ts +15 -0
- package/src/controller/package.controller.spec.ts +6 -0
- package/src/controller/package.controller.ts +7 -2
- package/src/controller/query.controller.ts +26 -21
- package/src/errors.ts +19 -0
- package/src/json_utils.spec.ts +51 -0
- package/src/json_utils.ts +33 -0
- package/src/logger.spec.ts +18 -1
- package/src/logger.ts +3 -1
- package/src/materialization_metrics.spec.ts +89 -4
- package/src/materialization_metrics.ts +155 -5
- package/src/mcp/query_envelope.spec.ts +229 -0
- package/src/mcp/query_envelope.ts +230 -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/embedding_index.spec.ts +1236 -0
- package/src/mcp/tools/embedding_index.ts +808 -0
- package/src/mcp/tools/execute_query_tool.spec.ts +23 -3
- package/src/mcp/tools/execute_query_tool.ts +90 -151
- package/src/mcp/tools/get_context_eval.ts +194 -45
- package/src/mcp/tools/get_context_tool.spec.ts +358 -5
- package/src/mcp/tools/get_context_tool.ts +202 -56
- package/src/mcp/tools/reload_package_tool.ts +3 -29
- package/src/pg_helpers.spec.ts +201 -0
- package/src/pg_helpers.ts +44 -5
- package/src/server.ts +24 -0
- package/src/service/build_plan.spec.ts +128 -2
- package/src/service/build_plan.ts +239 -17
- package/src/service/compile_fragment_techniques.spec.ts +156 -0
- package/src/service/connection.spec.ts +371 -1
- package/src/service/connection.ts +339 -20
- package/src/service/connection_config.spec.ts +108 -0
- package/src/service/connection_config.ts +47 -8
- package/src/service/connection_federation.spec.ts +184 -0
- package/src/service/duckdb_instance_isolation.spec.ts +137 -0
- package/src/service/embedding_provider.spec.ts +329 -0
- package/src/service/embedding_provider.ts +236 -0
- package/src/service/environment.ts +274 -12
- package/src/service/environment_store.spec.ts +678 -3
- package/src/service/environment_store.ts +449 -33
- package/src/service/environment_store_clone.spec.ts +350 -0
- package/src/service/manifest_loader.spec.ts +68 -13
- package/src/service/manifest_loader.ts +67 -19
- package/src/service/materialization_build_session.spec.ts +435 -0
- package/src/service/materialization_build_session.ts +681 -0
- package/src/service/materialization_eligibility.spec.ts +158 -0
- package/src/service/materialization_eligibility.ts +305 -0
- package/src/service/materialization_serve_transform.spec.ts +1003 -0
- package/src/service/materialization_serve_transform.ts +779 -0
- package/src/service/materialization_service.spec.ts +774 -7
- package/src/service/materialization_service.ts +1107 -42
- package/src/service/materialization_test_fixtures.ts +7 -0
- package/src/service/model.spec.ts +207 -0
- package/src/service/model.ts +569 -59
- package/src/service/model_limits.spec.ts +28 -0
- package/src/service/model_limits.ts +21 -0
- package/src/service/model_storage_serve.spec.ts +193 -0
- package/src/service/model_storage_serve_joins.spec.ts +193 -0
- package/src/service/package.spec.ts +196 -0
- package/src/service/package.ts +385 -17
- package/src/service/persistence_policy.spec.ts +109 -0
- package/src/storage/duckdb/schema.ts +37 -0
- package/tests/fixtures/xlsx/database.xlsx +0 -0
- package/tests/integration/first_boot/readiness_line.integration.spec.ts +177 -0
- package/tests/integration/materialization/manifest_binding.integration.spec.ts +104 -0
- package/tests/integration/mcp/mcp_execute_query_tool.integration.spec.ts +37 -12
- package/tests/integration/mcp/mcp_get_context_semantic.integration.spec.ts +235 -0
|
@@ -13,7 +13,11 @@ import {
|
|
|
13
13
|
// compile_tool.spec.ts and reload_package_tool.spec.ts.
|
|
14
14
|
type Handler = (params: Record<string, unknown>) => Promise<{
|
|
15
15
|
isError?: boolean;
|
|
16
|
-
content: Array<{
|
|
16
|
+
content: Array<{
|
|
17
|
+
type?: string;
|
|
18
|
+
text?: string;
|
|
19
|
+
resource?: { text: string };
|
|
20
|
+
}>;
|
|
17
21
|
}>;
|
|
18
22
|
|
|
19
23
|
function captureHandler(store: Partial<EnvironmentStore>): Handler {
|
|
@@ -28,8 +32,8 @@ function captureHandler(store: Partial<EnvironmentStore>): Handler {
|
|
|
28
32
|
return handler;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
function parse(result: { content: Array<{ resource
|
|
32
|
-
return JSON.parse(result.content[0].resource
|
|
35
|
+
function parse(result: { content: Array<{ resource?: { text: string } }> }) {
|
|
36
|
+
return JSON.parse(result.content[0].resource!.text);
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
/**
|
|
@@ -140,4 +144,20 @@ describe("malloy_executeQuery error classification", () => {
|
|
|
140
144
|
// one; echoing the name back would undo that.
|
|
141
145
|
expect(parsed.error).not.toContain("salaries");
|
|
142
146
|
});
|
|
147
|
+
|
|
148
|
+
it("also states the error in a text block", async () => {
|
|
149
|
+
// The structured payload rides in an embedded resource block. A client
|
|
150
|
+
// that renders only text blocks on an isError result shows nothing at
|
|
151
|
+
// all for it, which is how a real diagnostic surfaces to the agent as a
|
|
152
|
+
// bare "Unknown error". Every error must say it in plain text too.
|
|
153
|
+
const handler = captureHandler(
|
|
154
|
+
storeWhoseQueryThrows(new MalloyError("unexpected '@'", [])),
|
|
155
|
+
);
|
|
156
|
+
const result = await handler(args);
|
|
157
|
+
const parsed = parse(result);
|
|
158
|
+
|
|
159
|
+
const textBlock = result.content.find((b) => b.type === "text");
|
|
160
|
+
expect(textBlock).toBeDefined();
|
|
161
|
+
expect(textBlock!.text).toContain(parsed.error);
|
|
162
|
+
});
|
|
143
163
|
});
|
|
@@ -16,6 +16,9 @@ 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 { bigIntReplacer } from "../../json_utils";
|
|
19
22
|
import { MCP_ERROR_MESSAGES } from "../mcp_constants";
|
|
20
23
|
|
|
21
24
|
/**
|
|
@@ -61,6 +64,26 @@ const executeQueryShape = {
|
|
|
61
64
|
),
|
|
62
65
|
};
|
|
63
66
|
|
|
67
|
+
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).
|
|
68
|
+
|
|
69
|
+
## Contract rules
|
|
70
|
+
- 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.
|
|
71
|
+
- Never sum or count the returned rows to state a total when _limit_hit or _rows_truncated is set. Aggregate in the query instead.
|
|
72
|
+
- _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.
|
|
73
|
+
- Use source, view, and field names exactly as malloy_getContext returned them.
|
|
74
|
+
|
|
75
|
+
## Response
|
|
76
|
+
A JSON object, the same shape Credible's execute_query returns, so a data app behaves the same authored locally and served in production:
|
|
77
|
+
- rows: flat objects keyed by column name, the shape an in-package data app receives.
|
|
78
|
+
- _meta: the Malloy metadata flat rows drop (schema with field types and render tags, annotations, connection_name, query_timezone).
|
|
79
|
+
- _query_row_limit: the cap pushed into the SQL, from the query's own limit: or the server default.
|
|
80
|
+
- _limit_source: "query" when the cap came from the query's own limit:/top:, "server_default" otherwise.
|
|
81
|
+
- _limit_hit: the row count equals that cap AND the cap was the server default.
|
|
82
|
+
- _rows_truncated / _total_rows / _returned_rows: present only when the payload cap dropped rows.
|
|
83
|
+
- warning, renderLogErrors: present only when they apply.
|
|
84
|
+
|
|
85
|
+
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.`;
|
|
86
|
+
|
|
64
87
|
// Type inference is handled automatically by the MCP server based on the executeQueryShape
|
|
65
88
|
|
|
66
89
|
/**
|
|
@@ -72,7 +95,7 @@ export function registerExecuteQueryTool(
|
|
|
72
95
|
): void {
|
|
73
96
|
mcpServer.tool(
|
|
74
97
|
"malloy_executeQuery",
|
|
75
|
-
|
|
98
|
+
EXECUTE_QUERY_DESCRIPTION,
|
|
76
99
|
executeQueryShape,
|
|
77
100
|
/** Handles requests for the malloy_executeQuery tool */
|
|
78
101
|
async (params) => {
|
|
@@ -120,29 +143,10 @@ export function registerExecuteQueryTool(
|
|
|
120
143
|
|
|
121
144
|
// Handle errors during package/model access (e.g., not found, initial compilation)
|
|
122
145
|
if ("error" in modelResult) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
error: modelResult.error.message,
|
|
127
|
-
suggestions: modelResult.error.suggestions,
|
|
128
|
-
},
|
|
129
|
-
null,
|
|
130
|
-
2,
|
|
146
|
+
return jsonToolError(
|
|
147
|
+
"error://executeQuery/modelAccess",
|
|
148
|
+
modelResult.error,
|
|
131
149
|
);
|
|
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
150
|
}
|
|
147
151
|
|
|
148
152
|
// --- Execute Query ---
|
|
@@ -160,123 +164,77 @@ export function registerExecuteQueryTool(
|
|
|
160
164
|
let querySlot: QuerySlotHandle | null = null;
|
|
161
165
|
try {
|
|
162
166
|
querySlot = tryAcquireQuerySlot("mcp:executeQuery");
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
167
|
+
// The two call modes differ only in which arguments carry the
|
|
168
|
+
// query; everything after the run is identical, so they share one
|
|
169
|
+
// path rather than two copies that can drift.
|
|
170
|
+
const { result, compactResult, rowLimit, rowLimitSource } =
|
|
171
|
+
await runWithQueryTimeout(
|
|
166
172
|
(abortSignal) =>
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
+
),
|
|
176
192
|
getQueryTimeoutMs(),
|
|
177
193
|
);
|
|
178
|
-
const { validateRenderTags } = await import(
|
|
179
|
-
"@malloydata/render-validator"
|
|
180
|
-
);
|
|
181
|
-
const renderLogs = validateRenderTags(result);
|
|
182
194
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
const resultString = JSON.stringify(result, null, 2);
|
|
191
|
-
|
|
192
|
-
const content = [
|
|
193
|
-
{
|
|
194
|
-
type: "resource" as const,
|
|
195
|
-
resource: {
|
|
196
|
-
type: "application/json",
|
|
197
|
-
uri: resultUri,
|
|
198
|
-
text: resultString,
|
|
199
|
-
},
|
|
200
|
-
},
|
|
201
|
-
];
|
|
202
|
-
|
|
203
|
-
if (renderLogs.length > 0) {
|
|
204
|
-
return {
|
|
205
|
-
isError: false,
|
|
206
|
-
content: [
|
|
207
|
-
...content,
|
|
208
|
-
{
|
|
209
|
-
type: "text" as const,
|
|
210
|
-
text: `Render tag warnings:\n${JSON.stringify(renderLogs, null, 2)}`,
|
|
211
|
-
},
|
|
212
|
-
],
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return { isError: false, content };
|
|
217
|
-
} else if (queryName) {
|
|
218
|
-
const { result } = await runWithQueryTimeout(
|
|
219
|
-
(abortSignal) =>
|
|
220
|
-
model.getQueryResults(
|
|
221
|
-
sourceName,
|
|
222
|
-
queryName,
|
|
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);
|
|
195
|
+
// Render-tag validation reads the FULL Malloy result: the tags live
|
|
196
|
+
// in its schema annotations, which the flat rows do not carry. It
|
|
197
|
+
// runs regardless of which shape is returned.
|
|
198
|
+
const { validateRenderTags } = await import(
|
|
199
|
+
"@malloydata/render-validator"
|
|
200
|
+
);
|
|
201
|
+
const renderLogs = validateRenderTags(result);
|
|
235
202
|
|
|
236
|
-
|
|
203
|
+
const resultUri = buildMalloyUri(
|
|
204
|
+
{
|
|
237
205
|
environment: environmentName,
|
|
238
206
|
package: packageName,
|
|
239
207
|
resourceType: "models" as const,
|
|
240
208
|
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
|
-
];
|
|
209
|
+
},
|
|
210
|
+
"result",
|
|
211
|
+
);
|
|
255
212
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
},
|
|
265
|
-
],
|
|
266
|
-
};
|
|
267
|
-
}
|
|
213
|
+
const envelope = buildQueryEnvelope(
|
|
214
|
+
compactResult,
|
|
215
|
+
rowLimit,
|
|
216
|
+
result,
|
|
217
|
+
renderLogs.map((log) => log.message),
|
|
218
|
+
undefined,
|
|
219
|
+
rowLimitSource,
|
|
220
|
+
);
|
|
268
221
|
|
|
269
|
-
|
|
270
|
-
|
|
222
|
+
// A capped or truncated result, and a broken render tag, are the
|
|
223
|
+
// things an agent most needs to notice, so they are stated in text
|
|
224
|
+
// rather than left for a client that parses the payload.
|
|
225
|
+
const notes = [
|
|
226
|
+
envelope.warning,
|
|
227
|
+
envelope.renderLogErrors &&
|
|
228
|
+
`Render tag problems: ${envelope.renderLogErrors.join("; ")}`,
|
|
229
|
+
].filter(Boolean);
|
|
271
230
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
);
|
|
231
|
+
return jsonResource(resultUri, envelope, {
|
|
232
|
+
space: 2,
|
|
233
|
+
// BigInt reaches here: compactResult is raw driver output and
|
|
234
|
+
// DuckDB returns count() as one.
|
|
235
|
+
replacer: bigIntReplacer,
|
|
236
|
+
text: notes.length > 0 ? notes.join("\n\n") : undefined,
|
|
237
|
+
});
|
|
280
238
|
} catch (queryError) {
|
|
281
239
|
// Handle query execution errors (syntax errors, invalid queries, etc.)
|
|
282
240
|
logger.error(
|
|
@@ -306,29 +264,10 @@ export function registerExecuteQueryTool(
|
|
|
306
264
|
);
|
|
307
265
|
}
|
|
308
266
|
|
|
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
|
-
};
|
|
267
|
+
return jsonToolError("error://executeQuery/queryExecution", {
|
|
268
|
+
message: errorDetails.message,
|
|
269
|
+
suggestions,
|
|
270
|
+
});
|
|
332
271
|
} finally {
|
|
333
272
|
// Release on every exit path — success, error, or
|
|
334
273
|
// unreachable code-path throw. `release()` is idempotent
|
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Retrieval eval for malloy_getContext: lexical baseline vs the optional
|
|
3
|
+
* embedding-backed semantic mode.
|
|
3
4
|
*
|
|
4
5
|
* Runs a labeled set of plain-English queries against the live MCP endpoint
|
|
5
|
-
* and reports recall@K: whether the expected entity appears in the top-K
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* and reports recall@K: whether the expected entity appears in the top-K
|
|
7
|
+
* results. Each case is labeled with the retrieval mode the server reported
|
|
8
|
+
* (`semantic` / `lexical`; servers without an embedding provider report no
|
|
9
|
+
* mode and run the lexical baseline).
|
|
10
|
+
*
|
|
11
|
+
* A/B: run once against a server started WITHOUT `EMBEDDING_API_KEY`
|
|
12
|
+
* (lexical baseline), then once against a server started with it, and
|
|
13
|
+
* compare the two summaries. The cases marked `gap` share no tokens with
|
|
14
|
+
* their target entity's name or docs, so the lexical run is expected to
|
|
15
|
+
* miss them and the semantic run to hit them.
|
|
9
16
|
*
|
|
10
17
|
* bun run packages/server/src/mcp/tools/get_context_eval.ts [K]
|
|
11
18
|
*
|
|
12
|
-
* Targets MCP_URL (default http://localhost:4040/mcp)
|
|
13
|
-
*
|
|
19
|
+
* Targets MCP_URL (default http://localhost:4040/mcp); start the server
|
|
20
|
+
* first. Cases cover the bundled `examples` environment (storefront), so
|
|
21
|
+
* the default config works out of the box, plus the classic ecommerce/faa
|
|
22
|
+
* cases for a malloy-samples config (`samples` environment); cases whose
|
|
23
|
+
* environment is not served are skipped, not counted as misses.
|
|
14
24
|
*/
|
|
15
25
|
|
|
16
26
|
interface EvalCase {
|
|
27
|
+
env: string;
|
|
17
28
|
pkg: string;
|
|
18
29
|
query: string;
|
|
19
30
|
expect: string; // substring expected in a top-K result name (case-insensitive)
|
|
31
|
+
gap?: boolean; // true = no token overlap; lexical is expected to miss
|
|
20
32
|
}
|
|
21
33
|
|
|
22
34
|
interface ResultEntity {
|
|
@@ -25,34 +37,108 @@ interface ResultEntity {
|
|
|
25
37
|
source?: string | null;
|
|
26
38
|
}
|
|
27
39
|
|
|
28
|
-
|
|
40
|
+
interface GetContextPayload {
|
|
41
|
+
retrieval?: string;
|
|
42
|
+
results?: ResultEntity[];
|
|
43
|
+
error?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Ground truth verified against the bundled examples (storefront) and the
|
|
47
|
+
// malloy-samples demo packages (ecommerce, faa).
|
|
29
48
|
const CASES: EvalCase[] = [
|
|
49
|
+
// examples/storefront: served by the default config, works out of the box.
|
|
50
|
+
{
|
|
51
|
+
env: "examples",
|
|
52
|
+
pkg: "storefront",
|
|
53
|
+
query: "revenue by product category",
|
|
54
|
+
expect: "category",
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
env: "examples",
|
|
58
|
+
pkg: "storefront",
|
|
59
|
+
query: "top selling products",
|
|
60
|
+
expect: "top_products",
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
env: "examples",
|
|
64
|
+
pkg: "storefront",
|
|
65
|
+
query: "monthly sales trend",
|
|
66
|
+
expect: "sales_by_month",
|
|
67
|
+
},
|
|
68
|
+
// Token-gap cases: no word here appears in the entity's name or #(doc)
|
|
69
|
+
// text, so a token-based index misses them; embeddings should not.
|
|
70
|
+
{
|
|
71
|
+
env: "examples",
|
|
72
|
+
pkg: "storefront",
|
|
73
|
+
query: "how much money did we make",
|
|
74
|
+
expect: "total_sales",
|
|
75
|
+
gap: true,
|
|
76
|
+
},
|
|
30
77
|
{
|
|
78
|
+
env: "examples",
|
|
79
|
+
pkg: "storefront",
|
|
80
|
+
query: "refund percentage",
|
|
81
|
+
expect: "return_rate",
|
|
82
|
+
gap: true,
|
|
83
|
+
},
|
|
84
|
+
// malloy-samples (needs a config serving them as environment "samples").
|
|
85
|
+
{
|
|
86
|
+
env: "samples",
|
|
31
87
|
pkg: "ecommerce",
|
|
32
88
|
query: "revenue by product category",
|
|
33
89
|
expect: "category",
|
|
34
90
|
},
|
|
35
|
-
{
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
{
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
91
|
+
{
|
|
92
|
+
env: "samples",
|
|
93
|
+
pkg: "ecommerce",
|
|
94
|
+
query: "total sales revenue",
|
|
95
|
+
expect: "total_sales",
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
env: "samples",
|
|
99
|
+
pkg: "ecommerce",
|
|
100
|
+
query: "customer state location",
|
|
101
|
+
expect: "state",
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
env: "samples",
|
|
105
|
+
pkg: "ecommerce",
|
|
106
|
+
query: "order count",
|
|
107
|
+
expect: "order_count",
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
env: "samples",
|
|
111
|
+
pkg: "ecommerce",
|
|
112
|
+
query: "product brand",
|
|
113
|
+
expect: "brand",
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
env: "samples",
|
|
117
|
+
pkg: "faa",
|
|
118
|
+
query: "flights by carrier",
|
|
119
|
+
expect: "carrier",
|
|
120
|
+
},
|
|
121
|
+
{ env: "samples", pkg: "faa", query: "airport", expect: "airport" },
|
|
122
|
+
{ env: "samples", pkg: "faa", query: "aircraft model", expect: "aircraft" },
|
|
123
|
+
// The original lexical-gap case: the field is "dep_delay", which shares
|
|
124
|
+
// no tokens with "departure delay".
|
|
125
|
+
{
|
|
126
|
+
env: "samples",
|
|
127
|
+
pkg: "faa",
|
|
128
|
+
query: "departure delay",
|
|
129
|
+
expect: "delay",
|
|
130
|
+
gap: true,
|
|
131
|
+
},
|
|
45
132
|
];
|
|
46
133
|
|
|
47
134
|
const ENDPOINT = process.env.MCP_URL || "http://localhost:4040/mcp";
|
|
48
|
-
const EVAL_ENV = process.env.EVAL_ENV || "samples";
|
|
49
135
|
const K = Number(process.argv[2] || 5);
|
|
136
|
+
const WARMUP_ATTEMPTS = 15;
|
|
137
|
+
const WARMUP_DELAY_MS = 2_000;
|
|
50
138
|
|
|
51
|
-
async function
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
limit: number,
|
|
55
|
-
): Promise<ResultEntity[]> {
|
|
139
|
+
async function callGetContext(
|
|
140
|
+
args: Record<string, unknown>,
|
|
141
|
+
): Promise<GetContextPayload> {
|
|
56
142
|
const res = await fetch(ENDPOINT, {
|
|
57
143
|
method: "POST",
|
|
58
144
|
headers: {
|
|
@@ -63,36 +149,91 @@ async function getContext(
|
|
|
63
149
|
jsonrpc: "2.0",
|
|
64
150
|
id: 1,
|
|
65
151
|
method: "tools/call",
|
|
66
|
-
params: {
|
|
67
|
-
name: "malloy_getContext",
|
|
68
|
-
arguments: {
|
|
69
|
-
environmentName: EVAL_ENV,
|
|
70
|
-
packageName: pkg,
|
|
71
|
-
query,
|
|
72
|
-
limit,
|
|
73
|
-
},
|
|
74
|
-
},
|
|
152
|
+
params: { name: "malloy_getContext", arguments: args },
|
|
75
153
|
}),
|
|
76
154
|
});
|
|
77
155
|
const text = await res.text();
|
|
78
156
|
const dataLine = text.split("\n").find((l) => l.startsWith("data: "));
|
|
79
|
-
if (!dataLine) return [];
|
|
157
|
+
if (!dataLine) return { results: [] };
|
|
80
158
|
const msg = JSON.parse(dataLine.slice(6)) as {
|
|
81
159
|
result?: { content?: { resource?: { text?: string } }[] };
|
|
82
160
|
};
|
|
83
161
|
const payloadText = msg.result?.content?.[0]?.resource?.text;
|
|
84
|
-
if (!payloadText) return [];
|
|
85
|
-
|
|
86
|
-
|
|
162
|
+
if (!payloadText) return { results: [] };
|
|
163
|
+
return JSON.parse(payloadText) as GetContextPayload;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** True when the environment exists on the server (tier-2 listing works). */
|
|
167
|
+
async function environmentServed(env: string): Promise<boolean> {
|
|
168
|
+
const payload = await callGetContext({ environmentName: env });
|
|
169
|
+
return payload.error === undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* On a semantic-capable server the first query against a package kicks off
|
|
174
|
+
* the embedding sync and answers lexically; poll until the mode flips (or
|
|
175
|
+
* give up and report whatever mode the server settles on).
|
|
176
|
+
*/
|
|
177
|
+
async function warmUp(env: string, pkg: string): Promise<string | undefined> {
|
|
178
|
+
let mode: string | undefined;
|
|
179
|
+
for (let attempt = 0; attempt < WARMUP_ATTEMPTS; attempt++) {
|
|
180
|
+
const payload = await callGetContext({
|
|
181
|
+
environmentName: env,
|
|
182
|
+
packageName: pkg,
|
|
183
|
+
query: "warm up",
|
|
184
|
+
limit: 1,
|
|
185
|
+
});
|
|
186
|
+
mode = payload.retrieval;
|
|
187
|
+
// No marker: the server has no embedding provider configured, so
|
|
188
|
+
// there is nothing to warm up.
|
|
189
|
+
if (mode === undefined || mode === "semantic") return mode;
|
|
190
|
+
await new Promise((resolve) => setTimeout(resolve, WARMUP_DELAY_MS));
|
|
191
|
+
}
|
|
192
|
+
return mode;
|
|
87
193
|
}
|
|
88
194
|
|
|
89
195
|
async function main(): Promise<void> {
|
|
90
196
|
console.log(
|
|
91
|
-
`get_context
|
|
197
|
+
`get_context retrieval eval, recall@${K} (endpoint ${ENDPOINT})\n`,
|
|
92
198
|
);
|
|
199
|
+
|
|
200
|
+
const servedEnvs = new Map<string, boolean>();
|
|
201
|
+
for (const env of new Set(CASES.map((c) => c.env))) {
|
|
202
|
+
servedEnvs.set(env, await environmentServed(env));
|
|
203
|
+
}
|
|
204
|
+
for (const [env, served] of servedEnvs) {
|
|
205
|
+
if (!served) {
|
|
206
|
+
console.log(
|
|
207
|
+
` [SKIP] environment "${env}" is not served; skipping its cases`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const warmed = new Set<string>();
|
|
93
213
|
let hits = 0;
|
|
214
|
+
let scored = 0;
|
|
215
|
+
const modesSeen = new Set<string>();
|
|
216
|
+
|
|
94
217
|
for (const c of CASES) {
|
|
95
|
-
|
|
218
|
+
if (!servedEnvs.get(c.env)) continue;
|
|
219
|
+
|
|
220
|
+
const pkgKey = `${c.env}/${c.pkg}`;
|
|
221
|
+
if (!warmed.has(pkgKey)) {
|
|
222
|
+
warmed.add(pkgKey);
|
|
223
|
+
await warmUp(c.env, c.pkg);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const payload = await callGetContext({
|
|
227
|
+
environmentName: c.env,
|
|
228
|
+
packageName: c.pkg,
|
|
229
|
+
query: c.query,
|
|
230
|
+
limit: K,
|
|
231
|
+
});
|
|
232
|
+
const results = payload.results ?? [];
|
|
233
|
+
const mode = payload.retrieval ?? "lexical (no provider)";
|
|
234
|
+
modesSeen.add(mode);
|
|
235
|
+
|
|
236
|
+
scored++;
|
|
96
237
|
const rank = results.findIndex((r) =>
|
|
97
238
|
r.name.toLowerCase().includes(c.expect.toLowerCase()),
|
|
98
239
|
);
|
|
@@ -104,20 +245,28 @@ async function main(): Promise<void> {
|
|
|
104
245
|
.map((r) => `${r.kind}:${r.name}`)
|
|
105
246
|
.join(", ") || "(none)";
|
|
106
247
|
const tag = hit ? `HIT@${rank + 1}` : "MISS ";
|
|
107
|
-
|
|
248
|
+
const gap = c.gap ? " [gap]" : "";
|
|
249
|
+
console.log(
|
|
250
|
+
` [${tag}] [${mode}] ${c.env}/${c.pkg} / "${c.query}" (want ~${c.expect})${gap}`,
|
|
251
|
+
);
|
|
108
252
|
console.log(` top: ${top}`);
|
|
109
253
|
}
|
|
110
|
-
|
|
111
|
-
|
|
254
|
+
|
|
255
|
+
if (scored === 0) {
|
|
256
|
+
console.log("\nNo cases ran: no listed environment is served.");
|
|
257
|
+
process.exit(1);
|
|
258
|
+
}
|
|
259
|
+
const pct = ((hits / scored) * 100).toFixed(0);
|
|
112
260
|
console.log(
|
|
113
|
-
|
|
261
|
+
`\nrecall@${K}: ${hits}/${scored} (${pct}%) mode(s): ${[...modesSeen].join(", ")}`,
|
|
114
262
|
);
|
|
115
263
|
console.log(
|
|
116
|
-
"
|
|
264
|
+
"The [gap] cases share no tokens with their target entity, so a lexical",
|
|
117
265
|
);
|
|
118
266
|
console.log(
|
|
119
|
-
"
|
|
267
|
+
"run misses them by construction; a semantic run (EMBEDDING_API_KEY set)",
|
|
120
268
|
);
|
|
269
|
+
console.log("is expected to close them. Compare the two summaries.");
|
|
121
270
|
}
|
|
122
271
|
|
|
123
272
|
main().catch((err) => {
|