-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
52 lines (42 loc) · 1.38 KB
/
Copy pathserver.py
File metadata and controls
52 lines (42 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("unity-ctl")
def post(tool_name, data=None):
try:
response = requests.post(f"http://localhost:6400/{tool_name}", json=data)
return response.json()
except requests.exceptions.ConnectionError:
return {"success": False, "error": "Could not connect to Unity. Is the editor running?"}
def get(tool_name, data=None):
try:
response = requests.get(f"http://localhost:6400/{tool_name}", json=data)
return response.json()
except requests.exceptions.ConnectionError:
return {"success": False, "error": "Could not connect to Unity. Is the editor running?"}
@mcp.tool()
def create_gameobject(name: str, x: float, y: float, components: list[str]=None):
if components is None:
components = []
return post("create_gameobject", data = {
"name": name,
"x": x,
"y": y,
"components": components
})
@mcp.tool()
def add_component(gameobject_name: str, component: str):
return post("add_component", data = {
"gameobject_name": gameobject_name,
"component": component
})
@mcp.tool()
def enter_playmode():
return post("enter_playmode")
@mcp.tool()
def exit_playmode():
return post("exit_playmode")
@mcp.tool()
def get_console_logs():
return get("get_console_logs")
if __name__ == "__main__":
mcp.run()