Connect to local MCP servers over stdio

This commit is contained in:
Mark Gregory
2026-11-16 14:04:16 +00:00
parent caa0a8733b
commit 515f609f8d
6 changed files with 357 additions and 23 deletions
+36
View File
@@ -0,0 +1,36 @@
import json
import sys
import time
initialized = False
tool = {
"name": "echo",
"description": "Echo the supplied text",
"inputSchema": {"type": "object"},
}
for line in sys.stdin:
request = json.loads(line)
method = request["method"]
params = request.get("params", {})
if method == "initialize":
assert request["params"]["protocolVersion"] == "2025-11-25"
result = {"protocolVersion": "2025-06-18", "capabilities": {"tools": {}}}
elif method == "notifications/initialized":
initialized = True
continue
elif method == "tools/list":
assert initialized
if params.get("cursor") == "next":
result = {"tools": [tool]}
else:
result = {"tools": [], "nextCursor": "next"}
elif method == "tools/call":
assert initialized and params["name"] == "echo"
if params["arguments"].get("wait"):
time.sleep(20)
result = {"content": [{"type": "text", "text": params["arguments"].get("text", "")}]}
else:
raise ValueError(method)
print(json.dumps({"jsonrpc": "2.0", "method": "notifications/message"}), flush=True)
print(json.dumps({"jsonrpc": "2.0", "id": request["id"], "result": result}), flush=True)