# SQL Injection in MCP Servers > Classic SQLi and read-only-mode bypasses in the database connectors (MCP servers) and LLM gateways that expose SQL engines to AI agents — where the "untrusted input" is often the agent itself. ## How it works Model Context Protocol servers wrap a database as tools (`execute_query`, `describe_table`, `update_data`) for an LLM agent. They inherit every SQLi failure mode of ordinary software, with two twists: (1) tools frequently interpolate agent-supplied **identifiers** (table names, filters) that can't be parameterized, and (2) they advertise a **read-only mode** as the primary safety guarantee — so a read-only bypass, not just data theft, is the core finding. These are OWASP-Top-10 bugs in new plumbing ([[raw/securitylabs.datadoghq.com/mcp-vulnerability-case-study-sql-injection-in-the-postgres-mcp-server-4796|Datadog case study]]). Related surface: LLM gateways that store provider credentials in SQL. Link: [[MCP Security]]. ## Techniques / payloads (bypass patterns) - **Transaction escape** — Anthropic's reference Postgres MCP server ran queries inside a read-only transaction; appending `COMMIT;` ends it and lets subsequent statements write ([[raw/securitylabs.datadoghq.com/mcp-vulnerability-case-study-sql-injection-in-the-postgres-mcp-server-4796|Datadog]]). - **Statement chaining / `ATTACH`** — sqlite-mcp: statement chaining in `execute_query` and SQLi in `describe_table` both enable `ATTACH DATABASE` to escape read-only, read arbitrary SQLite files, and create files anywhere the process can write ([[raw/github.com/sqlite-mcp-read-only-bypass-via-statement-chaining-and-sqli-15338|sqlite-mcp]]). - **Comment-parsing divergence** — DBHub (GHSA-j656-3hf2-fvjc): its parser treats `--` as a comment regardless of the next char, but MySQL/MariaDB only start a `--` comment when followed by whitespace. So `SELECT 1--1;DROP TABLE victim` passes the read-only check as one SELECT, but the connector (opened with `multipleStatements: true`) runs both ([[raw/advisories.gitlab.com/dbhub-mysql-comment-interpretation-bypasses-read-only-sql-validation-13401|DBHub]]). - **PRAGMA/stacked-statement injection** — Tabularis (CVE-2026-118378): a crafted `table_name` into `describe_table` closes the `PRAGMA` argument and appends stacked statements to read/modify/drop SQLite data, bypassing read-only and write-approval ([[raw/github.com/tabularis-mcp-sql-injection-read-only-bypass-cve-2026-118378-12627|Tabularis]]). - **Identifier-validation vs. lexer mismatch** — SurrealDB MCP (GHSA-cr79-9p8r-p5wx): escaped backticks mishandled between identifier validation and SQL lexing let an attacker break out of a quoted identifier; mssql-mcp interpolated unparameterized table names/predicates in `update_data`/`delete_data`, reaching stacked statements and `xp_cmdshell`. ## Notable cases - **Anthropic reference Postgres MCP** — deprecated/archived but still ~21k weekly npm downloads; patched in the Zed Industries fork ([[raw/securitylabs.datadoghq.com/mcp-vulnerability-case-study-sql-injection-in-the-postgres-mcp-server-4796|Datadog]]). - **Tabularis CVE-2026-118378 (CVSS 4.0: 8.5)** — read-only bypass in the SQLite driver before 0.28.0. - **DBHub, sqlite-mcp, mssql-mcp (patched 0.6.0), SurrealDB (fixed 4.0.0)** — a cluster of 2026 MCP DB-connector SQLi/read-only bypasses; a bug hunter tracked three serious MCP DB flaws, one left unpatched ([[raw/theregister.com/bug-hunter-tracks-down-three-serious-mcp-database-flaws-one-left-unpat-7922|The Register]]). - **LiteLLM CVE-2026-42208 / Kong AI Gateway** — LLM gateways with SQLi in key-verification and usage counters, leaking stored provider credentials (covered in [[SQL Injection]] and [[Union-Based SQL Injection]]). ## Defenses Parameterize queries and validate identifiers in MCP tools exactly as in any app — read-only mode is not a substitute. Enforce read-only at the **database/connection** layer (a read-only role, `default_transaction_read_only`), not by parsing the SQL string, since parser divergence defeats string checks. Disable `multipleStatements`/stacked queries on the connection. Don't expose deprecated reference servers to sensitive data. Treat the agent's SQL as untrusted input, because indirect prompt injection can make the agent emit attacker-chosen queries ([[Indirect Prompt Injection]]). ## Related - [[SQL Injection]] - [[MCP Security]] - [[Indirect Prompt Injection]] - [[SQL Injection to RCE]] ## Sources - [[raw/securitylabs.datadoghq.com/mcp-vulnerability-case-study-sql-injection-in-the-postgres-mcp-server-4796|MCP Vulnerability Case Study: SQLi in the Postgres MCP Server]] - [[raw/advisories.gitlab.com/dbhub-mysql-comment-interpretation-bypasses-read-only-sql-validation-13401|DBHub: MySQL comment interpretation bypasses read-only validation]] - [[raw/github.com/sqlite-mcp-read-only-bypass-via-statement-chaining-and-sqli-15338|sqlite-mcp: read-only bypass via statement chaining and SQLi]] - [[raw/github.com/tabularis-mcp-sql-injection-read-only-bypass-cve-2026-118378-12627|Tabularis MCP SQL injection read-only bypass (CVE-2026-118378)]] - [[raw/github.com/surrealdb-mcp-tool-identifier-statement-injection-13219|SurrealDB MCP tool identifier statement injection]] - [[raw/github.com/mssql-mcp-sql-injection-via-unparameterized-table-names-15198|mssql-mcp SQL injection via unparameterized table names]] - [[raw/theregister.com/bug-hunter-tracks-down-three-serious-mcp-database-flaws-one-left-unpat-7922|Bug hunter tracks down three serious MCP database flaws]]