Tool Post-Processors
Relevant source files
The following files were used as context for generating this wiki page:- core/src/utcp/interfaces/tool_post_processor.py
- core/src/utcp/implementations/post_processors/filter_dict_post_processor.py
- core/src/utcp/implementations/post_processors/limit_strings_post_processor.py
- core/README.md
- core/src/utcp/data/utcp_client_config.py
- core/src/utcp/implementations/utcp_client_implementation.py
Tool Post-Processors are a feature within the UTCP client that allows for the modification of a tool's result immediately after it is received and before it is returned to the caller. This enables developers to transform, filter, or format the output of any tool call in a standardized and configurable way. Post-processors are defined as a list in the UtcpClientConfig and are applied sequentially to the result of a tool call.
This system is designed to be extensible, allowing for custom post-processing logic to be implemented and plugged into the client. Each post-processor can be configured to apply to all tools or only to a specific subset of tools, providing granular control over the final output.
Sources: core/src/utcp/interfaces/tool_post_processor.py:10-16, core/src/utcp/data/utcp_client_config.py:27
Architecture and Data Flow
Post-processors are integrated into the call_tool method of the UtcpClient. After a tool is executed via its communication protocol and a result is obtained, the client iterates through the configured post-processors and applies each one's post_process method to the result.
Sources: core/src/utcp/implementations/utcp_client_implementation.py:126-129
The following diagram illustrates the data flow within the call_tool method, highlighting the post-processing step.
This diagram shows that post-processing occurs within the UtcpClient after the raw result is received from the communication protocol layer.
Sources: core/src/utcp/implementations/utcp_client_implementation.py:111-130
Class Structure
The post-processor system is built around the ToolPostProcessor abstract base class and a corresponding ToolPostProcessorConfigSerializer for configuration management.
This diagram shows the abstract ToolPostProcessor and its concrete implementations. The ToolPostProcessorConfigSerializer is responsible for creating instances of these concrete classes from dictionary configurations.
Sources: core/src/utcp/interfaces/tool_post_processor.py, core/src/utcp/implementations/post_processors/filter_dict_post_processor.py, core/src/utcp/implementations/post_processors/limit_strings_post_processor.py
Configuration
Tool post-processors are configured within the UtcpClientConfig via the post_processing field, which is a list of post-processor configurations.
Sources: core/src/utcp/data/utcp_client_config.py:27
Example Configuration
The following JSON snippet shows how to configure a filter_dict post-processor in a client configuration file. This example configures the processor to only include the name and key fields for a specific tool's output.
{
"post_processing": [
{
"tool_post_processor_type": "filter_dict",
"only_include_keys": ["name", "key"],
"only_include_tools": ["openlibrary.read_search_authors_json_search_authors_json_get"]
}
]
}
Sources: core/README.md:121-128
Core Interfaces
ToolPostProcessor
This is the abstract base class that all post-processors must inherit from. It defines the fundamental contract for post-processing.
- File:
core/src/utcp/interfaces/tool_post_processor.py - Key Attributes:
tool_post_processor_type: str: A unique string identifier for the processor type.
- Key Methods:
post_process(self, caller: 'UtcpClient', tool: Tool, manual_call_template: 'CallTemplate', result: Any) -> Any: The abstract method that contains the logic to process the tool call result.
Sources: core/src/utcp/interfaces/tool_post_processor.py:9-28
ToolPostProcessorConfigSerializer
This class is responsible for serializing and deserializing post-processor configurations. It uses a dictionary of registered implementations (tool_post_processor_implementations) to delegate validation and conversion to the appropriate concrete serializer based on the tool_post_processor_type field.
- File:
core/src/utcp/interfaces/tool_post_processor.py - Key Methods:
to_dict(self, obj: ToolPostProcessor) -> dict: Converts aToolPostProcessorobject into a dictionary.validate_dict(self, data: dict) -> ToolPostProcessor: Validates a dictionary and converts it into aToolPostProcessorobject.
Sources: core/src/utcp/interfaces/tool_post_processor.py:30-61
Implementations
The UTCP core provides several built-in post-processor implementations.
FilterDictPostProcessor
This processor filters the keys of a dictionary or a list of dictionaries returned by a tool. It can be configured to either include or exclude specific keys.
tool_post_processor_type:"filter_dict"- Configuration Options:
| Field | Type | Description |
|---|---|---|
only_include_keys | Optional[List[str]] | If set, only these keys will be kept in the dictionary. |
only_exclude_keys | Optional[List[str]] | If set, these keys will be removed from the dictionary. |
only_include_tools | Optional[List[str]] | A list of tool names to which this processor should apply. If None, it applies to all tools. |
only_exclude_tools | Optional[List[str]] | A list of tool names to which this processor should not apply. |
Note: only_include_keys and only_exclude_keys are mutually exclusive.
Sources: core/src/utcp/implementations/post_processors/filter_dict_post_processor.py:12-32
LimitStringsPostProcessor
This processor truncates long strings within a tool's result to a specified maximum length. It recursively traverses the result data structure (including dictionaries and lists) and shortens any string that exceeds max_length.
tool_post_processor_type:"limit_strings"- Configuration Options:
| Field | Type | Default | Description |
|---|---|---|---|
max_length | int | 1000 | The maximum allowed length for any string in the result. |
only_include_tools | Optional[List[str]] | None | A list of tool names to which this processor should apply. |
only_exclude_tools | Optional[List[str]] | None | A list of tool names to which this processor should not apply. |
Sources: core/src/utcp/implementations/post_processors/limit_strings_post_processor.py:12-25
Summary
Tool Post-Processors provide a powerful and flexible mechanism for manipulating tool outputs directly within the UtcpClient. By leveraging a simple, extensible interface and a declarative configuration model, developers can easily tailor the results of tool calls to fit specific needs, such as reducing verbosity, filtering sensitive information, or transforming data into a more convenient format, without altering the tools themselves.