mysql-mcp-demo
Provides a read-write MCP interface to a MySQL database, enabling tools to query tables, inspect schema and relationships, and execute data or schema changes such as INSERT, UPDATE, DELETE, and ALTER.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@mysql-mcp-demowhat's the schema of the orders table?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
mysql-mcp-demo
A small, heavily-commented MCP server for MySQL that demonstrates all three Model Context Protocol primitives — tools, resources, and prompts — in about 1,100 lines of Python.
This repository exists to be read, not just run. It is the companion to a workshop on building MCP servers, and every file is written as teaching material: one primitive per file, comments that explain why rather than what, and a demo database with deliberate flaws so the examples find something real.
mcp_server/
├── database.py read-only introspection — the only file not about MCP
├── execution.py running queries and writes, plus every safety control
├── tools.py 6 TOOLS — inspect structure (cannot read or change a row)
├── data_tools.py 6 TOOLS — read rows, and INSERT / UPDATE / DELETE / ALTER
├── resources.py 4 RESOURCES + 2 templates — content the APPLICATION attaches
├── prompts.py 6 PROMPTS — workflows the USER invokes
└── server.py wires them together (about 10 meaningful lines)The server is read-write: it answers questions about the data by running
real queries, and it can change data and schema. It is locked to a single
throwaway demo database, and the controls that make that safe are in
execution.py and explained below — that design is itself part of the lesson.
The one idea worth taking away
Most MCP tutorials only cover tools, which leaves people thinking MCP is tools. It is three primitives, and they differ by who is in control:
Primitive | Who decides | When it happens | Analogy |
Tool | the model | mid-conversation, autonomously | a function the model may call |
Resource | the application | up front, chosen by a human | a file you attach |
Prompt | the user | explicitly, from a menu | a saved expert question |
Same data can appear as more than one. In this repo get_table_ddl is a tool
and schema://table/{name}/ddl is a resource — the same bytes, reached two
ways, because "the model fetches it when it needs it" and "the human attaches it
before starting" are genuinely different needs.
Quick start
git clone https://github.com/Khushboo-Mishra/mysql-mcp-demo.git
cd mysql-mcp-demo
bash scripts/setup.shsetup.sh checks prerequisites, creates the virtualenv, installs the two
dependencies, creates the demo database, and verifies the server end to end. It
stops with a specific message at the first thing that is missing.
Then see all three primitives in one pass:
bash scripts/run_explorer.shRequirements
Python 3.10+
MySQL 8.x running locally (
brew services start mysql)Node.js — optional, only for the MCP Inspector
Defaults to root on 127.0.0.1:3306 with no password — the Homebrew default,
so most people change nothing. Otherwise export MYSQL_USER, MYSQL_PASSWORD,
MYSQL_HOST, MYSQL_PORT.
What gets built
12 tools, 4 resources + 2 URI templates, and 6 prompts, over a six-table demo database.
Tools — the model calls these
Split across two files by blast radius, not by subsystem. That is a deliberate design choice worth copying: it keeps the risky surface small and obvious to anyone reviewing the server or writing its database GRANT.
tools.py — inspect structure. Cannot read a row, cannot change anything.
Tool | Purpose |
| every table and view, with row estimates |
| columns, types, keys, indexes, foreign keys |
| the exact |
| every declared foreign key |
| columns whose name suggests PII or secrets |
| find a column when you forget which table it is in |
data_tools.py — read rows and change data. This is the half with consequences.
Tool | Purpose |
| run a SELECT and get the rows — this is what answers data questions |
| INSERT / UPDATE / DELETE / CREATE / ALTER / DROP / TRUNCATE |
| structured insert, values sent as bound parameters |
| structured update, |
| structured delete, |
| every statement the server has executed |
Why both a general execute_statement and structured wrappers? Structured
tools are safer — arguments are typed and values are bound, so the model never
writes SQL text and cannot produce something malformed. But they only do what
you anticipated. A general SQL door handles the long tail: window functions,
an ALTER you did not foresee. Real servers ship both, and the walkthrough
should say why.
Resources — the application attaches these
URI | Type | Contents |
| JSON | table inventory |
| SQL | DDL for the whole schema |
| JSON | all foreign keys |
| Markdown | human-readable summary |
| JSON | one table — templated |
| SQL | one table's DDL — templated |
A static resource has a fixed URI and appears in resources/list, so a
client can show it in a picker. A templated resource has {placeholders}
and appears in resources/templates/list instead — there is no fixed list, so
the client fills in the blank.
Prompts — the user invokes these
Prompt | Arguments | What it does |
| — | five-step health check: keys, relationships, PII, naming |
|
| explains one table in plain language |
|
| writes the query, runs it, and answers in plain language |
|
| preview → confirm → apply → verify, for changes |
| — | generates reference documentation |
|
| a guided first look, tailored to a role |
Deciding: tool, resource, or prompt?
The question people get stuck on. Work through it in this order.
1. Does it perform an action, or fetch something the model chooses? → Tool. Anything the model should be able to decide to do on its own.
2. Is it a document a human would sensibly attach before starting? → Resource. Reference material, whole-schema context, anything stable.
3. Is it a task someone repeats, where the way you ask is the expertise? → Prompt. Ship the good question instead of expecting rediscovery.
Two heuristics that resolve most remaining doubt:
Who initiates? Model → tool. Application → resource. User → prompt.
Would you want this in a menu? If yes, it is a prompt. Menus are for people, and only prompts are surfaced to people as commands.
Worked examples from this repo
Feature | Choice | Why |
Fetch one table's structure | tool | the model needs it mid-reasoning, unpredictably |
Whole-schema DDL | both | tool for the model; resource for a human to attach up front |
Schema audit | prompt | a repeatable task where knowing what to ask is the value |
Search for a column | tool | takes an argument the model chooses at call time |
Markdown overview | resource | passive reference, no decision required |
Where people get it wrong
Everything as tools. Works, but the model burns calls fetching context a human could have attached once — and users get no discoverable entry points.
Resources for things that need arguments the model picks. If the model decides the parameter, it is a tool.
Prompts that do work. A prompt returns text. If you find yourself querying the database inside a prompt, you wanted a tool.
The demo database
mcp_demo, six tables, deliberately imperfect so the examples find real
problems:
Table | Deliberate flaw |
|
|
|
|
| (clean — the reference example) |
|
|
| no primary key at all |
|
|
Run audit_schema against it and every one of those should surface. That is the
demo: the tools find genuine problems, not toy ones.
Running it
The explorer — every primitive in one pass
bash scripts/run_explorer.shPrints the initialize handshake, then lists and exercises tools, resources
(static and templated), and prompts. Best first thing to run, and the clearest
thing to show on a terminal during a talk.
The MCP Inspector — Anthropic's own client
bash scripts/run_inspector.shOpen the printed http://localhost:6274?... URL — the token is required. It has
separate Tools, Resources, and Prompts tabs, which is the most
convincing way to show all three: none of it is our code, so if the Inspector
drives the server, the server is genuinely spec-compliant.
Suggested tour: Tools → describe_table with ORDERS; Resources →
schema://overview; Prompts → audit_schema.
Claude Desktop / Claude Code
bash scripts/install_claude.sh # Claude Code
bash scripts/install_claude.sh --desktop # also Claude DesktopThen ask: "Audit this database" — or use the audit_schema prompt from the
menu, which is where prompts finally become visible.
--desktopmust be run from Terminal.app, not from inside Claude Desktop. Claude Desktop holds its config in memory and rewrites the file from that copy, so an edit made while it is running is silently discarded. The script quits the app, edits, and relaunches — which would kill the session you launched it from.
Code walkthrough order
For presenting, this order builds up cleanly:
server.py— 10 lines. The whole architecture in one screen.database.py— plain MySQL, no MCP. Establishes that MCP is a thin layer over code you already have. Stop onsafe_identifierand explain why table names cannot be bound parameters.tools.py— the decorator, and how the docstring is the prompt the model reads.resources.py— static vs templated URIs, and whyget_table_ddlis deliberately duplicated as a resource.prompts.py— that a prompt returns text, and that the text tells the model which tools to use.examples/explore_server.py— the client side, showing what actually crosses the wire.
Going further
This server is scoped to one database to keep the examples short. To take it further:
Multiple schemas — take
schemaas a tool argument rather than readingMYSQL_DEMO_SCHEMA. Add an allowlist so an agent cannot reach production.Query execution — a
run_querytool. Doable, but it changes the security story completely: the server then needs credentials that read your tables, and results enter the model's context. EnforceSELECT-only, inject aLIMIT, and use a read-only database user.Remote transport —
mcp.run(transport="streamable-http"). Same tools, same code, different pipe. Add authentication before exposing it.Caching —
describe_tablehits the database on every call. A short TTL cache is worth it once a model starts calling it in a loop.
Security notes
This server can change your data. That is a deliberate choice for a workshop — showing how to build write capability safely is more useful than pretending the question never comes up — but it means the controls matter.
The five controls, all in execution.py
Control | What it stops |
Schema lock | every statement runs on a connection pinned to the demo database; a reference to any other database is refused |
One statement per call | a second statement cannot ride along on a legitimate one |
Separate read/write doors |
|
Row cap | a broad |
Audit log | every statement is recorded and readable via |
A denylist also refuses statements that would escape the schema lock, reach the filesystem, or change server-wide state — privilege changes, user management, file import/export, and database-level operations.
One subtlety worth showing in a walkthrough: the schema lock cannot work by
pattern alone, because a.b in SQL is usually alias.column (SELECT c.NAME FROM CUSTOMERS c), not schema.table. Rejecting every dotted name breaks
ordinary joins — which is exactly the bug the first version had. So it compares
each qualifier against the actual list of databases on the server: a real
database name is refused, a table alias passes untouched.
Point it at a restricted user
The controls above are defense in depth, not the defense. In anything beyond a demo, connect as a MySQL user whose grant covers only the schema you intend to expose. If the credentials cannot reach production, neither can a prompt-injection or a model mistake.
Two more things worth stating plainly:
Table names cannot be bound parameters.
SHOW CREATE TABLE %sis not valid SQL, so identifiers must be interpolated — a genuine injection sink.database.safe_identifieris what makes it safe, and it is the single most important function in the project.The connecting MySQL user is the real boundary. Give it a read-only
GRANTscoped to the schemas you mean to expose. The code's read-only-ness is defense in depth, not the defense.
License
MIT — see LICENSE.
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Connectors
Connect to PlanetScale databases, branches, schema, query insights, and execute SQL
MCP server for managing Prisma Postgres.
GibsonAI MCP server: manage your databases with natural language
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/Khushboo-Mishra/mysql-mcp-demo'
If you have feedback or need assistance with the MCP directory API, please join our Discord server