Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/35145-mcp-related-read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@objectstack/mcp': patch
'@objectstack/runtime': patch
---

feat(mcp): a record with its related records in one call — `expand` on `query_records`, `related` on `get_record`

Clause-②: yes (widening)
Metadata-protocol: none

`query_records` takes `expand`: up to 3 of the queried object's lookup, master-detail, user or tree fields, each answered as the record it names, one level deep. `get_record` takes `related`: up to 3 child objects that point at the record, each answered as a list of at most 20 rows (never above the tool's `maxQueryLimit`), as `{ record, related: [{ object, field, records, total, hasMore }] }`.

- Every object a new read shape reaches is judged by the MCP read guard exactly as the object the call names. A target the guard refuses refuses the whole call with `PERMISSION_DENIED` / 403 before any record is read. A related object whose definition is not served to the caller refuses it with `OBJECT_NOT_FOUND` / 404, also before any read.
- An `expand` name that is not a relationship field of the object as the caller is served it, and a `related` link `field` that does not point at the record, are refused with `INVALID_FIELD` / 400 before any read. A child that points at the record through several fields needs `field`.
- `expand` runs through the engine's one expansion path: one batched `id IN (...)` read per field, under the target's own permissions, row-level security and field masking. It combines with `latestPer`. Only the read guard's refusal refuses the call: a record the caller may not read on an admitted target object is not expanded, and the field keeps its stored id.
- A related list is one `query_records` read of the child, filtered only by the link to the record, with the child's own `fields` and `orderBy` and no caller filter. It is read only after the record itself. A list with more rows than its cap carries `truncated: true`.
- Each related list carries the delegation notice `query_records` carries (`delegationNarrowed`, `warning`) when the agent ceiling narrowed the read.
- The record and each related list carry `withheldFields` exactly as a plain `get_record` and a plain `query_records` of that object do.
- The stdio bridge serves `expand` too, built as the data door builds it (a target that does not serve `get` keeps its stored id). `McpDataBridge.query` gains an optional `expand` member.

Without `expand` or `related`, both tools answer exactly as before. `@objectstack/spec` is unchanged.
51 changes: 50 additions & 1 deletion content/docs/ai/connect-mcp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ service is installed:
|:---|:---|
| `list_objects` / `describe_object` | Discover which objects exist and their fields |
| `validate_expression` | Check a CEL expression against an object's schema before authoring it into a formula, validation, or flow condition |
| `query_records` / `get_record` | Read data (list queries are capped at 50 rows per page by default) |
| `query_records` / `get_record` | Read data (list queries are capped at 50 rows per page by default); `expand` and `related` read related records in the same call |
| `aggregate_records` | Grouped aggregation (registered when the active driver supports it) |
| `create_record` / `update_record` / `delete_record` | Write data |
| `delete_records` | Delete many records of one object, by a filter or an id list, at most 200 per call (an OAuth token needs both `data:read` and `data:write`) |
Expand Down Expand Up @@ -313,6 +313,55 @@ names the groups and the order that picks each group's row:
is read, as it is for an object with an `afterFind` hook or object-scoped read
middleware.

### A record with its related records: `expand` and `related`

`query_records` takes `expand`: up to 3 of the object's lookup, master-detail,
user or tree fields. Each one comes back as the record it names, one level
deep:

```json
{ "objectName": "crm_opportunity", "expand": ["account", "owner_id"] }
```

`get_record` takes `related`: up to 3 child objects that point at the record,
each answered as a list of at most 20 rows (`limit` lowers it):

```json
{
"objectName": "crm_account",
"recordId": "acc_1",
"related": [
{ "object": "crm_opportunity", "orderBy": [{ "field": "close_date", "order": "desc" }] },
{ "object": "crm_activity", "field": "account", "limit": 5 }
]
}
```

- The answer is then `{ record, related: [{ object, field, records, total, hasMore }] }`.
A list with more rows than came back also carries `truncated: true`.
- `field` is the child's field that points at the record. It can be left out
when the child has exactly one such field.
- A related list is filtered only by the link to the record. It takes the
child's own `fields` and `orderBy`, and no other filter.
- Every expanded or related object is read as you, under that object's
permissions, row-level security and field masking. A field you may not read is
not offered for `expand` or as a link.
- The record and each related list name the fields withheld from them in
`withheldFields`, as a plain `get_record` and `query_records` do.
- Every object a new read shape reaches is judged by the MCP read guard exactly
as the object the call names. An object the guard refuses (a system object
that is not opted into AI reads) refuses the whole call with
`PERMISSION_DENIED` before anything is read. A related object that is not
served to you refuses it too. An expanded field is different: when the guard
admits its object but you may not read the record it names (no permission on
that object, or a row it hides from you), the call is answered and the field
keeps its stored id.
- Each related list carries the delegation notice (`delegationNarrowed`,
`warning`) that `query_records` carries when the agent ceiling narrowed the
read.
- A name that is not a relationship field, or a `field` that does not point at
the record, is refused with `INVALID_FIELD`.

### Governed metrics and the `initialize` instructions

`list_datasets` and `query_dataset` are registered with the `data:read` scope,
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/security/second-object-read-exposure.pin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,27 @@ const CALLERS: Record<string, Classification> = {
wiring: { file: 'packages/services/service-analytics/src/analytics-service.ts', call: 'assertObjectsExposed(objects, provider' },
pin: 'packages/services/service-analytics/src/__tests__/cross-object-relation-group-join.test.ts',
},
// [#35145] The MCP stdio bridge's `expand` (`query_records`), built as the data door builds it.
'packages/mcp/src/stdio-data-bridge.ts': {
kind: 'decided',
read: "the MCP stdio bridge's `expand` on `query_records` (the engine's expansion sub-read, entered from an engine-only bridge)",
operation: 'get',
decision: { file: 'packages/mcp/src/stdio-data-bridge.ts', fn: 'stdioExpandMap' },
wiring: { file: 'packages/mcp/src/stdio-data-bridge.ts', call: 'stdioExpandMap(reads, object' },
pin: 'packages/mcp/src/mcp-relation-reads.test.ts',
},
// [#35145] The MCP tools' relation judging for `expand` and `related`.
'packages/mcp/src/mcp-relation-reads.ts': {
kind: 'executes',
read:
"the MCP tools' `expand` (`query_records`) and `related` (`get_record`): the targets are judged here by the MCP " +
"read guard, then read through the data door, whose `servedExpand` decides each expansion target",
decidedIn: 'packages/metadata-protocol/src/protocol-query-validation.ts',
also:
'reads no record: `referenceTargetOf` names the object an `expand` field points at (asked of the MCP read guard, ' +
'then expanded by `findData`, which asks its exposure) and checks that a `related` link field points at the parent ' +
'record; each related list is a `findData` read of the child it names, judged by that door as any list.',
},
'packages/core/src/utils/relation-condition-sites.ts': {
kind: 'not-a-served-read',
reason:
Expand Down Expand Up @@ -527,6 +548,7 @@ describe('[#22661] every read that reaches a second object asks that object its
'servesPayloadDisplayTarget',
'servesReferenceTarget',
'servesSummaryTitleTarget',
'stdioExpandMap',
]);

for (const d of decided) {
Expand Down
Loading
Loading