Python MCP Server: Connect LLMs to Real-World Data (Step-by-Step Guide)
Want to extend your Large Language Models (LLMs) beyond basic text generation? Discover how to build a Python MCP server and seamlessly integrate it with tools like Cursor and Claude Desktop! This guide provides a hands-on approach to connecting your LLMs to external data sources, unlocking a world of new possibilities.
Why You Need a Model Context Protocol (MCP) Server
LLMs like GPT and Claude are powerful, but they can't inherently access external data or trigger actions in the real world. Think of it this way: they can write a great email, but they can't send it without help. That's where the Model Context Protocol (MCP) comes in. MCP acts as a universal adapter, allowing LLMs to interact with:
- Databases
- APIs
- Other external tools
An MCP server is the key component that handles these interactions, bridging the gap between the LLM's language capabilities and the world outside.
What You'll Achieve: Building a Simple SQLite Query Tool
In this tutorial, you'll build a functional MCP server that queries a SQLite database and returns results to your LLM client. Specifically, you'll create a server that retrieves the top chatters from a community database. You'll learn how to:
- Set up a Python MCP server.
- Connect this server to both Cursor and Claude Desktop.
- Write a simple SQLite query tool to fetch data.
- Test your setup end-to-end.
Prerequisites: Getting Ready to Code
Before diving in, ensure you have the following:
- Python 3.7+ installed.
- SQLite (with a
community.db
file — we'll provide a sample). - Cursor Pro and Claude Desktop installed.
- A terminal (macOS/Linux) or PowerShell/CMD (Windows).
Understanding the MCP Architecture
The MCP architecture consists of three key components:
- Host: The application you interact with (e.g., Cursor, Claude Desktop).
- Client: The agent within the host that speaks the MCP protocol and forwards requests.
- Server: Your custom server that performs the actual data retrieval or action.
Here's how the process works step by step:
- You send a request to the LLM in your chosen host application.
- The LLM client analyzes the request and verifies whether an MCP tool exists for the task.
- If a fitting tool exists (like our SQLite query tool), the client forwards the request to your Python MCP server via the MCP protocol.
- The MCP server then performs the requested task, such as querying your local SQLite database.
- The server sends the results back to the client who hands it back to the LLM.
- Finally, the LLM formats the results and presents them to you in a readable format within the host application.
Step 1: Setting Up Your Python Environment
-
Create a Virtual Environment:
-
Install the MCP Python SDK:
Step 2: Obtaining the Sample Database
Download the community.db
file, containing a chatters
table with sample data. This will serve as your data source for the MCP server.
Step 3: Crafting Your Python MCP Server (sqlite-server.py
)
Create a new file named sqlite-server.py
and paste the following code:
This code defines a single tool, get_top_chatters
, which connects to your SQLite database, retrieves the data, and returns it in a structured format. This is a simple example of a SQLite query tool that can be triggered by Cursor and Claude.
Adding Your MCP Server to Cursor (Pro Required)
-
Open Cursor → Settings → MCP (requires Cursor Pro).
-
Click "Add a New Global MCP Server." This opens the MCP server configuration file (
~/.cursor/mcp.json
). -
Update the file with your server details:
Replace
/path/to/your/project/
with the actual path to your project directory. -
Save the file and return to MCP Settings. You should see your server listed with a green dot indicating it's active.
Testing Your MCP Server in Cursor
- Open a chat in Cursor and ask: "How many chatters are in the database?".
- Cursor will recognize that it needs an external tool.
- Grant permission when prompted to run the tool.
- The MCP server will query the database and the AI will present the information in the chat, including the number of chatters, names and message counts.
Integrating with Claude Desktop
-
Open Claude Desktop → Settings → Developer → Edit Config.
-
Add the same server block to
claude_desktop_config.json
: -
Save, close, and reopen Claude Desktop to apply the changes.
-
Verify the MCP server is listed in Claude Desktop's settings. A tool icon in the chat window indicates external tools are connected.
Testing Your MCP Server in Claude Desktop
- Open a chat in Claude Desktop and ask "Show me the list of top chatters".
- Claude Desktop will recognize the need for an external tool.
- Approve the prompt to run the MCP tool.
- You will receive an output that includes the names and number of chatters in the community database.
Now you’ve successfully connected Claude Desktop and Cursor to your MCP server enabling both GPT and Claude to leverage external data for responses.
Frequently Asked Questions
- What does this MCP server do? The MCP server queries a SQLite database to find statistics about community members and reports it the client application.
- How do I integrate my MCP Server with Claude Desktop? You can integrate the MCP server by adding the server block to
claude_desktop_config.json
and re-opening Claude Desktop. - Can I expand this tutorial to build bigger things? Absolutely, you can expand this basic application to email or SMS capabilities.
Conclusion: Your First Step into the MCP Ecosystem
You've successfully built a basic Python MCP server and integrated it with leading LLM platforms. This opens the door to a world of possibilities, enabling your LLMs to:
- Access and analyze real-world data.
- Trigger actions and automate tasks.
- Become more intelligent and versatile.
Continue exploring the MCP ecosystem and build even more advanced applications!