Code mode with a stateful REPL

Reddit r/AI_Agents Tools

Summary

The author introduces ptc_runner_mcp, an MCP server that provides a stateful, sandboxed REPL using a Clojure-like language, allowing AI agents to perform exploratory computations without overwhelming the context window.

I’ve been working on `ptc_runner_mcp`, an MCP server that gives an AI agent a stateful, sandboxed REPL using a small Clojure-like language. The problem I kept running into with MCP was not only tool discovery. It was what happens after the tool call. A lot of agent tasks are exploratory computation tasks. If all of that data comes back into the model context, the LLM starts doing the computation “in its head”. That is where you get wrong results and a shrinking context window. `ptc_runner_mcp` exposes one main tool: `lisp_eval`. Inside `lisp_eval`, the agent gets a REPL-like session. It can inspect available MCP servers/tools, call them, store results in session memory, define helper functions, and keep working from the same state. Discovery is also REPL-shaped. Instead of pushing every tool schema into context up front, the agent can use things it already understands: (mcp/servers) (dir 'github) (doc 'github/search_issues) (apropos "calendar") Then it can make a small probe call, learn the result shape, fetch the real data, and compute over it locally. For example, instead of pulling 1,000 records into the conversation, it can do something like: (def traces (fetch-all-traces "org-acme" "production")) (defn cost [t] (Double/parseDouble (get t "total_cost"))) (->> traces (group-by day) (map (fn [[day xs]] {:day day :total (reduce + (map cost xs))})) (sort-by :total)) The model sees the result of the computation, truncated if too long. On the next turn it can keep using the helper functions it already defined. The other reason I went this way is performance and operational simplicity. Sessions run on the BEAM, so they are lightweight. You can keep many concurrent stateful REPL sessions around on one machine with low latency, instead of managing a pool of heavier Python/JS sandboxes.
Original Article

Similar Articles

Code execution with MCP: Building more efficient agents

Anthropic Engineering

This article from Anthropic explores how integrating code execution with the Model Context Protocol (MCP) can improve the efficiency of AI agents. It addresses challenges like token overload from tool definitions and intermediate results, proposing code execution as a solution to reduce latency and costs.