106 lines
2.8 KiB
Python
106 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify Python execution capability in CtrEditor via MCP
|
|
"""
|
|
|
|
import json
|
|
import socket
|
|
import time
|
|
|
|
|
|
def send_mcp_request(host="localhost", port=5006, method="tools/call", params=None):
|
|
"""Send MCP request to CtrEditor"""
|
|
try:
|
|
# Create socket connection
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.settimeout(10)
|
|
sock.connect((host, port))
|
|
|
|
# Prepare request
|
|
request = {"jsonrpc": "2.0", "id": 1, "method": method, "params": params or {}}
|
|
|
|
# Send request
|
|
message = json.dumps(request) + "\n"
|
|
sock.send(message.encode("utf-8"))
|
|
|
|
# Receive response
|
|
response_data = ""
|
|
while True:
|
|
chunk = sock.recv(4096).decode("utf-8")
|
|
if not chunk:
|
|
break
|
|
response_data += chunk
|
|
if "\n" in response_data:
|
|
break
|
|
|
|
sock.close()
|
|
|
|
# Parse response
|
|
if response_data.strip():
|
|
return json.loads(response_data.strip())
|
|
return None
|
|
|
|
except Exception as e:
|
|
print(f"Error sending request: {e}")
|
|
return None
|
|
|
|
|
|
def test_python_execution():
|
|
"""Test Python execution functionality"""
|
|
print("Testing Python execution in CtrEditor...")
|
|
|
|
# Test 1: Simple Python code
|
|
print("\n1. Testing simple Python execution...")
|
|
params = {
|
|
"name": "execute_python",
|
|
"arguments": {
|
|
"code": "result = 2 + 2\nprint(f'Result: {result}')",
|
|
"return_variables": ["result"],
|
|
},
|
|
}
|
|
|
|
response = send_mcp_request(params=params)
|
|
if response:
|
|
print(f"Response: {json.dumps(response, indent=2)}")
|
|
else:
|
|
print("No response received")
|
|
|
|
# Test 2: Access CtrEditor objects
|
|
print("\n2. Testing CtrEditor object access...")
|
|
params = {
|
|
"name": "execute_python",
|
|
"arguments": {
|
|
"code": """
|
|
# Access CtrEditor objects
|
|
object_count = len(objects) if objects else 0
|
|
canvas_info = f"Canvas size: {canvas.Width}x{canvas.Height}" if canvas else "No canvas"
|
|
app_info = f"App type: {type(app).__name__}" if app else "No app"
|
|
|
|
print(f"Object count: {object_count}")
|
|
print(canvas_info)
|
|
print(app_info)
|
|
""",
|
|
"return_variables": ["object_count", "canvas_info", "app_info"],
|
|
},
|
|
}
|
|
|
|
response = send_mcp_request(params=params)
|
|
if response:
|
|
print(f"Response: {json.dumps(response, indent=2)}")
|
|
else:
|
|
print("No response received")
|
|
|
|
# Test 3: Get Python help
|
|
print("\n3. Testing Python help...")
|
|
params = {"name": "python_help", "arguments": {}}
|
|
|
|
response = send_mcp_request(params=params)
|
|
if response:
|
|
print(f"Response: {json.dumps(response, indent=2)}")
|
|
else:
|
|
print("No response received")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_python_execution()
|