Building an MCP Server with FastMCP

Install

Setup

The official Python SDK is the mcp package. The [cli] extra adds the developer tools.

pip install "mcp[cli]"

The Server

# shopease_mcp.py
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("shopease")

ORDERS = {"SE1042": {"status": "delivered", "item": "Boat Airdopes 141"},
          "SE1077": {"status": "out for delivery", "item": "Prestige Pressure Cooker 3L"}}
POLICIES = {"returns": "Most items can be returned within 10 days. Electronics: 7-day replacement only.",
            "refunds": "Refunds reach the original payment method in 5-7 working days."}

@mcp.tool()
def get_order_status(order_id: str) -> dict:
    """Get the status and item of a ShopEase order. Use when the user mentions an order ID."""
    return ORDERS.get(order_id, {"error": f"order {order_id} not found"})

@mcp.resource("policy://{name}")
def get_policy(name: str) -> str:
    """A ShopEase policy document: returns or refunds."""
    return POLICIES.get(name, "unknown policy")

@mcp.prompt()
def support_reply(customer_message: str) -> str:
    """Draft a polite ShopEase support reply."""
    return f"You are ShopEase support. Reply in under 80 words, kindly and clearly:\n\n{customer_message}"

if __name__ == "__main__":
    mcp.run()          # stdio; use mcp.run(transport="streamable-http") to serve over HTTP

Type hints and docstrings become the tool's input schema and description automatically.

Test It with the Inspector

Run it

The MCP Inspector opens a web UI where you can list and call tools by hand.

mcp dev shopease_mcp.py

Example output

Starting MCP inspector...
Proxy server listening on 127.0.0.1:6277
MCP Inspector is up and running at http://127.0.0.1:6274

Use It from Claude Desktop

{
  "mcpServers": {
    "shopease": {
      "command": "python",
      "args": ["C:/projects/shopease/shopease_mcp.py"]
    }
  }
}

Add this to Claude Desktop's claude_desktop_config.json and restart it. In Claude Code, run claude mcp add shopease -- python shopease_mcp.py.

A Python Client

import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def main():
    params = StdioServerParameters(command="python", args=["shopease_mcp.py"])
    async with stdio_client(params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            tools = await session.list_tools()
            print("tools:", [t.name for t in tools.tools])
            result = await session.call_tool("get_order_status", {"order_id": "SE1077"})
            print("result:", result.content[0].text)

asyncio.run(main())

Run it

python mcp_client.py

Example output

tools: ['get_order_status']
result: {"status": "out for delivery", "item": "Prestige Pressure Cooker 3L"}