0Pricing
MCP Academy · Lekcja

Bezpieczne udostępnianie zapytań SQL

Udostępnij dostęp tylko do odczytu do bazy danych bez ryzyka iniekcji.

Bezpieczne udostępnianie zapytań SQL to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej MCP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MCP Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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. 🎯

Często zadawane pytania

Czy lekcja „Bezpieczne udostępnianie zapytań SQL” jest bezpłatna?

Tak — pełny tekst „Bezpieczne udostępnianie zapytań SQL” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu MCP Academy, przejdź na CoddyKit PRO. Kurs MCP Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Bezpieczne udostępnianie zapytań SQL”?

Udostępnij dostęp tylko do odczytu do bazy danych bez ryzyka iniekcji. Ćwiczysz MCP Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć MCP Academy?

Nie wymagamy żadnego doświadczenia. MCP Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „Bezpieczne udostępnianie zapytań SQL”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji MCP Academy?

Tak. Każda lekcja MCP Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Bezpieczne udostępnianie zapytań SQL
  2. Opakowywanie API REST jako narzędzi
  3. Pule połączeń w Lifespan
  4. Buforowanie i ograniczanie przepustowości usług nadrzędnych
← Powrót do MCP Academy