SQLクエリを安全に公開する
インジェクションを防ぎながら、データベースへの読み取りアクセスを提供します。
「SQLクエリを安全に公開する」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎯
よくある質問
「SQLクエリを安全に公開する」レッスンは無料ですか?
はい。「SQLクエリを安全に公開する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「SQLクエリを安全に公開する」で何を学びますか?
インジェクションを防ぎながら、データベースへの読み取りアクセスを提供します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「SQLクエリを安全に公開する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。