Core (v1.1.0)

toolbox_core.client

ToolboxClient Objects

class ToolboxClient()

An asynchronous client for interacting with a Toolbox service.

Provides methods to discover and load tools defined by a remote Toolbox service endpoint. It manages an underlying aiohttp.ClientSession, if one is not provided.

__init__

def __init__(url: str,
             session: Optional[ClientSession] = None,
             client_headers: Optional[Mapping[str,
                                              Union[Callable[[], str],
                                                    Callable[[],
                                                             Awaitable[str]],
                                                    str]]] = None,
             protocol: Protocol = Protocol.MCP,
             client_name: Optional[str] = None,
             client_version: Optional[str] = None,
             telemetry_enabled: bool = False)

Initializes the ToolboxClient.

Arguments:

  • url - The base URL for the Toolbox service API (e.g., “http://localhost:5000”).
  • session - An optional existing aiohttp.ClientSession to use. If None (default), a new session is created internally. Note that if a session is provided, its lifecycle (including closing) should typically be managed externally.
  • client_headers - Headers to include in each request sent through this client.
  • protocol - The communication protocol to use.
  • client_name - Optional client name for identification.
  • client_version - Optional client version for identification.
  • telemetry_enabled - Whether to enable OpenTelemetry tracing and metrics. (Default: False)

__aenter__

async def __aenter__()

Enter the runtime context related to this client instance.

Allows the client to be used as an asynchronous context manager (e.g., async with ToolboxClient(...) as client:).

Returns:

  • self - The client instance itself.

__aexit__

async def __aexit__(exc_type, exc_val, exc_tb)

Exit the runtime context and close the internally managed session.

Allows the client to be used as an asynchronous context manager (e.g., async with ToolboxClient(...) as client:).

close

async def close()

Asynchronously closes the underlying client session. Doing so will cause any tools created by this Client to cease to function.

If the session was provided externally during initialization, the caller is responsible for its lifecycle.

load_tool

async def load_tool(
    name: str,
    auth_token_getters: Mapping[str, Union[Callable[[], str],
                                           Callable[[], Awaitable[str]]]] = {},
    bound_params: Mapping[str, Union[Callable[[], Any],
                                     Callable[[], Awaitable[Any]], Any]] = {}
) -> ToolboxTool

Asynchronously loads a tool from the server.

Retrieves the schema for the specified tool from the Toolbox server and returns a callable object (ToolboxTool) that can be used to invoke the tool remotely.

Arguments:

  • name - The unique name or identifier of the tool to load.
  • auth_token_getters - A mapping of authentication service names to callables that return the corresponding authentication token.
  • bound_params - A mapping of parameter names to bind to specific values or callables that are called to produce values as needed.

Returns:

  • ToolboxTool - A callable object representing the loaded tool, ready for execution. The specific arguments and behavior of the callable depend on the tool itself.

Raises:

  • ValueError - If the loaded tool instance fails to utilize at least one provided parameter or auth token (if any provided).

load_toolset

async def load_toolset(name: Optional[str] = None,
                       auth_token_getters: Mapping[str, Union[Callable[
                           [], str], Callable[[], Awaitable[str]]]] = {},
                       bound_params: Mapping[str,
                                             Union[Callable[[], Any],
                                                   Callable[[],
                                                            Awaitable[Any]],
                                                   Any]] = {},
                       strict: bool = False) -> list[ToolboxTool]

Asynchronously fetches a toolset and loads all tools defined within it.

Arguments:

  • name - Name of the toolset to load. If None, loads the default toolset.
  • auth_token_getters - A mapping of authentication service names to callables that return the corresponding authentication token.
  • bound_params - A mapping of parameter names to bind to specific values or callables that are called to produce values as needed.
  • strict - If True, raises an error if any loaded tool instance fails to utilize all of the given parameters or auth tokens. (if any provided). If False (default), raises an error only if a user-provided parameter or auth token cannot be applied to any loaded tool across the set.

Returns:

  • list[ToolboxTool] - A list of callables, one for each tool defined in the toolset.

Raises:

  • ValueError - If validation fails based on the strict flag.

add_headers

@deprecated(
    "Use the `client_headers` parameter in the ToolboxClient constructor instead."
)
def add_headers(
    headers: Mapping[str, Union[Callable[[], str], Callable[[],
                                                            Awaitable[str]]]]
) -> None

Add headers to be included in each request sent through this client.

Arguments:

  • headers - Headers to include in each request sent through this client.

Raises:

  • ValueError - If any of the headers are already registered in the client.

toolbox_core.sync_client

ToolboxSyncClient Objects

class ToolboxSyncClient()

A synchronous client for interacting with a Toolbox service.

Provides methods to discover and load tools defined by a remote Toolbox service endpoint.

__init__

def __init__(url: str,
             client_headers: Optional[Mapping[str,
                                              Union[Callable[[], str],
                                                    Callable[[],
                                                             Awaitable[str]],
                                                    str]]] = None,
             protocol: Protocol = Protocol.MCP,
             client_name: Optional[str] = None,
             client_version: Optional[str] = None,
             telemetry_enabled: bool = False)

Initializes the ToolboxSyncClient.

Arguments:

  • url - The base URL for the Toolbox service API (e.g., “http://localhost:5000”).
  • client_headers - Headers to include in each request sent through this client.
  • protocol - The communication protocol to use.
  • client_name - Optional client name for identification.
  • client_version - Optional client version for identification.
  • telemetry_enabled - Whether to enable OpenTelemetry tracing and metrics. (Default: False)

close

def close()

Synchronously closes the underlying client session. Doing so will cause any tools created by this Client to cease to function.

load_tool

def load_tool(
    name: str,
    auth_token_getters: Mapping[str, Union[Callable[[], str],
                                           Callable[[], Awaitable[str]]]] = {},
    bound_params: Mapping[str, Union[Callable[[], Any],
                                     Callable[[], Awaitable[Any]], Any]] = {}
) -> ToolboxSyncTool

Synchronously loads a tool from the server.

Retrieves the schema for the specified tool from the Toolbox server and returns a callable object (ToolboxSyncTool) that can be used to invoke the tool remotely.

Arguments:

  • name - The unique name or identifier of the tool to load.
  • auth_token_getters - A mapping of authentication service names to callables that return the corresponding authentication token.
  • bound_params - A mapping of parameter names to bind to specific values or callables that are called to produce values as needed.

Returns:

  • ToolboxSyncTool - A callable object representing the loaded tool, ready for execution. The specific arguments and behavior of the callable depend on the tool itself.

load_toolset

def load_toolset(name: Optional[str] = None,
                 auth_token_getters: Mapping[str, Union[Callable[
                     [], str], Callable[[], Awaitable[str]]]] = {},
                 bound_params: Mapping[str, Union[Callable[[], Any],
                                                  Callable[[], Awaitable[Any]],
                                                  Any]] = {},
                 strict: bool = False) -> list[ToolboxSyncTool]

Synchronously fetches a toolset and loads all tools defined within it.

Arguments:

  • name - Name of the toolset to load. If None, loads the default toolset.
  • auth_token_getters - A mapping of authentication service names to callables that return the corresponding authentication token.
  • bound_params - A mapping of parameter names to bind to specific values or callables that are called to produce values as needed.
  • strict - If True, raises an error if any loaded tool instance fails to utilize all of the given parameters or auth tokens. (if any provided). If False (default), raises an error only if a user-provided parameter or auth token cannot be applied to any loaded tool across the set.

Returns:

  • list[ToolboxSyncTool] - A list of callables, one for each tool defined in the toolset.

Raises:

  • ValueError - If validation fails based on the strict flag.

add_headers

@deprecated(
    "Use the `client_headers` parameter in the ToolboxClient constructor instead."
)
def add_headers(
    headers: Mapping[str, Union[Callable[[], str],
                                Callable[[], Awaitable[str]], str]]
) -> None

Add headers to be included in each request sent through this client.

Arguments:

  • headers - Headers to include in each request sent through this client.

Raises:

  • ValueError - If any of the headers are already registered in the client.

__enter__

def __enter__()

Enter the runtime context related to this client instance.

Allows the client to be used as a context manager (e.g., with ToolboxSyncClient(...) as client:).

Returns:

  • self - The client instance itself.

__exit__

def __exit__(exc_type, exc_val, exc_tb)

Exit the runtime context and close the internally managed session.

Allows the client to be used as a context manager (e.g., with ToolboxSyncClient(...) as client:).

toolbox_core.tool

ToolboxTool Objects

class ToolboxTool()

A callable proxy object representing a specific tool on a remote Toolbox server.

Instances of this class behave like asynchronous functions. When called, they send a request to the corresponding tool’s endpoint on the Toolbox server with the provided arguments.

It utilizes Python’s introspection features (__name__, __doc__, __signature__, __annotations__) so that standard tools like help() and inspect work as expected.

__init__

def __init__(
    transport: ITransport, name: str, description: str,
    params: Sequence[ParameterSchema],
    required_authn_params: Mapping[str, list[str]],
    required_authz_tokens: Sequence[str],
    auth_service_token_getters: Mapping[str, Union[Callable[[], str],
                                                   Callable[[],
                                                            Awaitable[str]]]],
    bound_params: Mapping[str, Union[Callable[[], Any],
                                     Callable[[], Awaitable[Any]], Any]],
    client_headers: Mapping[str, Union[Callable[[], str],
                                       Callable[[], Awaitable[str]], str]])

Initializes a callable that will trigger the tool invocation through the Toolbox server.

Arguments:

  • transport - The transport used for making API requests.
  • name - The name of the remote tool.
  • description - The description of the remote tool.
  • params - The args of the tool.
  • required_authn_params - A map of required authenticated parameters to a list of alternative services that can provide values for them.
  • required_authz_tokens - A sequence of alternative services for providing authorization token for the tool invocation.
  • auth_service_token_getters - A dict of authService -> token (or callables that produce a token)
  • bound_params - A mapping of parameter names to bind to specific values or callables that are called to produce values as needed.
  • client_headers - Client specific headers bound to the tool.

__call__

async def __call__(*args: Any, **kwargs: Any) -> str

Asynchronously calls the remote tool with the provided arguments.

Validates arguments against the tool’s signature, then sends them as a JSON payload in a POST request to the tool’s invoke URL.

Arguments:

  • *args - Positional arguments for the tool.
  • **kwargs - Keyword arguments for the tool.

Returns:

The string result returned by the remote tool execution.

add_telemetry_attributes

def add_telemetry_attributes(
        telemetry_attributes: TelemetryAttributes) -> "ToolboxTool"

Returns a copy of this tool with the given telemetry attributes set.

The original tool is left untouched, mirroring the immutable pattern used by add_auth_token_getter and bind_params. Attributes are sent to the server on every invocation of the returned tool. A second call replaces (does not merge with) the previous attributes.

Arguments:

  • telemetry_attributes - The telemetry attributes to attach.

Returns:

A new ToolboxTool instance with the attributes attached.

add_auth_token_getters

def add_auth_token_getters(
    auth_token_getters: Mapping[str, Union[Callable[[], str],
                                           Callable[[], Awaitable[str]]]]
) -> "ToolboxTool"

Registers auth token getter functions that are used for AuthServices when tools are invoked.

Arguments:

  • auth_token_getters - A mapping of authentication service names to callables that return the corresponding authentication token.

Returns:

A new ToolboxTool instance with the specified authentication token getters registered.

Raises:

  • ValueError - If an auth source has already been registered either to the tool or to the corresponding client.

add_auth_token_getter

def add_auth_token_getter(
    auth_source: str, get_id_token: Union[Callable[[], str],
                                          Callable[[], Awaitable[str]]]
) -> "ToolboxTool"

Registers an auth token getter function that is used for AuthService when tools are invoked.

Arguments:

  • auth_source - The name of the authentication source.
  • get_id_token - A function that returns the ID token.

Returns:

A new ToolboxTool instance with the specified authentication token getter registered.

Raises:

  • ValueError - If the auth source has already been registered either to the tool or to the corresponding client.

bind_params

def bind_params(
    bound_params: Mapping[str, Union[Callable[[], Any],
                                     Callable[[], Awaitable[Any]], Any]]
) -> "ToolboxTool"

Binds parameters to values or callables that produce values.

Arguments:

  • bound_params - A mapping of parameter names to values or callables that produce values.

Returns:

A new ToolboxTool instance with the specified parameters bound.

Raises:

  • ValueError - If a parameter is already bound or is not defined by the tool’s definition.

bind_param

def bind_param(
    param_name: str, param_value: Union[Callable[[], Any],
                                        Callable[[], Awaitable[Any]], Any]
) -> "ToolboxTool"

Binds a parameter to the value or callable that produce the value.

Arguments:

  • param_name - The name of the bound parameter.
  • param_value - The value of the bound parameter, or a callable that returns the value.

Returns:

A new ToolboxTool instance with the specified parameter bound.

Raises:

  • ValueError - If the parameter is already bound or is not defined by the tool’s definition.

toolbox_core.sync_tool

ToolboxSyncTool Objects

class ToolboxSyncTool()

A callable proxy object representing a specific tool on a remote Toolbox server.

Instances of this class behave like synchronous functions. When called, they send a request to the corresponding tool’s endpoint on the Toolbox server with the provided arguments.

It utilizes Python’s introspection features (__name__, __doc__, __signature__, __annotations__) so that standard tools like help() and inspect work as expected.

__init__

def __init__(async_tool: ToolboxTool, loop: AbstractEventLoop, thread: Thread)

Initializes a callable that will trigger the tool invocation through the Toolbox server.

Arguments:

  • async_tool - An instance of the asynchronous ToolboxTool.
  • loop - The event loop used to run asynchronous tasks.
  • thread - The thread to run blocking operations in.

__call__

def __call__(*args: Any, **kwargs: Any) -> str

Synchronously calls the remote tool with the provided arguments.

Validates arguments against the tool’s signature, then sends them as a JSON payload in a POST request to the tool’s invoke URL.

Arguments:

  • *args - Positional arguments for the tool.
  • **kwargs - Keyword arguments for the tool.

Returns:

The string result returned by the remote tool execution.

add_auth_token_getters

def add_auth_token_getters(
    auth_token_getters: Mapping[str, Union[Callable[[], str],
                                           Callable[[], Awaitable[str]]]]
) -> "ToolboxSyncTool"

Registers auth token getter functions that are used for AuthServices when tools are invoked.

Arguments:

  • auth_token_getters - A mapping of authentication service names to callables that return the corresponding authentication token.

Returns:

A new ToolboxSyncTool instance with the specified authentication token getters registered.

Raises:

  • ValueError - If an auth source has already been registered either to the tool or to the corresponding client.

add_auth_token_getter

def add_auth_token_getter(
    auth_source: str, get_id_token: Union[Callable[[], str],
                                          Callable[[], Awaitable[str]]]
) -> "ToolboxSyncTool"

Registers an auth token getter function that is used for AuthService when tools are invoked.

Arguments:

  • auth_source - The name of the authentication source.
  • get_id_token - A function that returns the ID token.

Returns:

A new ToolboxSyncTool instance with the specified authentication token getter registered.

Raises:

  • ValueError - If the auth source has already been registered either to the tool or to the corresponding client.

bind_params

def bind_params(
    bound_params: Mapping[str, Union[Callable[[], Any],
                                     Callable[[], Awaitable[Any]], Any]]
) -> "ToolboxSyncTool"

Binds parameters to values or callables that produce values.

Arguments:

  • bound_params - A mapping of parameter names to values or callables that produce values.

Returns:

A new ToolboxSyncTool instance with the specified parameters bound.

Raises:

  • ValueError - If a parameter is already bound or is not defined by the tool’s definition.

bind_param

def bind_param(
    param_name: str, param_value: Union[Callable[[], Any],
                                        Callable[[], Awaitable[Any]], Any]
) -> "ToolboxSyncTool"

Binds a parameter to the value or callable that produce the value.

Arguments:

  • param_name - The name of the bound parameter.
  • param_value - The value of the bound parameter, or a callable that returns the value.

Returns:

A new ToolboxSyncTool instance with the specified parameter bound.

Raises:

  • ValueError - If the parameter is already bound or is not defined by the tool’s definition.

add_telemetry_attributes

def add_telemetry_attributes(
        telemetry_attributes: TelemetryAttributes) -> "ToolboxSyncTool"

Returns a copy of this tool with the given telemetry attributes set.

Mirrors ToolboxTool.add_telemetry_attributes. The original tool is left untouched. Attributes are sent to the server on every invocation of the returned tool; a second call replaces (does not merge with) the previous attributes.

Arguments:

  • telemetry_attributes - The telemetry attributes to attach.

Returns:

A new ToolboxSyncTool instance with the attributes attached.

toolbox_core.auth_methods

This module provides functions to obtain Google ID tokens for a specific audience.

The tokens are returned as “Bearer” strings for direct use in HTTP Authorization headers. It uses a simple in-memory cache to avoid refetching on every call.

Example Usage: from toolbox_core import auth_methods

URL = “https://toolbox-service-url” async with ToolboxClient( URL, client_headers={“Authorization”: auth_methods.aget_google_id_token}) as toolbox: tools = await toolbox.load_toolset()

get_google_id_token

def get_google_id_token(
        audience: Optional[str] = None,
        clock_skew_in_seconds: int = DEFAULT_CLOCK_SKEW) -> Callable[[], str]

Returns a SYNC function that, when called, fetches a Google ID token. This function uses Application Default Credentials for local systems and standard google auth libraries for Google Cloud environments. It caches the token in memory.

Arguments:

  • audience - The audience for the ID token (e.g., a service URL or client ID).
  • clock_skew_in_seconds - The number of seconds to tolerate when checking the token. Must be between 0-60. Defaults to 0.

Returns:

A function that when executed returns string in the format “Bearer <google_id_token>”.

Raises:

  • GoogleAuthError - If fetching credentials or the token fails.
  • ValueError - If the fetched token is invalid.

aget_google_id_token

def aget_google_id_token(
    audience: Optional[str] = None,
    clock_skew_in_seconds: int = DEFAULT_CLOCK_SKEW
) -> Callable[[], Coroutine[Any, Any, str]]

Returns an ASYNC function that, when called, fetches a Google ID token. This function uses Application Default Credentials for local systems and standard google auth libraries for Google Cloud environments. It caches the token in memory.

Arguments:

  • audience - The audience for the ID token (e.g., a service URL or client ID).
  • clock_skew_in_seconds - The number of seconds to tolerate when checking the token. Must be between 0-60. Defaults to 0.

Returns:

An async function that when executed returns string in the format “Bearer <google_id_token>”.

Raises:

  • GoogleAuthError - If fetching credentials or the token fails.
  • ValueError - If the fetched token is invalid.

toolbox_core.protocol

TelemetryAttributes Objects

class TelemetryAttributes(BaseModel)

Attributes passed to the server via _meta and exported on client spans.

Field names map to OpenTelemetry-style keys via serialization aliases, so model_dump(by_alias=True, exclude_none=True) produces the wire payload. Empty strings are coerced to None so they never reach the server, where they would otherwise appear as client.model= in SQL Commenter output.

The Python field for the model name is llm_model (not model) because pydantic v2 reserves model as a method namespace; declaring a field literally named model breaks the serializer. The wire alias client.model is unchanged from the server’s perspective.

Protocol Objects

class Protocol(str, Enum)

Defines how the client should choose between communication protocols.

get_supported_mcp_versions

@staticmethod
def get_supported_mcp_versions() -> list[str]

Returns a list of supported MCP protocol versions.

AdditionalPropertiesSchema Objects

class AdditionalPropertiesSchema(BaseModel)

Defines the value type for ‘object’ parameters.

get_value_type

def get_value_type() -> Type

Converts the string type to a Python type.

ParameterSchema Objects

class ParameterSchema(BaseModel)

Schema for a tool parameter.

has_default

@property
def has_default() -> bool

Returns True if default was explicitly provided in schema input.

ToolSchema Objects

class ToolSchema(BaseModel)

Schema for a tool.

ManifestSchema Objects

class ManifestSchema(BaseModel)

Schema for the Toolbox manifest.