feat(mcp): port the save_document tool
Stack. This is one PR in a series that adds a built-in MCP server to DataHub Core. It is based on na--mcp-structured-properties (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
43. #21509
44. #21510
45. #21511 ← this PR
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
save_document(document_type, title, content, urn?, topics?,
related_documents?, related_assets?, parent_urn?) saves a standalone
document (an insight, decision, FAQ, ...) to DataHub's knowledge base.
Without urn it creates a new document, owned by the caller, under
parent_urn or else under a shared folder the tool keeps ("Shared",
optionally with a subfolder per author). With the urn of a stored
document it replaces that document's title, body and type in place and
keeps everything else: parent, owners, status, visibility, created
stamp, and the topics and related links not passed. It is in the Write
metadata group, destructive and not idempotent (each create is a new
document). TOOLS_IS_MUTATION_ENABLED=false or SAVE_DOCUMENT_TOOL_ENABLED
=false hides it.
From DataHub Cloud's tools/save_document.py and
document_edit_policy.py: the tool and its description, parent_urn for
new documents (a parent must exist; setting it on an update is refused,
moves are update_document's), the in-place update that fixes the
re-parenting of the legacy OSS server's version (it rebuilt the
document on every update, which moved it into the shared folder and
reset its status, created stamp and visibility), restoring a
soft-deleted shared folder (also legacy OSS c978c57), DocumentSettings
stamped with the caller on every save, certification left out of the
upsert, the retrieval-policy configuration guard, the host edit policy
(entry point datahub.mcp.document_edit_policy) and its pre-approval
validator, and the gates with DataHub Cloud's defaults:
SAVE_DOCUMENT_TOOL_ENABLED on, SAVE_DOCUMENT_RESTRICT_UPDATES off (when
on, only documents inside the shared folder may be updated),
SAVE_DOCUMENT_ORGANIZE_BY_USER off, SAVE_DOCUMENT_PARENT_TITLE "Shared".
@min_version(oss="1.4.0", cloud="0.3.16"). From the legacy OSS server:
topics, related_documents and related_assets as OptionalStrLists (a
bare or stringified list is accepted; "" and "null" count as omitted)
(d05ef3d, 01054c4). DataHub Cloud's MemorySynthesizer helper
(create_user_scoped_document) is not a tool and is not ported.
version_requirements gains @pre_approval_validator, which sets the
attribute approval hosts read.
Fixed while porting, so a failed or partial save never creates a
duplicate or re-parents a document:
- A failed read of the document to update (existence or fetch) stops
the call; DataHub Cloud could fall through to creating it again in the
shared folder. - A shared folder that cannot be created or restored stops the save,
instead of saving the document under a missing (hidden) folder. - Writes are single attempts: a refusal gives GMS's reason (the
validator's message for a rejected aspect), and no reply says the
save may or may not have been applied, naming the URN; for a create
it says to retry with urn=, which updates instead of making
a second document. - related_documents must be document URNs and related_assets URNs;
blank topics are dropped; SDK usage errors become tool errors.
Adapted to this package: reads go through ReadBackend (existence,
folder status and the shared-folder ancestry via aspects); the caller is
read with getDocumentAuthor in document_writes.gql (valid on GMS 1.7.0,
1.8.0 and master); loguru; ToolErrors throughout.
Testing
unit tests against a fake catalog with real SDK documents: a new
document in the shared folder (owner, tags from topics, related asset,
settings actor), new URNs per create, a missing shared folder created
first, a soft-deleted one restored, a folder that cannot be created
stopping the save, a new document under a given parent, invalid
arguments writing nothing, a create without a reply naming the URN to
retry with; an update in place keeping parent, status, visibility,
related assets and owners with no folder touched, an update replacing
the lists it is given, a refused update written once with its original
parent and never turned into a create, an update without a reply, an
update whose document cannot be read (exists or get) writing nothing, an
unknown URN created at that URN, SAVE_DOCUMENT_RESTRICT_UPDATES, a
locked field refused by the pre-approval validator and the tool, the
registration (annotations, group, stringified lists) and the gate on and
off. Live on a local OSS DataHub with auth on, through the frontend's
/mcp, with run-unique titles: a new insight landed in a newly created
"Shared" folder with its tag, related asset and owner; a document
created under another test document as parent, then updated through
save_document in place (same URN, title, body and type replaced, parent
and owners kept); parent_urn on an update, a missing parent and a
dataset URN as a related document are errors; a given unknown URN was
created there; a temporary non-admin user's PAT got "DataHub rejected
saving ...: ... unauthorized to modify entity" for a create and an
update, and the update left the document unchanged. All test documents
and the shared folder (absent before the test) were hard-deleted
(deleteDocument only soft-deletes).
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