feat(mcp): port the grep_entities tool

Merged
NW
4d ago
+0-0

Stack. This is one PR in a series that adds a built-in MCP server to DataHub Core. It is based on na--mcp-grep-documents (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. #21435
24. #21436
25. #21437
26. #21438
27. #21439
28. #21440 ← this PR
29. #21441
30. #21442
31. #21443
32. #21444
33. #21445
34. #21447
35. #21501
36. #21502
37. #21503
38. #21504
39. #21505
40. #21506
41. #21507
42. #21508
43. #21509
44. #21510
45. #21511
46. #21512

What this does

grep_entities lets an agent search every string in one entity's metadata
(all aspects: schema fields and their descriptions, tags, terms,
ownership, properties, ...) with an RE2 regex, when it is looking for
something without knowing which aspect holds it. Each match comes with
the value's path (e.g. schemaMetadata.fields[fieldPath=zip_code]
.description), a snippet of the deterministic "path: value" stream around
it, and stream offsets; ".*" with next_offset reads the whole entity page
by page. A pattern that matches no value is retried against whole
"path: value" lines (searched: "lines"), since agents copy that format.

From DataHub Cloud: the tool and its description, the stream and path
rendering, merged snippets for adjacent hits, the 60K JSON-character
budget per response (GREP_OUTPUT_CHAR_BUDGET, counted on the serialized
response) with max_matches (default 20) and continuation that keeps a
match spanning the cursor, ToolError for a pattern that cannot compile
(oversized {m,n} repetitions are clamped with a pattern_note), for a
missing entity, a failed read and a page nothing fits on, the urn and
offset aliases, retrieval-policy configuration kept out, and reader-policy
labels. The GREP_ENTITIES_ENABLED gate (default on) is kept.

Adapted to this package: reads go through the existing
ReadBackend.exists/aspects, the RE2 clamp and error text are shared with
grep_documents instead of copied, a missing entity raises ToolError rather
than the SDK's ItemNotFoundError, and the tool is registered with the
progress heartbeat. The legacy OSS server has no grep_entities.

Version gates: none needed; the tool reads aspects through the SDK, not
GraphQL, and DataHub Cloud sets no @min_version.

Testing

DataHub Cloud's unit tests against a test ReadBackend (value and line
search, anchors, paths, budgets, paging the full stream, continuation,
errors), plus registration through FastMCP and the env gate. Live on a
local OSS DataHub (showcase-ecommerce): status/total columns on the
Snowflake orders table, glossary-term URNs on the Postgres orders table,
owners on the dbt model, a paged ".*" read (next_offset), an unclosed
group, an unknown dataset and a bare name, each failing as a tool error.

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