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
20 changes: 20 additions & 0 deletions .changeset/34763-list-datasets-candidates-fill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@objectstack/service-analytics': patch
'@objectstack/mcp': patch
---

feat(analytics): `list_datasets` offers `candidates` for a term that matched nothing, and the selection text says to ask about them

Clause-②: no
Metadata-protocol: none

A business word that equalled no member's name, label or synonym used to get nothing back, so an AI went straight to the object query tools even when a listed measure was obviously what the user meant (`invoice` beside the measure labelled `Invoices`).

`list_datasets` now fills the `candidates` member the spec already declares: for each term with no match in any listed dataset, the members near it, as `{ term, dataset, member }` pointers to members listed in the same answer.

- **Near** means that, after the same normalization the matcher uses, one of the member's names, labels (any locale) or synonyms contains the term, or the term contains it, with both at least two characters (code points) long. `金额` is near `合同金额` and the reverse. No edit distance, stems or embeddings.
- **Only what the caller may read.** Candidates come from the datasets and members the answer lists. A member over a field the caller cannot read, a member over an `internal` field, and a dataset the caller may not query are never offered.
- **Never beside a match.** A term with a match anywhere in the answer has no candidates. What counts as a match is unchanged.
- **Listing order, capped.** Terms in the order sent (a repeated term is offered once), then datasets in the order listed, then each dataset's measures before its dimensions. At most `LIST_DATASETS_MAX_CANDIDATES_PER_TERM` (5) per term, with no marker for the cut. When nothing is offered the key is absent, never an empty list.

The `list_datasets` tool description and the `initialize` selection rule the HTTP MCP server sends now say: each word that matches exactly one measure is computed, and two such words are two metrics computed both; one word matching several measures is a question for the user; a word with `candidates` is the same question, listing each candidate's label, dataset label and description, and a candidate is never picked or treated as a match; no match and no candidates falls back to the object tools, saying the answer is not from a governed metric.
12 changes: 8 additions & 4 deletions content/docs/ai/connect-mcp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,14 @@ between a governed metric and an object query, because a client reads a tool's
description only after it has chosen that tool. Where both dataset tools are
registered for the connection, the instructions say: look for a governed metric
first by calling `list_datasets` with the question's business words; a word
matches only when it equals a name, label or synonym; one measure match is
answered with `query_dataset`; several matches are a question for the user; no
match falls back to `query_records` / `aggregate_records`, saying the answer is
not from a governed metric. A token without `data:read` gets neither tool and no
matches only when it equals a name, label or synonym; each word with exactly one
measure match is answered with `query_dataset`, and two such words are two
metrics computed both; several matches are a question for the user; a word with
no match but with `candidates` in the answer (members offered as possible
readings) is the same question, listing each candidate's label, dataset label
and description, and is never treated as a match; no match and no candidates
falls back to `query_records` / `aggregate_records`, saying the answer is not
from a governed metric. A token without `data:read` gets neither tool and no
such sentence.

The instructions also name the published skills package and the version to
Expand Down
23 changes: 18 additions & 5 deletions docs/qa/platform-checklist/areas/ai.json
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@
"title": "MCP serves the governed datasets: list_datasets lists each dataset this caller may query with its members and reports which terms match which member, and query_dataset answers rows with provenance, a row count and a truncation fact, never SQL",
"since": "v18.4",
"status": "active",
"revision": 2,
"revision": 3,
"priority": "P1",
"surface": "api",
"personas": [
Expand All @@ -1157,13 +1157,14 @@
"call query_dataset { datasetName: 'showcase_invoice_metrics', selection: { measures: ['invoice_count'], dimensions: ['status'] } }",
"the same with selection.limit: 1",
"query_dataset { datasetName: 'qa_no_such_dataset', selection: { measures: ['invoice_count'] } }",
"query_dataset { dataset: 'showcase_invoice_metrics', selection: { measures: ['invoice_count'] } }; then { datasetName: 'showcase_invoice_metrics', measures: ['invoice_count'] }"
"query_dataset { dataset: 'showcase_invoice_metrics', selection: { measures: ['invoice_count'] } }; then { datasetName: 'showcase_invoice_metrics', measures: ['invoice_count'] }",
"a near miss: list_datasets { terms: ['invoice'] }; then list_datasets { terms: ['invoices'] }"
],
"acceptance": [
{
"clause": "a keyed session lists both tools, each described with the selection rule the model reads",
"oracle": "network",
"verify": "tools/list names list_datasets and query_dataset; the list_datasets description says one match on a measure wins and several matches ask the user (packages/services/service-analytics/src/dataset-tools.ts#getDatasetToolDefinitions; packages/mcp/src/mcp-http-tools.ts#registerDatasetTools)",
"verify": "tools/list names list_datasets and query_dataset; the list_datasets description says one match on a measure wins, several matches ask the user, and a word with candidates asks the same way and is never a match (packages/services/service-analytics/src/dataset-tools.ts#getDatasetToolDefinitions; packages/mcp/src/mcp-http-tools.ts#registerDatasetTools)",
"evidence": "the two tool entries"
},
{
Expand All @@ -1175,7 +1176,7 @@
{
"clause": "terms are matched after normalization against name, label and synonym, and each match names its member",
"oracle": "network",
"verify": "the invoice entry's matches hold { term: 'invoices', member: 'measures.invoice_count', matchedOn: 'label' } and { term: 'status', member: 'dimensions.status', matchedOn: 'name' }; qa_no_word matches nothing anywhere (packages/spec/src/ui/dataset.zod.ts#normalizeDatasetTerm; packages/spec/src/ai/dataset-tools.zod.ts#DatasetTermMatchSchema)",
"verify": "the invoice entry's matches hold { term: 'invoices', member: 'measures.invoice_count', matchedOn: 'label' } and { term: 'status', member: 'dimensions.status', matchedOn: 'name' }; qa_no_word matches nothing anywhere, and the answer carries no `candidates` key (packages/spec/src/ui/dataset.zod.ts#normalizeDatasetTerm; packages/spec/src/ai/dataset-tools.zod.ts#DatasetTermMatchSchema)",
"evidence": "the matches arrays"
},
{
Expand Down Expand Up @@ -1207,6 +1208,12 @@
"oracle": "test",
"verify": "run each file in automated.ref with its package's vitest and `--reporter=tree`, which prints one line per file (RUNNER.md's environment facts); each reports ≥1 test and 0 failed",
"evidence": "the vitest summary line per file, with the subject sha"
},
{
"clause": "a term that matches nothing is answered with candidates: members near it that this caller may read, offered for a question and never a match; a term that matches has none",
"oracle": "network",
"verify": "terms ['invoice']: no dataset's matches holds the term, and `candidates` holds { term: 'invoice', dataset: 'showcase_invoice_metrics', member: 'measures.invoice_count' }, each entry a pointer to a member the same answer lists, at most 5 per term. terms ['invoices']: the label match of step 2 and no `candidates` key (packages/spec/src/ai/dataset-tools.zod.ts#DatasetTermCandidateSchema; packages/services/service-analytics/src/dataset-term-matcher.ts#findDatasetTermCandidates)",
"evidence": "both answers' matches and candidates"
}
],
"negative": [
Expand All @@ -1219,7 +1226,7 @@
],
"automated": {
"kind": "unit",
"ref": "packages/services/service-analytics/src/__tests__/dataset-tools.test.ts, packages/mcp/src/mcp-dataset-tools.test.ts, packages/runtime/src/mcp-dataset-tools-bridge.test.ts; no live spec opens a keyed MCP session against the datasets"
"ref": "packages/services/service-analytics/src/__tests__/dataset-tools.test.ts, packages/services/service-analytics/src/__tests__/dataset-term-matcher.test.ts, packages/mcp/src/mcp-dataset-tools.test.ts, packages/runtime/src/mcp-dataset-tools-bridge.test.ts; no live spec opens a keyed MCP session against the datasets"
},
"source": [
"docs/adr/0149-ai-answers-business-questions-through-the-dataset-semantic-layer.md D2 and D3",
Expand All @@ -1242,6 +1249,12 @@
"date": "2026-11-15",
"change": "acceptance[7].verify names `--reporter=tree`, the reporter that prints the one line per pin file this is scored on: in a runner's non-TTY shell vitest's default reporter prints the run's totals only (RUNNER.md's environment facts; QA run #33784's note 10, comment 6893243542). No clause or threshold changed. Card #33969",
"ref": "#34068"
},
{
"revision": 3,
"date": "2026-11-17",
"change": "a near-miss step and its clause: list_datasets answers a term that matched nothing with `candidates` (ADR-0149 D3 amendment note), and qa_no_word carries none; the description clause names the candidates row; automated.ref names the candidate pins. Card #34763",
"ref": "#34763"
}
]
},
Expand Down
10 changes: 6 additions & 4 deletions packages/mcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ grant, then two parts:

- **The selection rule**, only where this request registers both
`list_datasets` and `query_dataset`: look for a governed metric first with
`list_datasets` and the question's business words; one measure match answers
through `query_dataset`; several matches are a question for the user; no
match falls back to the object tools and says the answer is not from a
governed metric. A grant without `data:read` gets no such sentence.
`list_datasets` and the question's business words; each word with exactly
one measure match answers through `query_dataset`, and two such words are two
metrics computed both; several matches are a question for the user; a word
with `candidates` in the answer is the same question, never a match; no match
and no candidates falls back to the object tools and says the answer is not
from a governed metric. A grant without `data:read` gets no such sentence.
- **The skills install line**: `Published skills: @objectstack/skills <version>`
and its `pnpm add -D` command, at this package's own version (the two are
released together), never the `version` option.
Expand Down
76 changes: 75 additions & 1 deletion packages/mcp/src/mcp-dataset-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
* bridge's definitions carry, never a copy in this package; the arguments reach
* the bridge as sent, with the tool's page cap beside them; an undeclared key is
* refused before the bridge is asked, in the spec's words; a refusal the bridge
* throws comes back as the coded tool error.
* throws comes back as the coded tool error; `list_datasets.candidates` comes
* back exactly as the executor answered it (#34763).
*/

import { describe, it, expect, beforeEach, vi } from 'vitest';
import {
LIST_DATASETS_TOOL_NAME,
ListDatasetsOutputSchema,
MCP_OAUTH_SCOPE_DATA_READ,
MCP_OAUTH_SCOPE_DATA_WRITE,
QUERY_DATASET_TOOL_NAME,
Expand Down Expand Up @@ -369,3 +371,75 @@ describe('[ADR-0149 D2] dataset tools on MCP — the spec refusal is kept whole'
expect(bridge.calls.filter((c) => c[0] === 'listDatasets')).toEqual([]);
});
});

/**
* [#34763, ADR-0149 D3 amendment note] `list_datasets.candidates` reaches an
* MCP client exactly as the executor answered it. The members offered for a
* term that matched nothing are chosen by the service-analytics executor
* (`findDatasetTermCandidates`, pinned there); the MCP tool layer holds no copy
* of that rule and must neither drop the key, reorder its entries nor add one.
*/
describe('[#34763] list_datasets candidates pass through MCP unchanged', () => {
const WITH_CANDIDATES: ListDatasetsOutput = {
datasets: [
{
name: 'showcase_invoice_metrics',
label: 'Invoice Metrics',
object: 'showcase_invoice',
dimensions: [{ name: 'status', label: 'Status', type: 'string' }],
measures: [
{ name: 'invoice_count', label: 'Invoices', aggregate: 'count' },
{ name: 'paid_count', label: 'Paid Invoices', aggregate: 'count' },
],
matches: [],
},
],
candidates: [
{ term: 'invoice', dataset: 'showcase_invoice_metrics', member: 'measures.invoice_count' },
{ term: 'invoice', dataset: 'showcase_invoice_metrics', member: 'measures.paid_count' },
],
};
const WITHOUT_CANDIDATES: ListDatasetsOutput = {
datasets: [{
...WITH_CANDIDATES.datasets[0],
matches: [{ term: 'invoices', member: 'measures.invoice_count', matchedOn: 'label' }],
}],
};

function bridgeAnswering(answer: ListDatasetsOutput) {
const bridge = makeBridge();
bridge.listDatasets = async (input) => {
bridge.calls.push(['listDatasets', input]);
return answer;
};
return bridge;
}

let runtime: MCPServerRuntime;
beforeEach(() => {
runtime = new MCPServerRuntime({ name: 't', version: '1.0.0' });
});

it('the fixtures are answers the spec accepts', () => {
expect(ListDatasetsOutputSchema.safeParse(WITH_CANDIDATES).success).toBe(true);
expect(ListDatasetsOutputSchema.safeParse(WITHOUT_CANDIDATES).success).toBe(true);
});

it('passes the executor\'s candidates through unchanged: same entries, same order, nothing added', async () => {
const bridge = bridgeAnswering(WITH_CANDIDATES);
const json = await callTool(runtime, bridge, LIST_DATASETS_TOOL_NAME, { terms: ['invoice'] });
expect(json.result?.isError).toBeFalsy();
const answer = JSON.parse(json.result!.content[0].text) as ListDatasetsOutput;
expect(answer).toEqual(WITH_CANDIDATES);
expect(answer.candidates).toEqual(WITH_CANDIDATES.candidates);
expect(bridge.calls.filter((c) => c[0] === 'listDatasets')).toEqual([['listDatasets', { terms: ['invoice'] }]]);
});

it('an answer without candidates comes back without the key', async () => {
const json = await callTool(runtime, bridgeAnswering(WITHOUT_CANDIDATES), LIST_DATASETS_TOOL_NAME, { terms: ['invoices'] });
expect(json.result?.isError).toBeFalsy();
const answer = JSON.parse(json.result!.content[0].text) as ListDatasetsOutput;
expect(answer).toEqual(WITHOUT_CANDIDATES);
expect('candidates' in answer).toBe(false);
});
});
21 changes: 15 additions & 6 deletions packages/mcp/src/mcp-server-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1805,17 +1805,26 @@ const DEFAULT_HTTP_INSTRUCTIONS =
* tools' own descriptions and in the served skill's data loop
* (`ASK_SKILL_DATA_LOOP`, generated from `skills/objectstack-ask/SKILL.md` and
* rendered by `skill-md.ts`). All three hold an exact name, label or synonym as
* the only match, one measure match as the answer, and several matches on one
* word as a question for the user; the skill also asks before it falls back
* when a word with no match is a loose form of several listed measures.
* the only match, one measure match as the answer (two words with one measure
* match each are two metrics, both computed), and several matches on one word
* as a question for the user. [#34763] A word with no match may come back with
* `list_datasets` `candidates` (the ADR-0149 D3 amendment note): the rule makes
* that the same question, listing each candidate's label, dataset label and
* description, never a pick and never a match, even for a single candidate;
* only a word with no match and no candidate falls back to the object tools,
* with the answer labelled. The skill's own loose-form rows predate
* `candidates` and are amended in its lane.
* Sent only where both dataset tools register ({@link datasetToolsServed}).
*/
const DATASET_SELECTION_RULE =
'For a question about a business number, look for a governed metric first: call list_datasets with the '
+ 'question\'s business words. A word matches only when it equals a measure\'s or dimension\'s name, label or '
+ 'synonym, never a similar word. One measure match: answer with query_dataset. Several matches: ask the user '
+ 'which one they mean; do not pick. No match: fall back to query_records / aggregate_records and say the answer '
+ 'is not from a governed metric.';
+ 'synonym, never a similar word. Each word that matches exactly one measure: answer with query_dataset; two '
+ 'such words are two metrics, so compute both. One word matching several measures: ask the user which one '
+ 'they mean; do not pick. A word with no match but with candidates in the answer: ask the same way, listing '
+ 'each candidate\'s label, dataset label and description; do not pick one or treat it as a match. No match '
+ 'and no candidates: fall back to query_records / aggregate_records and say the answer is not from a governed '
+ 'metric.';

/**
* [#34686] The published skills package an open server points a client to
Expand Down
Loading
Loading