| name | mainframe-bash |
| description | Use when writing bash scripts, shell automation, CLI tools, or any bash programming task with Google AI CLI. |
Google AI CLI - Mainframe Integration
Instructions for using Mainframe (3,821+ pure bash functions) with Google's AI CLI tools.
Overview
Mainframe provides 3,821+ pure bash functions across 152 libraries with zero external dependencies. Integrate with Google AI CLI for high-performance bash scripting (20-72x faster than spawning sed/awk/jq).
Installation
git clone https://github.com/gtwatts/mainframe.git ~/.mainframe
export MAINFRAME_ROOT="$HOME/.mainframe"
Configuration
Google CLI Configuration File
Add Mainframe configuration to your Google CLI config (typically ~/.config/google-ai/config or project-specific):
export MAINFRAME_ROOT="$HOME/.mainframe"
export PATH="$MAINFRAME_ROOT/bin:$PATH"
Environment Setup
Google CLI inherits shell environment. Add to your shell profile (~/.bashrc, ~/.zshrc):
export MAINFRAME_ROOT="$HOME/.mainframe"
export PATH="$MAINFRAME_ROOT/bin:$PATH"
export MAINFRAME_OUTPUT_FORMAT="usop"
Per-Script Loading
Always include this at the start of generated bash scripts:
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
Mainframe Integration Steps
Step 1: Verify Installation
if [[ -f "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh" ]]; then
echo "Mainframe ready"
else
echo "Install: git clone https://github.com/gtwatts/mainframe.git ~/.mainframe"
fi
Step 2: Source the Library
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
Step 3: Use Mainframe Functions
Replace external tool calls with Mainframe functions:
| External Tool | Mainframe Replacement | Speedup |
|---|
jq '.name' | json_get '$json' 'name' | 35x |
sed 's/foo/bar/g' | replace_all '$str' 'foo' 'bar' | 20x |
awk '{print $1}' | array_first ${arr[@]} | 72x |
cat file | read_file '$path' | 18x |
tr '[:upper:]' '[:lower:]' | to_lower '$str' | 45x |
USOP Output Handling
Universal Structured Output Protocol (USOP) enables structured communication between Google CLI and Mainframe.
Enabling USOP
export MAINFRAME_OUTPUT_FORMAT="usop"
mainframe --format=usop <command>
source "${MAINFRAME_ROOT}/lib/common.sh"
mainframe_output_format "usop"
USOP Format Structure
result=$(json_object \
"status=success" \
"action=file_processed" \
"data:raw={\"count\":42}")
mainframe usop wrap "$result"
Parsing USOP in Google CLI
When receiving USOP output, parse with:
usop_content=$(echo "$output" | sed -n '/\[USOP:v1\]/,\[\/USOP:v1\]/p' | head -n -1 | tail -n +2)
status=$(echo "$usop_content" | json_get "" "status")
Multi-Turn Conversation Support
Google CLI maintains conversation context. Use Mainframe's Agent Working Memory (AWM) for state persistence:
Session Management
session_id=$(awm_init "google-cli-task-$(uuid)")
export AWM_SESSION="$session_id"
awm_checkpoint "step" "3"
awm_checkpoint "input_files" "file1.txt,file2.txt,file3.txt"
awm_progress "processing" "3/10"
Resuming Sessions
source "${MAINFRAME_ROOT}/lib/common.sh"
awm_resume "$AWM_SESSION"
last_step=$(awm_get "step" "0")
files=$(awm_get "input_files" "")
log_info "Resuming from step $last_step"
Context-Aware Processing
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
if [[ -n "$AWM_SESSION" ]]; then
awm_resume "$AWM_SESSION"
log_info "Resumed session: $AWM_SESSION"
else
session_id=$(awm_init "google-cli-session")
export AWM_SESSION="$session_id"
log_info "New session: $session_id"
fi
for i in {1..10}; do
process_item "$i"
awm_checkpoint "last_processed" "$i"
awm_progress "items" "$i/10"
done
awm_close
Core Function Reference
JSON Operations
json_object "name=John" "age:number=30" "active:bool=true"
json_array "a" "b" "c"
json_array_typed number 1 2 3
json_get '{"name":"John"}' "name"
json_keys '{"a":1,"b":2}'
json_merge '{"a":1}' '{"b":2}'
json_valid '{"a":1}'
String Operations
trim_string " text "
to_lower "HELLO"
to_upper "hello"
replace_all "foo bar" "foo" "baz"
contains "hello" "ell"
capitalize "hello world"
Array Operations
arr=(5 3 1 4 2)
array_sort "${arr[@]}"
array_unique 1 2 2 3
array_join ", " "${arr[@]}"
array_contains "3" "${arr[@]}"
array_filter "is_int" "${arr[@]}"
Validation & Security
validate_email "user@example.com"
validate_url "https://example.com"
validate_path_safe "$path" "/allowed"
sanitize_shell_arg "$input"
sanitize_filename "bad<file>.txt"
File Operations
read_file "$path"
file_write "$path" "content"
file_exists "$path"
dir_exists "$path"
file_head "$path" 10
file_tail "$path" 5
path_join "/base" "file.txt"
Git Operations
git_branch
git_is_dirty
git_files_changed
git_commit_hash
git_summary
HTTP Operations
http_get "https://api.example.com"
http_post "https://api.example.com" '{"key":"val"}'
http_status
http_body
http_is_success
Multi-Agent Coordination
Agent IPC
agent_register "google-cli-1" code.analyze python
reviewers=$(agent_discover "code.review")
agent_send "kimi-analyzer-1" '{
"task": "analyze_imports",
"file": "app.py",
"language": "python"
}'
response=$(agent_receive 30)
Universal Agent Protocol (UAP)
source "${MAINFRAME_ROOT}/lib/uap.sh"
msg=$(uap_task_request \
--from-platform "google-cli" \
--to-platform "claude-code" \
--to-id "reviewer-1" \
--task "code_review" \
--params '{"file":"main.py"}')
platform=$(uap_detect_platform)
Example Workflows
Example 1: Data Processing Pipeline
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
input_file="$1"
validate_path_safe "$input_file" "$(pwd)" || die 1 "Invalid path"
session_id=$(awm_init "data-pipeline")
lines=$(file_lines "$input_file")
log_info "Processing $lines lines"
processed=0
while IFS= read -r line; do
clean=$(trim_string "$line" | to_lower)
if (( processed % 100 == 0 )); then
awm_checkpoint "progress" "$processed/$lines"
fi
echo "$clean" >> output.txt
((processed++))
done < "$input_file"
awm_close
success "Processed $processed lines"
Example 2: API Integration with Retry
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
api_url="https://api.example.com/data"
fetch_data() {
http_json_get "$api_url"
}
if retry 5 fetch_data; then
data=$(http_body)
count=$(echo "$data" | json_get "" "count")
json_object \
"status=success" \
"count:number=$count" \
"timestamp=$(now_iso)"
else
json_object \
"status=error" \
"message=Failed after 5 retries"
fi
Example 3: Multi-Agent Code Review
#!/usr/bin/env bash
source "${MAINFRAME_ROOT:-$HOME/.mainframe}/lib/common.sh"
files=$(git_files_changed)
log_info "Reviewing $(array_length $files) files"
agent_register "google-cli-reviewer" code.review
for file in $files; do
agent_broadcast "$(json_object \
"event=review_request" \
"file=$file" \
"from=google-cli-reviewer")"
done
timeout 60 agent_receive_all "review_response"
Google CLI Specific Notes
- Environment Inheritance: Google CLI sessions inherit shell environment variables
- Structured Output: Use USOP format for reliable output parsing
- State Persistence: Use AWM for long-running tasks across conversation turns
- Parallel Execution: Use
parallel function for concurrent operations
Quick Reference
mainframe quickref
mainframe quickref json
mainframe quickref --search
Reference Files
~/.mainframe/CHEATSHEET.md - All 3,821+ function signatures
~/.mainframe/FUNCTIONS.json - Machine-readable function index
~/.mainframe/DECISION_TREES.md - Usage guidance
~/.mainframe/docs/ORCHESTRATION.md - Multi-agent coordination
Repository