# Porterminal - AI agent instructions

Porterminal is a terminal on this machine. A human uses the web UI at http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/ ;
you (an AI agent) can control a real shell over MCP or the REST fallback API.

## Connect

MCP endpoint (Streamable HTTP): http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/mcp
Machine-readable discovery: http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/.well-known/mcp.json (MCP server.json)

Point any MCP client at that URL. Example client config:

    {
      "mcpServers": {
        "porterminal": { "url": "http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/mcp" }
      }
    }

After connecting, call `tools/list` to confirm the tools below.

## Tools

- `run_command` - Run a shell command in the agent's persistent terminal and return {output, exit_code, status}. status='completed' on success; status='waiting' means it didn't finish (likely an interactive prompt) - use read_screen / send_keys.
- `read_screen` - Return the current terminal screen as clean text plus cursor position. Use for interactive prompts, REPLs, and TUIs.
- `send_keys` - Send raw keystrokes to the terminal (no automatic Enter; include '\r' to submit). Use to answer prompts or drive TUIs.
- `send_signal` - Send a control signal. 'int' (Ctrl-C) interrupts the running command. 'eof' (Ctrl-D) ends input on POSIX shells but is a no-op on Windows.

Guidance: prefer `run_command` for ordinary commands (clean output + exit code).
If it returns `status: "waiting"`, the command is interactive - use `read_screen`
to see the prompt and `send_keys` / `send_signal` to drive it.

## REST fallback

Use this if your host cannot register/connect an MCP server but can make HTTP
requests. First call `run` without a session id; reuse the returned `session_id`
for subsequent calls:

    POST http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/api/agent/run
    {"command": "echo hello", "timeout": 30}
    -> {"session_id": "rest-...", "status": "completed", "exit_code": 0, "output": "hello"}

    GET  http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/api/agent/screen?session_id=rest-...
    POST http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/api/agent/keys    {"session_id": "rest-...", "text": "answer"}
    POST http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/api/agent/signal  {"session_id": "rest-...", "signal": "int"}
    DELETE http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/api/agent/session?session_id=rest-...

REST uses the same visible agent tab and shell backend as MCP. The `session_id`
must come from a prior REST response. Unknown or closed IDs return 404 and never
create a replacement shell or tab.

If your HTTP client or local shell pretty-prints the REST response as a table,
long `output` fields may be visually truncated with an ellipsis. Treat that as
client-side display clipping: inspect the raw `output` field, or make the remote
command print JSON/plain text before relying on exact values.

## Browser fallback

If your environment cannot use MCP or REST, open http://127.0.0.1:8765/N9DgIdRyIhgq36J43J5g384gQp94VgM9UBaSukIzZpsMRwBR/ in a browser. Read the
element named "Terminal screen", type commands into "Terminal input", and press
Enter. This fallback is less reliable, but it is intentionally exposed as
ordinary page text for browser-driving agents.

## Example

    run_command(command="echo hello")
    -> {"status": "completed", "exit_code": 0, "output": "hello"}

## Notes

- Each MCP or REST agent session gets its own persistent shell, shown as a tab
  the human can watch and take over. MCP sessions are cleaned up when you
  disconnect; REST sessions close on DELETE, shell exit, or idle cleanup.
- Security: the complete generated URL (including its random access path) is
  the credential. The bare hostname exposes no routes, but anyone with the full
  URL has shell access. The shell runs non-elevated (it can't install software
  that requires admin).
