FRESH DEALS: KVM VPS PROMOS NOW AVAILABLE IN SELECT LOCATIONS!

DediRock is Waging War On High Prices Sign Up Now

The Ultimate Guide to MCP Server in Python: Insights and Tips I Wish I Knew from the Start

As a developer working with Large Language Models (LLMs), I recently immersed myself in setting up Model Context Protocol (MCP) Servers. Initially, I struggled to comprehend the concept, build a server, and understand how it could streamline my workflow. Many developers face difficulties integrating LLMs with external tools and data sources, which limits their capabilities beyond simple text generation.

After spending considerable time reading documentation and experimenting, I successfully created several custom servers. These servers now allow for various tasks, such as querying databases, sending emails and SMS, and deploying applications to DigitalOcean—seamlessly integrated within Cursor and Claude Desktop. This integration solves the challenge of LLMs being constrained to text generation by providing a standardized way to link them with external tools and data sources, ultimately enhancing their usefulness.

Note: For a broader overview, see DigitalOcean’s MCP 101: An Introduction to Model Context Protocol guide.

What You’ll Learn

  • Why MCP matters.
  • How to set up a Python MCP server.
  • How to connect it to Cursor and Claude Desktop.
  • How to write a simple SQLite query tool.
  • How to test everything from start to finish.

Prerequisites

Before diving in, ensure you have:

  • Python 3.7+ installed on your system.
  • SQLite with a community.db file.
  • Cursor Pro and Claude Desktop.
  • A terminal (macOS/Linux) or PowerShell/CMD (Windows).

What Is MCP and Why Do We Need It?

LLMs like GPT or Claude excel at generating text, but ultimately, they only predict the next token in a sequence. By themselves, they cannot fetch files or trigger real-world actions; they need a link to interact with the outside world.

The Model Context Protocol (MCP) standardizes how applications provide context to LLMs. Think of it as a USB-C port, offering a universal connection for AI applications to access data sources and external tools. In essence, MCP empowers LLMs to perform tasks beyond conversation—they can engage with external data and execute actions.

How the Pieces Fit

When chatting with an LLM in applications like Cursor or Claude Desktop, those applications serve as the host—the interface that users interact with. Each host contains an MCP client, responsible for forwarding requests to MCP servers.

The LLM alone cannot query a SQLite database, send an email, or call an API. That’s where the MCP server comes into play. To summarize how it works:

  1. You send a request to the LLM (e.g., "List the top chatters.").
  2. The LLM checks for the availability of an MCP tool for that request.
  3. If a relevant tool exists, the MCP client forwards the request over the MCP protocol to the MCP server.
  4. The MCP server completes the request, either by querying a local database or reaching out to remote services.
  5. It returns the results to the MCP client, which passes them to the LLM, which formats and displays the information to you.

In short, the host provides the interface, the client routes requests, and the server performs the heavy lifting—connecting AI language capabilities to real-world actions.

Building Your First MCP Server

Let’s create a local MCP Server in Python that queries a SQLite database for the top chatters in a community.

Step 1 – Set Up Your Environment:

  • Create a virtual environment using:

    python -m venv mcp-envsource mcp-env/bin/activate  # For Windows: mcp-envScriptsactivate
  • Install the MCP Python SDK:

    pip install mcp

Step 2 – Grab the Sample Database:Download community.db, which contains a chatters table with sample data.

Step 3: Write Your MCP Server:Create a file named sqlite-server.py and use the following code:

# sqlite-server.pyfrom mcp.server.fastmcp import FastMCPimport sqlite3# Initialize the MCP server with a friendly namemcp = FastMCP("Community Chatters")# Define a tool to fetch the top chatters from the SQLite database@mcp.tool()def get_top_chatters():    """Retrieve the top chatters sorted by number of messages."""    conn = sqlite3.connect('community.db')    cursor = conn.cursor()    cursor.execute("SELECT name, messages FROM chatters ORDER BY messages DESC")    results = cursor.fetchall()    conn.close()        chatters = [{"name": name, "messages": messages} for name, messages in results]    return chatters# Run the MCP server locallyif __name__ == '__main__':    mcp.run()

This basic server defines a tool named get_top_chatters, which connects to your SQLite database, retrieves sorted data, and returns it in an accessible format.

Adding Your MCP Server to Cursor

To integrate your MCP Server with Cursor:

  1. Open Cursor → Settings → MCP.
  2. Click “Add a New Global MCP Server” to open the MCP server configuration file.
  3. Update the file with your server details and save changes.

Testing Your MCP Server in Cursor

With your server added, open a chat in Cursor and ask a question (e.g., “How many chatters are in the database?”). If everything is set up correctly, you should see the number of chatters displayed alongside their names and message counts.

Adding Your MCP Server to Claude Desktop

To integrate your MCP Server with Claude Desktop:

  1. Open Claude Desktop → Settings → Developer → Edit Config.
  2. Add the same server block to claude_desktop_config.json, ensuring to save and refresh Claude Desktop.

Testing Your MCP Server in Claude Desktop

Open a chat and ask a question like “Show me the list of top chatters.” Approve the prompt when it requests to run the MCP tool, and check the output in Claude Desktop.

FAQs

  • What is the purpose of the MCP Server?
    The MCP Server queries the SQLite database to provide information about chatters, enhancing user experience.

  • How do I integrate my MCP Server with Claude Desktop?
    Add the server block to claude_desktop_config.json and restart the application for the changes to take effect.

  • Can I use this tutorial as a starting point for advanced MCP applications?
    Yes, the tutorial provides a foundation for many advanced applications, including email and SMS notifications and cloud deployments.

Conclusion

In this tutorial, we learned about the MCP—a standardized interface bridging LLMs with external data sources. You gained hands-on experience in setting up a virtual environment, using a pre-existing database, and creating a simple MCP server. This marks the beginning of your journey into MCP development, where the potential for innovation is limitless. Happy coding!


Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.

Share this Post

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Search

Categories

Tags

0
Would love your thoughts, please comment.x
()
x