From 3e53302b695ad8a0c2fa44308e14f0408e59c26c Mon Sep 17 00:00:00 2001 From: seyeong Date: Fri, 27 Nov 2026 03:10:51 +0900 Subject: [PATCH] Report all unresolved business terms in one scoped MCP check --- src/lang2sql/tools/resolve_business_terms.py | 19 ++++++++++++------ tests/test_mcp.py | 21 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/lang2sql/tools/resolve_business_terms.py b/src/lang2sql/tools/resolve_business_terms.py index 10b1e479..41a6d86b 100644 --- a/src/lang2sql/tools/resolve_business_terms.py +++ b/src/lang2sql/tools/resolve_business_terms.py @@ -50,11 +50,14 @@ def spec(self): }, ) - def definitions(self, terms, ctx): + def definitions(self, terms, ctx, *, missing_terms=None): if ctx.store is None: + if missing_terms is not None: + missing_terms.extend(terms) return None entries = _load_all(ctx.store, ctx.identity.kv_scope) resolved = [] + missing = [] for term in terms: # Exact canonical names only: fuzzy/alias guessing defeats clarification. entry = _resolve_entry( @@ -63,9 +66,12 @@ def definitions(self, terms, ctx): ctx.identity.user_id, ) if entry is None or entry.inferred or not entry.definition.strip(): - return None - resolved.append(asdict(entry)) - return resolved + missing.append(term) + else: + resolved.append(asdict(entry)) + if missing_terms is not None: + missing_terms.extend(missing) + return None if missing else resolved async def reject(self, ctx, reason): logger.warning( @@ -101,11 +107,12 @@ async def run(self, args, ctx): or any(not isinstance(t, str) or not t.strip() for t in terms) ): return await self.reject(ctx, "Supply all business terms as nonempty names") - definitions = self.definitions(terms, ctx) + missing = [] + definitions = self.definitions(terms, ctx, missing_terms=missing) if definitions is None: return await self.reject( ctx, - "At least one requested term has no confirmed definition in this scope", + "No confirmed definition in this scope for: " + json.dumps(missing, ensure_ascii=False), ) token = secrets.token_urlsafe() self.receipt = (token, tuple(terms), definitions, ctx.identity.session_key()) diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 7282439c..791edc69 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -276,3 +276,24 @@ async def test_literal_query_without_catalog_still_requires_single_use_receipt( "resolve_business_terms", {"terms": ["undefined_net_revenue"]} ) assert missing.isError and "DEFINITION_REQUIRED" in missing.content[0].text + + +@pytest.mark.asyncio +async def test_missing_definitions_are_reported_together_and_revoke_receipt(tmp_path): + async with connected(fixture_db(tmp_path), tmp_path / "metadata.db") as client: + await client.call_tool("term_custom", { + "term": "known", "definition": "SUM(orders.amount)", "layer": "team" + }) + check = await client.call_tool("resolve_business_terms", {"terms": ["known"]}) + token = json.loads(check.content[0].text)["definition_check_id"] + check = await client.call_tool("resolve_business_terms", { + "terms": ["missing_a", "known", "missing_b"] + }) + assert check.isError + message = check.content[0].text + assert '["missing_a", "missing_b"]' in message + assert '"known"' not in message + rejected = await client.call_tool("run_sql", { + "sql": "SELECT COUNT(*) FROM orders", "definition_check_id": token + }) + assert rejected.isError