ResourceClient
ResourceClient
ResourceClient(
port: int,
host: str = "localhost",
connect_retries: int = 10,
connect_retry_interval: float = 1.0,
)
Context manager for
- loading the source code of Python modules and generated MCP client functions
from an
ExecutionContainer
. - generating Python client functions from MCP server tool metadata and storing
the generated sources in an
ExecutionContainer
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
port
|
int
|
Host port for the container's resource port |
required |
host
|
str
|
Hostname or IP address of the container's host |
'localhost'
|
connect_retries
|
int
|
Number of connection retries. |
10
|
connect_retry_interval
|
float
|
Delay between connection retries in seconds. |
1.0
|
Source code in ipybox/resource/client.py
generate_mcp_sources
async
Generate Python client functions for tools provided by an MCP server.
One MCP client function is generated per MCP tool from its metadata. The generated function is stored in a
module named /app/{relpath}/{server_name}/{tool_name}.py
. Importing this module and calling the function
invokes the corresponding MCP tool. This works for both stdio
and sse
based MCP servers. stdio
based
MCP servers are executed inside the container, sse
based MCP servers are expected to run elsewhere.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relpath
|
str
|
Path relative to the container's |
required |
server_name
|
str
|
An application-defined name for the MCP server. Must be a valid Python module name. |
required |
server_params
|
dict[str, Any]
|
MCP server configuration. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
List of tool names provided by the MCP server. Tool names are sanitized to ensure they can be used as Python module names. |
Source code in ipybox/resource/client.py
get_mcp_sources
async
Get the source code of generated MCP client functions for given MCP server_name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relpath
|
str
|
Path relative to the container's |
required |
server_name
|
str
|
Application-defined name of an MCP server |
required |
Returns:
Type | Description |
---|---|
dict[str, str]
|
Source code of generated MCP client functions. Keys are tool names, values are generated sources. |
Source code in ipybox/resource/client.py
get_module_sources
async
Get the source code of Python modules on the container's Python path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_names
|
list[str]
|
A list of Python module names. |
required |
Returns:
Type | Description |
---|---|
dict[str, str]
|
Source code of Python modules. Keys are module names, values are module sources. |
Source code in ipybox/resource/client.py
ConnectionError
Bases: Exception
Raised when a connection to a resource server cannot be established.