feat(mcp): port add_owners and remove_owners

Merged
NW
3d ago
+4551-17

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-domain (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
41. #21507
42. #21508 ← this PR
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

add_owners(owner_urns, entity_urns, ownership_type) makes users and
groups owners of assets with one ownership type; remove_owners
(owner_urns, entity_urns, ownership_type?) takes them off, of one type
or of any. Each is one batch write over all the assets, answering with
the number covered and the per-call scope note. The type is a built-in
(TECHNICAL_OWNER, BUSINESS_OWNER, DATA_STEWARD; also by value, by URN,
or spelled as in the UI, e.g. "technical owner") or a custom type by
its name, case-insensitive, or its URN. Both are in the Write metadata
group and idempotent; remove_owners is destructive.
TOOLS_IS_MUTATION_ENABLED=false hides both.

From DataHub Cloud's tools/owners.py: the tools, their descriptions and
response shapes, built-in and custom type resolution (exact-name match on
listOwnershipTypes, an ambiguous name refused), owner entity types from
the URN, the destructive marker on remove_owners and
@min_version(oss="1.4.0", cloud="0.3.16"). From the legacy OSS server:
ownership_type published as a single-type {"type": "string"} schema
rather than anyOf [enum, string], which constrains nothing and which
Gemini and several OpenAI-compatible endpoints reject, failing the whole
tool list (41ff2dd); owner_urns and entity_urns as StrLists (d05ef3d).
propose_owners is not ported.

Adapted to this package: ToolErrors instead of ValueErrors and
RuntimeErrors; writes through execute_write (one attempt; no reply means
"may or may not have been applied"); mutations and the type lookup in
owners.gql, valid on GMS 1.7.0, 1.8.0 and master. Reads go through
ReadBackend: owner existence (DataHub Cloud ran its own entities query) and a
new ownership_types_named lookup (the test fakes implement it). Owner
URNs must be user or group URNs (a name or another entity's URN is
refused with a search hint) and, for add_owners, exist; remove_owners
does not require that, since removing a deleted user's ownership is how
it gets cleaned up (DataHub Cloud checked both). Duplicate owners are
dropped. Asset URNs get the checks add_tags gives them. A custom type
not found says that search can miss a type made seconds ago and to pass
its URN. The built-in examples now pass strings, as clients do.

A new test in test_mcp_server checks that no registered tool publishes a
parameter whose anyOf/oneOf branches differ in type (nullable is fine),
with every gate open, so a union added later fails there.

Testing

unit tests for a user and a group added to two assets (input
shape, the existence checks), built-in types in four spellings with no
lookup, a custom type by exact name among similar ones, an unknown and
an ambiguous custom type writing nothing, removing any type without an
existence check, removing one type, invalid arguments writing nothing,
a false result, a refused write, an unknown outcome, and through an MCP
client the published ownership_type schemas, stringified lists and the
annotations. Live on a local OSS DataHub with auth on, through the
frontend's /mcp: a user and a group added as technical owners of the
Postgres orders dataset, the same add again ("technical owner", bare and
stringified lists) changed nothing; a data steward added to the dbt
orders dataset and then removed by type left its original owners as they
were; a custom ownership type made for the test (run-unique name) was
used and removed by name in other cases once search had indexed it (two
seconds); a missing user, a bare name and an unknown type are tool
errors; removing all, then again, succeeded; a temporary non-admin
user's PAT got "DataHub rejected batchAddOwners / batchRemoveOwners:
Unauthorized to update owners ...". Both datasets' owners were checked
equal to the originals and the custom type 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