environmentd: add cluster/replica targeting and EXPLAIN ANALYZE to MCP developer tool#36792
Draft
sjwiesman wants to merge 1 commit into
Draft
environmentd: add cluster/replica targeting and EXPLAIN ANALYZE to MCP developer tool#36792sjwiesman wants to merge 1 commit into
sjwiesman wants to merge 1 commit into
Conversation
…P developer tool Extend the Developer MCP `query_system_catalog` tool with optional `cluster` and `cluster_replica` parameters and allow `EXPLAIN ANALYZE`. Introspection relations in the `mz_introspection` schema are per-replica: their results depend on the active `cluster` / `cluster_replica` session variables, and on a multi-replica cluster a replica must be targeted or the read is rejected. `EXPLAIN ANALYZE` is likewise planned as a SELECT against `mz_introspection` and runs on the active cluster/replica. Until now the tool always ran on the session default cluster and rejected `EXPLAIN ANALYZE` outright, so neither capability was reachable. Changes: - Add optional `cluster` and `cluster_replica` to `QuerySystemCatalogParams`, threaded through dispatch into the handler. When set, the handler prepends `SET CLUSTER` / `SET CLUSTER_REPLICA` inside the existing `BEGIN READ ONLY` wrapper. `cluster_replica` without `cluster` is rejected, since the replica target would otherwise be ambiguous. - Add `mz_introspection` to the handler's `search_path` so unqualified introspection relation names resolve. - Allow `Statement::ExplainAnalyzeObject` and `Statement::ExplainAnalyzeCluster` in `validate_readonly_query`. `validate_system_catalog_query` needs no change: the `FOR <object>` explainee is not a table reference, so it neither trips the non-system-table check nor the "must reference a system table" SELECT guard. - Update the tool description and input schema to document the new parameters and EXPLAIN ANALYZE support. Note: because the handler pins `search_path` to system schemas only, an `EXPLAIN ANALYZE ... FOR <object>` target must be fully qualified (`database.schema.name`); an unqualified user object name will not resolve. Tests: adds `test_validate_readonly_query_explain_analyze` and `test_validate_system_catalog_query_allows_explain_analyze`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The Developer MCP endpoint's
query_system_catalogtool always ran on the session default cluster and routed catalog queries tomz_catalog_server. That left two troubleshooting capabilities unreachable:mz_introspection.*relations are per-replica. Their results depend on the activecluster/cluster_replicasession variables, and on a multi-replica cluster a replica must be targeted or the read is rejected (UntargetedLogRead).EXPLAIN ANALYZE(MEMORY / CPU / HINTS, object- and cluster-scoped) is planned as aSELECTagainstmz_introspectionand runs on the active cluster/replica — but the tool's read-only allowlist rejected it outright.This change adds optional
clusterandcluster_replicaparameters to the tool and enablesEXPLAIN ANALYZE, so an MCP client can target a specific replica for introspection and resource-usage analysis. All changes are read-only and confined tosrc/environmentd/src/http/mcp.rs.What changed
clusterandcluster_replicatoQuerySystemCatalogParams, threaded through dispatch into the handler. When set, the handler prependsSET CLUSTER/SET CLUSTER_REPLICA(via the existingescaped_string_literal) inside the existingBEGIN READ ONLYwrapper.cluster_replicaprovided withoutclusteris rejected, sinceSET cluster_replicatargets a replica of the current cluster and the target would otherwise be ambiguous.mz_introspectionto the handler'ssearch_pathso unqualified introspection relation names resolve.validate_readonly_query's allowlist to permitStatement::ExplainAnalyzeObjectandStatement::ExplainAnalyzeCluster.validate_system_catalog_queryneeds no change: theFOR <object>explainee is not aTableFactor, so it neither trips the non-system-table check nor the "must reference a system table" SELECT guard.Known limitation
Because the handler pins
search_pathto system schemas only, anEXPLAIN ANALYZE ... FOR <object>target must be fully qualified (database.schema.name); an unqualified user object name will not resolve. This is documented in the handler and noted here for reviewers — it's the tradeoff of the locked-down search_path that prevents unqualifiedmz_*names resolving to user objects. Open to relaxing search_path specifically for EXPLAIN ANALYZE if reviewers prefer.Testing
Adds
test_validate_readonly_query_explain_analyzeandtest_validate_system_catalog_query_allows_explain_analyze. Manually verified end-to-end against a localenvironmentd:tools/listadvertises the new parameters;EXPLAIN ANALYZE CLUSTER MEMORYandEXPLAIN ANALYZE MEMORY FOR MATERIALIZED VIEW <fully-qualified>return results from the targeted replica;cluster_replicawithoutclusterreturns the validation error.Release note
This release adds optional
clusterandcluster_replicaparameters to the developer MCPquery_system_catalogtool and supportsEXPLAIN ANALYZE, enabling per-replica introspection queries and resource-usage analysis.🤖 Generated with Claude Code