Leverages LangChain to build the client agent that translates natural language prompts into appropriate database tool calls.
Utilizes LangGraph's React agent framework to orchestrate the reasoning process when determining which database operations to perform.
Provides tools to query an existing SQLite database, including listing tables, retrieving table schemas, counting rows, and executing read-only SQL SELECT queries.
SQLite MCP Server and Client
This project implements an MCP (Model Context Protocol) server and client for interacting with a SQLite database. The server provides tools to query an existing SQLite database, while the client, built with Chainlit and LangGraph, allows users to send natural language prompts to perform database operations via a web-based chat interface.
Project Overview
- MCP Server (
mcp_sqlite_server.py
): A server using theFastMCP
library that exposes four tools to interact with a SQLite database (example.db
):list_tables
: Lists all tables in the database.get_table_schema
: Retrieves column names and data types for a specified table.count_rows
: Counts the number of rows in a specified table.execute_query
: Executes read-only SQL SELECT queries and returns results.
- MCP Client (
mcp_client_chainlit.py
): A Chainlit-based client using LangGraph'screate_react_agent
and thelangchain-mcp-adapters
library to connect to the server. Users can input natural language prompts (e.g., "List all tables" or "Show me the first 5 rows of the users table"), and the agent dynamically calls the server's tools to generate responses. - Transport: The server uses
streamable-http
transport (HTTP with Server-Sent Events) for JSON-RPC 2.0 communication, running athttp://localhost:8080/mcp
. - Security: The server restricts queries to SELECT statements and validates table names to prevent SQL injection.
Prerequisites
- Python: Version 3.8 or higher.
- SQLite Database: An existing
example.db
file in the project directory with data (e.g., tables likeusers
ororders
). - API Key: An Anthropic API key for the client (or another LLM provider like OpenAI if preferred).
Dependencies
The project requires the following Python packages, listed in requirements.txt
:
Setup Instructions
- Clone the Repository (if applicable):
- Create and Activate a Virtual Environment:
- Install Dependencies:
- Set Environment Variables:
- Configure the Anthropic API key for the client:
- If using another LLM (e.g., OpenAI), set the appropriate API key:and update
mcp_client_chainlit.py
to uselangchain-openai
instead oflangchain-anthropic
.
- Configure the Anthropic API key for the client:
- Prepare the SQLite Database:
- Ensure an
example.db
file exists in the project directory with the desired tables and data. - The server does not initialize or modify the database; it assumes the database is pre-populated.
- Ensure an
Running the Project
- Start the MCP Server:
- Run the server script to expose the SQLite database tools:
- The server will start at
http://localhost:8080/mcp
usingstreamable-http
transport. - Verify it’s running by checking the terminal for log messages (e.g., "Starting MCP server with streamable-http transport...").
- Run the server script to expose the SQLite database tools:
- Start the Chainlit Client:
- Run the client script to launch the web-based chat interface:
- Open a browser and navigate to
http://localhost:8001
(or the port shown in the terminal). - The Chainlit UI will display a welcome message: "Welcome to the SQLite MCP Client! Ask about the database..."
- Run the client script to launch the web-based chat interface:
Usage
- Interact via Chainlit UI:
- In the Chainlit interface (
http://localhost:8001
), enter natural language prompts to query the database. - Example prompts:
- "List all tables in the database" →
["users", "orders"]
- "What is the schema of the users table?" →
[{"column_name": "id", "data_type": "INTEGER"}, {"column_name": "name", "data_type": "TEXT"}]
- "How many rows are in the orders table?" →
42
(example) - "Run SELECT name FROM users WHERE id = 1" →
Results: name = Alice
- "List all tables in the database" →
- The LangGraph agent will use the MCP server's tools to process the prompt and return a formatted response.
- In the Chainlit interface (
- Expected Behavior:
- The client sends prompts to the LangGraph agent, which decides which tools to call (e.g.,
list_tables
,execute_query
). - The
MultiServerMCPClient
communicates with the server athttp://localhost:8080/mcp
to execute tool requests. - Responses are displayed in the Chainlit chat interface.
- The client sends prompts to the LangGraph agent, which decides which tools to call (e.g.,
Example Interactions
- Prompt: "Show the schema and row count for the users table."
- Agent Process:
- Calls
get_table_schema("users")
→[{"column_name": "id", "data_type": "INTEGER"}, {"column_name": "name", "data_type": "TEXT"}]
- Calls
count_rows("users")
→10
(example) - Response: "The users table has columns: id (INTEGER), name (TEXT). It contains 10 rows."
- Calls
- Agent Process:
- Prompt: "Get all names from the users table."
- Agent Process:
- Calls
execute_query("SELECT name FROM users")
→{"columns": ["name"], "rows": [["Alice"], ["Bob"]]}
(example) - Response: "Names in the users table: Alice, Bob"
- Calls
- Agent Process:
Troubleshooting
- Server Not Running: Ensure
mcp_sqlite_server.py
is running and accessible athttp://localhost:8080/mcp
. Check terminal logs for errors. - Client Errors:
- If the Chainlit UI fails to load, verify the port (
8001
) is not in use. - If the agent fails to connect to the server, check the
MCP_SERVER_CONFIG
URL inmcp_client_chainlit.py
.
- If the Chainlit UI fails to load, verify the port (
- LLM Issues: Ensure the Anthropic API key is valid. For alternative LLMs, install the appropriate package (e.g.,
langchain-openai
) and update the client script. - Database Errors: Confirm
example.db
exists and is readable. Check server logs (loguru
) for SQL errors.
Notes
- Security: The server restricts queries to SELECT statements and validates table names to prevent SQL injection. For production, consider adding OAuth 2.1 authentication.
- Extensibility: Add more tools to the server by defining new
@mcp.tool()
functions inmcp_sqlite_server.py
. The client will automatically discover them viaMultiServerMCPClient
. - LLM Flexibility: The client uses Anthropic's Claude by default. To use another LLM (e.g., OpenAI's GPT-4o), replace
langchain-anthropic
withlangchain-openai
inrequirements.txt
and update the LLM import inmcp_client_chainlit.py
. - Dependencies: Check for version compatibility in
requirements.txt
. Update versions if newer releases are available.
Contributing
Contributions are welcome! To add features or fix bugs:
- Fork the repository.
- Create a branch (
git checkout -b feature/your-feature
). - Commit changes (
git commit -m "Add your feature"
). - Push to the branch (
git push origin feature/your-feature
). - Open a pull request.
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
Implements a Model Context Protocol server that enables natural language interactions with SQLite databases, providing tools to list tables, retrieve schemas, count rows, and execute read-only SQL queries.
Related MCP Servers
- AsecurityAlicenseAqualityA Model Context Protocol server that provides database interaction capabilities through SQLite, enabling users to run SQL queries, analyze business data, and automatically generate business insight memos.Last updated -616TypeScriptMIT License
- -securityAlicense-qualityA Model Context Protocol server implementation that enables AI assistants to execute SQL queries and interact with SQLite databases through a structured interface.Last updated -7TypeScriptMIT License
- -securityFlicense-qualityA Model Context Protocol server that enables SQL operations (SELECT, INSERT, UPDATE, DELETE) and table management through a standardized interface with SQLite databases.Last updated -45JavaScript
- AsecurityAlicenseAqualityA Model Context Protocol server that allows users to store, retrieve, update, and delete memories using SQLite storage.Last updated -56PythonMIT License