Skip to main content
MCPPythonAI AgentsFastMCPClaudeModel Context Protocol

How to Build an MCP Server in Python: Step-by-Step Guide (2026)

8 min read

Muhammad Aashir Tariq

CEO & Head of AI, Afnexis

How to Build an MCP Server in Python: Step-by-Step Guide (2026)

10,000+ MCP servers are already running in production. Anthropic, OpenAI, Google, and Microsoft all adopted the Model Context Protocol within 12 months of launch. 97 million monthly SDK downloads as of March 2026. If you're building AI agents, MCP is the integration layer you need.

Before MCP, connecting an AI agent to your tools meant writing a custom integration for every combination of model and data source. One model, five tools, five integrations. Switch models, rewrite everything. That's the N×M problem.

MCP collapses that to N+M: one server, any compatible client.

We've built AI agent systems for clients in healthcare, fintech, and real estate. Before MCP, every project required weeks of custom connector work. After MCP, that same work takes days. Here's exactly how to build one.

What Is an MCP Server?

An MCP server is a process that exposes tools, resources, or prompts to AI models through the Model Context Protocol. The server sits between your data and the AI. When the model needs information or wants to take an action, it calls your MCP server, which handles the operation and returns the result.

Think of it like a USB-C port for AI. USB-C works with any device that follows the spec. MCP works with any model that speaks the protocol: Claude, ChatGPT, Cursor, VS Code Copilot, and Google Gemini.

Build the server once. Every compatible client uses it.

What You Need Before You Start

Get these ready before writing any code. You don't need a cloud account for local development.

  • Python 3.10 or newer
  • FastMCP — the fastest Python library for building MCP servers
  • Claude Desktop or Cursor — to test your server
  • A text editor and terminal

Step 1: Install FastMCP

FastMCP is the standard Python library for MCP development. It wraps the lower-level MCP SDK and removes the protocol boilerplate. One decorator and your Python function becomes a tool the AI can call. It reduces development time by 5x compared to the raw SDK.

pip install fastmcp

Verify the installation:

python -c "import fastmcp; print(fastmcp.__version__)"

Step 2: Create Your First MCP Server

Create a file called server.py. Here's a minimal working MCP server:

from fastmcp import FastMCP

mcp = FastMCP("My First Server")

@mcp.tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    # Replace with a real API call in production
    return f"Weather in {city}: 22°C, sunny"

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

Three things are happening here. FastMCP("My First Server") creates the server instance. The @mcp.tool decorator registers the function as a callable tool. The docstring tells the AI what the tool does and when to use it.

Write docstrings clearly. The model reads them.

Step 3: Add Business Logic Tools

A weather tool is a demo. Here's something closer to what you'd actually ship: tools that query your internal data and perform real calculations.

@mcp.tool
def get_customer_status(customer_id: str) -> dict:
    """Fetch a customer's subscription plan and last activity date."""
    # In production: query your database here
    return {
        "customer_id": customer_id,
        "plan": "Enterprise",
        "status": "active",
        "last_login": "2026-04-20"
    }

@mcp.tool
def calculate_roi(investment: float, annual_savings: float) -> dict:
    """Calculate ROI and payback period for a software investment."""
    roi = ((annual_savings * 3 - investment) / investment) * 100
    payback = (investment / annual_savings) * 12
    return {
        "roi_percent": round(roi, 1),
        "payback_months": round(payback, 1)
    }

The AI model decides when to call these tools based on what the user is asking. You don't hardcode the logic into the prompt. The model reads the docstrings, picks the right tool, and calls it.

Step 4: Connect to Claude Desktop

Open your Claude Desktop config file. On Mac it's at ~/Library/Application Support/Claude/claude_desktop_config.json. On Windows it's at %APPDATA%\Claude\claude_desktop_config.json.

{
  "mcpServers": {
    "my-server": {
      "command": "python",
      "args": ["/absolute/path/to/server.py"]
    }
  }
}

Restart Claude Desktop. Ask: "What's the ROI on a $60,000 investment saving $25,000 per year?" Claude calls your tool automatically and returns real numbers. No extra prompt engineering needed.

Step 5: Test Without Claude Using MCP Inspector

Don't wait for Claude to test your tools. FastMCP ships with a browser-based inspector. Run this in your terminal:

fastmcp dev server.py

This opens a UI at localhost:5173. Call each tool directly, see inputs and outputs, debug responses without any AI client. Test everything here before connecting to Claude.

How We Use MCP at Afnexis

We built an MCP server for a B2B SaaS client that connected Claude to their PostgreSQL customer database. Before MCP, their support team manually copied customer data into ChatGPT for every ticket. After MCP, Claude queried any customer's plan, billing history, and usage metrics in real time during the conversation.

Three tools. Three days to implement. First-response accuracy went from 64% to 91%. The support team stopped filing tickets asking engineers for data exports.

We now include MCP server development as a standard component in our AI agent projects. It's the cleanest way to give models access to business data without prompt-stuffing or building bespoke integrations. If you're choosing a framework, Google ADK natively supports MCP — see our Google ADK vs LangGraph comparison for where it fits.

Common Mistakes to Avoid

Empty docstrings. The AI reads your docstring to decide whether and how to call the tool. If it's missing, the model can't use the tool reliably. Write one clear sentence describing what the tool does and what it returns.

Too many tools on one server. Keep each MCP server focused. Under 20 tools is the practical limit. More than that and the model starts picking the wrong one or ignoring tools entirely.

No error handling. An unhandled exception crashes the session. Wrap your tool logic in try/except. Return a clear error string rather than raising. The AI can handle an error message. It can't handle a silent crash.

Using stdio for remote deployments. stdio works fine for local Claude Desktop. For cloud deployment or multi-client use, switch to HTTP transport: mcp.run(transport='http', port=8000). Google Cloud Run supports MCP HTTP streaming natively and is the recommended deployment target in 2026.

Frequently Asked Questions

What is the difference between MCP tools, resources, and prompts?

Tools are functions the AI calls to perform actions or fetch live data. Resources are read-only data the model can access, like a config file or knowledge base. Prompts are pre-written templates for specific tasks. Most real-world MCP servers primarily expose tools.

Can I use an MCP server with OpenAI models?

Yes. OpenAI's Agents SDK added native MCP support in 2026. You connect your server to GPT-4o the same way you connect to Claude. Google Gemini and Microsoft Copilot Studio also support the protocol. MCP is model-agnostic by design.

How do I deploy an MCP server to production?

Switch from stdio to HTTP transport, containerize with Docker, then deploy to Google Cloud Run or AWS Lambda. Add OAuth 2.1 authentication at the server level. Cloud Run is the recommended option because it supports MCP HTTP streaming natively and Google documented the deployment process in detail in 2026.

Is MCP secure for enterprise use?

Yes, with proper setup. The MCP spec includes OAuth 2.1 with PKCE and scoped tool permissions. Run over HTTPS in production, validate all inputs in your tool functions, and implement audit logging. Auth is your responsibility at the server level — MCP itself doesn't enforce it.

How is MCP different from a regular REST API?

A REST API requires a developer to write explicit code deciding which endpoint to call and when. An MCP server exposes tools that the AI discovers and calls autonomously at runtime, based on what the user is asking. You describe the tool. The model decides when to use it.

Build AI Agents That Use Real Data

MCP is the fastest way to give your AI agents access to live business data without building custom integrations. If you're starting a new AI agent project or adding intelligence to an existing product, MCP should be in your architecture from day one.

We build production AI agent systems with MCP at Afnexis. If you want to talk through your use case, reach out.

M

Written by

Muhammad Aashir Tariq

CEO & Head of AI, Afnexis

Aashir has shipped 50+ AI systems to production across healthcare, fintech, and real estate. He writes about what actually works RAG pipelines, LLM integration, HIPAA-compliant AI, and getting models out of staging.

Share:

Liked this article?

Every Tuesday, we send one actionable AI insight, one tool recommendation, and one update from our lab.

No fluff. Just what works in production AI.

Join tech leaders already reading.

Ready to Transform Your Business with AI?

Let's discuss how our AI solutions can help you achieve your goals.