Repository navigation
Commit 34a30e8
feat(mcp): serve list_datasets and query_dataset over MCP under data:read (#31773)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9DiJcGW3TmZrvWPuvNopKmm)_
Fixes #31590
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
- 改了什么:MCP 新增 `list_datasets` / `query_dataset` 两个只读工具(`data:read`
范围),HTTP 与 stdio 两条桥都接到 service-analytics 的数据集执行器;生成的 SKILL.md
教客户端「先查数据集、精确命中才用指标、多义先问、无命中退回对象查询并注明非指标口径」。
- 为什么改:ADR-0149 D2/D3 —— 外部 AI 客户端与产品内问数用同一套数据集工具、同一口径,答案带出处。
- 风险与回滚:纯新增;没有 analytics 服务(或服务不带 `datasetTools`)的部署工具列表与现在完全一样;不带
`data:read` 的令牌看不到这两个工具。回滚即 revert 本 PR。依赖 #31589 合入后才真正有服务端可调。
- 先看:`packages/mcp/src/mcp-http-tools.ts` 的
`registerDatasetTools`(注册条件与范围门)、`packages/runtime/src/domains/mcp.ts` 的
`datasetToolMembers`(数据集读取、本地化、按调用者身份执行)、`packages/mcp/src/skill-md.ts`
的选择规则文案。
- 两分钟验证:`pnpm --filter @objectstack/mcp exec vitest run
src/mcp-dataset-tools.test.ts`,预期 20 个用例全绿。
- 需要你做的:无,席位落地(在 #31589 之后)。
## What changes
- **Bridge members (`packages/mcp/src/mcp-http-tools.ts`).**
`McpDataBridge` gains three optional members: `listDatasets(input)`,
`queryDataset(input, { maxRows })` and `datasetToolDefinitions()`. The
third carries the service-analytics definitions, so this package holds
no copy of the tool descriptions (seat ruling 1, the third member
accepted by the seat).
- **`registerDatasetTools`, called from `wireBridgeTools`.** Registers
`list_datasets` / `query_dataset` only when the bridge carries all three
members AND the grant admits `data:read` (`grantedScopes` undefined
keeps them, as every read tool). Name from the spec constants,
description from the bridge's definitions (a tool the definitions do not
describe is not registered), input schema = the spec declaration
`ListDatasetsInputSchema` / `QueryDatasetInputSchema` taken whole (the
precedent: `aggregate_records` takes `AggregationNodeSchema` whole), so
an undeclared key is refused before dispatch with the spec's
prescription. `query_dataset` hands the tool's page cap
(`maxQueryLimit`, default 50, the `query_records` cap) to the bridge.
Refusals render through `errorResultWithCode`, the coded envelope the
other data tools use. Annotations: read-only, not destructive, closed
world.
- **HTTP bridge (`packages/runtime/src/domains/mcp.ts`).**
`handleMcpRequest` resolves the `analytics` service per request; the
bridge carries the three members only when it has `datasetTools` (seat
ruling C: `AnalyticsService.datasetTools(deps)` from #31589). The
executor is bound to: the saved datasets from the protocol's merged
`getMetaItems({ type: 'dataset' })` (the read the REST door resolves
`datasetName` from), localized with `translateMetaList` for the
request's locale; the request's security service as the narrowing probe;
and the tool's page cap. Every call runs as the request's
`ExecutionContext`. A throwing dataset read reaches the executor as a
throw (its degraded-read answer), never as an empty catalogue. The
`/mcp/skill` route renders the dataset section exactly where the bridge
would carry the pair.
- **stdio bridge (`packages/mcp/src/stdio-data-bridge.ts`,
`plugin.ts`).** `createStdioDataBridge` takes an optional `datasetTools`
service; the plugin resolves it in `start()` (all services are
registered by then, and the tools must be registered before the
transport attaches). Datasets come from the protocol's merged read, or
the metadata service's `list('dataset')` on a host with none; calls run
as the stdio principal, re-resolved per call.
- **SKILL.md (`packages/mcp/src/skill-md.ts`).** New `datasetTools`
render option (the `fileTools` pattern): the two tool entries and the D3
selection rule render only when the host's bridge carries the pair and
the grant admits `data:read`.
- Changeset: patch for `@objectstack/mcp` and `@objectstack/runtime`.
- Not touched: the existing tool shapes (`run_action` / `list_actions` /
`agent_prompt`), E8's by-name readers, `packages/spec/**`,
`packages/services/service-analytics/**`, `ui/**`.
## Tests
- New `packages/mcp/src/mcp-dataset-tools.test.ts` (20 cases):
registration with all three members; descriptions verbatim from the
definitions; input schemas closed; each partial-member case (each one of
the three missing, and all three) registers neither tool and leaves the
rest of the surface unchanged; a definition missing → that tool absent;
`data:read` admits both; a `data:write`-only grant registers neither and
a call gets the same unknown-tool refusal as `query_records`, with the
bridge never asked; argument pass-through and page cap (default and host
`maxQueryLimit`); undeclared key refused before the bridge naming
`datasetName`; coded refusal envelope; SKILL.md rendering by bridge and
grant; the stdio bridge's members (absent without the executor, run as
the stdio principal with the cap, merged read and metadata-service
fallback).
- New `packages/runtime/src/mcp-dataset-tools-bridge.test.ts` (6 cases):
the seam is served only by an `analytics` service carrying
`datasetTools` (absent service and method-absent cases pinned, as the
seat asked); no seam → no members; definitions handed over unchanged;
both calls run as the request's principal; page cap and narrowing probe
bound; a throwing dataset read stays a throw.
- **Forced test edit:**
`packages/mcp/src/skill-md-surface-guard.test.ts` — the full-surface
bridge gains the three dataset members and the two renders pass
`datasetTools: true`, so the drift guard covers the new tools.
## Acceptance notes
- **Lands after #31589.** This branch compiles and passes without it
(the service face is declared structurally, as `McpRecordFileService`
is), but no deployment serves `datasetTools` until #31589 merges; then
merge main here. The MCP row of #31589's four-face parity test is
#31589's to fill once both are on main.
- **stdio datasets are not localized.** The stdio transport has no
request locale (an API-key principal), so labels come as authored; the
HTTP bridge localizes as the REST door does.
- **The stdio bridge passes no narrowing probe**, matching
`query_records` on stdio (an API key is not delegated).
## Verification
Head `5078be21` (merge of `origin/main` `f65c36fc` into the card
commits; that main commit touches `plugin-webhooks` only), measured in a
card container.
- `@objectstack/mcp` test: Test Files 62 passed (62), Tests 979 passed
(979).
- `@objectstack/runtime` test: Test Files 564 passed (564), Tests 9065
passed, 19 skipped (9084).
- `@objectstack/mcp` and `@objectstack/runtime` typecheck (`tsc
--noEmit` + `check:test-typecheck`): exit 0 on `37710911` (later
commits: the changeset and the merge, which brought no file of either
package). The runtime reading needed `driver-turso` built first: an
untouched kernel test imports it.
- Lint: `eslint --no-inline-config packages/mcp packages/runtime` exit
0.
- Derived gates: 68 derived, 67 run with exit 0, 1 NOT MEASURED
(`check:dual-build-cjs-loads`, exit 3: it needs every workspace package
built; left to CI). `check-plugin-teardown-shape --self-test` first
exited 3 on the shallow clone and is exit 0 after fetching its pinned
fixture commit. `dispatch-gates --ran`: 0 UNRUN.
<details><summary>Red on main: 12 of 17 registration/call pins fail on
the origin/main source</summary>
`mcp-http-tools.ts` and `skill-md.ts` restored from `origin/main`
`0105a687`, the new test file kept: `Tests 12 failed | 5 passed (17)`.
The five that pass are the "no dataset tool" cases, which hold on main
by construction. (The three stdio cases were added after this leg.)
</details>
<details><summary>Ablations: four legs, each red, each restored
(scripts/ablation-replace.mjs)</summary>
The tests import source directly (no dist leg). Each leg: anchor x1 to
x0, marker x0 to x1, run red, restore proved by blob == HEAD and an
empty `git diff HEAD`.
- A1 drop the `data:read` gate in `registerDatasetTools`: red `a grant
without data:read registers neither…` (1 failed, 16 passed).
- A2 require only `listDatasets` in `hasDatasetTools`: red `a bridge
without ["queryDataset"]…` and `…["datasetToolDefinitions"]…` (2
failed).
- A3 serve the HTTP seam from any `analytics` service, method or not:
red `the seam is served by an analytics service carrying datasetTools,
and not without one` (1 failed, 5 passed).
- A4 attach the stdio members without the executor: red `a bridge built
without the analytics executor carries no dataset member` (1 failed, 19
passed).
A1's first attempt used a replacement already present in the file; the
tool refused it before running anything (replacement count did not rise)
and it was re-run with a marker.
</details>
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes.
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: n/a, not touched.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01ADHkmQuUSo11ZX1avrvbez)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 41106b5 commit 34a30e8
9 files changed
Lines changed: 861 additions & 7 deletions
File tree
- .changeset
- packages
- mcp/src
- runtime/src
- domains
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 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 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
0 commit comments