Skip to content

Commit edb20d0

Browse files
committed
refactor(mcp): aggregate_records takes the spec's closed aggregation node whole
The tool's own closed copy of the entry folds into AggregationNodeSchema, which now refuses undeclared keys at the producer: one refusal, the REST door's words, and the retired distinct answers its tombstone on both doors. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5m158G1nvmxNYr7Jwq
1 parent 28eaade commit edb20d0

2 files changed

Lines changed: 42 additions & 58 deletions

File tree

‎packages/mcp/src/mcp-http-tools.aggregation-entry.test.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@
2323
*
2424
* CONTROLS. Entries without a filter reach the bridge exactly as before, and
2525
* the top-level unknown-key posture is unchanged.
26+
*
27+
* [#28609] The entry is now the spec's `AggregationNodeSchema` itself, closed at
28+
* the producer, instead of this tool's own closed copy of its members. So a
29+
* refused entry reads here in the very words the REST query door prints for
30+
* the same body (`packages/rest/src/aggregation-entry-undeclared-key-door.test.ts`
31+
* pins that half against the same spec message), and the retired `distinct`
32+
* answers its tombstone sentence on both doors. `tools/list` declares
33+
* `distinct` as a schema nothing satisfies (`not: {}`), with the removal text.
2634
*/
2735

2836
import { describe, it, expect, beforeEach } from 'vitest';
2937

38+
import { AggregationNodeSchema } from '@objectstack/spec/data';
39+
3040
import { MCPServerRuntime } from './mcp-server-runtime.js';
3141
import type { McpDataBridge } from './mcp-http-tools.js';
3242

@@ -54,6 +64,13 @@ function makeBridge(): RecordingBridge {
5464

5565
let nextId = 1;
5666

67+
/** The refusal the spec node prints for one entry — the words the REST door answers too. */
68+
function nodeRefusal(entry: Record<string, unknown>): string {
69+
const parsed = AggregationNodeSchema.safeParse(entry);
70+
expect(parsed.success, 'the spec node refuses this entry').toBe(false);
71+
return parsed.error!.issues[0]!.message;
72+
}
73+
5774
async function rpc(runtime: MCPServerRuntime, bridge: RecordingBridge, method: string, params?: unknown) {
5875
const body = { jsonrpc: '2.0', id: nextId++, method, ...(params === undefined ? {} : { params }) };
5976
const req = new Request('http://localhost/api/v1/mcp', {
@@ -124,6 +141,8 @@ describe('aggregate_records — the aggregation entry (#28550)', () => {
124141
});
125142
expect(refused).toBe(true);
126143
expect(text).toContain('bogusKey');
144+
// [#28609] The spec node's own sentence, the one REST prints for this entry.
145+
expect(text).toContain(nodeRefusal({ function: 'count', alias: 'n', bogusKey: 1 }));
127146
expect(bridge.calls).toHaveLength(0);
128147
});
129148

@@ -135,16 +154,18 @@ describe('aggregate_records — the aggregation entry (#28550)', () => {
135154
expect(refused).toBe(true);
136155
expect(text).toContain('where');
137156
expect(text).toContain('filter');
157+
expect(text).toContain(nodeRefusal({ function: 'count', alias: 'n', where: { status: 'active' } }));
138158
expect(bridge.calls).toHaveLength(0);
139159
});
140160

141-
it('the retired `distinct` entry key is refused, not dropped', async () => {
161+
it('the retired `distinct` entry key is refused with its tombstone sentence, as on REST', async () => {
142162
const { refused, text } = await aggregate(runtime, bridge, {
143163
objectName: 'showcase_project',
144164
aggregations: [{ function: 'sum', field: 'budget', alias: 's', distinct: true }],
145165
});
146166
expect(refused).toBe(true);
147-
expect(text).toContain('distinct');
167+
expect(text).toContain('`query.aggregations[].distinct` was removed in @objectstack/spec 17 (ADR-0049)');
168+
expect(text).not.toContain('Unrecognized key');
148169
expect(bridge.calls).toHaveLength(0);
149170
});
150171

@@ -170,13 +191,17 @@ describe('aggregate_records — the aggregation entry (#28550)', () => {
170191
expect(bridge.calls).toHaveLength(0);
171192
});
172193

173-
it('`tools/list` advertises the entry as closed, with `filter` declared', async () => {
194+
it('`tools/list` advertises the entry as closed, with `filter` declared and `distinct` unsatisfiable', async () => {
174195
const json = await rpc(runtime, bridge, 'tools/list');
175196
const tool = (json.result?.tools ?? []).find((t: any) => t.name === 'aggregate_records');
176197
expect(tool).toBeDefined();
177198
const entry = tool.inputSchema?.properties?.aggregations?.items;
178199
expect(entry?.additionalProperties).toBe(false);
179-
expect(Object.keys(entry?.properties ?? {}).sort()).toEqual(['alias', 'field', 'filter', 'function']);
200+
expect(Object.keys(entry?.properties ?? {}).sort()).toEqual(['alias', 'distinct', 'field', 'filter', 'function']);
201+
// [#28609] The tombstone: declared so the refusal carries its prescription,
202+
// and advertised as a schema no value satisfies.
203+
expect(entry?.properties?.distinct?.not).toEqual({});
204+
expect(entry?.properties?.distinct?.description).toMatch(/^\[REMOVED\] /);
180205
expect(entry?.required).toEqual(expect.arrayContaining(['function', 'alias']));
181206
});
182207
});

‎packages/mcp/src/mcp-http-tools.ts‎

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@
3939
import { z } from 'zod';
4040
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4141
import type { EngineAggregateOptions } from '@objectstack/spec/data';
42-
// [#28550] The engine's aggregation entry. Each `aggregate_records` entry is
43-
// built from ITS members, so this door and the REST query door (which parses
44-
// `FindDataRequestSchema`, whose `aggregations` are this node) judge one shape.
42+
// [#28550, #28609] The engine's aggregation entry. Each `aggregate_records`
43+
// entry IS this node, so this door and the REST query door (which parses
44+
// `FindDataRequestSchema`, whose `aggregations` are this node) judge one shape
45+
// with one refusal.
4546
import { AggregationNodeSchema } from '@objectstack/spec/data';
4647
// [#23529] The catalogue's spelling of `PERMISSION_DENIED`, the code whose
4748
// `details` `errorResultFromThrown` withholds. Imported, never hand-spelled.
@@ -693,56 +694,6 @@ function strictToolInput<T extends z.ZodRawShape>(
693694
.strict();
694695
}
695696

696-
/**
697-
* [#28550] One `aggregate_records` aggregation entry: closed, and carrying the
698-
* per-aggregation `filter`.
699-
*
700-
* The entry used to be a plain `z.object({ function, field, alias })`. Zod's
701-
* default for an object is `.strip`, and {@link strictToolInput} closes only the
702-
* tool's TOP-LEVEL keys, so an entry's `filter` was deleted before the handler
703-
* ran: `{ function: 'count', alias: 'n', filter: { status: 'active' } }` answered
704-
* the unconditional count, beside the REST query door answering the filtered
705-
* one for the same body. Any other unknown entry key vanished the same way.
706-
*
707-
* Both halves of the fix are the entry's shape, nothing else:
708-
*
709-
* - **`filter` is declared**, and it is the engine's own member,
710-
* `AggregationNodeSchema.shape.filter` (`FilterConditionSchema`), the slot the
711-
* REST door parses. The handler forwards `aggregations` to the bridge
712-
* unchanged, and every bridge hands them to `engine.aggregate`, which applies
713-
* the filter and runs it through the same refusal doors as `where` (shape,
714-
* unknown field, and the security middleware's field-read check). ⛔ No filter
715-
* is evaluated here: a second evaluator in this package would be a second
716-
* answer to the same question.
717-
* - **The entry is strict**, through {@link strictToolInput}, so an undeclared
718-
* entry key is refused by name, as an undeclared top-level key already is.
719-
* The aliases are semantic near-misses for `filter` and `alias`; they are
720-
* named in the refusal and never parsed.
721-
*
722-
* `function`, `field` and `alias` are the engine's members too. Each keeps the
723-
* description this tool advertised before; `.describe` copies the schema and
724-
* leaves its parse unchanged.
725-
*/
726-
function aggregationEntrySchema() {
727-
const node = AggregationNodeSchema.shape;
728-
return strictToolInput(
729-
{
730-
surface: 'this aggregate_records aggregation entry',
731-
aliases: { where: 'filter', filters: 'filter', conditions: 'filter', as: 'alias' },
732-
},
733-
{
734-
function: node.function.describe('Aggregation function'),
735-
field: node.field.describe('Field to aggregate (omit for count(*))'),
736-
alias: node.alias.describe('Result column name'),
737-
filter: node.filter.describe(
738-
'Optional per-aggregation filter, e.g. {"status":"active"}: only rows matching it feed THIS '
739-
+ 'aggregation, while the other aggregations in the call still read every row. Same operators '
740-
+ 'as where.',
741-
),
742-
},
743-
);
744-
}
745-
746697
/**
747698
* The spellings every object-scoped tool shares, split out because they are the
748699
* same guess wherever an object name is taken.
@@ -1233,7 +1184,15 @@ export function registerObjectTools(
12331184
{
12341185
objectName: z.string().describe('The object/table name'),
12351186
aggregations: z
1236-
.array(aggregationEntrySchema())
1187+
// [#28550 → #28609] One entry is the engine's own node, taken whole:
1188+
// `AggregationNodeSchema` is closed at the producer, so an
1189+
// undeclared entry key is refused here in the words the REST
1190+
// query door prints for the same body (`aliases` and the retired
1191+
// `distinct`'s tombstone included), and `filter` reaches the
1192+
// bridge unchanged for `engine.aggregate` to apply. ⛔ No local
1193+
// copy of the entry: the copy this tool closed by itself (#28550)
1194+
// was a second key list, which this node now is.
1195+
.array(AggregationNodeSchema)
12371196
.min(1)
12381197
.describe(
12391198
'Metrics to compute, e.g. [{"function":"sum","field":"amount","alias":"total"},'

0 commit comments

Comments
 (0)