feat(mcp): port set_domains and remove_domains

Merged
NW
3d ago
+5307-19

Stack. This is one PR in a series that adds a built-in MCP server to DataHub Core. It is based on na--mcp-create-glossary-term-version (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
39. #21505
40. #21506 ← this PR
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

set_domains(domain_urn, entity_urns) puts assets in a domain, replacing
the domain each one had (an asset belongs to at most one); remove_domains
(entity_urns) takes them out of their domain. Both are one batchSetDomain
write over all the assets (remove sends a null domain), answer with the
number of assets covered and the per-call scope note, and are in the
Write metadata group, destructive (a set replaces the previous domain)
and idempotent. TOOLS_IS_MUTATION_ENABLED=false hides both.

From DataHub Cloud's tools/domains.py: the tools, their descriptions
and response shapes (domain_urn, entities_updated, the scope note), the
destructive marker and @min_version(oss="1.4.0", cloud="0.3.16"). From
the legacy OSS server: entity_urns as a StrList, so a bare URN or a
JSON-stringified list is accepted while the schema stays an array
(d05ef3d). propose_domain is not ported.

Adapted to this package: ToolErrors instead of ValueErrors and
RuntimeErrors; the write goes through execute_write (one attempt; no
reply means "may or may not have been applied", with what to check);
batchSetDomain is in domains.gql, valid on GMS 1.7.0, 1.8.0 and master.
The domain must be a domain URN that exists, checked through ReadBackend
(DataHub Cloud ran its own entity query); the asset URNs get the same checks
add_tags gives them (label_writes.require_entity_urns, now public), so a
name or malformed URN fails the whole call, with a search hint, before
anything is written. DataHub Cloud's examples used dataFlow and dashboard URNs
that do not parse; they are replaced.

Testing

unit tests for a set over two assets (input, scope note, the one
existence check), remove sending a null domain with no domain check,
invalid arguments writing nothing (a name, a tag URN, a missing domain,
no assets, a bad asset URN), a false result, a refused write, an unknown
outcome, the domain check going through a bound ReadBackend, and through
an MCP client a stringified list and the annotations and group. Live on
a local OSS DataHub with auth on, through the frontend's /mcp, with a
run-unique domain made for the test: set on a dbt and a Postgres orders
dataset (both read back in the domain), again with a stringified list; a
missing domain, a tag URN and a bare name are tool errors; remove left
the Postgres dataset without a domain and a second remove succeeded; a
temporary non-admin user's PAT got "DataHub rejected batchSetDomain:
Unauthorized ..." from both tools. Both datasets' domains were restored
(the dbt one to its original domain) and the test domain was deleted.

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