Tool Executor
ipybox.tool_exec.server.ToolServer
ToolServer(
host="localhost",
port: int = 8900,
approval_required: bool = False,
approval_timeout: float = 60,
connect_timeout: float = 30,
log_to_stderr: bool = False,
log_level: str = "INFO",
)
HTTP server that manages MCP servers and executes their tools with optional approval.
ToolServer provides HTTP endpoints for executing MCP tools and a WebSocket endpoint for sending approval requests to clients. MCP servers are started on demand when tools are first executed and cached for subsequent calls.
Endpoints:
PUT /reset: Closes all started MCP serversPOST /run: Executes an MCP tool (with optional approval)WS /approval: WebSocket endpoint forApprovalClientconnections
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
Hostname the server binds to. |
'localhost'
|
|
port
|
int
|
Port number the server listens on. |
8900
|
approval_required
|
bool
|
Whether tool calls require approval. |
False
|
approval_timeout
|
float
|
Timeout in seconds for approval requests. |
60
|
connect_timeout
|
float
|
Timeout in seconds for starting MCP servers. |
30
|
log_to_stderr
|
bool
|
Whether to log to stderr instead of stdout. |
False
|
log_level
|
str
|
Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
'INFO'
|
start
async
ipybox.tool_exec.client.ToolRunner
ToolRunner(
server_name: str,
server_params: dict[str, Any],
host: str = "localhost",
port: int = 8900,
)
Client for executing MCP tools on a ToolServer.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_name
|
str
|
Name of the MCP server. |
required |
server_params
|
dict[str, Any]
|
MCP server parameters. |
required |
host
|
str
|
Hostname of the |
'localhost'
|
port
|
int
|
Port number of the |
8900
|
run
async
Execute a tool on the configured MCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to execute. |
required |
tool_args
|
dict[str, Any]
|
Arguments to pass to the tool. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | str | None
|
The tool execution result. |
Raises:
| Type | Description |
|---|---|
ToolRunnerError
|
If tool execution fails or approval is denied. |
run_sync
Synchronous version of run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to execute. |
required |
tool_args
|
dict[str, Any]
|
Arguments to pass to the tool. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | str | None
|
The tool execution result. |
Raises:
| Type | Description |
|---|---|
ToolRunnerError
|
If tool execution fails or approval is denied. |
ipybox.tool_exec.client.ToolRunnerError
Bases: Exception
Raised when tool execution fails on the server or when approval is rejected.
ipybox.tool_exec.approval.client.ApprovalClient
Client for handling tool call approval requests.
ApprovalClient connects to a ToolServer's
ApprovalChannel and receives
approval requests. Each request is passed to the registered callback, which must
accept or reject the request.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
ApprovalCallback
|
Async function called for each approval request. |
required |
host
|
str
|
Hostname of the |
'localhost'
|
port
|
int
|
Port number of the |
8900
|
ipybox.tool_exec.approval.client.ApprovalRequest
ApprovalRequest(
server_name: str,
tool_name: str,
tool_args: dict[str, Any],
respond: Callable[[bool], Awaitable[None]],
)
An MCP tool call approval request.
ApprovalRequest instances are passed to the approval callback registered with
ApprovalClient. The callback
must call accept
or reject for making
an approval decision.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_name
|
str
|
Name of the MCP server providing the tool. |
required |
tool_name
|
str
|
Name of the tool to execute. |
required |
tool_args
|
dict[str, Any]
|
Arguments to pass to the tool. |
required |
respond
|
Callable[[bool], Awaitable[None]]
|
Function to make an approval decision. |
required |
ipybox.tool_exec.approval.server.ApprovalChannel
Server-side channel for tool call approval over WebSocket.
ApprovalChannel accepts WebSocket connections from an
ApprovalClient, sends approval
requests via JSON-RPC, and processes approval responses.
When approval_required is False, all approval requests are automatically granted.
When True, requests are sent to the connected ApprovalClient and the channel waits
for a response within the configured timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
approval_required
|
bool
|
Whether approval is required for tool execution. |
False
|
approval_timeout
|
float
|
Timeout in seconds for approval requests. |
60
|
connect
async
Accept a WebSocket connection and process approval responses.
This method runs until the WebSocket disconnects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
websocket
|
WebSocket
|
The WebSocket connection to accept. |
required |
join
async
join(timeout: float = 5)
Wait for the this approval channel to close.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Timeout in seconds to wait. |
5
|
request
async
Request approval for a tool call.
If approval_required is False, returns True immediately. Otherwise, sends an
approval request to the connected ApprovalClient and waits for a response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_name
|
str
|
Name of the MCP server providing the tool. |
required |
tool_name
|
str
|
Name of the tool to execute. |
required |
tool_args
|
dict[str, Any]
|
Arguments to pass to the tool. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no |
TimeoutError
|
If the approval request times out. |