TheRobotLearns Platform · ProxySQL: from first install to production

MCP Targets, Auth Profiles, and Query Rule Governance

  • about 20 min read

Learning objectives

  • Explain how editable Admin tables, persistent copies, and runtime projections shape MCP profiles and rules.
  • Bind target identifiers to backend protocols, ProxySQL hostgroups, and auth profiles without passing database credentials.
  • Load and persist MCP profile and rule changes, then verify query-handler state through Admin projections.
  • Control `run_sql_readonly` and `explain_sql` by tuning active rule match chains.
  • Use `list_targets` and descriptions for discovery; treat `log`, `max_rows`, and `timeout_ms` as metadata unless enforced.
  • Monitor MCP rule activity with `stats_mcp_query_rules`, avoiding assumptions about all MCP endpoints.

Why MCP policy appears ignored

You change mcp_query_rules, but runtime_mcp_query_rules keeps showing what the MCP Query handler loaded. The MCP Query handler is the part that evaluates run_sql_readonly and explain_sql requests. The editable row and the live projection are not the same object.

ProxySQL v4.0.10 separates MCP configuration into editable Admin tables, persistent copies in the configuration database, and live runtime_mcp_* projections of module state. The editable tables are where you define policy rows. The runtime views are read-only projections of loaded rows, so a SELECT against them shows the module snapshot rather than your pending edit.

mcp_query_rules defines policy for SQL execution through the MCP Query handler. In v4.0.10, rules are evaluated by run_sql_readonly and explain_sql; they are not a universal filter for /mcp/stats, /mcp/config, or every other endpoint.

mcp_target_profiles stores max_rows, timeout_ms, allow_explain, and allow_discovery. In v4.0.10, the profile loader carries those fields into an intermediate context, but the released Query handler does not copy or enforce them in the executable target registry. They remain configuration metadata, not dependable limits.

Takeaway: Editable MCP tables hold changes; runtime_mcp_* views show loaded module state.

How targets hide backend credentials

MCP clients should call list_targets and pass a target_id; they must not submit backend credentials as tool arguments. mcp_target_profiles is the bridge. Each row maps a client-visible logical target to a backend protocol, a ProxySQL hostgroup, and an auth_profile_id. The protocol can be mysql or pgsql. The hostgroup routes the query, while the auth profile supplies server-side credentials. description is client-visible or operator text.

mcp_auth_profiles stores db_username, db_password, default_schema, and TLS intent. The Query executor applies those credentials internally. Because db_password is stored in the ProxySQL configuration databases, you must restrict Admin and filesystem access. It also appears in the runtime projection, so avoid broad SELECT * output in logs and never expose these tables or results to an untrusted MCP client.

Rule rows then act as request filters. username, target_id, schemaname, and tool_name select requests. match_pattern, negate_match_pattern, and re_modifiers match argument or SQL text. flagIN and flagOUT chain rules. Only active rules participate after loading. replace_pattern rewrites SQL or action when set. timeout_ms can override the Query-handler timeout when greater than zero. error_msg blocks with the supplied error. OK_msg returns the supplied success without executing SQL. apply stops further rule evaluation.

log requests logging, but the released Query handler contains a pending logging branch, so this field is not a complete audit guarantee.

Takeaway: target_id exposes a route, auth_profile_id holds credentials, and rule columns chain request handling.

Loading profiles and persisting rules

After editing mcp_query_rules, run the LOAD MCP QUERY RULES ... command family so active rows enter runtime state. To persist that change in the configuration database, run the SAVE MCP QUERY RULES ... command family. Rules use this command family, while profiles use explicit canonical commands.

Profiles are loaded as one runtime mapping. LOAD MCP PROFILES TO RUNTIME moves Memory to Runtime. LOAD MCP PROFILES FROM DISK moves Disk to Memory and Runtime. SAVE MCP PROFILES TO MEMORY moves Runtime to Memory. SAVE MCP PROFILES TO DISK moves Memory to Disk. Here, Memory refers to the editable Admin table, Runtime refers to the loaded projection, and Disk refers to the configuration database copy.

The runtime load is atomic for authentication and target profiles. It installs both editable tables and rebuilds the joined target and auth map. A later SELECT from a runtime_mcp_* table refreshes rows through the plugin callback. LOAD does not copy rows directly into that runtime table; it asks the chassis to project the in-memory snapshot.

To verify state, select from the editable table and from its matching runtime projection. The runtime side is read-only, so write only to the editable table.

SELECT target_id, protocol, hostgroup_id, auth_profile_id, active
FROM mcp_target_profiles;

SELECT target_id, protocol, hostgroup_id, auth_profile_id, active
FROM runtime_mcp_target_profiles;

Takeaway: Run LOAD MCP QUERY RULES ... or LOAD MCP PROFILES TO RUNTIME, then compare rows with runtime_mcp_*.

Reading MCP rule stats safely

Use stats_mcp_query_rules to see whether active rules are being hit. This stats-schema table reports hit counts from the loaded MCP rule set. Its columns are rule_id, username, target_id, and hits. Query it after loading rules, because the counts come from the loaded set rather than from pending edits. Use the table after an MCP rule load, because the hits reflect the rules in that loaded snapshot.

Do not read those hits as traffic-wide coverage. In v4.0.10, mcp_query_rules are evaluated by run_sql_readonly and explain_sql. They are not a universal filter for /mcp/stats, /mcp/config, or every other endpoint. Other MCP invocation and digest counters are listed under MCP Stats.

For discovery, list_targets returns target identifiers, and mcp_target_profiles.description supplies client-visible or operator text. That makes the target catalog available without revealing backend credentials. Treat log, max_rows, and timeout_ms as metadata in v4.0.10. The Query handler has a pending logging branch. Target limits are not reliably enforced.

SELECT rule_id, username, target_id, hits
FROM stats_mcp_query_rules;

Takeaway: Check stats_mcp_query_rules for loaded rule hits, not for every MCP endpoint.

Compare edited MCP rows and load profiles

SELECT rule_id, active, target_id, tool_name, flagIN, flagOUT, apply FROM mcp_query_rules WHERE active = 1;
SELECT rule_id, active, target_id, tool_name, flagIN, flagOUT, apply FROM runtime_mcp_query_rules WHERE active = 1;
SELECT target_id, protocol, hostgroup_id, auth_profile_id, description FROM mcp_target_profiles WHERE protocol = 'mysql';
SELECT target_id, protocol, hostgroup_id, auth_profile_id FROM runtime_mcp_target_profiles;
LOAD MCP PROFILES TO RUNTIME;
SAVE MCP PROFILES TO DISK;
SELECT rule_id, username, target_id, hits FROM stats_mcp_query_rules;

This script compares editable MCP rows with loaded projections, loads profiles, persists them, and checks rule hits. The first SELECT lists editable active rules. The second SELECT shows the read-only loaded rule projection for active rows. The third SELECT lists editable target rows whose protocol is mysql. The fourth SELECT shows the read-only loaded target projection. LOAD MCP PROFILES TO RUNTIME installs both editable profile tables and rebuilds the joined target and auth map. SAVE MCP PROFILES TO DISK writes the Memory copy to the configuration database. The final SELECT returns hit counts from the loaded MCP rule set.

Try it: config_layer_sim

Run SELECT rule_id, active, target_id, tool_name FROM mcp_query_rules; in the Admin schema. Run the same columns from runtime_mcp_query_rules. The first table is editable; the second is a read-only projection of loaded state. Run SELECT target_id, protocol, hostgroup_id, auth_profile_id, description FROM mcp_target_profiles; and the same columns from runtime_mcp_target_profiles. Then run LOAD MCP PROFILES TO RUNTIME, and select those target columns from runtime_mcp_target_profiles. Finally, select rule_id, username, target_id, and hits from stats_mcp_query_rules.

Sources

Do more with an account

MCP Targets, Auth Profiles, and Query Rule Governance — ProxySQL: from first install to production