forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
265 lines (237 loc) · 9.55 KB
/
Copy pathmcp_server.py
File metadata and controls
265 lines (237 loc) · 9.55 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
Modly MCP Server
Exposes Modly's capabilities as MCP tools for external agents (Claude Desktop, Codex CLI, etc.).
Usage:
python mcp_server.py
Configuration in Claude Desktop (~/.config/claude/claude_desktop_config.json):
{
"mcpServers": {
"modly": {
"command": "python",
"args": ["C:/path/to/modly/desktop/api/mcp_server.py"]
}
}
}
Requires Modly's FastAPI backend to be running on http://localhost:8765.
"""
import asyncio
import mimetypes
import httpx
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
API_BASE = "http://localhost:8765"
server = Server("modly")
@server.list_tools()
async def list_tools() -> list[Tool]:
return [
Tool(
name="modly_list_models",
description="List all 3D generation models available in Modly (downloaded and ready to use).",
inputSchema={"type": "object", "properties": {}},
),
Tool(
name="modly_switch_model",
description="Switch the active 3D generation model in Modly.",
inputSchema={
"type": "object",
"properties": {
"model_id": {"type": "string", "description": "The model ID to activate."},
},
"required": ["model_id"],
},
),
Tool(
name="modly_generate_from_image",
description="Generate a 3D mesh from a 2D image file. Returns a job_id to track progress.",
inputSchema={
"type": "object",
"properties": {
"image_path": {
"type": "string",
"description": "Absolute path to the image file on disk.",
},
"model_id": {
"type": "string",
"description": "Which model to use. If omitted, uses the currently active model.",
},
"remesh": {
"type": "string",
"enum": ["quad", "triangle", "none"],
"description": "Remesh strategy after generation. Default: quad.",
},
},
"required": ["image_path"],
},
),
Tool(
name="modly_get_generation_status",
description="Poll the status of a 3D generation job. Call repeatedly until status is 'done' or 'error'.",
inputSchema={
"type": "object",
"properties": {
"job_id": {"type": "string", "description": "Job ID returned by modly_generate_from_image."},
},
"required": ["job_id"],
},
),
Tool(
name="modly_decimate_mesh",
description="Reduce the polygon count of a mesh using quadric edge collapse decimation.",
inputSchema={
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Workspace-relative path to the mesh (e.g. 'Default/mesh.glb').",
},
"target_faces": {
"type": "integer",
"description": "Target number of faces after decimation (minimum 100).",
},
},
"required": ["path", "target_faces"],
},
),
Tool(
name="modly_smooth_mesh",
description="Apply Laplacian smoothing to a mesh. More iterations = smoother surface but less detail.",
inputSchema={
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Workspace-relative path to the mesh (e.g. 'Default/mesh.glb').",
},
"iterations": {
"type": "integer",
"description": "Number of smoothing iterations (1–20).",
},
},
"required": ["path", "iterations"],
},
),
Tool(
name="modly_import_mesh",
description="Import a mesh file from disk into Modly's workspace (.glb, .obj, .stl, .ply).",
inputSchema={
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Absolute path to the mesh file on disk.",
},
},
"required": ["path"],
},
),
Tool(
name="modly_unload_models",
description="Unload all 3D generation models from GPU VRAM. Useful before running VRAM-intensive tasks.",
inputSchema={"type": "object", "properties": {}},
),
Tool(
name="modly_get_settings",
description="Get the current Modly settings (models directory, workspace directory).",
inputSchema={"type": "object", "properties": {}},
),
]
@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
async with httpx.AsyncClient(timeout=60.0) as client:
try:
result = await _dispatch(client, name, arguments)
except httpx.ConnectError:
result = (
"Cannot connect to Modly API at http://localhost:8765. "
"Make sure Modly is running."
)
except httpx.HTTPStatusError as e:
result = f"Modly API error {e.response.status_code}: {e.response.text[:300]}"
except Exception as e:
result = f"Error: {e}"
return [TextContent(type="text", text=result)]
async def _dispatch(client: httpx.AsyncClient, name: str, args: dict) -> str:
if name == "modly_list_models":
r = await client.get(f"{API_BASE}/model/all")
r.raise_for_status()
models = [m for m in r.json() if m.get("downloaded")]
if not models:
return "No models downloaded yet. Download one from the Models tab in Modly."
return "\n".join(f"- {m['id']}: {m.get('name', m['id'])}" for m in models)
elif name == "modly_switch_model":
r = await client.post(f"{API_BASE}/model/switch", params={"model_id": args["model_id"]})
r.raise_for_status()
return f"Switched active model to: {args['model_id']}"
elif name == "modly_generate_from_image":
image_path: str = args["image_path"]
with open(image_path, "rb") as f:
img_bytes = f.read()
mime = mimetypes.guess_type(image_path)[0] or "image/png"
filename = image_path.replace("\\", "/").split("/")[-1]
form_data = {
"remesh": args.get("remesh", "quad"),
}
if args.get("model_id"):
form_data["model_id"] = args["model_id"]
r = await client.post(
f"{API_BASE}/generate/from-image",
files={"image": (filename, img_bytes, mime)},
data=form_data,
timeout=30.0,
)
r.raise_for_status()
job_id = r.json()["job_id"]
return (
f"Generation started. Job ID: {job_id}\n"
f"Use modly_get_generation_status with this ID to track progress."
)
elif name == "modly_get_generation_status":
r = await client.get(f"{API_BASE}/generate/status/{args['job_id']}")
r.raise_for_status()
s = r.json()
parts = [f"Status: {s['status']}", f"Progress: {s.get('progress', 0)}%"]
if s.get("step"):
parts.append(f"Step: {s['step']}")
if s.get("output_url"):
parts.append(f"Output: {s['output_url']}")
if s.get("error"):
parts.append(f"Error: {s['error']}")
return " | ".join(parts)
elif name == "modly_decimate_mesh":
r = await client.post(
f"{API_BASE}/optimize/mesh",
json={"path": args["path"], "target_faces": args["target_faces"]},
)
r.raise_for_status()
data = r.json()
return f"Decimated mesh to {data.get('face_count', '?')} faces. New file: {data.get('url', '')}"
elif name == "modly_smooth_mesh":
r = await client.post(
f"{API_BASE}/optimize/smooth",
json={"path": args["path"], "iterations": args["iterations"]},
)
r.raise_for_status()
data = r.json()
return f"Smoothed mesh ({args['iterations']} iterations). New file: {data.get('url', '')}"
elif name == "modly_import_mesh":
r = await client.post(f"{API_BASE}/optimize/import-by-path", json={"path": args["path"]})
r.raise_for_status()
data = r.json()
return f"Mesh imported. URL: {data.get('url', '')}"
elif name == "modly_unload_models":
r = await client.post(f"{API_BASE}/model/unload-all")
r.raise_for_status()
return "All 3D generation models unloaded from VRAM."
elif name == "modly_get_settings":
r = await client.get(f"{API_BASE}/settings/paths")
r.raise_for_status()
data = r.json()
return f"Models directory: {data.get('models_dir')}\nWorkspace directory: {data.get('workspace_dir')}"
else:
return f"Unknown tool: {name}"
async def main():
async with stdio_server() as (read_stream, write_stream):
await server.run(read_stream, write_stream, server.create_initialization_options())
if __name__ == "__main__":
asyncio.run(main())