feat(mcp): port the grep_documents tool

Closed
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-search-documents (the previous PR in the stack); review only its own commit. It changes the shared datahub-core-mcp-tools package, datahub-ai-service and the image's locked dependencies (docker/datahub-ai-service/requirements.lock). 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 ← this PR
28. #21440
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_documents lets an agent read inside documents it found with
search_documents: it runs an RE2 regex over each document's body and
returns excerpts with context_chars of surrounding text, absolute positions
and per-document match counts. pattern=".*" reads a body in consecutive
windows; start_offset and next_offset page through a long document.

From DataHub Cloud: the tool and its description, the 60K-character output
budget shared across documents (GREP_OUTPUT_CHAR_BUDGET; excerpts cut at
the budget are marked truncated, every document with more to show gets a
next_offset, and later documents are listed with no excerpts), excerpts
that never repeat text, a ToolError (never a result) for a pattern that
cannot compile, clamping of oversized {m,n} repetitions with a
pattern_note, skipped_urns for names, malformed URNs, missing, deleted or
unviewable documents and configuration documents (the rest are still
searched), stringified URN lists, the parameter aliases (documentUrns,
document_urns, ...; offset/start for start_offset), and trust labels.
The pattern is compiled before any document is read.

Added here: ReadBackend.document_contents (document_content.gql; a deleted
or unviewable document's info-null stub reads as missing), example and
resolve_with on the URN helpers so the errors show a document URN and point
at search_documents, and the tool is registered with the progress
heartbeat (register_tools' add() takes slow=). The google-re2 dependency is
added to the package and the service lock file. The legacy OSS server's
grep_documents is an older copy with no OSS-only fix to carry over: its
returned error dict for a bad pattern is the behavior Cloud replaced with
ToolError.

Version gates: @min_version(cloud=0.3.16, oss=1.4.0) as in DataHub Cloud;
document_content.gql validates on GMS 1.7.0, 1.8.0 and master unmarked.

Testing

unit tests against a test ReadBackend (excerpts and positions,
limits and next_offset, start_offset, skipped URNs, bad and clamped
patterns, windowed reads, the output budget across one and several
documents, configuration documents and labels) and through FastMCP with
the GraphQL backend. Live on a local OSS DataHub (showcase-ecommerce),
with google-re2 installed into the running service container: a regex
over three runbooks/references found the staleness runbook's matches, an
AOV lookup in the metrics reference with a missing URN and a name reported
both in skipped_urns, ".*" windows returned next_offset, and an unclosed
group failed 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