The proxy.py module#

Summary#

ProxyState

Routing state of one proxy process.

lane_url

Return the MCP endpoint URL of a lane.

is_auth_rejection

Check whether an exception is, or wraps, a BackendAuthError.

broken_connection_hint

Return a recovery hint if a tool output reveals a broken AEDT connection.

diagnose_status

Classify a lane check_aedt_status payload.

probe_lane_status

Return a lane check_aedt_status payload within a time bound.

probe_lane_aedt_port

Return the AEDT gRPC port a lane is attached to, within a time bound.

make_routing_tool

Build a tool that forwards upstream_tool to the selected lane.

list_upstream_tools

List the tools a lane started with backend_args would serve.

build_instructions

Return the proxy instructions: the regular system prompt plus lane guidance.

register_management_tools

Add the lane management and recovery tools to server.

build_proxy

Build the routing proxy server.

Description#

Stdio routing proxy that multiplexes AEDT sessions (“lanes”).

The proxy is what an MCP client starts with ansys-aedt-mcp --lanes. It:

  1. Lists the regular PyAEDT-MCP tools in-process, applying the same tag filters as the regular CLI. No backend is spawned at startup.

  2. Registers one routing wrapper per tool. Each wrapper forwards the call to the lane selected in this proxy process, or fails fast when none is.

  3. Adds lane management tools (spawn_lane, switch_lane, and so on) plus health and recovery helpers.

The selected lane is per proxy process, so every client window gets an independent view of the same machine-wide fleet.

Module detail#

proxy.lane_url(host: str, port: int) → str#

Return the MCP endpoint URL of a lane.

Parameters:
hoststr

Lane host.

portint

Lane HTTP port.

Returns:
str

Endpoint URL.

proxy.is_auth_rejection(exc: BaseException) → bool#

Check whether an exception is, or wraps, a BackendAuthError.

Parameters:
excBaseException

Exception to inspect, including exception groups and chained causes.

Returns:
bool

True if a lane rejected the token.

proxy.broken_connection_hint(text: str) → str | None#

Return a recovery hint if a tool output reveals a broken AEDT connection.

Parameters:
textstr

Text output of a routed tool.

Returns:
str or None

Hint pointing at check_lane and recycle_lane, or None.

proxy.diagnose_status(status: dict[str, Any]) → dict[str, Any]#

Classify a lane check_aedt_status payload.

The AEDT process view comes from the backend’s own available_sessions scan; the client view comes from its PyAEDT Desktop.

Parameters:
statusdict[str, Any]

Parsed check_aedt_status output.

Returns:
dict[str, Any]

verdict (healthy, no_aedt, connection_broken, aedt_gone, or version_mismatch), a detail sentence, and the facts the verdict is based on.

async proxy.probe_lane_status(host: str, port: int, timeout: float | None = DEFAULT_PROBE_TIMEOUT, auth_token: str | None = None) → dict[str, Any]#

Return a lane check_aedt_status payload within a time bound.

Parameters:
hoststr

Lane host.

portint

Lane HTTP port.

timeoutfloat, optional

Seconds to wait. None waits forever.

auth_tokenstr, optional

Lane bearer token.

Returns:
dict[str, Any]

Parsed status payload.

Raises:
TimeoutError

If the lane does not answer in time.

async proxy.probe_lane_aedt_port(host: str, port: int, timeout: float | None = DEFAULT_PROBE_TIMEOUT, auth_token: str | None = None) → int | None#

Return the AEDT gRPC port a lane is attached to, within a time bound.

Parameters:
hoststr

Lane host.

portint

Lane HTTP port.

timeoutfloat, optional

Seconds to wait. None waits forever.

auth_tokenstr, optional

Lane bearer token.

Returns:
int or None

AEDT gRPC port, or None when the lane is not attached.

Raises:
TimeoutError

If the lane does not answer in time.

proxy.make_routing_tool(upstream_tool: mcp.types.Tool, state: ProxyState) → fastmcp.tools.Tool#

Build a tool that forwards upstream_tool to the selected lane.

The routed tool keeps the upstream name, description, schemas, title, annotations, and tags, so clients see the same tool surface as with the regular server.

Parameters:
upstream_toolmcp.types.Tool

Upstream tool definition.

stateProxyState

Routing state shared by every tool of the proxy.

Returns:
fastmcp.tools.Tool

Routing tool.

async proxy.list_upstream_tools(backend_args: list[str]) → list[mcp.types.Tool]#

List the tools a lane started with backend_args would serve.

The regular server is imported in-process and the same tag filters as its CLI are applied: --connect hides the connection tools and --include-context adds the context helpers.

Parameters:
backend_argslist[str]

Additional ansys-aedt-mcp arguments for the lanes.

Returns:
list[mcp.types.Tool]

Upstream tool definitions.

proxy.build_instructions() → str#

Return the proxy instructions: the regular system prompt plus lane guidance.

Returns:
str

Server instructions.

proxy.register_management_tools(server: fastmcp.FastMCP, state: ProxyState) → None#

Add the lane management and recovery tools to server.

Every tool returns an {"ok": False, "error": ...} payload instead of raising.

Parameters:
serverfastmcp.FastMCP

Proxy server.

stateProxyState

Routing state shared with the routing tools.

async proxy.build_proxy(host: str = spawner.DEFAULT_HOST, startup_timeout: float = spawner.DEFAULT_STARTUP_TIMEOUT, backend_args: list[str] | None = None, probe_timeout: float | None = DEFAULT_PROBE_TIMEOUT, handshake_timeout: float = DEFAULT_HANDSHAKE_TIMEOUT) → fastmcp.FastMCP#

Build the routing proxy server.

Nothing is spawned: the upstream tools are listed in-process and no lane is selected until the client calls spawn_lane or switch_lane.

Parameters:
hoststr, default: “127.0.0.1”

Host lanes listen on.

startup_timeoutfloat, default: 60.0

Seconds a new lane gets to bind its port.

backend_argslist[str], optional

Additional ansys-aedt-mcp arguments for spawned lanes.

probe_timeoutfloat, default: 10.0

Seconds a lane gets to answer a status probe.

handshake_timeoutfloat, default: 5.0

Seconds a lane gets to answer the MCP handshake of a routed call.

Returns:
fastmcp.FastMCP

Proxy server ready to run over stdio.

Raises:
ValueError

If backend_args contains an unsupported option.

proxy.DEFAULT_SHUTDOWN_TIMEOUT = 10.0#
proxy.DEFAULT_PROBE_TIMEOUT = 10.0#
proxy.DEFAULT_HANDSHAKE_TIMEOUT = 5.0#
proxy.DEFAULT_RECONNECT_TIMEOUT = 180.0#
proxy.HEARTBEAT_INTERVAL = 10.0#

Seconds between progress notifications while a routed call is pending.

proxy.ROUTED_TAG = 'routed'#
proxy.LANES_TOOLSET = 'lanes'#
proxy.UNSUPPORTED_BACKEND_ARGS = ('--dynamic-tool-discovery',)#

Backend arguments the proxy cannot support.

The proxy opens a new MCP session per routed call, so per-session tool gating would hide the AEDT-only tools forever.

proxy.LANE_INSTRUCTIONS = Multiline-String#
Show Value
"""## Lanes

This server routes AEDT tool calls through "lanes". Every AEDT tool call is forwarded
to the lane currently selected.

- A lane is an isolated backend that owns at most one AEDT session. Independent lanes
  let you run parallel work (e.g. baseline vs. experiment) without projects interfering.
- No lane is selected when this server starts, so AEDT tools fail with "No lane selected"
  until you pick one. Start with list_lanes: switch_lane to continue on an existing lane
  (lanes are shared by every client on this machine), or spawn_lane to create one.
- Name lanes like git branches, after the work ("patch-antenna", "filter-tuning"),
  never generic ("default", "main", "test") and never ports or numeric ids.
- "AEDT session" means a running ansysedt process, as reported by check_aedt_status.
  It is not the same as a lane. One lane owns one AEDT session.

Typical flow:
1. list_lanes()                  # anything to reuse? then switch_lane("") and skip 2-3
2. spawn_lane("patch-antenna")   # ~5s: creates the lane and routes to it
3. launch_aedt(...)              # ~30-90s: runs INSIDE patch-antenna
4. ... use any AEDT tool normally ...
5. stop_lane("patch-antenna")    # when the work is finished and nobody else uses it

Rules:
- spawn_lane does NOT launch AEDT. Call launch_aedt (or connect_to_aedt) next.
- Use switch_lane to change where subsequent AEDT tool calls land.
- current_lane / list_lanes tell you where you are and what exists.
- stop_lane shuts a lane down; if it was the selected one, no lane is selected afterwards.
- If AEDT calls on a lane start failing oddly (e.g. after an external PyAEDT script or
  toolkit used the same AEDT, or "[WinError 127]"), run check_lane. recycle_lane restarts
  the lane's backend and re-attaches it to the same AEDT without closing AEDT.
- If things get wedged: reap_lanes (soft), reset_lanes (harder), unlock_registry_files.
- "Lane ... is busy" means that lane's backend is blocked in a long AEDT call and your call
  was NOT sent. Don't retry in a tight loop: wait, check current_lane (in_flight), ask the
  user whether AEDT shows a dialog or is solving, or continue on another lane.
"""
proxy.LANES_TOOLSET_ENTRY: dict[str, Any]#
proxy.logger#