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.
71 lines
2.9 KiB
71 lines
2.9 KiB
|
3 weeks ago
|
# -*- coding: utf-8 -*-
|
||
|
|
"""Phase 1: MCP handshake + tools/list (full schemas) + lanhu_get_pages.
|
||
|
|
Saves raw outputs under .scratch/lead-defect-a2-a7-3-1/research/raw/.
|
||
|
|
"""
|
||
|
|
import json, os, urllib.request
|
||
|
|
|
||
|
|
URL = "http://127.0.0.1:8000/mcp"
|
||
|
|
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "raw")
|
||
|
|
os.makedirs(OUT, exist_ok=True)
|
||
|
|
HEADERS = {"Content-Type": "application/json", "Accept": "application/json, text/event-stream"}
|
||
|
|
PROTO_URL = ("https://lanhuapp.com/web/#/item/project/product?tid=b013dded-642f-4899-b829-daa5c093fdf0"
|
||
|
|
"&pid=e528bb49-1eea-4d76-b7e0-4055bc7c1b70"
|
||
|
|
"&image_id=bade4454-52aa-44db-8ba2-dec594732ecb"
|
||
|
|
"&docId=bade4454-52aa-44db-8ba2-dec594732ecb&docType=axure"
|
||
|
|
"&versionId=1d22a058-a107-477d-bdd0-5318114609f3"
|
||
|
|
"&pageId=7ed53a17f2e6455786994e61537dd29c&parentId=9e6b7b28-ffda-441b-bda7-7658d44f9980")
|
||
|
|
|
||
|
|
|
||
|
|
def post(body, session=None, timeout=300):
|
||
|
|
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
|
||
|
|
|
||
|
|
|
||
|
|
def save(name, obj):
|
||
|
|
p = os.path.join(OUT, name)
|
||
|
|
with open(p, "w", encoding="utf-8") as f:
|
||
|
|
if isinstance(obj, str):
|
||
|
|
f.write(obj)
|
||
|
|
else:
|
||
|
|
json.dump(obj, f, ensure_ascii=False, indent=1)
|
||
|
|
print("SAVED", p, os.path.getsize(p), flush=True)
|
||
|
|
|
||
|
|
|
||
|
|
sid, _ = post({"jsonrpc": "2.0", "id": 1, "method": "initialize",
|
||
|
|
"params": {"protocolVersion": "2024-11-05", "capabilities": {},
|
||
|
|
"clientInfo": {"name": "wayfinder-research", "version": "1.0"}}}, timeout=60)
|
||
|
|
post({"jsonrpc": "2.0", "method": "notifications/initialized"}, session=sid, timeout=30)
|
||
|
|
print("SESSION", sid, flush=True)
|
||
|
|
|
||
|
|
_, tools = post({"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}, session=sid, timeout=120)
|
||
|
|
save("tools.json", tools)
|
||
|
|
|
||
|
|
_, pages = post({"jsonrpc": "2.0", "id": 3, "method": "tools/call",
|
||
|
|
"params": {"name": "lanhu_get_pages", "arguments": {"url": PROTO_URL}}}, session=sid, timeout=600)
|
||
|
|
if pages and "result" in pages:
|
||
|
|
content = pages["result"].get("content", [])
|
||
|
|
texts = [c.get("text", "") for c in content if c.get("type") == "text"]
|
||
|
|
save("pages.txt", "\n".join(texts))
|
||
|
|
save("pages.json", pages)
|
||
|
|
elif pages:
|
||
|
|
save("pages_error.json", pages)
|
||
|
|
print("PHASE1 DONE", flush=True)
|