CLI Protocol Plugin
Relevant source files
The following files were used as context for generating this wiki page:The CLI Protocol Plugin is a component of the Universal Tool Calling Protocol (UTCP) that enables the integration and execution of command-line tools and processes. It allows developers to wrap local shell commands and multi-step scripts, making them available as standard UTCP tools. This enables seamless interaction with command-line applications within the broader UTCP ecosystem, treating them just like any other tool, such as an HTTP API.
Key features of this plugin include multi-command sequential execution within a single stateful subprocess, cross-platform script generation for PowerShell (Windows) and Bash (Unix-like systems), flexible argument substitution, and the ability to reference the output of previous commands in subsequent steps. This makes it suitable for a wide range of use cases, from simple commands to complex data pipelines and build workflows.
Sources: plugins/communication_protocols/cli/README.md, README.md
Architecture and Core Components
The plugin's architecture revolves around two primary Pydantic models that define the configuration and behavior of CLI-based tools. These models are not present in the provided file list but their structure is detailed in the READMEs.
CliCallTemplate: This is the main configuration model for a CLI tool. It defines the sequence of commands to be executed, environment variables, the working directory, and execution timeout. It inherits from the baseCallTemplate.CliCommand: A sub-model used withinCliCallTemplateto define a single command in the execution sequence. It includes the command string and a flag to control whether its output is appended to the final result.
The following class diagram illustrates the relationship between these components.
This diagram shows that CliCallTemplate is a specific type of CallTemplate that contains one or more CliCommand objects.
Sources: core/tests/client/test_utcp_client.py:180-185, plugins/communication_protocols/cli/README.md
Installation and Registration
The CLI plugin is distributed as a separate package, utcp-cli, and can be installed via pip.
pip install utcp-cli
The plugin automatically registers itself with the UTCP core library using Python's entry points mechanism. The pyproject.toml file defines this entry point.
[project.entry-points."utcp.plugins"]
cli = "utcp_cli:register"
This configuration ensures that when the utcp-cli package is installed, the UTCP client becomes aware of the cli call_template_type and can delegate execution to this plugin.
Sources: plugins/communication_protocols/cli/README.md, plugins/communication_protocols/cli/pyproject.toml:29-30
Configuration
A CLI tool is configured using a CliCallTemplate object, typically defined within the manual_call_templates section of the UtcpClient configuration.
CliCallTemplate Fields
| Field | Type | Description | Default |
|---|---|---|---|
name | str | A unique name for this manual call template. | - |
call_template_type | str | Must be "cli". | "cli" |
commands | List[CliCommand] | A list of CliCommand objects to be executed sequentially. | - |
env_vars | Dict[str, str] | A dictionary of environment variables to set for the subprocess. | None |
working_dir | str | The working directory where the commands will be executed. | None |
auth | None | Not used for the CLI protocol; should be null. | None |
Sources: plugins/communication_protocols/cli/README.md, README.md
CliCommand Fields
| Field | Type | Description | Default |
|---|---|---|---|
command | str | The command-line string to execute. | - |
append_to_final_output | bool | If True, this command's output is appended to the final result. If False, it's hidden. For the last command in the sequence, this defaults to True. | False |
Sources: plugins/communication_protocols/cli/README.md
Configuration Example
Here is a JSON example of a CliCallTemplate for a multi-step Git analysis tool.
{
"name": "git_analysis",
"call_template_type": "cli",
"commands": [
{
"command": "git clone UTCP_ARG_repo_url_UTCP_END temp_repo",
"append_to_final_output": false
},
{
"command": "cd temp_repo",
"append_to_final_output": false
},
{
"command": "git log --oneline -10",
"append_to_final_output": true
}
],
"env_vars": {
"GIT_AUTHOR_NAME": "UTCP Bot"
}
}
Sources: plugins/communication_protocols/cli/README.md, README.md
Execution Flow
When a CLI tool is called, the plugin generates a single script containing all the specified commands. This script is then executed in an isolated subprocess shell. This design ensures that state, such as the current working directory (cd) and environment variables, is preserved across the sequence of commands.
The execution flow is as follows:
This diagram shows the process from the initial tool call to the final result or error.
Sources: plugins/communication_protocols/cli/README.md
Cross-Platform Scripting
The plugin automatically generates the appropriate script based on the operating system:
- Windows: A PowerShell script is created.
- Unix/Linux/macOS: A Bash script is created.
This allows for writing universal commands (like git status) that work across platforms, but platform-specific commands (like ls vs. Get-ChildItem) can also be used if the target environment is known.
Sources: plugins/communication_protocols/cli/README.md
Special Features
Argument Substitution
Tool arguments are passed into commands using a special placeholder format: UTCP_ARG_argname_UTCP_END. The plugin replaces these placeholders with the values provided in the tool_args dictionary during a call_tool invocation.
Example:
A command echo UTCP_ARG_message_UTCP_END called with tool_args={"message": "hello world"} will execute as echo "hello world".
Sources: plugins/communication_protocols/cli/README.md, core/tests/client/test_utcp_client.py:183
Output Referencing
The standard output of any command in the sequence can be referenced in subsequent commands using environment variables like $CMD_0_OUTPUT, $CMD_1_OUTPUT, etc. The number corresponds to the zero-based index of the command in the commands list.
Example:
{
"name": "conditional_processor",
"call_template_type": "cli",
"commands": [
{
"command": "git status --porcelain",
"append_to_final_output": false
},
{
"command": "echo \"Changes detected: $CMD_0_OUTPUT\"",
"append_to_final_output": true
}
]
}
In this example, the second command uses the output from the first git status command.
Sources: plugins/communication_protocols/cli/README.md
Usage Example
The following Python code demonstrates how to define and execute a multi-command CLI tool using UtcpClient.
import pytest
from utcp.utcp_client import UtcpClient
@pytest.mark.asyncio
async def test_multi_command_cli_tool():
client = await UtcpClient.create(config={
"manual_call_templates": [{
"name": "test_cli",
"call_template_type": "cli",
"commands": [
{
"command": "echo UTCP_ARG_message_UTCP_END",
"append_to_final_output": false
},
{
"command": "echo \"Previous: $CMD_0_OUTPUT\""
}
]
}]
})
result = await client.call_tool("test_cli.echo_chain", {"message": "hello"})
assert "Previous: hello" in result
This test defines a tool named test_cli that first echoes a message and then uses that output in a second echo command. The final result demonstrates the output referencing feature.
Sources: plugins/communication_protocols/cli/README.md, core/tests/client/test_utcp_client.py:534-555