This project demonstrates how to use the Model Context Protocol (MCP) to create a natural language interface to SQLite databases. It's a practical introduction to building AI systems that can interact with external tools.
MCP is designed as a universal protocol that connects AI systems with data sources and tools using a standardized communication layer. As described by Anthropic:
"The Model Context Protocol is an open standard that enables developers to build secure, two-way connections between their data sources and AI-powered tools." anthropic.com It functions somewhat like an "Internet for Agents," providing a standardized way for AI systems to access external tools without needing custom integrations for each one.
This demo shows how an LLM (Groq's Llama3-70B) can execute SQL queries through MCP, without directly accessing the database.
- Make sure you have Python 3.12+ installed
- Set your Groq API key in the
.env
file:GROQ_API_KEY=your_api_key_here
- Install dependencies:
uv venv .venv --python=3.12 .venv\Scripts\activate uv pip install -e .
- Run the demo:
This will open two windows - an MCP server and an MCP client.
run_sql_demo.bat
In the client window, try natural language queries like:
- "all users" - The LLM converts this to a SQL query using MCP's tool interface
- "users older than 30" - Watch how complex queries get properly translated
- "average age of users" - See aggregation functions at work
- "add a new user named Alice Smith with email [email protected] and age 35" - Try data manipulation
Each query demonstrates how MCP enables an LLM to use external tools (SQL queries) safely and effectively.
-
MCP Server (mcp_server.py):
- Defines tools (like
query_data
) with schemas - Handles execution of SQL queries against a database
- Returns results in a structured format
- Defines tools (like
-
MCP Client (mcp_client.py):
- Takes natural language input from users
- Uses Groq's LLM to generate tool calls via MCP
- Handles the communication protocol with the server
The magic happens through MCP's standardized communication, allowing LLMs to use tools without accessing the underlying code.
mcp_server.py
: MCP server with SQL execution capabilitiesmcp_client.py
: MCP client for LLM-driven tool callingdatabase.db
: Sample SQLite database with user datarun_sql_demo.bat
: Script to run both MCP componentspyproject.toml
: Python project configuration.env
: Environment variables (API keys)