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, wri
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
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")
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"]
}
}
}
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
stdiofor local LLM tools andsse(Server-Sent Events) for distributed cloud setups. -
Built-in Security Filters: Seamlessly pairs with
agent-cyber-sentinelto sanitize incoming tool requests and block SSRF attacks.
Sovereign Ecosystem & Open Source
- ⭐ GitHub Repository: https://github.com/Benni-OS/mcp-forge
- 🌐 Benni OS Portal: https://benni-os.net
- 📖 Full Architecture Article: https://benni-os.net/blog/como-utilizar-nemesis-gateway-no-ecossistema-benni-os.html
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.