Top-Level Functions
The most common entry points. Import with import mitk_workbench_remote as mw.
- mitk_workbench_remote.connect(url='http://localhost:8080', *, token=None, timeout=30.0, transfer_mode=None, trust_env=None)
Connect to a running MITK Workbench REST server.
No HTTP call is made during construction — the connection is established lazily on first use.
- Parameters:
url (
str(default:'http://localhost:8080')) – Base URL of the Workbench REST server.token (
str|None(default:None)) – Optional API token (Authorization: Bearerheader).timeout (
float(default:30.0)) – Request timeout in seconds.transfer_mode (
str|None(default:None)) – Override transfer mode ("direct"or"file-reference"). WhenNonethe mode is auto-detected from server capabilities on first data request.trust_env (
bool|None(default:None)) – Whether to honor environment proxy settings. WhenNone(default) env proxies are bypassed for loopback URLs and honored for remote hosts — a forward/corporate proxy cannot reach the caller’s own machine, so proxying alocalhostrequest would fail. PassTrueto always honor env proxies orFalseto never.
- Return type:
- Returns:
A
Workbenchhandle ready for use.
- mitk_workbench_remote.discover(ports=range(8080, 8100), *, timeout=0.5)
Probe localhost ports for running MITK Workbench instances.
Probes are executed concurrently. Any port that responds to
GET /api/v1/healthwith a 2xx status is included in the result.
- mitk_workbench_remote.launch(executable=None, *, port=None, token=None, timeout=30.0, extra_args=None, new_instance=False)
Start a new MITK Workbench process with the REST API enabled.
Spawns the process, waits for the REST server to become healthy, and returns a connected
Workbenchhandle.Single-instance behavior. MITK Workbench is single-instance: starting it while an instance is already running makes the new process hand off to the running one and exit without bringing up a REST server on the requested port.
launch()detects a running instance up front (Windows) and, by default, raisesMitkErrorwith guidance toconnect()/discover()instead. Passnew_instance=Trueto force a genuinely separate, isolated instance (see below).Authentication token. The token is passed to the process through the
MITK_REST_API_TOKENenvironment variable, which the server prefers over the stored preference. The token is therefore never written to disk — not to the preferences file this call generates, nor to anyprefs.xmlthe server persists — whilerequireAuth=truestill enforces it.``new_instance=True``. When another Workbench is already running, this forces a separate instance via
--BlueBerry.newInstance. That instance runs from a fresh MITK-managed temporary storage directory (its REST preferences are seeded with--MITK.preferences-patch; no token is written there). Note that MITK does not remove this temporary directory afterward, so forced instances leave behind (secret-free) temp directories. When no instance is running,new_instancehas no effect — the normal launch already yields a fresh instance without a throwaway storage directory. This flag is only acted on where a running instance can be detected (Windows).The forced instance only gets its own throwaway storage if MITK actually starts a separate instance — i.e. the already-running Workbench is the single-instance owner. In the unusual case where a Workbench process exists but is not that owner (e.g. it is itself a forced instance), MITK does not fork and the seeded REST preferences (
enabled/autoStart/port, never the token) are written to the normal preferences store instead.On Windows the shipped launcher is a
.batwrapper thatstart /Bdetaches the realMitkWorkbench.exe; the trackedcmd.exethen exits 0 within ~0.1 s, long before the REST server is up.launch()therefore tolerates an exit code of 0 and keeps polling/health; only a non-zero exit is treated as an immediate failure. The trade-off: a direct executable that genuinely exits 0 right away (e.g.--help) is not detected until thetimeoutdeadline rather than instantly.The child’s stderr is redirected to a temp file (never an inherited PIPE, which the detached exe would hold open and deadlock on). On success the file is removed by
shutdown(); on a failed launch it is removed before raising. Any failure also kills the process listening onport(the detached exe), since a raisedlaunch()returns no handle for the caller to clean up later.- Parameters:
executable (
str|Path|None(default:None)) – Path to the MITK Workbench executable. WhenNone, theMITK_WORKBENCHenvironment variable is used. Pass either this argument or set the env var.port (
int|None(default:None)) – Port for the REST server. WhenNone, the first free port in 8080-8099 is chosen automatically.token (
str|None(default:None)) – API token for the new server. WhenNone, a secure random 32-character hex token is generated.timeout (
float(default:30.0)) – Maximum seconds to wait for the server to become healthy before giving up.extra_args (
list[str] |None(default:None)) – Additional command-line arguments appended after the preference file reference.new_instance (
bool(default:False)) – Force a separate, isolated instance when a Workbench is already running, instead of raising. See the note above.
- Return type:
- Returns:
A
Workbenchhandle backed by the newly started process.- Raises:
MitkError – If
executableisNoneandMITK_WORKBENCHis not set, if no free port is available, if a Workbench is already running andnew_instanceisFalse, or if the Workbench process exits immediately with a preference failure.FileNotFoundError – If the resolved executable path does not exist.
MitkConnectionError – If the server does not become healthy within
timeoutseconds.