This project integrates an SQLite database with the OpenAI GPT-3.5 model using the FastMCP server framework. The main objective is to allow users to query a database through the OpenAI model using natural language.
With this setup, the MCP (Model-Controlled Prompting) server allows users to run SQL queries on an SQLite database via conversational AI prompts, interacting with the data seamlessly and in real-time.
- Connects to an SQLite database.
- Provides tools to query the database schema and run read-only SQL queries.
- Integrates the OpenAI GPT-3.5 model for natural language understanding of database queries.
- Uses the FastMCP framework for easy model-tool interaction.
- Retrieves database schema information and executes SQL queries based on user input.
- Python 3.7+
- OpenAI API Key (Get it from OpenAI)
- SQLite database (.db file) to query.
Clone the project to your local machine:
git clone https://github.com/Sakushal/MCP_OpenAI_Database-Integration.git
cd MCP_OpenAI_Database-IntegrationCreate a virtual environment and activate it:
python3 -m venv .venv
source .venv/bin/activate # On Linux/MacOS
.venv\Scripts\activate # On WindowsInstall the necessary dependencies:
pip install -r requirements.txtCreate a .env file in the root of the project directory and add the following content:
OPENROUTER_API_KEY=your-openai-api-key-hereReplace 'your-openai-api-key-here' with your actual OpenAI API key.
Run the program using:
python mcp_server1.py
When you run the program, you will be prompted to enter a natural language related to the database. The OpenAI model will interpret your query and use the tools available to fetch or query the SQLite database.
"Show me all users who are older than 30 years"This will generate the appropriate SQL query and return the results from the database.
MCP_OpenAI_Database-Integration/
├── .venv/
├── .gitignore
├── .env
├── .py files
├── .csv files and .db
├── requirements.txt
└── README.md