Skip to main content

UTCP communication protocol plugin for wrapping local command-line tools.

Project description

UTCP CLI Plugin

PyPI Downloads

Command-line interface plugin for UTCP, enabling integration with command-line tools and processes.

Features

  • Multi-Command Execution: Execute multiple commands sequentially in a single subprocess
  • State Preservation: Directory changes and environment persist between commands
  • Cross-Platform Script Generation: PowerShell on Windows, Bash on Unix/Linux/macOS
  • Flexible Output Control: Choose which command outputs to include in final result
  • Argument Substitution: UTCP_ARG_argname_UTCP_END placeholder system
  • Output Referencing: Access previous command outputs with $CMD_0_OUTPUT, $CMD_1_OUTPUT
  • Environment Variables: Secure credential and configuration passing
  • Working Directory Control: Execute commands in specific directories
  • Timeout Management: Configurable execution timeouts
  • Error Handling: Comprehensive subprocess error management

Installation

pip install utcp-cli

Quick Start

from utcp.utcp_client import UtcpClient

# Multi-step CLI tool
client = await UtcpClient.create(config={
    "manual_call_templates": [{
        "name": "file_analysis",
        "call_template_type": "cli",
        "commands": [
            {
                "command": "cd UTCP_ARG_target_dir_UTCP_END",
                "append_to_final_output": false
            },
            {
                "command": "find . -type f -name '*.py' | wc -l"
            }
        ]
    }]
})

result = await client.call_tool("file_analysis.count_python_files", {"target_dir": "/project"})

Configuration Examples

Basic Multi-Command Operation

{
  "name": "file_analysis",
  "call_template_type": "cli",
  "commands": [
    {
      "command": "cd UTCP_ARG_target_dir_UTCP_END",
      "append_to_final_output": false
    },
    {
      "command": "ls -la"
    }
  ],
  "working_dir": "/tmp"
}

With Environment Variables and Output Control

{
  "name": "python_pipeline",
  "call_template_type": "cli",
  "commands": [
    {
      "command": "python setup.py install",
      "append_to_final_output": false
    },
    {
      "command": "python script.py --input UTCP_ARG_input_file_UTCP_END --result \"$CMD_0_OUTPUT\"",
      "append_to_final_output": true
    }
  ],
  "env_vars": {
    "PYTHONPATH": "/custom/path",
    "API_KEY": "${API_KEY}"
  }
}

Cross-Platform Git Operations

{
  "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
    },
    {
      "command": "echo \"Repository has $(find . -name '*.py' | wc -l) Python files\"",
      "append_to_final_output": true
    }
  ],
  "env_vars": {
    "GIT_AUTHOR_NAME": "UTCP Bot",
    "GIT_AUTHOR_EMAIL": "bot@utcp.dev"
  }
}

Referencing Previous Command Output

{
  "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
    }
  ]
}

Cross-Platform Considerations

Command Syntax

Commands should use appropriate syntax for the target platform:

Windows (PowerShell):

{
  "commands": [
    {"command": "Get-ChildItem UTCP_ARG_path_UTCP_END"},
    {"command": "Set-Location UTCP_ARG_dir_UTCP_END"}
  ]
}

Unix/Linux/macOS (Bash):

{
  "commands": [
    {"command": "ls -la UTCP_ARG_path_UTCP_END"},
    {"command": "cd UTCP_ARG_dir_UTCP_END"}
  ]
}

Universal Commands

Some commands work across platforms:

{
  "commands": [
    {"command": "git status"},
    {"command": "python --version"},
    {"command": "node -v"}
  ]
}

Security Considerations

  • Commands execute in isolated subprocesses with controlled environment
  • Environment variables provide secure credential passing
  • Previous command outputs should be used carefully to avoid injection
  • Commands should use platform-appropriate syntax

Error Handling

from utcp.exceptions import ToolCallError

try:
    result = await client.call_tool("cli_tool.multi_command", {
        "repo_url": "https://github.com/example/repo.git",
        "target_dir": "analysis_temp"
    })
except ToolCallError as e:
    print(f"CLI tool execution failed: {e}")
    # Script execution failed - check individual command outputs

Common Use Cases

  • Multi-step Builds: setup → compile → test → package
  • Git Workflows: clone → analyze → commit → push
  • Data Pipelines: fetch → transform → validate → output
  • File Operations: navigate → search → process → report
  • Development Tools: install dependencies → run tests → generate docs
  • System Administration: check status → backup → cleanup → verify
  • Custom Workflows: Any sequence of command-line operations

Testing CLI Tools

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

Related Documentation

Examples

For complete examples, see the UTCP examples repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

utcp_cli-1.1.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

utcp_cli-1.1.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file utcp_cli-1.1.1.tar.gz.

File metadata

  • Download URL: utcp_cli-1.1.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for utcp_cli-1.1.1.tar.gz
Algorithm Hash digest
SHA256 d87bb938f1117f92880a14ed19fb2e1335175ddb93f836edbc1e309fc17b96a9
MD5 e33947854ffef10648c46200a09fcbe7
BLAKE2b-256 c8bcdbc88a0b82c6235521e0d0ad8340ce5d9ee21d9c95b1ec773dfd4ec16836

See more details on using hashes here.

File details

Details for the file utcp_cli-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: utcp_cli-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for utcp_cli-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 925f002d5a5a98b05b7f09b162e028d504e7d84f309fc05b428a8d0f6bc3e3ee
MD5 e77e757faa11517d625b8e4394d6493f
BLAKE2b-256 9cbe20d7f2dccccbcdf83417fda34af3d9232e5fb68f1f8c2b2413020ccd351e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page