You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
3.4 KiB
79 lines
3.4 KiB
# -*- coding: utf-8 -*-
|
|
"""Командный клиент MCP-сервера FreeCAD (freecad-mcp 0.1.32) — управление запущенным FreeCAD по протоколу MCP
|
|
из скриптов проекта (тот же сервер, что подключён к Claude Code через .mcp.json).
|
|
|
|
Предварительно: bash tools/start_freecad_gui.sh
|
|
Примеры:
|
|
tools/venv/bin/python tools/fcmcp.py tools # список инструментов
|
|
tools/venv/bin/python tools/fcmcp.py exec "print(App.ActiveDocument)"
|
|
tools/venv/bin/python tools/fcmcp.py execfile cad/mcp_show.py # выполнить файл в FreeCAD
|
|
tools/venv/bin/python tools/fcmcp.py view out/renders/iso.png Isometric
|
|
tools/venv/bin/python tools/fcmcp.py call list_documents '{}'
|
|
"""
|
|
import asyncio
|
|
import base64
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from mcp import ClientSession, StdioServerParameters
|
|
from mcp.client.stdio import stdio_client
|
|
|
|
SERVER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "venv", "bin", "freecad-mcp")
|
|
|
|
|
|
async def _run(tool, args, save_png=None):
|
|
params = StdioServerParameters(command=SERVER, args=[])
|
|
async with stdio_client(params) as (r, w):
|
|
async with ClientSession(r, w) as s:
|
|
await s.initialize()
|
|
if tool is None:
|
|
for t in (await s.list_tools()).tools:
|
|
print("%-28s %s" % (t.name, (t.description or "").strip().split("\n")[0][:90]))
|
|
return 0
|
|
res = await s.call_tool(tool, args)
|
|
ok = not getattr(res, "isError", False)
|
|
for c in res.content:
|
|
if getattr(c, "type", "") == "text":
|
|
print(c.text)
|
|
elif getattr(c, "type", "") == "image":
|
|
if save_png:
|
|
os.makedirs(os.path.dirname(os.path.abspath(save_png)), exist_ok=True)
|
|
with open(save_png, "wb") as f:
|
|
f.write(base64.b64decode(c.data))
|
|
print("[изображение сохранено: %s]" % save_png)
|
|
else:
|
|
print("[изображение %s, %d байт base64]" % (getattr(c, "mime_type", getattr(c, "mimeType", "")), len(c.data)))
|
|
return 0 if ok else 1
|
|
|
|
|
|
def main(argv):
|
|
if len(argv) < 2 or argv[1] in ("-h", "--help"):
|
|
print(__doc__)
|
|
return 0
|
|
cmd = argv[1]
|
|
if cmd == "tools":
|
|
return asyncio.run(_run(None, None))
|
|
if cmd == "exec":
|
|
return asyncio.run(_run("execute_code", {"code": argv[2]}))
|
|
if cmd == "execfile":
|
|
with open(argv[2], encoding="utf-8") as f:
|
|
code = f.read()
|
|
extra = " ".join(argv[3:])
|
|
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
code = "PROJECT_ROOT = %r\n" % root + ("ARGS = %r\n" % extra if extra else "") + code
|
|
return asyncio.run(_run("execute_code", {"code": code}))
|
|
if cmd == "view":
|
|
out = argv[2]
|
|
view = argv[3] if len(argv) > 3 else "Isometric"
|
|
return asyncio.run(_run("get_view", {"view_name": view}, save_png=out))
|
|
if cmd == "call":
|
|
return asyncio.run(_run(argv[2], json.loads(argv[3]) if len(argv) > 3 else {},
|
|
save_png=argv[4] if len(argv) > 4 else None))
|
|
print("неизвестная команда:", cmd)
|
|
return 2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main(sys.argv))
|