TheRobotLearns Platform · ProxySQL: from first install to production

RAG Ingestion, Hybrid Retrieval, and NL2SQL Boundaries

  • about 20 min read

Learning objectives

  • Ingest MySQL or MariaDB rows into RAG documents using mappings, metadata, chunks, and filters.
  • Compare full-text, vector, hybrid, and NL2SQL flows exposed through MCP or REST.
  • Wire agent requests to MCP target identifiers and server-side auth profiles, not database credentials.
  • Design safer NL2SQL by limiting query rules to actions they evaluate: `run_sql_readonly` and `explain_sql`.
  • Explain why MCP query rules and policy fields need backend or client enforcement.
  • Use stats and rule activity to identify which MCP query rules hit agent retrieval and SQL use.

Why retrieval and SQL differ

An agent asks how to tune a slow MySQL query. RAG can return indexed chunks, and the MCP Query handler can run SQL.

ProxySQL RAG combines full-text search and semantic vector search, so it can return chunks from documents, articles, or tickets. RAG stores transformed documents, chunks, metadata, and indexes in the SQLite3 RAG database on port 6030. MCP tools such as rag.search_fts, rag.search_vector, and rag.search_hybrid query that index. The MCP Query handler uses policy tables for SQL execution through run_sql_readonly and explain_sql. In ProxySQL v4.0.10, these rules are not a universal filter for /mcp/stats, /mcp/config, or every other endpoint.

Think of the two paths as a library desk and a database desk. The library desk returns searchable content. The database desk routes SQL through a target profile and hostgroup. MCP integration also exposes REST endpoints for programmatic access.

Retrieval uses rag_* tables and search tools. Query rules use mcp_query_rules, target profiles, auth profiles, and rule stats.

Takeaway: RAG retrieval and MCP SQL use different tables and tools, even when one agent request needs both.

Ingest rows as searchable chunks

You add a rag_sources row pointing at MySQL documents, but rag_ingest must process it before search tools use the result. rag_sources is the ingestion definition. It contains columns for the backend host, port, source table, primary key, where_sql, JSON mapping, chunk settings, and embedding settings. ProxySQL v4.0+ supports RAG features when enabled.

The mapping turns each source row into a document. It can build doc_id, title, and body from columns such as id, title, and content. Metadata can carry fields like tags, created_at, and author. Chunk settings split long text into pieces, for example 4000-character chunks with 400-character overlap.

Filtering can narrow ingestion and search. where_sql can ingest only published = 1, while later search requests can use metadata filters such as tags_any and created_after.

Incremental ingestion tracks work in rag_sync_state, so it processes new primary keys and skips existing documents. Run rag_ingest init, then rag_ingest ingest, to create schema and load rows. You can re-process a source by deleting its rag_sync_state rows.

Takeaway: rag_sources holds mapping, filters, chunking, and embedding choices; rag_ingest turns it into searchable chunks.

Route agents to MCP targets

An MCP agent wants to query a backend, but you do not want the agent to send database credentials. mcp_auth_profiles stores db_username, db_password, default_schema, and TLS intent on the server. mcp_target_profiles maps a client-visible target_id to a protocol, hostgroup_id, and auth_profile_id.

Clients call list_targets to discover available target_id values, then pass one value instead of backend credentials. This keeps tool requests focused on the logical target, while the server keeps the join between target and auth profile.

Loading makes the mapping live. LOAD MCP PROFILES TO RUNTIME installs the editable auth and target tables and rebuilds the joined target/auth map. Released aliases include FROM MEMORY, FROM MEM, and TO RUN. LOAD MCP PROFILES FROM DISK brings disk rows into memory and runtime.

SAVE MCP PROFILES TO DISK persists memory rows, and no released alias is listed for that save direction. runtime_mcp_target_profiles and runtime_mcp_auth_profiles are read-only projections refreshed on each SELECT.

Takeaway: Store credentials in mcp_auth_profiles, expose target_id through mcp_target_profiles, then load profiles to runtime.

Guardrails you cannot assume

You set error_msg='Blocked' in a rule, yet an agent still calls /mcp/stats. ProxySQL v4.0.10 evaluates mcp_query_rules for run_sql_readonly and explain_sql. It is not a universal filter for every MCP endpoint. A rule only participates when active=1 and loaded.

match_pattern, negate_match_pattern, and re_modifiers control whether the request matches. apply stops further rule evaluation when set. error_msg blocks the request with the supplied error, while OK_msg returns the supplied success without executing SQL.

Other fields are metadata more than enforcement. The target profile columns max_rows, timeout_ms, allow_explain, and allow_discovery reach the intermediate profile context. The released Query handler does not copy or enforce them in its executable target registry.

The Query executor context does not receive use_ssl or ssl_mode from the joined target/auth map. Enforce limits through backend privileges, applicable query rules, and external client or network timeouts. The configuration databases store db_password, and runtime projections show it too.

Use stats_mcp_query_rules to see which loaded rule hit for a username or target. Query runtime_mcp_query_rules only as a read-only snapshot.

Takeaway: MCP query rules guard run_sql_readonly and explain_sql; use backend privileges and stats to verify real enforcement.

Activate profiles, inspect rule hits

LOAD MCP PROFILES TO RUNTIME;
SAVE MCP PROFILES TO DISK;
LOAD MCP QUERY RULES TO RUNTIME;
SAVE MCP QUERY RULES TO DISK;
SELECT target_id, protocol, hostgroup_id, auth_profile_id FROM runtime_mcp_target_profiles WHERE active=1;
SELECT rule_id, username, target_id, hits FROM stats_mcp_query_rules ORDER BY hits DESC;

LOAD MCP PROFILES TO RUNTIME installs the editable auth and target tables and rebuilds the joined target/auth map. SAVE MCP PROFILES TO DISK writes memory profile rows to the configuration database. LOAD MCP QUERY RULES TO RUNTIME makes active query rules participate after loading. SAVE MCP QUERY RULES TO DISK writes memory rule rows to the configuration database. The SELECT on runtime_mcp_target_profiles returns active target rows from the read-only runtime projection. The SELECT on stats_mcp_query_rules returns hit counts from the loaded MCP rule set.

Try it: config_layer_sim

Connect to ProxySQL Admin on port 6032. Insert one mcp_auth_profiles row and one mcp_target_profiles row, then run LOAD MCP PROFILES TO RUNTIME and SAVE MCP PROFILES TO DISK. Query runtime_mcp_target_profiles where active=1 to confirm the target projection. Insert one mcp_query_rules row with active=1, an error_msg, and a target_id, then run LOAD MCP QUERY RULES TO RUNTIME and SAVE MCP QUERY RULES TO DISK. Call run_sql_readonly for that target and username. Query stats_mcp_query_rules to see which rule row reports a hit.

Sources

Do more with an account

RAG Ingestion, Hybrid Retrieval, and NL2SQL Boundaries — ProxySQL: from first install to production