feat(mcp): port create_glossary_term and add_related_terms

Closed
NW
3d ago
+6271-44

Stack. This is one PR in a series that adds a built-in MCP server to DataHub Core. It is based on na--mcp-glossary-terms (the previous PR in the stack); review only its own commit. It changes only the shared datahub-core-mcp-tools package and datahub-ai-service. It is one tool port, so it stays one PR even where it runs past the stack's usual 1,000-line size. Everything user-facing is behind a GMS feature flag, MCP_SERVER_ENABLED, off by default (added in #21349).

Stack so far:

1–22. Merged to master as #21332.
23–34. #21435, the read tools (PRs 24–34 merged into it).
35. #21501
36. #21502
37. #21503
38. #21504 ← this PR
39. #21505
40. #21506
41. #21507
42. #21508
43. #21509
44. #21510
45. #21511
46. #21512
47–55. #21587, MCP settings: model, API and UI.
56–60. #21588, telemetry, metrics, usage events and the stdio server.
61–64. #21589, docs, release notes and smoke tests.
65–68. #21590, settings enforcement and the tool catalog.
69–70. #21591, the home page module.

What this does

create_glossary_term(name, description, parent_node_urn?, id?) adds a term
to the business glossary, optionally under a glossary node, and returns
its term_urn for add_terms. add_related_terms relates a term to other
terms: "isA" (inherits from, e.g. CreditCardNumber isA PII) or "hasA"
(contains). Both are in the Write metadata group and not destructive;
add_related_terms is idempotent (GMS skips a relationship that exists).
add_terms' description and its "term not found" message now point at
create_glossary_term. TOOLS_IS_MUTATION_ENABLED=false hides both.

From DataHub Cloud's tools/glossary_versioning.py, where both live: the
tools, their descriptions and response shapes, and
@min_version(oss="1.4.0", cloud="0.3.16"). They need no versioning and
DataHub Cloud registers them without ENTITY_VERSIONING_ENABLED, so here they
get their own module, glossary.py; the module docstring of
glossary_versioning.py now says only create_glossary_term_version goes
there. The legacy OSS server has neither tool.

Relationship kinds: DataHub Core's TermRelationshipType has only isA and
hasA, so on Core (register_tools with is_oss=True) the parameter is an
enum of those two and asking for one of the five kinds only DataHub
Cloud's schema has (synonymOf, antonymOf, translatesTo, hasValue,
isRelatedTo) gets a validation error that names the two Core supports.
DataHub Cloud builds this same package, so with is_oss=False the tool is
registered as DataHub Cloud has it: all seven kinds in the enum, DataHub Cloud's
description of each, every kind passed to addRelatedTerms as is. Both
variants share the checks and the write. "Inherits" / "Contains" (the
UI's labels), other spellings of isA / hasA and any case of a kind's name
are accepted.

Adapted to this package: ToolErrors instead of RuntimeErrors, mutations
in glossary_terms.gql, related_term_urns as a StrList. Arguments are
checked before writing: term and node URNs by type (with a glossary-URN
example rather than a dataset one), an id containing "(", ")" or ",", a
blank name, a term related to itself, duplicates dropped. A parent node
must exist (ReadBackend.exists): GMS records any parent URN it is given,
so a missing node would leave the term outside the visible hierarchy.
create_glossary_term always sends an id, a UUID when none is given (GMS
would generate the same kind), so a create that gets no reply still names
the term to check instead of inviting a retry that could make a
duplicate; GMS's own refusals (same name at the same level, privileges,
missing related terms) reach the caller as they are.

Testing

unit tests for a term under a node, the generated id named in an
unknown outcome, a missing node, a non-node parent and a bare name,
unusable ids and names, a duplicate name, isA with duplicates dropped,
invalid relationships writing nothing, a false result, and through an
MCP client the Core enum schema, the "Inherits" alias, a Cloud-only kind
refused, the annotations, and the Cloud variant's seven-kind enum and
description with synonymOf passed through. glossary_terms.gql validates
on GMS 1.7.0, 1.8.0 and master. Live on a local OSS DataHub with auth
on, through the frontend's /mcp, under a run-unique glossary node made
for the test: a term created with an id and one without (UUID URN), both
under the node; the same name again gets GMS's "already exists at this
level"; a missing parent node is refused; add_related_terms with
"Inherits" stores isRelatedTerms (checked in the aspect) and a second
call succeeds without change; synonymOf is refused as not on Core; a
missing related term gets GMS's "does not exist"; a temporary non-admin
user's PAT gets "Unauthorized" from both tools. The test node and terms
were deleted afterwards.

Checklist

  • The PR conforms to DataHub's Contributing Guideline (particularly PR Title Format)
  • Tests for the changes have been added/updated (if applicable)
  • Docs related to the changes have been added/updated (if applicable). The MCP docs come later in the stack.
  • For any breaking change/potential downtime/deprecation/big changes an entry has been made in Breaking Changes. The release notes come later in the stack.

🤖 Generated with Claude Code