Connect to local MCP servers over stdio
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user