Haram@haram
AI Frontier

Creating Your Own MCP Server with Python — The Easiest Way to Integrate with Claude
Wouldn't it be convenient if you could give Claude your own Python code to act as a custom tool? With the Model Context Protocol (MCP), you can easily connect local files or scripts to the Claude Desktop app. Let's set aside complex theory for a moment and build your first functional MCP server in Python in under 5 minutes.
Why bother building an MCP server yourself?
While chatting with Claude, you might often think, "I really wish this thing could read my local files or run the automation scripts I wrote." An MCP server is the powerful tool that bridges that imagination with reality.
There is absolutely no need to build a massive system from scratch. You only need to write down your routine file organization scripts or simple data processing logic in Python. Once you launch the MCP server, your Python function transforms into a dedicated tool that executes directly from the Claude Desktop app with a single click.
In this article, we'll use the FastMCP library from Prefect, which helps beginners achieve this magical integration with just a few lines of code. Let's look at the most intuitive way to build your own AI assistant without needing to memorize complex API structures.
Ultra-lightweight server code in 5 minutes with FastMCP
In the past, there was a high barrier to entry because you had to write complex official SDK code manually. Recently, however, development has become surprisingly easy thanks to the FastMCP framework managed by Prefect. Now, you can give Claude powerful new capabilities with just a single Python function and zero tedious configuration.
The principle is simple: just add a decorator to the Python function you use every day. FastMCP automatically analyzes Python type hints and docstrings to convert them into a tool blueprint that Claude can understand and execute on its own.
Let's walk through this process using an ultra-lightweight tool server that adds two numbers. The following code is written based on FastMCP version 4.x.
Looking at the code, you'll see a type hint for integers next to the input arguments, and a clear description below. Based on this, FastMCP creates an API manual for Claude in real-time, telling it, "You need to send two numbers to use this tool."
A common mistake for beginners: Can't use print()?
When building an MCP server yourself, you'll often feel frustrated when code that worked perfectly fine stops functioning as soon as you connect it to Claude Desktop. The culprit is the print() function, which we habitually use for debugging.
A local MCP server communicates with Claude using Standard I/O (STDIO) by default. Claude is waiting to interpret clean, structured data sent by the server via standard output. If a normal print() accidentally gets mixed in, the message protocol becomes corrupted, causing an immediate disconnection.
Therefore, when you want to track progress or debug errors during development, you must use standard error (stderr). Let's briefly compare the correct and incorrect logging methods based on the Python standard library.
In a FastMCP environment, it also supports advanced features where you can receive a Context object in your tool function to print logs directly to the Claude app screen. However, even before using complex features, following the basic rule of not interfering with standard output flows will prevent over half of the MCP errors beginners face.
Connecting and testing with Claude Desktop
Now it's time to integrate the server you worked hard on into the actual Claude Desktop app. There are two ways to do this: using the auto-install command or editing the configuration file manually.
The fastest and easiest way is to use the auto-install command provided by FastMCP. Simply run the following command in your terminal to register it automatically.
When you run this command, FastMCP automatically registers our server in the Claude Desktop configuration file. If you prefer to register it manually, you can edit the file directly. The configuration file, claude_desktop_config.json, is located in the following path depending on your operating system.
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
Since Claude Desktop runs independently from your terminal environment, you must use an absolute path for both the Python executable and the server script when editing the configuration file to avoid errors. Use the example below.
If you want to visually test if your tool is working correctly before integrating it into the app, try using the mcp dev command. Running mcp dev server.py in the terminal will open an MCP Inspector window where you can conveniently check the server status in your web browser.
However, there is one security precaution. Due to a security vulnerability (CVE-2025-49596) recently discovered in the MCP Inspector tool, token authentication has been strictly enforced in the latest versions. Therefore, to test locally and safely, you must either include the security token ( ?MCP_PROXY_AUTH_TOKEN=... ) from the link printed in the terminal, or if testing in a local standalone environment, temporarily set the DANGEROUSLY_OMIT_AUTH=true environment variable to ensure a smooth connection.
Building your own agent ecosystem
You've successfully taken the first step in connecting a custom tool to Claude with a few lines of Python. Now, try adding practical features you actually need in your daily life, such as fetching real-time weather information or summarizing local files. Building your own AI agent by providing tools yourself is not as hard as it seems.