Skip to content

refactor(api): route MCP PromQL connection lookups through the MCP helper - #3777

Open
brandon-pereira wants to merge 2 commits into
mainfrom
claude/mcp-promql-connection-lookup
Open

brandon-pereira wants to merge 2 commits into
mainfrom
claude/mcp-promql-connection-lookup

Conversation

@brandon-pereira

Copy link
Copy Markdown
Member

Summary

Follow-up to #3776.

#3776 routed MCP connection lookups through mcp/utils/connections.ts. Two PromQL paths still skipped it, because they get their connection inside the shared controllers/promql.ts:

  • PromQL tiles (runPromqlTile, via resolvePromqlTarget)
  • PromQL describe_source (describePromqlSource, via getPromqlLabelCatalog)

This PR gives both controller functions an optional getConnection lookup. It defaults to the existing team-scoped getConnectionById, so the REST /prometheus router is unchanged. The MCP paths now take the full McpContext and pass getMcpConnection, so every MCP connection lookup goes through the helper. The comment in connections.ts drops the PromQL exception that #3776 added.

No behavior change: the same connection is found, the password is selected the same way, and the "Connection not found" errors are unchanged.

No changeset: internal refactor with no user-facing change.

How to reproduce

Starting point: N/A, internal refactor with no user-visible change.

Testing:

  1. cd packages/api && yarn ci:unit runs the new controllers/__tests__/promqlConnectionLookup.test.ts (default lookup, custom lookup, and not-found handling) and a new describeSourceTimeout.test.ts case that checks PromQL describe_source looks up connections through the MCP helper.
  2. make dev-int FILE="promql prometheus" covers the MCP PromQL tools and tiles plus the REST /prometheus router: 5 suites, 189 tests pass. The full MCP integration suite (make dev-int FILE=src/mcp/__tests__) also passes: 23 suites, 636 tests.

How to test on Vercel preview

N/A, non-UI change.

References

…lper

resolvePromqlTarget and getPromqlLabelCatalog take an optional
getConnection lookup that defaults to the team-scoped controller lookup,
so the REST /prometheus router is unchanged. The MCP PromQL tile and
describe_source paths pass getMcpConnection, so every MCP connection
lookup now goes through mcp/utils/connections.ts.
@brandon-pereira
brandon-pereira requested a review from a team as a code owner November 23, 2026 20:08
@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hyperdx-oss Ready Ready Preview Nov 23, 2026 9:13pm UTC
hyperdx-storybook Ready Ready Preview Nov 23, 2026 9:13pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 12d0cf0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the review/tier-2 Low risk — AI review + quick human skim label Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

🔵 Tier 2 — Low Risk

Small, isolated change with no API route or data model modifications.

Why this tier:

  • Standard feature/fix — introduces new logic or modifies core functionality

Review process: AI review + quick human skim (target: 5–15 min). Reviewer validates AI assessment and checks for domain-specific concerns.
SLA: Resolve within 4 business hours.

Stats
  • Production files changed: 6
  • Production lines changed: 74 (+ 154 in test files, excluded from tier calculation)
  • Branch: claude/mcp-promql-connection-lookup
  • Author: brandon-pereira

To override this classification, remove the review/tier-2 label and apply a different review/tier-* label. Manual overrides are preserved on subsequent pushes.

@github-actions

Copy link
Copy Markdown
Contributor

Duplicate code analysis

No new duplicate code introduced by this PR.


jscpd compared this branch against origin/main
across app, api and common-utils. Only new clones are listed; the codebase
currently holds 107 in total (1.12%).
Run yarn dupes locally to reproduce, or yarn dupes:all for the full picture.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR Review

If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:

  • 🔴 P0/P1 in code this PR adds or changes: fix before asking for review.
  • 🟡 P2 in code this PR changes: your call. Fix if small, otherwise reply.
  • 🔵 P3 is collapsed at the bottom: nobody needs to act on it before merge.
  • Anything about surrounding code, or asking you to widen the change (hoist a helper,
    dedupe with another file, fix other call sites): never fix here, whatever the
    priority. Reply with one sentence; if it is P0, say so plainly so a human sees it.

One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.

1 finding(s): 🔴 0 P0/P1 · 🟡 0 P2 · 🔵 1 P3

No inline comments. 1 P3 folded at the bottom.

🔵 1 P3 — no action needed before merge
  • 🔵 P3 packages/api/src/mcp/__tests__/promqlTileConnection.test.ts:256 — New MCP tests can't tell whether the lookup goes through getMcpConnection, the one thing this PR changes → Both new MCP tests (this one and the new case in describeSourceTimeout.test.ts:189) call the captured getConnection and then only check that the low-level getConnectionById got ('team-1', 'conn-1', true). getMcpConnection (packages/api/src/mcp/utils/connections.ts:20) simply passes those arguments on, so a callback that skips the helper and calls getConnectionById(context.teamId, …) directly would still pass. Any future MCP-specific rule added to the helper would then go unchecked. Fix: mock @/mcp/utils/connections and assert getMcpConnection was called with (context, 'conn-1', { selectPassword: true }).

Priority comes from a separate grading pass over the finished review. P3 is folded here instead of posted inline. No finding blocks merge automatically; a maintainer decides.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Deep Review

Scope: PR #3777 — routes MCP PromQL connection lookups through getMcpConnection by adding an injectable getConnection param to controllers/promql.ts. 9 files, ~74 production lines + tests.

Intent: Follow-up to #3776. Give resolvePromqlTarget and getPromqlLabelCatalog an optional getConnection lookup defaulting to the team-scoped getConnectionById, so REST /prometheus is unchanged while MCP paths inject getMcpConnection. Claimed no behavior change.

✅ No critical issues found.

Security and correctness independently traced both the REST and MCP paths and confirmed equivalence: teamConnectionLookup(teamId) and the MCP adapter both reduce to getConnectionById(context.teamId, connectionId, selectPassword), with identical team scoping and password selection. All four REST callers in routers/api/prometheus.ts omit getConnection and correctly fall through to the default. All changed-signature call sites (runPromqlTile, describePromqlSource) were updated. Types are sound — PromiseLike is the correct return type for the Mongoose Query thenable. No authz regression, no password leak, no missed caller.

🔵 P3 nitpicks (3)
  • packages/api/src/controllers/promql.ts:292 — getPromqlLabelCatalog's default-lookup branch (getConnection = teamConnectionLookup(teamId)) is never exercised by a test, unlike resolvePromqlTarget's default branch which is covered.
    • Fix: Add a case calling getPromqlLabelCatalog without getConnection and assert getConnectionById is invoked with (teamId, connectionId, true).
    • testing
  • packages/api/src/controllers/promql.ts:293 — getPromqlLabelCatalog now takes both teamId and getConnection, but in the MCP path teamId is only consumed by the default factory and otherwise unread, so an injected lookup and the passed teamId could silently reference different team scopes.
    • Fix: Document that an injected getConnection must match the passed teamId's scope, or derive both from a single source.
    • maintainability
  • packages/api/src/controllers/promql.ts:260 — The PromqlConnectionLookup indirection currently has two implementations that are behaviorally identical (getMcpConnection reduces to the same getConnectionById call as the default), so the seam earns its keep only as a future consolidation point.
    • Fix: Keep the abstraction only if MCP-specific connection rules are expected to diverge in getMcpConnection; otherwise it is accidental indirection.
    • maintainability

Reviewers (7): correctness, testing, maintainability, project-standards, kieran-typescript, security, api-contract.

Testing gaps:

  • No regression guard asserts the REST /prometheus path omits getConnection and keeps the team-scoped default lookup (new tests cover only the MCP injection).
  • The custom-lookup resolvePromqlTarget test asserts only toMatchObject({ connection }); it does not confirm databaseName/tableName remain populated on the injected path.
  • Changeset intentionally omitted; consistent with the repo's rule 7 (internal refactor, no @hyperdx/api behavior change) — not a violation.

No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

E2E Test Results

✅ All tests passed • 476 passed • 1 skipped • 1104s

Status Count
✅ Passed 476
❌ Failed 0
⚠️ Flaky 9
⏭️ Skipped 1

Tests ran across 4 shards in parallel.

View full report →

@brandon-pereira

Copy link
Copy Markdown
Member Author

Re the review's P3 findings:

  • runPromqlTile passes getMcpConnection (promqlTile.ts:129): fixed in 12d0cf0. mcp/__tests__/promqlTileConnection.test.ts calls the getConnection that runPromqlTile passes and checks it reaches the team-scoped lookup. It fails if the argument is dropped.
  • Source lookup skips getMcpSource (promqlTile.ts:127): not changed here. resolvePromqlTarget already called getSource(teamId, sourceId) directly, and PromQL tiles already ignored lookupSource, before this PR. This PR only covers connection lookups, so routing PromQL source lookups through the MCP source helpers is left for a separate follow-up.

This branch was successfully deployed

2 active deployments
Preview – hyperdx-storybook — 12d0cf07 Deployed Nov 23, 2026 by vercel[bot]
Preview – hyperdx-oss — 12d0cf07 Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review/tier-2 Low risk — AI review + quick human skim

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants