Skip to content

refactor(api): look up MCP connections through one helper - #3776

Merged
kodiakhq[bot] merged 2 commits into
mainfrom
claude/mcp-connection-lookup-helper
Nov 23, 2026
Merged

kodiakhq[bot] merged 2 commits into
mainfrom
claude/mcp-connection-lookup-helper

Conversation

@brandon-pereira

Copy link
Copy Markdown
Member

Summary

MCP tools and prompts called the connection controller directly (getConnectionsByTeam, getConnectionById, validateConnectionId) from five places. This adds mcp/utils/connections.ts with three helpers that take the full McpContext:

  • getMcpConnections(context)
  • getMcpConnection(context, connectionId, { selectPassword }?)
  • validateMcpConnectionId(context, connectionId)

Every MCP call site now uses them, including getMcpClickhouseClient. The rules about which connections an MCP caller can use now live in one place, matching how mcp/utils/clickhouse.ts and mcp/utils/sources.ts already work.

No behavior change: same return values, null when a connection is not found, and the same validation messages and tool output.

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 src/mcp/__tests__/mcpConnections.test.ts and the existing MCP unit tests, including the dashboard prompt tests.
  2. make dev-int FILE=src/mcp/__tests__ runs the MCP integration suites (sources, dashboards, saved searches and more): 23 suites, 636 tests pass.

How to test on Vercel preview

N/A, non-UI change.

References

Add mcp/utils/connections.ts (getMcpConnections, getMcpConnection,
validateMcpConnectionId) taking the full McpContext, and route every MCP
tool and prompt connection lookup through it, including
getMcpClickhouseClient. No behavior change.
@brandon-pereira
brandon-pereira requested a review from a team as a code owner November 23, 2026 19:49
@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 7:57pm UTC
hyperdx-storybook Ready Ready Preview Nov 23, 2026 7:57pm UTC

Request Review

@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: 83 (+ 76 in test files, excluded from tier calculation)
  • Branch: claude/mcp-connection-lookup-helper
  • 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.

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

No inline comments. 2 P3 folded at the bottom.

🔵 2 P3 — no action needed before merge
  • 🔵 P3 packages/api/src/mcp/utils/connections.ts:296 — validateMcpConnectionId wraps teamId in an ObjectId the controller doesn't need, and can throw instead of returning a validation result → Pass context.teamId to validateConnectionId as-is. Its signature (packages/api/src/controllers/connection.ts:11) accepts string | ObjectId, and Mongoose casts the string in Connection.exists. That lets you drop the mongoose import. It also stops a malformed teamId from throwing a BSONError, which today escapes the helper as a 500 instead of the { ok: false } result the controller returns. Update the test at mcpConnections.test.ts:79, which currently pins the ObjectId conversion (toBeInstanceOf(mongoose.Types.ObjectId)).
  • 🔵 P3 packages/api/src/mcp/__tests__/mcpConnections.test.ts:11 — New unit test mocks the controller, so it cannot catch a team-scoping regression → Each case only checks that the mocked controller got teamId. The PR's stated goal is that these helpers own which connections an MCP caller can use. Add an integration case (alongside sources.int.test.ts) that creates a connection for another team and checks that getMcpConnection and getMcpConnections don't return it and that validateMcpConnectionId rejects it.

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

✅ No critical issues found.

This is a pure mechanical refactor: three thin wrappers in mcp/utils/connections.ts over the existing connection controller, with five MCP call sites rewired to use them. Behavioral equivalence was verified at every call site:

  • At each rewired site the former local teamId was const { teamId } = context, so getMcpConnections(context) → getConnectionsByTeam(context.teamId) is identical to the prior getConnectionsByTeam(teamId).
  • In listSources.ts the old getConnectionsByTeam(teamId.toString()) called .toString() on a value that is already a string, so the wrapper is equivalent.
  • getMcpConnection forwards selectPassword (default false); the two sites requesting passwords (clickhouse.ts, listSources.ts metric preview) both pass selectPassword: true, matching the prior true.
  • validateMcpConnectionId(context, input.connection) reproduces the old validateConnectionId(input.connection, new mongoose.Types.ObjectId(teamId)) exactly, preserving team-ownership enforcement in saveSource.ts.
  • null-on-not-found and return shapes (un-awaited mongoose query/Promise) are unchanged; the mongoose import retained in saveSource.ts is still used by the ValidationError/CastError guards.

Team-scoping and tenant isolation are preserved — every helper keys off context.teamId — and no credential is newly logged or surfaced to the agent. The new mcpConnections.test.ts covers all three helpers, including the selectPassword default/explicit branches and the null-return path.

No P0/P1/P2 findings.


Reviewers (6): ce-correctness-reviewer, ce-testing-reviewer, ce-maintainability-reviewer, ce-kieran-typescript-reviewer, ce-security-reviewer, ce-project-standards-reviewer

Testing gaps: none — new helper behavior is fully covered; rewired call sites are unchanged existing code.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

E2E Test Results

✅ All tests passed • 475 passed • 1 skipped • 1247s

Status Count
✅ Passed 475
❌ Failed 0
⚠️ Flaky 10
⏭️ Skipped 1

Tests ran across 4 shards in parallel.

View full report →

@brandon-pereira

Copy link
Copy Markdown
Member Author

🔵 1 P3 — no action needed before merge
🔵 P3 packages/api/src/mcp/utils/connections.ts:10 — Comment says every MCP lookup goes through these helpers, but the PromQL tools still look up connections directly → mcp/tools/query/promqlTile.ts:125 (resolvePromqlTarget) and mcp/tools/sources/describePromqlSource.ts (getPromqlLabelCatalog) both reach getConnectionById inside controllers/promql.ts (lines 289, 397) with a raw teamId. They skip these helpers, so a rule added here would not apply to PromQL queries. Either route those paths through getMcpConnection, for example by passing a connection resolver into resolvePromqlTarget, or reword the comment to name these two exceptions.

Will adjust PromQL in a different PR

@kodiakhq
kodiakhq Bot merged commit 5c3e351 into main Nov 23, 2026
29 checks passed
@kodiakhq
kodiakhq Bot deleted the claude/mcp-connection-lookup-helper branch November 23, 2026 20:06

This branch was successfully deployed

2 active deployments
Preview – hyperdx-oss — 7a63f3f2 Deployed Nov 23, 2026 by vercel[bot]
Preview – hyperdx-storybook — 7a63f3f2 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

automerge 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