mxcubecore.ipc

IPC layer exposing a whitelist of HardwareObject methods to third-party clients, over a JSON-RPC/TCP or NanoMQ (MQTT) transport.

See IPC_FORMAT.md for the wire format.

class mxcubecore.ipc.ErrorCode(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: IntEnum

Error codes carried in an mxcubecore.ipc.models.IPCError.

The negative range below -32000 mirrors the JSON-RPC 2.0 reserved codes so a plain JSON-RPC client can interpret them, as defined by the JSON-RPC 2.0 spec.

class mxcubecore.ipc.IPCError(*, code: int, message: str, data: Any | None = None)[source]

Bases: BaseModel

Error payload carried in an IPCErrorResponse.

Parameters:
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class mxcubecore.ipc.IPCErrorResponse(*, jsonrpc: Literal['2.0'] = '2.0', id: str | int | None = None, error: IPCError)[source]

Bases: _JsonRpc

Error reply to an IPCRequest.

id is None when the request’s id couldn’t be determined (e.g. parse error).

Parameters:
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class mxcubecore.ipc.IPCEvent(*, jsonrpc: ~typing.Literal['2.0'] = '2.0', method: str, params: ~typing.Dict[str, ~typing.Any] = <factory>)[source]

Bases: _JsonRpc

A server-initiated notification (no id, no reply expected).

Parameters:
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class mxcubecore.ipc.IPCRequest(*, jsonrpc: ~typing.Literal['2.0'] = '2.0', id: str | int, method: str, params: ~typing.Dict[str, ~typing.Any] = <factory>)[source]

Bases: _JsonRpc

A command sent by the client to invoke one whitelisted method.

Parameters:
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class mxcubecore.ipc.IPCResponse(*, jsonrpc: Literal['2.0'] = '2.0', id: str | int, result: Any = None)[source]

Bases: _JsonRpc

Successful reply to an IPCRequest.

Parameters:
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class mxcubecore.ipc.IPCServer(name: str)[source]

Bases: HardwareObject

Beamline-configured IPC server. See IPC_FORMAT.md for the wire format and mxcubecore/ipc/whitelist.py for the whitelist rules.

Parameters:

name (str)

class HOConfig(*, transport: str = 'jsonrpc', host: str = '127.0.0.1', port: int = 9999, tls_cert_path: str | None = None, tls_key_path: str | None = None, broker_host: str = 'localhost', broker_port: int = 1883, topic_prefix: str = 'mxcube/ipc', tls_ca_cert_path: str | None = None, auth_token: str | None = None, allowed_methods: List[str] = [], events: List[Dict[str, Any]] = [], allow_debug_calls: bool = False, **extra_data: Any)[source]

Bases: HOConfig

Parameters:
  • transport (str)

  • host (str)

  • port (int)

  • tls_cert_path (str | None)

  • tls_key_path (str | None)

  • broker_host (str)

  • broker_port (int)

  • topic_prefix (str)

  • tls_ca_cert_path (str | None)

  • auth_token (str | None)

  • allowed_methods (List[str])

  • events (List[Dict[str, Any]])

  • allow_debug_calls (bool)

  • extra_data (Any)

model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

init() → None[source]

‘public’ post-initialization method. Override as needed.

For ConfiguredObjects called after loading contained objects.

Return type:

None

stop() → None[source]

Gentler (?) alternative to abort.

Override as necessary to implement.

Return type:

None

exception mxcubecore.ipc.IPCWhitelistError[source]

Bases: Exception

Invalid whitelist configuration or call to a non-whitelisted method.

mxcubecore.ipc.build_whitelist(beamline: Any, allowed_methods: List[str]) → Dict[str, WhitelistEntry][source]

Build the whitelist from the configured “role.method” entries.

Raises:

IPCWhitelistError – when a role or method could not be resolved, or a method is not IPC-eligible.

Parameters:
Return type:

Dict[str, WhitelistEntry]

mxcubecore.ipc.dispatch(beamline: Any, registry: Dict[str, WhitelistEntry], method_path: str, params: Dict[str, Any]) → Any[source]

Validate params, call a whitelisted method and return a JSON-safe result.

Raises:
  • IPCWhitelistError – when method_path is not whitelisted.

  • pydantic.ValidationError – when params don’t validate.

  • TypeError – when the method returns something other than its declared pydantic response model.

Parameters:
Return type:

Any

mxcubecore.ipc.ipc_method(request_model: Type[BaseModel] | None = None, response_model: Type[BaseModel] | None = None) → Callable[[Callable], Callable][source]

Mark a method as IPC-eligible with explicit request/response models.

Only needed when the models can’t be inferred from the method signature.

Parameters:
  • request_model (Type[BaseModel] | None)

  • response_model (Type[BaseModel] | None)

Return type:

Callable[[Callable], Callable]

Modules

constants

Constants for the mxcubecore IPC layer: wire format version and error codes.

debug

Whitelist bypass for exploring and debugging - see IPC_FORMAT.md, "Debug bypass".

dispatcher_bridge

Bridges HardwareObject signals (mxcubecore.dispatcher, used by HardwareObjectMixin.emit/.connect) into IPCEvent notifications pushed to the connected IPC client.

events

The IPCServer's events: whitelist: which HardwareObject signals are forwarded as IPCEvents, and (optionally) the declared types of their positional arguments.

gateway

models

Pydantic envelope models for the mxcubecore IPC wire format.

server

IPCServer: a HardwareObject that exposes a whitelist of other HardwareObjects' methods to a single external client, over either a JSON-RPC/TCP or a NanoMQ (MQTT) transport.

transports

whitelist

Whitelist of HardwareObject methods callable over IPC.