exec-wrap-mcp: Give your AI agent a terminal that remembers

Source code (GitHub)

The result first

exec-wrap-mcp is a single Go binary that lets an AI agent launch an interactive program once, such as /bin/bash, lldb or a Python REPL, and keep talking to it across many tool calls. State survives: export ONE=1 in one call, echo $ONE in the next, and you get 1. It runs on Linux, macOS and Windows, speaks the Model Context Protocol over stdio, and is MIT licensed. Code and releases: github.com/Muthu-Palaniyappan-OL/exec-wrap-mcp.

The problem

Most “run a command” tools for agents start a fresh process on every call, so everything the program knew is gone when it exits:

call 1:  export ONE=1     -> new bash, sets ONE, exits
call 2:  echo $ONE        -> new bash, prints an empty line

That breaks cd, virtualenvs, ssh sessions, gdb breakpoints, open database transactions, and a Python REPL that already loaded your dataset. Anywhere the state is the point, the agent has to replay everything on each call or give up.

The idea

Wrap the program instead of re-running it. exec-wrap-mcp starts the program inside a pseudo-terminal, keeps it running, and exposes it to the agent as a session with an id. The agent sends input to the session and gets the output back.

It is not specific to shells. Anything that reads lines and prints a prompt works, and I tested bash, the Python REPL and lldb. Many sessions can run at the same time, and each one has its own state.

Wrap thisSo the agent can
bash, zsh, sh, cmd.exe, pwshkeep cd, env vars, virtualenvs and background jobs
gdb, lldbset breakpoints, step and inspect over many turns
python3, nodebuild up state in a REPL
psql, mysql, sqlite3run a transaction across several calls
ssh host, docker exec -it c shwork on a remote box without reconnecting

Install

With Go 1.25 or newer:

go install github.com/Muthu-Palaniyappan-OL/exec-wrap-mcp@latest

Or download a binary for Linux, macOS or Windows (amd64 and arm64) from the releases page. Every release ships a checksums.txt.

Then add it to your MCP client. The entry is the same everywhere:

{
  "mcpServers": {
    "exec-wrap": { "command": "exec-wrap-mcp" }
  }
}

For Claude Code it is one line: claude mcp add exec-wrap -- exec-wrap-mcp. Kiro, Cursor, VS Code and Claude Desktop take the JSON above in their own MCP config file.

Seven tools

ToolWhat it does
launchStart a program as a session. Returns session_id and the first output.
sendWrite input, flush it, and return the output it produced.
readFetch pending output without sending anything.
signalCtrl-C (interrupt), Ctrl-D (eof), terminate, kill, suspend.
resizeChange the terminal size.
listShow all sessions and their state.
killEnd a session, gracefully first, forced after a grace period.

A real session

This is the actual output from the released binary, with one call per line. The output and done_reason fields are the interesting part.

launch  {"command": "/bin/bash"}
        -> session_id "s_7fc971c5", tty_mode "pty", done_reason "prompt"

send    {"input": "export ONE=1"}
        -> output "",  done_reason "prompt", last_exit_status 0, 25 ms

send    {"input": "echo $ONE"}
        -> output "1", done_reason "prompt", last_exit_status 0, 26 ms

send    {"input": "ls /nonexistent"}
        -> output "ls: /nonexistent: No such file or directory",
           last_exit_status 1, 37 ms

send    {"input": "sleep 2; echo finished", "wait": {"timeout_ms": 500}}
        -> output "", done_reason "timeout"        (still running)

read    {"wait": {"timeout_ms": 5000}}
        -> output "finished", done_reason "prompt"

The second call is the whole point: the environment variable set in call 1 is still there in call 2. The last two calls show what happens with a slow command. send gives up waiting after the timeout, the process keeps running, and read collects the rest.

The hard part: knowing when a command is done

A terminal never announces “this command finished”. If you return too early you lose output, and if you wait too long every call feels slow. send returns on the first of these:

  1. exited: the process ended. You get the remaining output and the exit code.
  2. until: your own until_regex matched, for example "Listening on port".
  3. prompt: the program printed its prompt again. This is the normal, precise case.
  4. idle: no new output for a while, the fallback for programs without a known prompt.
  5. timeout: the hard limit elapsed. The process keeps running.

For shells I did not want to guess at $ , because a command can print that too. At launch the server switches bash, sh or zsh to a private prompt: an invisible record-separator character wrapped around the last exit status. The server strips it from the output and reports the number as last_exit_status. Bash and zsh also start without line editing and with terminal echo off, so the output comes back clean, with no echoed input and no escape sequences.

Other programs use built-in presets: (gdb) , (lldb) , >>> , postgres=# , > for Node, and the cmd and PowerShell prompts. For anything else, pass your own prompt_regex on launch, or rely on idle detection.

Try it with a debugger

The end-to-end tests include lldb, because it is the case that shows why persistence matters. Calls like these keep the debugger’s state across turns:

launch  {"command": "lldb", "args": ["./a.out"]}
send    {"input": "breakpoint set --name main"}
send    {"input": "run"}
send    {"input": "bt"}
send    {"input": "frame variable"}

Then ask your agent something like: “Launch lldb on ./a.out, break on main, run, and show me the backtrace after the crash.” It no longer has to rebuild a debugger session from scratch on every step.

Things I got right on purpose

  • No orphans. Every session is its own process group, and closing the server terminates all of them. I checked this by closing the client’s end of the pipe and looking for leftover processes.
  • Bounded memory. Output is buffered per session up to 4 MiB, and each response is capped at 64 KiB by default. When a response is too long, the server keeps the first 25% and last 75% and says how many bytes it dropped in between.
  • Clean text. Escape sequences are stripped by default, and carriage returns and backspaces are applied the way a terminal would, so progress bars do not turn into noise.
  • Many sessions, no mixing. Two sessions never share state, and a second call into a session that is busy returns a busy error instead of interleaving.
  • Real tests. The tests build the actual binary and drive it with the MCP client from the Go SDK, the same way an AI client would. Linux runs them on every change, and macOS and Windows run weekly and on demand.

Safety

This server runs arbitrary programs as your user. That is the feature, and it is also the risk, so treat it like handing the agent a shell.

  • It speaks stdio only. There is no network listener and no open port.
  • launch takes an executable and arguments, never a shell string, so the server adds no injection surface of its own.
  • --allow-command '/usr/bin/*' restricts which programs can start (repeatable). --max-sessions, --idle-ttl and --clean-env bound the rest.
  • Input and output are not logged unless you pass --log-io.
  • Keep human approval of tool calls on in your MCP client, and use a container or VM for untrusted work. There is no sandbox.

What it does not do

It reads a line-oriented stream, so full-screen programs like vim and htop are out of scope. Pagers are disabled by default (PAGER=cat, TERM=dumb) for the same reason.

How it is maintained

Dependabot checks the Go modules daily and the GitHub Actions weekly. Its pull requests merge themselves once CI passes. The first run already did this for a real case: golang.org/x/crypto had 13 open security advisories, and the fix PR merged without anyone touching it. Releases follow semantic versioning, and go install and the release binaries are both built from tags.

Try it

Start with the install line, point your MCP client at it, and ask your agent to open a bash session, set a variable, and read it back. If it prints the value, everything else (debuggers, REPLs, database shells) works the same way. Issues and ideas are welcome on GitHub.

Share this post

Back to blog