File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3939 demonstrating the bug.
4040
4141 placeholder : |
42+ #!/usr/bin/env uv run
43+ # /// script
44+ # requires-python = ">=3.11"
45+ # dependencies = [
46+ # "anyio",
47+ # "mcp",
48+ # ]
49+ # ///
50+ import threading
51+
52+ import anyio
53+
54+ from mcp.client.session import ClientSession
55+ from mcp.client.streamable_http import streamablehttp_client
4256 from mcp.server.fastmcp import FastMCP
4357
44- ...
58+
59+ async def run_server():
60+ mcp = FastMCP()
61+
62+ @mcp.tool()
63+ def add(a: int, b: int) -> int:
64+ """Add two numbers."""
65+ return a + b
66+
67+ # ...
68+
69+ await mcp.run_streamable_http_async()
70+
71+ async def run_client():
72+ async with streamablehttp_client("http://localhost:8000/mcp") as (read_stream, write_stream, _):
73+ async with ClientSession(read_stream, write_stream) as session:
74+ await session.initialize()
75+
76+ print(f'\nTool result: {await session.call_tool("add", {"a": 1, "b": 2})}\n')
77+
78+ # ...
79+
80+
81+ if __name__ == "__main__":
82+ # Run the server in a background thread
83+ threading.Thread(target=lambda: anyio.run(run_server), daemon=True).start()
84+
85+ anyio.run(run_client)
4586 render : Python
4687
4788 - type : textarea
You can’t perform that action at this time.
0 commit comments