Python MCP Server Tutorial: Integrate LLMs with Real-World Data (Cursor & Claude)
Are you struggling to move past basic text generation with Large Language Models (LLMs)? Do you want to connect your LLMs to real-world data sources such as databases and APIs? This in-depth guide will walk you through building your own MCP (Model Context Protocol) server in Python and integrating it with tools like Cursor and Claude Desktop.
What You'll Achieve
- Understand the importance of MCP for LLM development.
- Create a Python MCP server to query a SQLite database.
- Integrate your MCP server with Cursor & Claude Desktop.
- Learn how to test your setup end-to-end.
Why is Model Context Protocol (MCP) Essential?
LLMs are great at generating text, but they lack the ability to access external information or perform actions on their own. They are limited to generating text based on their training data. The Model Context Protocol (MCP) acts as a universal adapter that enables LLMs to interact with external tools and data sources, such as:
- Databases
- APIs
- Local files
Think of MCP as a USB-C port for your AI – a standardized way to connect your LLMs to the outside world! With a Python MCP server, your LLMs can do more than just chat; they can take actions and provide contextually relevant information.
How MCP Works: A Step-by-Step Overview
- The User Request: You send a request to the LLM within a host application like Cursor or Claude Desktop (e.g., "List the top chatters").
- LLM Processing and Tool Check: The LLM processes your text. It determines if an MCP tool is available to fulfill the request.
- MCP Client Forwarding: If a suitable tool exists, the MCP client (part of the host application) forwards the request to your Python MCP server.
- Server-Side Magic: The MCP server takes over:
- It queries a local data source like your SQLite database.
- Or, it might call remote services (e.g., email/SMS APIs).
- Results Returned: The server sends the results back to the MCP client.
- LLM Presentation: Finally, the LLM formats and presents the information/result to you within the host application.
Essentially, the host provides the UI, the client manages requests, and the Python MCP server handles the heavy lifting of connecting the LLM to real-world data and actions.
Prerequisites
Before diving into the code, ensure you have the following:
- Python 3.7+ installed
- SQLite (with a
community.db
file containing chat data) - Cursor Pro and Claude Desktop installed
- A terminal (macOS/Linux) or PowerShell/CMD (Windows)
Step-by-Step: Building Your First Python MCP Server
Step 1: Set Up Your Development Environment
-
Create a Virtual Environment:
-
Install the MCP Python SDK:
Step 2: Get the Sample Database
Download the community.db
file (ensure it contains a chatters
table with sample data).
Step 3: Code Your Python MCP Server (sqlite-server.py
)
This code creates a simple MCP server with one tool (get_top_chatters
). It connects to your SQLite database, retrieves the top chatters, and returns the data in a usable format.
Integrating with Cursor IDE
- Open Cursor. Go to
Settings
->MCP
(requires Cursor Pro). - Click "Add a New Global MCP Server." This opens the
~/.cursor/mcp.json
file. - Update the file with the following configuration:
- Replace
/path/to/your/project/
with the actual path to your project directory.
- Save and return to MCP Settings. A green dot should indicate successful server and tool recognition.
Test Your Integration with Cursor
- Ask a question like, "How many chatters are in the database?" in the Cursor chat window.
- Cursor will recognize the need for an external tool.
- Approve the prompt to run the MCP tool.
- The MCP server will query the database, and Cursor will display the number of chatters and details.
Integrate with Claude Desktop
- Open Claude Desktop. Go to
Settings
->Developer
->Edit Config
. - Add the following server block to
claude_desktop_config.json
:
- Adjust the
command
andargs
based to your environment and where you savedserver.py
.
- Save, close, and reopen Claude Desktop to apply the changes.
Test Your Integration with Claude Desktop
- In Claude Desktop, ask "Show me the list of top chatters".
- Approve the prompt to run the MCP tool.
- Claude Desktop will display the results retrieved from your SQLite Database.
Frequently Asked Questions
- What if Claude or Cursor can't find my Python MCP server? Double check the path, activate your virtual environment and restart the host application.
- Can I extend the Python MCP server to other data source? Yes, just implement additional
@mcp.tool()
decorated methods to expose other functionality. - Where can I learn more about building MCP integrations? Start by reviewing the official MCP documentation.
Congratulations! You've built a Python MCP server and integrated it with both Cursor and Claude Desktop. Now you can enhance your LLM interactions with real-world data!