Learn MCP Servers with Python (EASY)

4 min read 1 day ago
Published on Mar 22, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

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.

  1. Install Python 3.10 or higher:

  2. Install UV Package Manager:

    • Open your terminal and run:
      pip install uv
      
  3. Create a virtual environment:

    • Navigate to your project directory and execute:
      uv create my_mcp_project
      cd my_mcp_project
      
  4. Activate the virtual environment:

    • On Windows:
      .\venv\Scripts\activate
      
    • On macOS/Linux:
      source venv/bin/activate
      

Step 2: Build the MCP Server

Next, you will build the MCP server using the FastMCP SDK.

  1. Install FastMCP:

    • Inside your activated virtual environment, run:
      pip install fastmcp
      
  2. Create the MCP server file:

    • Create a file named mcp_server.py in your project directory.
  3. Write the server code:

    • Add the following code snippet to mcp_server.py:
      from fastmcp import MCPServer
      
      server = MCPServer()
      server.run()
      
  4. Run your MCP server:

    • Execute the script with:
      python mcp_server.py
      

Step 3: Integrate Google Search API

To enhance your server's functionality, integrate the Google Search API for documentation searches.

  1. Set up Google Search API:

  2. Install the required packages:

    • Run the following command:
      pip install httpx beautifulsoup4
      
  3. 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
      

Step 4: Create Helper Functions

Create reusable helper functions for processing web content.

  1. 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
      

Step 5: Configure Claude Desktop and Claude Code

Set up your MCP server for integration with Claude Desktop and Claude Code.

  1. Install Claude Desktop:

    • Follow the installation instructions on the Claude Desktop website.
  2. 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.

  1. 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}")
      
  2. Implement timeouts:

    • Set timeouts for your HTTP requests to avoid long waits:
      async with httpx.AsyncClient(timeout=10) as client:
      

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!