Skip to content

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

Merged
kodiakhq[bot] merged 3 commits into
mainfrom
claude/mcp-promql-connection-lookup
Nov 24, 2026
Merged

kodiakhq[bot] merged 3 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 24, 2026 5:12pm UTC
hyperdx-storybook Ready Ready Preview Nov 24, 2026 5:12pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 3aee381

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.

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

No inline comments. 3 P3 folded at the bottom.

🔵 3 P3 — no action needed before merge
  • 🔵 P3 packages/api/src/mcp/__tests__/promqlTileConnection.test.ts:256 — Test can't tell getMcpConnection apart from a direct getConnectionById call → getMcpConnection (mcp/utils/connections.ts:20) just forwards to getConnectionById(context.teamId, id, selectPassword). Asserting on mockGetConnectionById therefore still passes if runPromqlTile hands in its own getConnectionById wrapper and skips the helper, which is the regression this PR is meant to prevent. Mock @/mcp/utils/connections and assert getMcpConnection was called with (context, 'conn-1', { selectPassword: true }). The same gap exists in the new describeSourceTimeout.test.ts case at line 387.
  • 🔵 P3 packages/api/src/controllers/promql.ts:393 — PromQL tile source lookup still bypasses the MCP source helpers → resolvePromqlTarget still calls getSource(teamId, sourceId) directly. As a result, MCP PromQL tiles resolve their source without getMcpSource or getVisibleMcpSource from mcp/utils/sources.ts, whose header says every MCP tool resolves sources through it. This is the same half-routed exception this PR removes for connections. Inject a source lookup the same way as getConnection, or pass the resolved source in, so the MCP path uses its own helper for sources too.
  • 🔵 P3 packages/api/src/mcp/tools/query/promqlTile.ts:319 — Same getMcpConnection adapter closure written twice → (connectionId, options) => getMcpConnection(context, connectionId, options) appears here and again in describePromqlSource.ts:367. Export one adapter from mcp/utils/connections.ts, such as mcpConnectionLookup(context): PromqlConnectionLookup, and use it in both places so the routing stays in one spot.

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

Internal refactor in packages/api that routes the two remaining MCP PromQL connection lookups (runPromqlTile → resolvePromqlTarget, describePromqlSource → getPromqlLabelCatalog) through the shared getMcpConnection helper by adding an optional getConnection parameter that defaults to the team-scoped getConnectionById.

Independent verification confirms the refactor is behavior-preserving: all four REST /prometheus call sites pass only selectPassword and fall through to the unchanged team-scoped default; both migrated MCP call sites and the describePromqlSource caller in describeSource.ts are updated to the new context: McpContext signature; and the default argument getConnection = teamConnectionLookup(teamId) binds the same teamId already in scope. The security and correctness passes both found the authorization boundary (team-scoping, selectPassword secret handling) unchanged.

✅ No critical issues found.

🟡 P2 — recommended

  • packages/api/src/controllers/promql.ts:298 — the new getConnection default on getPromqlLabelCatalog (the team-scoped path REST and un-injected callers rely on) is never exercised; the new test only covers the injected-lookup branch.
    • Fix: Add a test that calls getPromqlLabelCatalog without getConnection and asserts it resolves through getConnectionById(teamId, …, true).
    • correctness, testing
🔵 P3 nitpicks (1)
  • packages/api/src/controllers/promql.ts:381 — the injectable getConnection and the teamId positional/field are now independent inputs a caller must keep aligned; a future caller could inject a lookup scoped to a different team than teamId and silently bypass the team-scoping invariant.
    • Fix: Document the "both must share one team" invariant on PromqlConnectionLookup, or shape the API so the team is a single source of truth.

Reviewers (2 returned): security, correctness.

Testing gaps:

  • Default (team-scoped) lookup branch of getPromqlLabelCatalog is uncovered; only resolvePromqlTarget's default branch is tested.

Coverage note: Of the dispatched review team, the two highest-stakes personas (security, correctness) returned clean before output was required; both corroborated the no-behavior-change claim. The prior-review points were independently confirmed addressed — runPromqlTile wiring to getMcpConnection is in place and tested (promqlTileConnection.test.ts), and the PromQL source-lookup-via-getMcpSource item remains legitimately pre-existing and out of this PR's scope.

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 • 477 passed • 1 skipped • 1256s

Status Count
✅ Passed 477
❌ Failed 0
⚠️ Flaky 11
⏭️ 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.

…nection-lookup

# Conflicts:
#	packages/api/src/mcp/tools/sources/describePromqlSource.ts
#	packages/api/src/mcp/tools/sources/describeSource.ts
@kodiakhq
kodiakhq Bot merged commit 0ae26e4 into main Nov 24, 2026
42 of 44 checks passed
@kodiakhq
kodiakhq Bot deleted the claude/mcp-promql-connection-lookup branch November 24, 2026 17:36

This branch was successfully deployed

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants