DEV Community

Benni Alencar
Benni Alencar

Posted on Originally published at benni-os.net

Building Model Context Protocol (MCP) Servers in 5 Lines of Python — The FastAPI of MCP

Anthropic's Model Context Protocol (MCP) has rapidly become the universal open standard for connecting LLMs (such as Claude Desktop, Cursor, and autonomous agents) to external data sources, APIs, and tools.

However, writing raw MCP servers with standard JSON-RPC handlers requires tedious boilerplate: manual JSON schema definitions, manual argument validation, Pydantic serialization glue, and error envelopes.

What if building an MCP server was as clean, expressive, and concise as building a FastAPI app?

Enter mcp-forge.


The Minimal MCP Server

With mcp-forge, you write standard Python functions with native type hints. Type inspection, JSON Schema compilation, and error handling are handled automatically:

pip install mcp-forge
Enter fullscreen mode Exit fullscreen mode

Create your server (server.py):

from mcp_forge import ForgeServer

server = ForgeServer(name="git-analytics-server", version="1.0.0")

@server.tool(description="Calculate repository churn and commit statistics")
def calculate_churn(repo_path: str, branch: str = "main", days: int = 7) -> dict:
    """Calculates commits, insertions, and deletions over the last N days."""
    stats = compute_git_metrics(repo_path, branch, days)
    return {
        "commits": stats.commit_count,
        "insertions": stats.additions,
        "deletions": stats.deletions,
        "churn_ratio": stats.churn_ratio
    }

if __name__ == "__main__":
    server.run(transport="stdio")
Enter fullscreen mode Exit fullscreen mode

Instant Integration with Claude Desktop & Cursor

Add the server to your claude_desktop_config.json:

{
  "mcpServers": {
    "git-analytics": {
      "command": "python",
      "args": ["/absolute/path/to/server.py"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Claude Desktop instantly discovers your tool, introspects the parameters, and can invoke it with full type safety.


Advanced Capabilities

  • Automatic Pydantic Model Introspection: Complex nested types are automatically converted to valid JSON Schema v7.
  • Bi-Directional Transport: Supports both stdio for local LLM tools and sse (Server-Sent Events) for distributed cloud setups.
  • Built-in Security Filters: Seamlessly pairs with agent-cyber-sentinel to sanitize incoming tool requests and block SSRF attacks.

Sovereign Ecosystem & Open Source

Top comments (1)

Collapse
 
alexshev profile image
Alex Shev •

Five lines can prove an MCP transport, but production value lives in the next layers: narrow tool contracts, authentication, tenant scoping, rate limits, and observable failure modes. The smallest server is a great demo; the smallest safe capability is the better starting point for real integrations.