Learn MCP Servers with Python (EASY)
Table of Contents
Introduction
In this tutorial, you will learn how to create a Model Context Protocol (MCP) server using Python. This guide will walk you through setting up your development environment, building the server, integrating it with Google Search API, and configuring it for use with Claude Desktop and Claude Code. By the end, you will have a functional MCP server tailored for AI applications.
Step 1: Set Up the Development Environment
To begin, you will need to establish a modern Python development environment.
-
Install Python 3.10 or higher:
- Download and install the latest version from the official Python website.
-
Install UV Package Manager:
- Open your terminal and run:
pip install uv
- Open your terminal and run:
-
Create a virtual environment:
- Navigate to your project directory and execute:
uv create my_mcp_project cd my_mcp_project
- Navigate to your project directory and execute:
-
Activate the virtual environment:
- On Windows:
.\venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
Step 2: Build the MCP Server
Next, you will build the MCP server using the FastMCP SDK.
-
Install FastMCP:
- Inside your activated virtual environment, run:
pip install fastmcp
- Inside your activated virtual environment, run:
-
Create the MCP server file:
- Create a file named
mcp_server.py
in your project directory.
- Create a file named
-
Write the server code:
- Add the following code snippet to
mcp_server.py
:from fastmcp import MCPServer server = MCPServer() server.run()
- Add the following code snippet to
-
Run your MCP server:
- Execute the script with:
python mcp_server.py
- Execute the script with:
Step 3: Integrate Google Search API
To enhance your server's functionality, integrate the Google Search API for documentation searches.
-
Set up Google Search API:
- Sign up for an API key from the Google Serper API.
-
Install the required packages:
- Run the following command:
pip install httpx beautifulsoup4
- Run the following command:
-
Add Google Search function:
- Modify
mcp_server.py
to include:import httpx from bs4 import BeautifulSoup async def fetch_search_results(query): async with httpx.AsyncClient() as client: response = await client.get(f'https://api.serper.dev/search?q={query}&api_key=YOUR_API_KEY') return response.json() # Implement this function in your existing server code
- Modify
Step 4: Create Helper Functions
Create reusable helper functions for processing web content.
- Add content parsing:
- Include a function to parse the search results:
def parse_results(data): results = [] for item in data['organic']: results.append({'title': item['title'], 'link': item['link']}) return results
- Include a function to parse the search results:
Step 5: Configure Claude Desktop and Claude Code
Set up your MCP server for integration with Claude Desktop and Claude Code.
-
Install Claude Desktop:
- Follow the installation instructions on the Claude Desktop website.
-
Configure the connection:
- Ensure your MCP server is running and accessible from Claude Desktop. Adjust the settings within Claude to point to your server’s local address.
Step 6: Handle Async Operations and Error Management
Implement proper error handling and manage asynchronous operations effectively.
-
Add error handling to your API calls:
- Use try-except blocks around your API requests:
try: results = await fetch_search_results(query) except Exception as e: print(f"Error fetching results: {e}")
- Use try-except blocks around your API requests:
-
Implement timeouts:
- Set timeouts for your HTTP requests to avoid long waits:
async with httpx.AsyncClient(timeout=10) as client:
- Set timeouts for your HTTP requests to avoid long waits:
Conclusion
You have now successfully set up a functional MCP server using Python, integrated it with the Google Search API, and configured it for use with Claude Desktop. Here are some key takeaways:
- Ensure your development environment is correctly set up.
- Familiarize yourself with FastMCP and how to handle async operations in Python.
- Test your server thoroughly to ensure all integrations work as expected.
Next steps may include expanding your server's capabilities or exploring additional APIs to enhance its functionality. Happy coding!