feat(ai): serve MCP at /mcp with caller-scoped DataHub clients

Merged
NW
4d ago
+6331-138

Stack. This is one PR in a series that adds a built-in MCP server to DataHub Core. It is based on na--ai-service-scaffold (the previous PR in the stack); review only its own commit. Nothing in these first five PRs is wired into quickstart, the frontend or Helm yet, so they change no running DataHub. The later PRs that expose MCP are all behind a GMS feature flag, MCP_SERVER_ENABLED, off by default.

Stack so far:

  1. #21332
  2. #21333
  3. #21334
  4. #21335 ← this PR
  5. #21336
  6. #21337
  7. #21338
  8. #21339
  9. #21340
  10. #21341
  11. #21342
  12. #21343
  13. #21344
  14. #21345
  15. #21346
  16. #21347
  17. #21348
  18. #21349
  19. #21350
  20. #21351
  21. #21352
  22. #21353
  23. #21435
  24. #21436
  25. #21437
  26. #21438
  27. #21439
  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

datahub-ai-service now serves the datahub-core-mcp-tools tools (today: get_me) over
stateless streamable HTTP at /mcp.

  • Every request carries the caller's DataHub token. An ASGI middleware in
    front of the MCP app checks it with GraphQL me and caches successful
    verdicts (keyed by a hash of the token) for
    DATAHUB_MCP_TOKEN_CACHE_TTL_SECONDS. Missing or rejected tokens get 401;
    when GMS is unreachable or erroring the request gets 503, never access.
  • Each MCP message runs with a DataHub client built from its caller's token
    and closed afterwards; no client is cached or shared between callers.
  • SSE responses by default; DATAHUB_AI_MCP_RESPONSE_MODE=json returns plain
    JSON bodies for proxies that can't stream.
  • listChanged is not advertised, since a stateless host can't send it.
  • Tool-call observer seam for telemetry; an observer failure never fails
    the call.

Tests run a real HTTP server against a fake GMS: missing/garbage tokens,
GMS outage, SSE and JSON modes, legacy and modern protocol clients, token
caching, and two concurrent callers held in flight at once.

Testing

  • Tests run a real HTTP server against a fake GMS: missing and garbage tokens (401), GMS down (503), SSE and JSON response modes, legacy and modern protocol clients, token caching, and two concurrent callers keeping their own identity.
  • Checked against a running DataHub Core instance with Claude Code connected using a personal access token.

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). Docs come later in the stack, with the user-facing pieces.
  • For any breaking change/potential downtime/deprecation/big changes an entry has been made in Breaking Changes. Not applicable: nothing user-facing changes yet.

šŸ¤– Generated with Claude Code