Repository navigation
Commit 084156d
feat(mcp): aggregate_records names a lookup group's record beside its id, read as the caller (#35202)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8AJJJRiYjshMUNgyMZSqBsr)_
Refs #35138 (item lookup group name)
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
- 改了什么:MCP 的 `aggregate_records` 按 lookup / master_detail
字段分组时,每个分组在键旁多出 `KEY__label`,值为被引用记录的名称(被引用对象的名称字段,按 ADR-0079
解析);工具描述要求模型用 `__label` 称呼该分组、永不写出 id,没有 `__label` 时说明该记录无法命名。HTTP 与
stdio 两个入口共用同一处理函数。
- 为什么改:#35138 W0 决定表「lookup 分组名称」一行:托管问数工具发现分组只给 id 时模型会把 id
当答案念出来;这一项从托管层挪进框架,开源 MCP 客户端也能拿到,拼写与托管 `aggregate_data` 相同。
- 风险与回滚:名称以调用方身份、经同一个 bridge
读取(一次批量读),不以任何其他权限读;读不到的记录与已删除的记录同样没有标签,答案里没有任何东西区分二者;对象、名称字段或 lookup
字段对调用方不可见、目标是工具不读的系统对象、或读取失败时,结果与之前完全一致。已有成员
`rows`/`totalGroups`/`truncated`/`currencies` 不变。回滚即 revert 本 PR。
- 先看:① `packages/mcp/src/mcp-http-tools.ts` 的 `recordNamesAsCaller`(读取走
`bridge.query`、可见性检查与失败即无标签);② 同文件
`withLookupGroupLabels`(只对截断后实际返回的分组读名称,非 lookup 键不加标签);③
`aggregate_records` 处理函数里传入的 `readGuard` 谓词。
- 两分钟验证:`pnpm --filter @objectstack/mcp exec vitest run --maxWorkers=2
src/mcp-http-tools.aggregation-entry.test.ts`,预期 27 个测试全绿,其中 8 个是本 PR
新增的 lookup 名称用例。
- 需要你做的:无,席位落地。
## What changes
`aggregate_records` (`packages/mcp/src/mcp-http-tools.ts`, one handler
serving both the HTTP and the stdio door) now names the record behind
each lookup group key:
- A group key is a lookup key when the caller's served schema
(`servedObjectDefinitionOf`, the member #35186 reads) shows it as a
`lookup` or `master_detail` field with a `reference`. A date-bucketed
key is never one.
- Beside each such key the group carries `KEY__label`, where KEY is the
group column (the key's alias when the call gives one). Its value is the
referenced record's value of the referenced object's name field,
resolved with `resolveDisplayField` from `@objectstack/spec/data`
(ADR-0079: `nameField`, then `displayNameField`, then the derived title
field) over the referenced object as the caller is served it.
- The names are read as the caller: one `bridge.query` per referenced
object, `where: { id: { $in: ids } }`, `fields: ['id', NAME_FIELD]`,
`limit: ids.length`, for the groups the tool serves (after the row cap),
through the same bridge and context as the aggregate. Two lookup keys on
the same referenced object share that one read. This is the read shape
`delete_records` already uses for an id list.
- A label is present only for a key whose record that read returns with
a non-blank name. Every other key carries no label and nothing else in
the answer changes.
- No label is added, and the result is exactly as before, when: the
lookup field is not served to the caller; the referenced object or its
name field is not served to the caller; the referenced object is one
this tool does not read (the same `readGuard` the read tools apply, so a
`sys_` object without the AI read opt-in is never read for a name); or
the read throws or answers no records list. A name read never fails the
aggregate.
- A column the aggregate already answers under the label's name is never
overwritten.
- The tool description gains: a lookup group carries `KEY__label` beside
the id, read with the caller's own permissions; name such a group by its
`__label`, never by the id; where a group has no `__label`, its record
cannot be named, so say that rather than quoting the id.
The type stays local to `packages/mcp`; the `aggregate_records` result
has no schema in `packages/spec`, and no guidance text enters the spec
(ADR-0149 M1). No existing member is renamed or removed.
## Tests
<details><summary>New pins: top-level describe in
packages/mcp/src/mcp-http-tools.aggregation-entry.test.ts (the shared
aggregate_records harness)</summary>
- a lookup and a master-detail group carry `KEY__label`: the referenced
record's name field (`nameField: 'title'`), placed right after the key;
an aliased key is labelled by its alias
- CONTROL: a second key that is not a lookup carries no label, even
where its value is a record id
- CONTROL: a record the caller's read does not return and a deleted
record answer the same, with no label (the two answers compared whole
and as JSON text)
- CONTROL: a record whose name is blank, and a null key, carry no label
- CONTROL: a referenced object, a name field or a lookup field the
caller is not served, or a system object the tool does not read, answers
exactly as before and makes no name read
- CONTROL: a name read that throws, or answers no records list, answers
exactly as before
- the names are one batched read of the referenced object, for the
groups served (60 groups, cap 50: one read, 52 ids, two keys on one
object), as the caller
- the stdio door: through `createStdioDataBridge` and a real MCP client,
the name read reaches the engine as one `find` under the principal's own
context, and only the record that read returns is labelled
</details>
<details><summary>Ablations (one or more per pin, through
scripts/ablation-replace.mjs; each restored to the HEAD blob
a3381bf)</summary>
Each mutation landed (anchor 1 then 0, blob changed) and the file went
red; every restore read `ok restored: blob == HEAD (a3381bf) and
git diff HEAD is empty`.
- A, the label never set: 5 failed (the label pin, both CONTROLs that
also expect a label, the batched-read pin, the stdio pin).
- B, names applied to any column: 1 failed (the non-lookup CONTROL).
- C, a placeholder label for a key with no name: 3 failed (the
unreadable/deleted CONTROL, the batched-read pin, the stdio pin).
- D1, the name field's served check removed: 1 failed (the not-served
CONTROL).
- D2, the referenced object's read guard replaced by `true`: 1 failed
(the not-served CONTROL, its system-object half).
- D3, the lookup-type check replaced by a field-name match: 1 failed
(the not-served CONTROL, its lookup-field half).
- E, the read's catch rethrows: 1 failed (the throwing-read CONTROL).
- F1, one read per id: 2 failed (the batched-read pin, the stdio pin).
- F2, names read for every group before the cap: 2 failed (the
batched-read pin, and #35186's cap pin).
The unreadable/deleted CONTROL is red under C on its shape half. Its
comparison half cannot be turned red by a mutation inside the handler,
because the handler sees only what the caller's read returns.
</details>
## Acceptance notes
- **#35193 (#35144, area o1-C) shares this handler and is still open.**
`overlap-check` reports conflicts with it in `mcp-http-tools.ts` and
`mcp-http-tools.aggregation-entry.test.ts`, both adjacent insertions.
Whichever PR merges `main` second keeps both behaviours: the cut path's
result (`aggregateGroups`) gets its served rows labelled the same way,
through `withLookupGroupLabels(bridge, ROWS, groupBy, FIELDS,
READ_GUARD_PREDICATE)` over `servedFieldsOf(bridge, objectName)`; the
default path keeps `groupCount` from #35193 and the labels from here.
- **Aliased keys.** The label member is named after the group column,
which is the alias when the call gives one (`{ field: 'parent', alias:
'p' }` gives `p__label`), so the label always sits beside the key it
names.
- **Name values.** A non-blank string is a name; a finite number (an
explicit numeric name field) is served as its string. Anything else is
no name.
- **Stdio query cost.** The stdio bridge's `query` counts the match when
a `limit` is passed, so the one name read there is a find plus a count
of the same filter, both as the caller. The `limit` is kept because it
is the read shape `delete_records` uses and it bounds the read on any
host page size.
## Verification
All readings below are at head `90e185aa5b`, the final commit.
<details><summary>Local subset</summary>
- `pnpm --filter @objectstack/mcp typecheck`, then `vitest run
--maxWorkers=2` over the whole package, under the shared verify lock:
`VERDICT command-exit 0`; `Test Files 72 passed (72)`, `Tests 1299
passed (1299)`; `check:test-typecheck: OK`.
- `src/mcp-http-tools.aggregation-entry.test.ts`: `Tests 27 passed
(27)`.
- `dispatch-gates --commands`: 68 derived families, each exit code
captured before any pipe. `--ran` reconciliation: `68 derived, 67 run, 1
NOT-MEASURED, 0 UNRUN`.
- NOT MEASURED: `check:dual-build-cjs-loads`, exit 3, `PREREQUISITE NOT
MET` (it reads every package's `dist`, and only the mcp closure was
built). CI runs it.
- `node scripts/check-nul-bytes.mjs`: OK.
- `overlap-check.mjs`: `VERDICT overlap exit 1`, against origin/main
`ba6556c617` (merges cleanly) and two open PR heads: #35179 merges
cleanly; #35193 conflicts as described above.
- `test:repo` not run locally: the diff touches no repo-project test and
the derivation names none; CI's `Test Core` runs it.
</details>
The families CI runs (`Lint & Repo Gates`, `Test Core`, `Build Core`,
the type-check lanes) are left to this PR's check-runs.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01JQwxbYRqkDAGuse5Wqbohi)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2d33205 commit 084156d
3 files changed
Lines changed: 383 additions & 7 deletions
File tree
- .changeset
- packages/mcp/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
37 | 40 | | |
38 | 41 | | |
| 42 | + | |
| 43 | + | |
39 | 44 | | |
40 | 45 | | |
| 46 | + | |
41 | 47 | | |
| 48 | + | |
42 | 49 | | |
43 | 50 | | |
44 | 51 | | |
| |||
359 | 366 | | |
360 | 367 | | |
361 | 368 | | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
0 commit comments