Skip to content

williamomeara/agent-headlock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Agent Headlock MCP

Control AI agents mid-execution with interactive terminal commands.

Agent Headlock is an MCP (Model Context Protocol) server that enables real-time user control over AI agent execution. It creates a "headlock" mode where AI agents pause between tasks and wait for user instructions, allowing for step-by-step interactive control of AI workflows.

🎯 Concept

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    AI Agent     │◄───────►│  Headlock MCP   │◄───────►│    Terminal     β”‚
β”‚                 β”‚         β”‚     Server      β”‚         β”‚     (User)      β”‚
β”‚  Enters         β”‚         β”‚                 β”‚         β”‚                 β”‚
β”‚  Headlock Mode  β”‚  HTTP/  β”‚  Holds agent    β”‚  HTTP/  β”‚  Sends          β”‚
β”‚  ───────────►   β”‚  WS     β”‚  state, routes  β”‚  WS     β”‚  instructions   β”‚
β”‚                 β”‚         β”‚  instructions   β”‚         β”‚  ───────────►   β”‚
β”‚  Waits...       β”‚         β”‚                 β”‚         β”‚                 β”‚
β”‚       ◄─────────│         β”‚                 β”‚         β”‚  Taps out       β”‚
β”‚  Executes       β”‚         β”‚                 β”‚         β”‚  ───────────►   β”‚
β”‚  instruction    β”‚         β”‚                 β”‚         β”‚                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Flow

  1. AI Agent enters Headlock Mode - Agent calls the MCP server and blocks
  2. User sees waiting session - Terminal shows agent is ready for instructions
  3. User sends instruction - Terminal sends command to MCP server
  4. Agent receives and executes - Instruction flows to waiting agent
  5. Agent reports result - Agent sends context back, re-enters headlock
  6. Cycle repeats - User can send more instructions
  7. User taps out - Sends termination signal, agent exits gracefully

✨ Features

  • Real-time Control: Pause AI agents mid-execution and provide step-by-step instructions
  • MCP Integration: Full Model Context Protocol support with streamable HTTP
  • Interactive Terminal: Rich CLI interface for managing sessions
  • WebSocket Updates: Live updates for session status and activity
  • Async Support: Both synchronous and asynchronous agent clients
  • Session Management: Track multiple concurrent AI agent sessions
  • Health Monitoring: Built-in health checks and session statistics

πŸš€ Quick Start

Installation

cd agent-headlock

# Install with pip
pip install -e .

# Or install dependencies directly
pip install fastapi uvicorn websockets pydantic rich click httpx python-dotenv mcp

Running the Server

# Option 1: Using the installed command
headlock-server

# Option 2: Direct Python
python -m src.server

# Option 3: With uvicorn directly
uvicorn src.server:app --host 0.0.0.0 --port 8765 --reload

Server runs at http://localhost:8765

VSCode Configuration

This project includes VSCode workspace settings that automatically activate the virtual environment in new terminals.

Automatic Setup:

  • VSCode will automatically activate the virtual environment when opening new terminals
  • The Textual terminal UI is configured as the default interface
  • Shell integration is enabled for better Python support

Manual Activation (if needed):

# Activate the virtual environment manually
source .venv/bin/activate

# Or use the convenience script
./activate.sh

# Check if everything is set up correctly
python check_env.py

Running the Terminal Client

# In a new terminal window

# Option 1: Using the installed command (Textual UI - recommended)
headlock-terminal

# Option 2: Direct Python (Textual UI)
python -m src.terminal

# Option 3: Connect to different server
headlock-terminal --server http://localhost:8765

# Option 4: Force simple mode (if needed)
headlock-terminal --simple

The terminal now features a modern Textual-based UI that provides:

  • Split-pane interface with session sidebar and main content area
  • Code editor-like input with proper multi-line support
  • Real-time session updates with color-coded status
  • Mouse support for clicking to select sessions
  • SSH-friendly operation with reliable key bindings
  • Rich formatting and visual feedback

πŸ“– Terminal Interface

Textual UI (Default)

The modern Textual interface provides a floating app experience:

Layout:

  • Left Sidebar: Session list with status indicators
  • Main Area: Split between output display and instruction input
  • Bottom: Status bar with keyboard shortcuts

Controls:

  • Click sessions in the sidebar to select them
  • Type instructions in the text area (Enter creates new lines)
  • Ctrl+J to submit instructions
  • Ctrl+R to refresh sessions
  • Ctrl+T to tap out current session
  • F1 for help
  • Ctrl+C to quit

Session States:

  • 🟒 WAITING - Agent ready for instructions
  • 🟑 PROCESSING - Agent executing instruction
  • πŸ”΅ COMPLETED - Session finished
  • πŸ”΄ TERMINATED - Session ended

Multi-line Input

The TextArea widget behaves like a code editor:

  • Enter = New line (always)
  • Ctrl+J = Submit instruction
  • Works perfectly over SSH
  • Syntax highlighting ready (can be added later)

Keyboard Shortcuts

Key Action
Ctrl+J Submit instruction
F1 Show help
F2 Refresh sessions
Ctrl+C Quit

Pro Tip: The interface is designed to work seamlessly over SSH connections with proper key binding support.

πŸ”Œ API Endpoints

MCP Endpoint (for AI Agents)

This server exposes an MCP Streamable HTTP endpoint at:

  • POST /mcp (and related MCP traffic at the same base path)

Tools exposed:

  • headlock-enter_headlock (optional session_id, optional context)
    • Enters headlock mode and blocks waiting for the user's first instruction
    • Returns {session_id, instruction, should_terminate}
  • headlock-continue_headlock (required session_id, optional context)
    • Continues headlock after executing a task, sends context and waits for next instruction
    • Returns {session_id, instruction, should_terminate}

Terminal Endpoints (for Users)

Endpoint Method Description
/sessions GET List all sessions
/sessions/waiting GET List waiting sessions
/sessions/{id} GET Get session details
/sessions/{id}/instruct POST Send instruction
/sessions/{id}/tap-out POST Terminate session
/health GET Health check

WebSocket Endpoints

  • ws://localhost:8765/ws - Global updates for all sessions
  • ws://localhost:8765/ws/{session_id} - Updates for specific session

πŸ€– Using with AI Agents

Python Client

from src.client import HeadlockClient

client = HeadlockClient("http://localhost:8765")

# Enter headlock - blocks until user sends instruction
response = client.enter_headlock(
    context="Agent ready for instructions"
)

while not response.should_terminate:
    # Execute the instruction
    result = do_something(response.instruction)
    
    # Continue in headlock with result
    response = client.continue_headlock(
        session_id=response.session_id,
        context=f"Completed: {result}"
    )

print("Session ended by user")

Async Python Client

from src.client import AsyncHeadlockClient

client = AsyncHeadlockClient()

response = await client.enter_headlock(context="Ready")

while not response.should_terminate:
    result = await do_task(response.instruction)
    response = await client.continue_headlock(
        session_id=response.session_id,
        context=result
    )

πŸ”§ Configuration

Environment variables:

Variable Default Description
HEADLOCK_SERVER_URL http://localhost:8765 Server URL for terminal
HOST 0.0.0.0 Server bind host
PORT 8765 Server port

πŸ“ Project Structure

agent-headlock/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ server.py          # FastAPI server with MCP integration
β”‚   β”œβ”€β”€ session_manager.py # Session state management
β”‚   β”œβ”€β”€ models.py          # Pydantic models and data structures
β”‚   β”œβ”€β”€ terminal.py        # Interactive CLI client
β”‚   β”œβ”€β”€ client.py          # Python client for agents
β”‚   └── mcp_tools.py       # MCP tool definitions
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ example_agent.py   # Synchronous agent example
β”‚   └── async_agent.py     # Asynchronous agent example
β”œβ”€β”€ tests/
β”‚   └── __init__.py
β”œβ”€β”€ pyproject.toml         # Project configuration
β”œβ”€β”€ requirements.txt       # Dependencies
└── README.md

πŸ§ͺ Testing

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

πŸ› οΈ Development

Setting up Development Environment

# Clone and enter directory
cd agent-headlock

# Install in development mode with dev dependencies
pip install -e ".[dev]"

# Run server in development mode
uvicorn src.server:app --reload --host 0.0.0.0 --port 8765

Code Quality

The project uses:

  • pytest for testing
  • Black for code formatting (if configured)
  • isort for import sorting (if configured)
  • mypy for type checking (if configured)

Architecture

  • Server: FastAPI-based MCP server with WebSocket support
  • Session Management: Async event-driven session handling
  • Terminal Client: Rich CLI with real-time updates
  • Client Libraries: Both sync and async Python clients for agents

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“§ Contact

For questions or support, please open an issue on GitHub.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors