You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.6 KiB
34 lines
1.6 KiB
import json, urllib.request
|
|
|
|
URL = "http://127.0.0.1:8000/mcp"
|
|
HEADERS = {"Content-Type": "application/json", "Accept": "application/json, text/event-stream"}
|
|
|
|
def post(body, session=None, timeout=120):
|
|
h = dict(HEADERS)
|
|
if session:
|
|
h["mcp-session-id"] = session
|
|
req = urllib.request.Request(URL, data=json.dumps(body).encode(), headers=h, method="POST")
|
|
resp = urllib.request.urlopen(req, timeout=timeout)
|
|
sid = resp.headers.get("mcp-session-id", session)
|
|
result = None
|
|
for raw in resp:
|
|
line = raw.decode("utf-8", "replace").strip()
|
|
if not line or line.startswith(":"):
|
|
continue
|
|
if line.startswith("data:"):
|
|
try:
|
|
d = json.loads(line[5:].strip())
|
|
except Exception:
|
|
continue
|
|
if isinstance(d, dict) and ("result" in d or "error" in d):
|
|
result = d
|
|
break
|
|
return sid, result
|
|
|
|
sid, _ = post({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"pi","version":"1.0"}}}, timeout=30)
|
|
h = dict(HEADERS); h["mcp-session-id"] = sid
|
|
urllib.request.urlopen(urllib.request.Request(URL, data=json.dumps({"jsonrpc":"2.0","method":"notifications/initialized"}).encode(), headers=h, method="POST"), timeout=15).read()
|
|
_, res = post({"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}, session=sid, timeout=60)
|
|
for t in res["result"]["tools"]:
|
|
print(t["name"], "::", t.get("description",""))
|
|
print(" input:", json.dumps(t.get("inputSchema",{}).get("properties",{}), ensure_ascii=False))
|
|
|