The spawner.py module#

Summary#

LaneHandle

Connection details of a running lane.

log_path_for

Return the log file used by the lane on port.

wait_for_port

Wait until a port is open, or closed when open_ is False.

find_listener_pid

Return the process listening on a TCP port.

backend_env

Build the environment of a lane backend.

backend_command

Build the command line of a lane backend.

spawn_backend

Start a lane backend detached from the current process.

ensure_backend

Return a live lane named server_id, spawning it if needed.

DEFAULT_HOST

DEFAULT_STARTUP_TIMEOUT

Seconds a new lane gets to bind its HTTP port.

BACKEND_MODULE

Description#

Spawn lane backends and record them in the registry.

A lane backend is python -m ansys.aedt.mcp --transport http started detached from the proxy, so it outlives the client window that created it. Its bearer token travels only through the environment, never through the command line, which other local users can read.

Module detail#

spawner.log_path_for(port: int) → pathlib.Path#

Return the log file used by the lane on port.

Parameters:
portint

HTTP port of the lane.

Returns:
pathlib.Path

Log file path in the state directory.

spawner.wait_for_port(host: str, port: int, timeout: float, *, open_: bool = True) → bool#

Wait until a port is open, or closed when open_ is False.

Parameters:
hoststr

Host to probe.

portint

Port to probe.

timeoutfloat

Seconds to wait.

open_bool, default: True

Desired state of the port.

Returns:
bool

True if the port reached the desired state in time.

spawner.find_listener_pid(port: int) → int | None#

Return the process listening on a TCP port.

Parameters:
portint

TCP port.

Returns:
int or None

Process ID, or None if it cannot be determined.

spawner.backend_env(auth_token: str) → dict[str, str]#

Build the environment of a lane backend.

Parameters:
auth_tokenstr

Bearer token the lane must require.

Returns:
dict[str, str]

Environment with the token and Host/Origin validation enabled, which guards the loopback port against DNS rebinding and cross-site requests.

spawner.backend_command(host: str, port: int, extra_args: list[str]) → list[str]#

Build the command line of a lane backend.

Parameters:
hoststr

Host to bind.

portint

Port to bind.

extra_argslist[str]

Additional ansys-aedt-mcp arguments.

Returns:
list[str]

Command line.

spawner.spawn_backend(host: str, port: int, extra_args: list[str], log_path: pathlib.Path, auth_token: str) → subprocess.Popen[bytes]#

Start a lane backend detached from the current process.

Parameters:
hoststr

Host to bind.

portint

Port to bind.

extra_argslist[str]

Additional ansys-aedt-mcp arguments.

log_pathpathlib.Path

File receiving the backend stdout and stderr.

auth_tokenstr

Bearer token the lane must require.

Returns:
subprocess.Popen

Handle of the spawned process.

spawner.ensure_backend(server_id: str, host: str = DEFAULT_HOST, extra_args: list[str] | None = None, startup_timeout: float = DEFAULT_STARTUP_TIMEOUT) → LaneHandle#

Return a live lane named server_id, spawning it if needed.

A free port is reserved in the registry before spawning, so concurrent proxies cannot pick the same one. The spawn itself runs outside the registry lock because it can take many seconds.

Parameters:
server_idstr

Lane name.

hoststr, default: “127.0.0.1”

Host the lane binds to.

extra_argslist[str], optional

Additional ansys-aedt-mcp arguments for a new lane.

startup_timeoutfloat, default: 60.0

Seconds a new lane gets to bind its port.

Returns:
LaneHandle

Connection details of the lane.

Raises:
RuntimeError

If a new lane does not start in time.

spawner.DEFAULT_HOST = '127.0.0.1'#
spawner.DEFAULT_STARTUP_TIMEOUT = 60.0#

Seconds a new lane gets to bind its HTTP port.

spawner.BACKEND_MODULE = 'ansys.aedt.mcp'#