Create examples/python_mcp_client.py
Browse files
examples/python_mcp_client.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Direct MCP client example (also works from LangGraph/agno/pydantic-ai etc.)."""
|
| 2 |
+
import asyncio, json
|
| 3 |
+
from mcp import ClientSession
|
| 4 |
+
from mcp.client.streamable_http import streamablehttp_client
|
| 5 |
+
|
| 6 |
+
SPACE = "https://YOUR-SPACE.hf.space"
|
| 7 |
+
|
| 8 |
+
async def main():
|
| 9 |
+
inp_text = open("model.inp").read()
|
| 10 |
+
async with streamablehttp_client(f"{SPACE}/mcp") as (read, write, _):
|
| 11 |
+
async with ClientSession(read, write) as s:
|
| 12 |
+
await s.initialize()
|
| 13 |
+
up = json.loads((await s.call_tool("upload_model",
|
| 14 |
+
{"inp_content": inp_text, "filename": "model.inp"})).content[0].text)
|
| 15 |
+
sid = up["session_id"]
|
| 16 |
+
run = json.loads((await s.call_tool("run_simulation",
|
| 17 |
+
{"session_id": sid})).content[0].text)
|
| 18 |
+
print("reconciliation:", run["rpt_reconciliation"])
|
| 19 |
+
links = json.loads((await s.call_tool("get_link_results",
|
| 20 |
+
{"session_id": sid, "limit": 5})).content[0].text)
|
| 21 |
+
for row in links["rows"]:
|
| 22 |
+
print(row["Link ID"], row.get("Peak Velocity (m/s)"))
|
| 23 |
+
|
| 24 |
+
asyncio.run(main())
|