ExamplescriptadvancedRunnableguided-flow
Client
Runnable example (advanced) for script using mcp, mcp[cli].
Key Facts
- Level
- advanced
- Runtime
- Python • Mcp + Mcp[cli]
- 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
client.py
python
import asyncio
import nest_asyncio
from mcp import ClientSession
from mcp.client.sse import sse_client
nest_asyncio.apply() # Needed to run interactive python
"""
Make sure:
1. The server is running before running this script.
2. The server is configured to use SSE transport.
3. The server is listening on port 8050.
To run the server:
uv run server.py
"""
async def main():
# Connect to the server using SSE
async with sse_client("http://localhost:8050/sse") as (read_stream, write_stream):
async with ClientSession(read_stream, write_stream) as session:
# Initialize the connection
await session.initialize()
# List available tools
tools_result = await session.list_tools()
print("Available tools:")
for tool in tools_result.tools:
print(f" - {tool.name}: {tool.description}")
# Call our calculator tool
result = await session.call_tool("add", arguments={"a": 2, "b": 3})
print(f"2 + 3 = {result.content[0].text}")
if __name__ == "__main__":
asyncio.run(main())
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 →