execute_python
Run Python code against a live Autodesk Inventor session to automate parametric modeling, inspect or modify active documents, and roll back changes on errors.
Instructions
Execute Python code against the live Inventor connection.
Pre-bound objects (re-resolved fresh on every call): inv — InventorConnection wrapper (all high-level methods, mm units) app — Inventor.Application COM object doc — active document comp — its ComponentDefinition body — comp.SurfaceBodies.Item(1) (or None) tg — TransientGeometry math — math module progress(msg) — status line manage_call shows while a long job runs cancelled() — True once manage_call(action='cancel') asked to stop: poll it in long loops Variables you assign persist between calls until the server restarts or reload_api runs — get_status shows the namespace's age and names. Each connected Inventor has its own namespace. Helpers you need every time: put them in a file and load_library it (or in ~/.inventor-mcp/startup.py) — they come back by themselves in a new namespace. Internal COM units are CENTIMETERS — divide mm by 10. Objects are late-bound: a Document from Documents.Open exposes ComponentDefinition directly. Don't use CastTo / gencache.EnsureDispatch (they create a gen_py cache). Runs with SilentOperation on, so Inventor answers its own prompts instead of opening a dialog. Save with the save_document tool or inv.save_document(): a plain doc.SaveAs also writes every dirty dependent, protected and Content Center files included. A call longer than the wait limit keeps running; follow it with manage_call(wait_seconds=30), which shows the prints so far; manage_call(action='cancel') stops it. Returns captured stdout + stderr (print your results!), or the traceback from your own code down on error (COM errors named: E_INVALIDARG, RPC_E_DISCONNECTED …).
Args: code: Python source. undo_on_error: Run inside one undo transaction on the active document and roll it back if the code raises — no half-built sketches or work planes left behind. timeout: Seconds; then cancelled() turns True, and the run is interrupted CANCEL_GRACE s later if it hasn't stopped — as manage_call(action='cancel') does (0 = none).
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"code": "<code>"
},
"name": "execute_python"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| timeout | No | ||
| instance | No | Which connected Inventor to use (list_instances). Leave empty when only one is connected. | |
| undo_on_error | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |