Skip to main content
Glama
freecad-mcp

freecad-mcp

execute_code_async

Runs long-running Python geometry computations in a background thread, keeping FreeCAD responsive; use commit() to apply document or view changes safely on the GUI thread.

Instructions

Execute Python code in FreeCAD without waiting for completion.

Use this ONLY for long-running background computations that do NOT touch the FreeCAD GUI or mutate the FreeCAD document tree directly.

This tool runs the submitted code in a background thread and returns immediately. Because it does not run on FreeCAD's main GUI thread, the code must NOT directly call FreeCADGui APIs, manipulate the active view or selection, create or edit document objects, change object properties, call doc.recompute(), or save documents. FreeCAD documents and the Coin3D scenegraph are not thread-safe: writing to them from this thread races the GUI thread and can wedge FreeCAD's event loop, after which the RPC server stops responding entirely and FreeCAD must be restarted.

Every document or view write must instead be handed to the GUI thread through the injected commit() helper:

commit(fn, timeout=120) -> fn's return value

Scripts share a live namespace. Saved functions can use commit() in later async calls; calling it from execute_code or a GUI callback raises immediately. Only one shared-namespace script is admitted at a time. Overlapping scripts return SCRIPT_BUSY without starting; status and document tools remain usable.

commit() queues fn on the GUI thread, waits for it, and raises RuntimeError if dispatch fails or times out. Example:

fused = base.fuse(addition).removeSplitter()   # slow, safe in background

def apply():                                   # runs on the GUI thread
    obj.Shape = fused
    doc.recompute()

commit(apply)

For code that is not dominated by heavy geometry computation, use execute_code instead. execute_code runs entirely on the FreeCAD GUI thread and is the safe default for normal FreeCAD automation.

Use execute_code_async only when the heavy part is long-running OCCT geometry (e.g. fuse/cut/loft on already-fetched shapes) or other CPU-bound computation that would exceed execute_code's 90 s GUI-thread budget.

Typical usage pattern:

  1. Fetch shapes into module-level variables first (via execute_code).

  2. Run the heavy computation via execute_code_async.

  3. Apply the result inside commit(), or store it in a module-level Python variable (not in the FreeCAD document) for a later execute_code call.

Performance note: boolean operations against shapes with many faces (e.g. a ribbed lid) are expensive. Fuse the additions together first, then apply a single boolean against the heavy shape, and avoid doc.recompute() unless the dependency graph really needs it.

Args: code: Background-safe Python code to execute. Use commit(fn) for all document and view writes.

Returns: A message with the job_id of the started background execution.

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_code_async"
  }
}

Input Schema

NameRequiredDescriptionDefault
codeYes

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.1.32

TDQS

A4.7/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are supplied, so the description carries the full burden and does so: it explains the background thread, the thread-safety hazard and its concrete failure mode (wedged event loop, unresponsive RPC server, restart required), the injected commit(fn, timeout=120) helper and its RuntimeError semantics, shared-namespace state, and SCRIPT_BUSY admission control. It even discloses that status and document tools remain usable during a busy state, which is unusual and genuinely useful.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Well front-loaded and chunked with an example and a numbered pattern, but there is real redundancy: the prohibition list ('call FreeCADGui APIs, manipulate the active view or selection, create or edit document objects...') is restated by 'Every document or view write must instead be handed to the GUI thread through commit().' The trailing performance note on fusing boolean additions is generic FreeCAD modeling advice rather than a property of this tool and could be dropped.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For an unannotated, no-output-schema tool it is nearly complete: it reports the return value ('a message with the job_id of the started background execution') and how to get results back (module-level variable, later execute_code, or commit()). The one gap is that it never names get_async_status or get_task_status as the way to poll the job_id it returns.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate, and it does: the Args block defines 'code' as background-safe Python and mandates commit(fn) for all document/view writes, and the surrounding body enumerates what the code must not do. That is far more constraining and actionable than the bare 'Code' string title in the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

States a specific verb and resource ('Execute Python code in FreeCAD without waiting for completion') and immediately scopes it against the sibling: 'For code that is not dominated by heavy geometry computation, use execute_code instead.' The async/background-thread distinction from execute_code is unmistakable without opening either schema.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Gives explicit when-to-use ('ONLY for long-running background computations'), explicit when-not (must not touch GUI or the document tree), and a quantified threshold against the alternative ('would exceed execute_code's 90 s GUI-thread budget'). A three-step typical usage pattern plus a performance note covers the realistic workflow end to end.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.