Skip to content

Call methods and host application tools

Use RPC to call a method exposed by a worker node. Use an application endpoint when the node needs to ask your application for a tool result, policy decision, or external service response. Both have correlated replies; a media output tap is a separate channel.

Exercise worker RPC modes

Install the MS2 native profile. Keep advanced_nodes.py, node_transform.py, and worker_session.py beside the client example:

python examples/python/rpc_modes.py

The complete client verifies a separate worker PID, configuration, streamed replies, fire-and-forget state changes, remote errors, and a deadline.

Worker declaration Client use Result
@rpc(reply=True) await ctrl.worker.describe() One correlated value
@rpc(stream=True) async for value in ctrl.worker.words(...) Stream of correlated values
@rpc await ctrl.worker.configure(...) Fire-and-forget dispatch; no application-result reply

When leaving a stream early, close the iterator returned by aiter(call). The deferred call object itself has no aclose():

async def first_word(ctrl):
    stream = aiter(ctrl.worker.words("one two three"))
    try:
        return await anext(stream)
    finally:
        await stream.aclose()

Use _timeout=... for a reply deadline. Remote method failures raise RpcError; a local reply deadline raises RpcTimeoutError. A deadline ending your wait does not establish that remote work stopped. Use the node's cancellation contract when work must stop. The example confirms that a later call still succeeds after a timed-out call.

Let a worker call your application

Run either transport against a real worker:

python examples/python/application_endpoint.py
python examples/python/application_endpoint_grpc.py

The worker calls await self.call_endpoint("application", "decorate", text=..., deadline_ms=3000) inside process(). The application hosts that name, receives an EndpointCall, checks the method and arguments, replies, and observes APP: HELLO as the processed output. The gRPC launcher runs the same flow through a loopback GrpcServer; it requires the native grpc feature.

For an already attached ctrl, the essential registration is:

async def serve_one(ctrl):
    host = await ctrl.host_endpoint("application", methods={"decorate": "reply"})
    try:
        call = await anext(host)
        ack = await call.reply({"text": call.kwargs["text"].upper()})
        assert ack.accepted
    finally:
        await host.close()

Production code should bound the wait, validate arguments, and handle endpoint errors. Hosting belongs to an attach, not to the lifetime of the model. Closing the host unregisters it; gRPC unregistration is asynchronous, so the example polls has_endpoint() with a three-second deadline. Losing the responder can fail an in-flight call with responder_gone.

Transport boundaries

The reviewed endpoint API also declares streaming and fire-and-forget methods. The runnable endpoint example validates request/reply only; the RPC example validates all three worker RPC modes.

In-process duplicate endpoint registration is tested to raise already_hosted. The tested gRPC build accepted a second registration from the same attach and replaced the host, despite the default takeover=False. Do not rely on duplicate-registration rejection being identical across transports. Endpoint enumeration also exposes different detail in-process and over gRPC.

For Entry routing, run the Scaling harness with --tools and a runner built with both loadable and python-source-plugin. It deploys the source plugin, calls echo.describe() through Entry, hosts the application endpoint through the same backend attach, and receives the worker's result. Backend tokens belong on the trusted admin connection; the join ticket belongs to the media member.

Cleanup and limits

Close streams, endpoint hosts, attaches, and finally the owned session. The MS2 supervisor also checks descendant process exit. Some native worker shutdowns log a copy-accounting closure error even when all children exit; see troubleshooting. Process cleanup and complete copy-accounting records are separate claims.