Build a Custom AI Tool: Your First MCP Server with Python Explained in 5 Minutes
In this article, we are going to build a very basic MCP server using Python. MCP, which stands for Model-Context-Protocol, is a standardized way to give your language model access to external tools.
What is an MCP Server?
An MCP server is a back-end system that exposes real-world tools or functions that a language model—like GPT, Mistral, or Claude—can call during a conversation. It acts as a bridge between a language model and real-world actions. This guide will walk you through building a basic server from scratch using Python and connecting it to the Claude desktop application.
For this demonstration, we'll keep it simple. We will build a tool that takes a query, automatically searches for it on Google, and opens the results in your default browser.
Prerequisites
Before we begin, ensure you have the following installed:
- Claude Desktop App: We will be adding our custom tool to the Claude app.
- UV: A fast, modern Python package manager. UV is a drop-in replacement for tools like
pip
andvirtualenv
. Make sure you have it installed, as this tutorial relies on it for managing our project.
Setting Up the MCP Server: A Step-by-Step Guide
We will follow the official MCP Python SDK documentation to set up the server.
Step 1: Create the Project Folder
First, create a project folder and open it in your preferred code editor, such as Visual Studio Code. Once the folder is created, open a terminal within that directory.
For this guide, the folder is named mcp-server
.
Step 2: Initialize the Python Project
Run the following command in your terminal to initialize a new Python project. This will set up a virtual environment, specify the Python version, and create project dependency files.
uv init
Step 3: Add Project Dependencies
Next, we need to add the MCP Python package to our project. Run this command to install the necessary dependencies using UV. This is similar to pip install
but also updates your pyproject.toml
file with the new dependency.
This command installs the core MCP library along with extra packages required for command-line interface (CLI) support.
uv add mcp[cli]
Step 4: Implementation
After the installation, you will find a main.py
file. This is where we will add our functionality. We'll start with a default template from the MCP documentation and modify it.
Here is the code for our Google Search tool:
import webbrowser
from urllib.parse import quote_plus
from mcp import FastMCP
# FastMCP is a class that wraps FastAPI to make building MCP servers easy.
# The instance is named 'Google Search', which will appear in your toolset.
app = FastMCP(
"Google Search",
description="A tool to search Google.",
)
@app.tool()
def open_google_search(query: str):
"""
Searches the query in Google and opens the result in the default browser.
This docstring serves as the description of the tool, helping the language model
understand when and how to use it.
"""
search_url = f"https://www.google.com/search?q={quote_plus(query)}"
webbrowser.open(search_url)
return f"Successfully opened Google search for: {query}"
Code Breakdown:
-
FastMCP("Google Search", ...)
: This creates an MCP server instance named "Google Search." This is the name that will show up in your toolset in applications like Claude. -
@app.tool()
: This decorator informs the MCP server that theopen_google_search
function is a tool a language model can call. -
open_google_search(query: str)
: This is our custom function. It takes aquery
string as an argument, constructs a Google search URL, and uses thewebbrowser
library to open the result in the default browser. - Docstring: The docstring inside the function is crucial. It acts as the tool's description, which helps the language model understand its purpose and how to use it effectively.
Adding the Tool to Your Claude Application
To add the newly created tool, run the following command in your terminal. Since our file is named main.py
, the command is:
uv run mcp install main.py
If you had renamed your file to server.py
, the command would be uv run mcp install server.py
.
After running this command, the tool should automatically be added to your Claude app and appear in the tool list.
Troubleshooting: What If the Tool Doesn't Appear?
Sometimes, the tool may not show up immediately. To fix this, you may need to configure the absolute path to uv
.
- Open the Claude app and navigate to File > Settings.
- Click on the Developer tab and then click Edit Config.
- This will open a
JSON
configuration file. Locate thecommand
section.
The configuration might look something like this:
{
"command": [
"uv",
"run",
"mcp",
"install",
"/path/to/your/main.py"
]
}
- Replace
"uv"
with the absolute path to youruv
executable.
To find the absolute path of uv
, you can use one of these commands:
* On macOS/Linux: which uv
* On Windows: where uv
In my system, the absolute path is /home/user/.local/bin/uv
. The updated configuration would be:
{
"command": [
"/home/user/.local/bin/uv",
"run",
"mcp",
"install",
"/path/to/your/main.py"
]
}
Note for Windows Users: When adding the path in the configuration file, make sure to use double backslashes (\\
). For example: C:\\Users\\YourUser\\...\\uv.exe
.
- Save the file and restart the Claude app. It's best to forcefully stop all Claude instances using your system's Task Manager or Activity Monitor to ensure a clean restart.
After restarting, open the Claude app, go to Settings > Developer, and your tool should now appear in a "running" state.
Testing the Tool
Now you can test your new tool directly in the Claude app. Try a prompt like:
Search in Google with what is machine learning
The app should use your MCP tool, and a new browser tab will open with the Google search results for "what is machine learning."
That's it! You have successfully created a basic MCP tool using an MCP server. You can build more complex functionalities on top of this foundation to create a variety of powerful AI-integrated tools.
Join the 10xdev Community
Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.