import json import urllib.request import urllib.error BRIDGE_URL = "http://127.0.0.1:39280/mcp" PROTOCOL_VERSION = "2025-11-25" def call_mcp_tool(tool_name, arguments): payload = { "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": tool_name, "arguments": arguments } } data = json.dumps(payload).encode("utf-8") req = urllib.request.Request( BRIDGE_URL, data=data, headers={ "Content-Type": "application/json", "MCP-Protocol-Version": PROTOCOL_VERSION } ) with urllib.request.urlopen(req) as resp: res = json.loads(resp.read().decode("utf-8")) return res def execute_freecad_code(code_str): return call_mcp_tool("execute_python", {"code": code_str}) if __name__ == "__main__": test_code = """ import Part doc = App.newDocument("LiveTestPart") size = 20.0 box_shape = Part.makeBox(size, size, size) top_edges = [] for edge in box_shape.Edges: v1 = edge.Vertexes[0].Point v2 = edge.Vertexes[1].Point if abs(v1.z - size) < 1e-4 and abs(v2.z - size) < 1e-4: top_edges.append(edge) chamfered = box_shape.makeChamfer(2.0, top_edges) obj = doc.addObject("Part::Feature", "CalibrationCube") obj.Shape = chamfered doc.recompute() Gui.SendMsgToActiveView("ViewFit") print("SUCCESS: LiveTestPart calibration cube created in FreeCAD GUI!") """ result = execute_freecad_code(test_code) print(json.dumps(result, indent=2))