Repository navigation
refactor(api): resolve MCP sources through one helper - #3771
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
|
🔵 Tier 2 — Low RiskSmall, isolated change with no API route or data model modifications. Why this tier:
Review process: AI review + quick human skim (target: 5–15 min). Reviewer validates AI assessment and checks for domain-specific concerns. Stats
|
Duplicate code analysis1 new clone introduced by this PR.
If one of these duplicates a helper that already exists, import it instead jscpd compared this branch against |
PR Review✅ No issues found. |
Deep Review✅ No critical issues found. This is a behavior-preserving internal refactor. 🟡 P2 -- recommended
🔵 P3 nitpicks (2)
Reviewers (6): correctness, maintainability, testing, kieran-typescript, api-contract, project-standards. Testing gaps: No regression coverage tying each converted call site to its intended visibility helper; 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 |
E2E Test Results✅ All tests passed • 473 passed • 1 skipped • 1110s
Tests ran across 4 shards in parallel. |
## 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
- Related PRs: #3771 (same pattern for source lookups)
…lper (#3777) ## 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 - Related PRs: #3776, #3771 (same pattern for source lookups)
Summary
MCP tools and prompts each looked up sources themselves, calling
getSource/getSourcesdirectly at about 16 places. A few of them also appliedisHiddenSourceon their own. So no single place decided which sources an MCP caller can see, and any visibility rule had to be copied into each tool.Every MCP source lookup now goes through
packages/api/src/mcp/utils/sources.ts. This makes it the single extension point for source resolution in the MCP server, the same way #3752 did for ClickHouse clients.getMcpSource(context, id)getSource(context.teamId, id)getMcpSources(context)getSources(context.teamId)getVisibleMcpSources(context)getMcpSources+isHiddenSourcefilterlistSources,create_dashboardpromptgetVisibleMcpSource(context, id)getMcpSource, null when hiddendescribeSource,deleteSourcePlaces that hid sources before still hide them, and places that didn't still don't. Return values, null-on-not-found, error messages and tool output are unchanged.
A few internal functions (
createSourceLookup,loadHealthSource,getReferenceTargets,validateCorrelatedSourceIds, the metric describe/list implementations) now takeMcpContextinstead ofteamId, so they can call the helpers.teamIdvariables left unused by that change are removed.One small difference:
clickstack_delete_sourcenow returns "Source not found" for a missing source before callingdeleteSource, rather than afterdeleteSourcereturns null. The output is the same.No changeset, since this is an internal refactor with no user-facing change.
How to test
mcpSources.test.tscovers the helpers: team scoping, null on not found, and which sources get hidden while PromQL is off or on.make ci-lintpasses.cd packages/api && yarn ci:unit: 1643 tests passed.make dev-int FILE=src/mcp/__tests__: 23 test files, 636 tests passed.References