DS DevShelfHub Projects · AI tools
Tutorials / MCP / Build Your First Server
MCP Intermediate · 10 min read Page 18 of 23

Build Your First MCP Server (Python)

By DevShelfHub

Write a minimal MCP server in Python and connect it to Claude Desktop. See how to add tools and resources in just a few lines of code.

Series progress18 / 23

Step 1: Install the MCP Python SDK

bash
pip3 install mcp

The mcp package provides the Python SDK for building servers.

Step 2: Write a minimal server

Create a file called server.py:

python
from mcp.server import Server
from mcp.types import Tool

server = Server("demo-server")

@server.call_tool()
def call_tool(name: str, arguments: dict):
    if name == "greet":
        return f"Hello, {arguments.get('name', 'World')}!"
    return "Unknown tool"

if __name__ == "__main__":
    server.run()

This creates a server with one tool called "greet" that accepts a name and returns a greeting.

Step 3: Register the tool

Update the server to declare the tool so Claude knows it exists:

python
from mcp.server import Server
from mcp.types import Tool, TextContent

server = Server("demo-server")

# Register the tool
server.tools = [
    Tool(
        name="greet",
        description="Greet someone by name",
        inputSchema={
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "The person's name"
                }
            },
            "required": ["name"]
        }
    )
]

@server.call_tool()
def call_tool(name: str, arguments: dict):
    if name == "greet":
        return [TextContent(
            type="text",
            text=f"Hello, {arguments.get('name')}!"
        )]
    return [TextContent(type="text", text="Unknown tool")]

if __name__ == "__main__":
    server.run()

Step 4: Run the server

bash
python3 server.py

The server starts and listens via stdio. Leave it running in a terminal.

Step 5: Connect to Claude Desktop

Add this to your claude_desktop_config.json:

json
{
  "mcpServers": {
    "demo": {
      "command": "python",
      "args": ["/path/to/server.py"]
    }
  }
}

Replace /path/to/server.py with the full path to your script.

Step 6: Test it

  1. Restart Claude Desktop.
  2. Click the 🔨 hammer icon to confirm your server is running.
  3. In a conversation, try: "Use the greet tool to greet Alice".
  4. Claude should call your tool and return the greeting.

Quick summary

  • Install the mcp Python SDK: pip3 install mcp
  • Create a Server and define tools with @server.call_tool()
  • Register tools so Claude knows their names and parameters
  • Connect in Claude Desktop config and test via the 🔨 icon
  • Next: deep dive into tool input schemas and error handling

Build First MCP Server FAQ

How do I install the Python MCP SDK?

Install the official Python MCP SDK with: pip install mcp or uv add mcp. The SDK provides the FastMCP class for defining servers, tools, and resources with minimal boilerplate.

What is the minimum code for a Python MCP server?

A minimal Python MCP server needs three things: import FastMCP, create an mcp = FastMCP('name') instance, and call mcp.run(). Add tools with the @mcp.tool() decorator and resources with @mcp.resource('uri').

How do I connect my Python MCP server to Claude Desktop?

Add an entry to claude_desktop_config.json's mcpServers object with command set to 'python' (or 'uv') and args pointing to your server script. Restart Claude Desktop and your tools will appear in the chat input.

Can I use async functions in Python MCP tools?

Yes. FastMCP supports both synchronous and async tool functions. Use async def for I/O-bound operations like database queries or HTTP requests. The SDK handles the event loop automatically.

What languages can I use to build MCP servers?

Anthropic provides official SDKs for Python, TypeScript, Kotlin, Go, Ruby, and C#. The TypeScript SDK is the reference implementation, but all SDKs expose the same MCP protocol surface.

Quick jump: API Reference