flowgentic.tools.service¶
service
¶
The tool service: one process that calls MCP tools for many agent processes.
Agent processes send requests on one request queue; the service answers each
process on its own reply queue. The queues are the caller's: anything with
put(bytes), get(timeout=...) and get(block=False) that raises
queue.Empty, such as Dragon's queues, multiprocessing.Queue or
queue.Queue. Flowgentic imports nothing from Dragon.
In the service process::
pool = SupervisedMcpPool(urls, clients=8, per_client=16)
stats = await serve(requests, replies, pool)
In agent process i::
client = ToolServiceClient(requests, replies[i], index=i)
client.start() # on the agent process's event loop
result = await client.call(uid, "lookup", {"key": "x"}, idempotent=True)
idempotent=True says the tool can safely run twice, as a lookup can: a
call that lost its connection is sent again, and a failure that left its
outcome unknown is not kept. Leave it out for tools with side effects.
A repeated uid is answered without calling again: while the first call runs,
the new asker joins its waiters; once it has ended, the asker gets the kept
answer at once. The service keeps each final answer until it stops, in memory
or in an answer_store: a result, or a failure after which a call that is
not idempotent may have run. Any other failure, such as a call that never left
the queue, reaches its waiters but is not kept, so asking again calls again. A
None on the request queue stops the service.
Requests and answers travel in batches of up to 512, pickled. Pickle is safe here only because both ends are processes of the same job; the queues must not be reachable from outside it.
Classes:
| Name | Description |
|---|---|
ToolServiceError |
The service could not complete a call; the message says why. |
ToolService |
What |
ToolServiceClient |
An agent process's side of the tool service. |
Functions:
| Name | Description |
|---|---|
serve |
Serve MCP calls until |
ToolServiceError
¶
Bases: Exception
The service could not complete a call; the message says why.
ToolService
¶
ToolService(
pool: SupervisedMcpPool,
out: Callable[[int, list], None],
call_timeout: float = 120.0,
listener: Callable[
[str, str, float, float, float, bool], None
]
| None = None,
*,
answer_store: AnswerStore | None = None,
)
What serve runs: MCP calls through pool, answered through
out(src, answers). serve is the way to run it; a benchmark of the
service can drive it directly, handing on_requests a burst.
Methods:
| Name | Description |
|---|---|
stats |
Requests received, calls made, repeats, failures not kept, answers |
stats
¶
Requests received, calls made, repeats, failures not kept, answers that could not be delivered, and outcomes per tool.
ToolServiceClient
¶
ToolServiceClient(
requests: Any,
replies: Any,
index: int,
*,
listener: Callable[[str, float, float], None]
| None = None,
timeout: float | None = None,
call_timeout: float = 120.0,
alive: Callable[[], bool] | None = None,
)
An agent process's side of the tool service.
await call(uid, tool, args) sends a request and returns the tool's
result, or raises ToolServiceError, also once the service has stopped.
Pass idempotent=True only for a tool that can safely run twice, such as
a lookup. Requests made in one pass of the event loop go out together. A
second call with a uid still in flight shares the first call's answer.
args travel as JSON values, as MCP takes them: pydantic models, enums
and other values with a JSON form are converted before they are sent, so
the service never needs the agent's classes. Arguments with no JSON form
fail their own call at once.
A call with no answer within timeout seconds fails, so an agent does
not wait forever on a service that is gone. Pass call_timeout, the one
serve runs with (120 s by default), and leave timeout out: it is then
call_timeout and 30 s for the queues, 150 s by default. The service
ends every call within its call_timeout, resends included, so a client
that waits longer never gives up on a call the service still makes; a
timeout not above call_timeout is refused. Asking again with the
same uid sends the request again.
alive, if given, tells whether the service is still running. The
client asks it from its reader thread, in the agent process, about once a
second while no answer arrives; once it returns false, the client fails
every waiting call and stops. For a service on the same node, test its
pid: os.kill(pid, 0) raises ProcessLookupError once the process is
gone and its parent has reaped it. A multiprocessing.Process's
is_alive works only in the process that started the service. A check
that raises counts as the service running, and is logged once as a
warning.
listener(uid, total_s, at_service_s) is called once per answer: the
time from asking to the answer, and of that, the time the service held the
call.
Methods:
| Name | Description |
|---|---|
start |
Start reading answers; call it on the event loop that makes calls. |
stop |
Stop reading answers, within a second, and fail the calls still |
stats |
Requests sent and answered, errors, calls that reached their |
mcp_data
¶
(ok, data) from an MCP CallToolResult.
The data is the structured content, if the result has some. Otherwise it
is the text: each text block parsed as JSON, or {"text": text} if it is
not JSON, and a list of them if there are several. A success with no
content, or only empty text, is (True, None): the tool ran and returned
nothing. A success whose only content is not text, such as an image, is a
failure, since the agent would get nothing it can read.
serve
async
¶
serve(
requests: Any,
replies: list,
pool: SupervisedMcpPool,
*,
call_timeout: float = 120.0,
connect_timeout: float = 60.0,
on_ready: Callable[[dict], Awaitable[None] | None]
| None = None,
listener: Callable[
[str, str, float, float, float, bool], None
]
| None = None,
drain_timeout: float = 60.0,
answer_store: AnswerStore | None = None,
) -> dict
Serve MCP calls until None arrives on requests; return counters.
replies[i] is agent process i's reply queue. The pool is started
here, with a client up on every server, and closed at the end. on_ready
is called with the pool's state once it is serving. listener(uid, tool,
received, called, answered, ok) is called once per call, for whoever keeps
per-call times.