ExamplescriptadvancedRunnableguided-flow
Server
Runnable example (advanced) for script using mcp, python-dotenv.
Key Facts
- Level
- advanced
- Runtime
- Python • Mcp + Python Dotenv
- Pattern
- Inspectable flow with visible system boundaries
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
server.py
python
from mcp.server.fastmcp import FastMCP
from dotenv import load_dotenv
load_dotenv("../.env")
# Create an MCP server
mcp = FastMCP(
name="Calculator",
host="0.0.0.0", # only used for SSE transport (localhost)
port=8050, # only used for SSE transport (set this to any port)
stateless_http=True,
)
# Add a simple calculator tool
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers together"""
return a + b
# Run the server
if __name__ == "__main__":
transport = "stdio"
if transport == "stdio":
print("Running server with stdio transport")
mcp.run(transport="stdio")
elif transport == "sse":
print("Running server with SSE transport")
mcp.run(transport="sse")
elif transport == "streamable-http":
print("Running server with Streamable HTTP transport")
mcp.run(transport="streamable-http")
else:
raise ValueError(f"Unknown transport: {transport}")
Related principles
- P6visibilityExpose meaningful operational state, not internal complexityPresent the state of the system in language and structures that are relevant to the user, rather than exposing low-level internals that do not support action or understanding.Open principle →
- P7trustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P8trustMake hand-offs, approvals, and blockers explicitWhen the system cannot proceed, the reason should be immediately visible, along with any action required from the user or another dependency.Open principle →
- P9orchestrationRepresent delegated work as a system, not merely as a conversationWhere work involves multiple steps, agents, dependencies, or concurrent activities, it should be represented as a structured system rather than solely as a message stream.Open principle →