Files
mcp_screen/virtual_screen_mcp/test_client.py
T

102 lines
2.8 KiB
Python

#!/usr/bin/env python3
import subprocess
import json
import base64
import time
import os
def run_test():
print("Starting server.py in a subprocess for stdio test...")
# Start server in subprocess on port 8089 to avoid port conflicts
proc = subprocess.Popen(
["python3", "server.py", "--port", "8089"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, # ignore logs on stderr
text=True,
bufsize=1
)
# Wait a moment for server to bind
time.sleep(1.5)
def send_cmd(cmd_dict):
proc.stdin.write(json.dumps(cmd_dict) + "\n")
proc.stdin.flush()
line = proc.stdout.readline()
if not line:
return None
return json.loads(line.strip())
print("\n[1/4] Sending initialize request...")
init_resp = send_cmd({
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {}
})
print("Response:", json.dumps(init_resp, indent=2))
print("\n[2/4] Sending clear_screen tool call...")
resp = send_cmd({
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "clear_screen",
"arguments": {"color": "#1e1e2e"}
}
})
print("Response:", json.dumps(resp, indent=2))
print("\n[3/4] Sending draw_text tool call...")
resp = send_cmd({
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "draw_text",
"arguments": {
"text": "TEST SUCCEEDED",
"x": 200,
"y": 280,
"size": 48,
"color": "#10b981"
}
}
})
print("Response:", json.dumps(resp, indent=2))
print("\n[4/4] Sending get_screenshot tool call...")
resp = send_cmd({
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": {
"name": "get_screenshot",
"arguments": {}
}
})
try:
content = resp["result"]["content"][0]
if content["type"] == "image":
img_b64 = content["data"]
output_file = "test_screenshot.png"
with open(output_file, "wb") as f:
f.write(base64.b64decode(img_b64))
print(f"Success! Captured screenshot saved to: {os.path.abspath(output_file)}")
else:
print("Failed: content is not an image", content)
except Exception as e:
print("Error reading screenshot response:", e)
print("Raw response:", resp)
# Stop subprocess
proc.terminate()
proc.wait()
print("\nVerification script finished.")
if __name__ == "__main__":
run_test()