0Pricing
MCP Academy · Lesson

Expose SQL Queries Safely

Offer read access to a database without injection.

Expose SQL Queries Safely is a free MCP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Databases Behind a Tool

An MCP server can give a model real data by wrapping a database behind a tool, so the AI asks questions instead of touching raw tables. 🗄️

Reads, Not Writes

For safety, start by exposing only read access. Let the model query and report, but keep inserts, updates, and deletes off the table.

The Injection Danger

Never paste model text straight into SQL. That invites SQL injection, where crafted input rewrites your query and leaks or destroys data.

query = "SELECT * FROM users WHERE id = " + user_input  # unsafe!

Use Parameterized Queries

The fix is parameterized queries: you write placeholders and the driver binds values safely, so input can never change the query shape.

cur.execute("SELECT * FROM users WHERE id = ?", (user_id,))

Take Values as Arguments

Let the model pass values as typed arguments, then you build the query. The model supplies data, never the SQL text itself.

@mcp.tool()
def find_user(user_id: int) -> str:
    ...

Pin the Query Shape

Keep the SQL fixed in your code and only slot in bound parameters. A locked query shape is the simplest way to stay safe.

Use a Read-Only Role

Defense in depth: connect with a database read-only user. Even a buggy tool then physically cannot modify or drop your data.

Bound the Result Size

Always cap rows with a LIMIT so one broad query cannot dump a huge table into the model and blow your token budget.

cur.execute("SELECT name FROM users LIMIT 50")

Whitelist Allowed Tables

If the model picks a table, validate it against an allowlist. Reject anything not on the list instead of trusting the name.

Return Tidy Rows

Format results as clean text or simple records so the model reads them easily. Clarity in, clarity out for the next reasoning step.

Hide Sensitive Columns

Select only the columns the task needs. Leaving out secrets like password hashes keeps private data out of the model entirely.

Quick Check

What protects a SQL tool from injection?

Recap: Safe SQL

You exposed a database safely: parameterized read-only queries, allowlisted tables, and row limits. Next, wrap a web API. 🎯

Frequently asked questions

Is the “Expose SQL Queries Safely” lesson free?

Yes — the full text of “Expose SQL Queries Safely” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Expose SQL Queries Safely”?

Offer read access to a database without injection. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Expose SQL Queries Safely” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Expose SQL Queries Safely
  2. Wrap a REST API as Tools
  3. Pool Connections in Lifespan
  4. Cache & Rate-Limit Upstreams
← Back to MCP Academy