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.
67 lines
2.4 KiB
67 lines
2.4 KiB
#!/usr/bin/env python3 |
|
"""Call one tool on an SDRangel MCP server (streamable HTTP, JSON-RPC). |
|
|
|
./mcp_call.py list_channel_types |
|
./mcp_call.py --url http://127.0.0.1:8096/mcp get_packets '{"limit": 5}' |
|
./mcp_call.py --list # tool names |
|
""" |
|
|
|
import argparse |
|
import json |
|
import sys |
|
import urllib.request |
|
|
|
|
|
def rpc(url, method, params, session=None, ident=1): |
|
body = {"jsonrpc": "2.0", "method": method, "params": params} |
|
if ident is not None: |
|
body["id"] = ident |
|
headers = {"Content-Type": "application/json", "Accept": "application/json, text/event-stream"} |
|
if session: |
|
headers["Mcp-Session-Id"] = session |
|
req = urllib.request.Request(url, json.dumps(body).encode(), headers) |
|
with urllib.request.urlopen(req, timeout=120) as r: |
|
session = r.headers.get("Mcp-Session-Id", session) |
|
text = r.read().decode() |
|
if not text.strip(): |
|
return None, session |
|
if text.lstrip().startswith("event:") or text.lstrip().startswith("data:"): |
|
text = "\n".join(l[5:].strip() for l in text.splitlines() if l.startswith("data:")) |
|
return json.loads(text), session |
|
|
|
|
|
def main(): |
|
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
|
ap.add_argument("--url", default="http://127.0.0.1:8096/mcp") |
|
ap.add_argument("--list", action="store_true", help="list tool names") |
|
ap.add_argument("tool", nargs="?") |
|
ap.add_argument("args", nargs="?", default="{}", help="tool arguments as JSON") |
|
a = ap.parse_args() |
|
|
|
init, session = rpc(a.url, "initialize", { |
|
"protocolVersion": "2025-06-18", "capabilities": {}, |
|
"clientInfo": {"name": "mcp_call", "version": "0.1"}}) |
|
rpc(a.url, "notifications/initialized", {}, session, ident=None) |
|
|
|
if a.list: |
|
res, _ = rpc(a.url, "tools/list", {}, session, 2) |
|
print("\n".join(t["name"] for t in res["result"]["tools"])) |
|
return |
|
if not a.tool: |
|
sys.exit("give a tool name, or --list") |
|
|
|
res, _ = rpc(a.url, "tools/call", {"name": a.tool, "arguments": json.loads(a.args)}, session, 2) |
|
if "error" in res: |
|
sys.exit(json.dumps(res["error"], indent=2)) |
|
for item in res["result"].get("content", []): |
|
text = item.get("text", "") |
|
try: |
|
print(json.dumps(json.loads(text), indent=2)) |
|
except ValueError: |
|
print(text) |
|
if res["result"].get("isError"): |
|
sys.exit(1) |
|
|
|
|
|
if __name__ == "__main__": |
|
main()
|
|
|