Skip to main content

Turn any coding agent into a tree-searching agent โ€” an MCTS test-time search harness for Claude Code, Codex CLI, Kimi CLI, and more.

Project description

agent-mcts ๐ŸŒณ

Turn any coding agent into a tree-searching agent.

agent-mcts is an open-source (MIT) test-time search harness that wraps coding agents โ€” Claude Code, Codex CLI, Kimi CLI โ€” with Monte Carlo Tree Search. Instead of a single trajectory that either works or doesn't, your agent explores multiple solution branches, backtracks from dead ends, and concentrates budget on the most promising path.

agent-mcts searching, applying the best branch, and the resulting diff

Status: v0.1. Early but real โ€” the full loop (search โ†’ live tree โ†’ apply) works today with Claude Code. Built in the open; star the repo to follow along, or jump in โ€” early contributors shape the architecture.

Why

Production coding agents (Claude Code, Codex, Kimi, ...) run a single linear loop: act, observe, retry in place. No branching, no backtracking, no principled exploration.

Research shows search helps: SWE-Search (ICLR 2025) reported a ~23% relative improvement on SWE-bench by adding MCTS on top of software agents. But that result lives in a research framework โ€” there is no tool that brings it to the agent you already use.

agent-mcts closes that gap:

  • Bring your own agent. A thin adapter layer speaks to each agent's headless mode (claude -p, codex exec, kimi -p). The search engine never knows which agent it's driving.
  • Real MCTS, not best-of-N. UCT selection, expansion, evaluation, backup. Budget flows toward branches that look promising, away from dead ends.
  • State you can trust. Every node is an isolated git worktree + a forked agent session. Your working tree is never touched until you apply a result.
  • Terminal-native. Watch the tree grow live in your terminal. 100% Python, no browser required.

Quickstart

uv tool install agent-mcts
cd your-project
agent-mcts run "fix the flaky test in tests/test_auth.py"

The run auto-detects your installed agent and your test command, then shows the plan before spending anything:

Agent: claude ยท Value: pytest -x -q ยท Budget: 12 nodes / $10.00
Proceed? [y/N]

While searching, the tree grows live in your terminal (real output):

task: fix the flaky test in tests/test_auth.py
run 20260802-190219 ยท episodes 3/12 ยท cost $0.31/$10.00
โœ“ n0 [r=0.00 Q=0.42 N=4]
โ”œโ”€โ”€ โœ“ n1 [r=0.67 Q=0.78 N=2] mocked the clock in the auth fixture
โ”‚   โ””โ”€โ”€ โ— n3 [r=? Q=0.00 N=0] expandingโ€ฆ
โ””โ”€โ”€ โœ— n2 [r=0.00 Q=0.00 N=1] sleep-based fix (tests timed out)

When it finishes (or when you Ctrl-C โ€” MCTS is anytime, the partial tree is saved):

agent-mcts show        # inspect the tree; `show n3` prints a node's prompt, summary, evaluation
agent-mcts apply       # stage the best node's changes (squash merge โ€” you review and commit)
agent-mcts apply n2    # ...or pick a different branch

How it works

                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                โ”‚        search engine        โ”‚   UCT selection ยท expansion
                โ”‚   (agent-agnostic, Python)  โ”‚   hybrid value fn ยท backup
                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ AgentBackend protocol
            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
            โ–ผ                  โ–ผ                  โ–ผ
     claude adapter      codex adapter      kimi adapter
      (claude -p)        (codex exec)        (kimi -p)
  • Node = (git worktree, agent session). Filesystem state branches via worktrees; conversation state branches via session forking (native on Claude Code, context-injection fallback elsewhere).
  • Value function = your tests. Exit 0 scores 1.0; a pytest-style summary earns partial credit by pass ratio, and the failing output is fed into children's revision prompts. pytest / npm test / make test are picked up automatically. (LLM-as-judge lands in v0.2.)
  • Every node is a git branch (agent-mcts/<run>/<node>), so apply is an ordinary squash merge and everything is auditable after the fact. Worktrees are disposable; branches are the record.
  • Every state change is journaled (append-only jsonl per run), so an interrupted search is still a valid, inspectable tree.

Supported agents

Agent Headless Session fork Status
Claude Code claude -p native (--fork-session) โœ… v0.1
Codex CLI codex exec resume + context injection planned (v0.2)
Kimi CLI kimi -p context injection planned (v0.3)
Gemini CLI, OpenCode, ... adapter PRs welcome!

Configuration

Zero config required. When you need it, .agent-mcts.toml:

agent = "claude"
model = "haiku"                   # optional agent-model override

[value]
command = "pytest tests/ -x -q"   # exit code + pass ratio โ†’ score in [0, 1]

[search]
max_nodes = 20                    # episode budget
max_cost_usd = 10.0               # hard cost ceiling
c_uct = 1.414                     # UCT exploration constant โ€” yes, it's exposed
root_width = 3                    # diverse first attempts under the root
refine_width = 2                  # revision children per node
max_depth = 3

CLI flags (-n, --max-cost, --value, --model) override the file. If your claude binary lives somewhere unusual, point AGENT_MCTS_CLAUDE_BIN at it.

Search hyperparameters are first-class: this is built by an MCTS researcher and meant to double as a research harness for test-time search over real software tasks.

Roadmap

  • v0.1 โ€” Claude Code adapter, UCT engine, worktree state, test-based value fn, live TUI
  • v0.2 โ€” Codex CLI adapter (+ --agent registry), LLM-as-judge value fn, resume, parallel expansion
  • v0.3 โ€” Kimi CLI adapter, in-agent activation (/tree inside Claude Code via MCP)
  • v0.4 โ€” search-tree export + richer visualization, benchmark mode (same task, N agents, compare trees)

Contributing

This project is open source under the MIT license and developed fully in the open โ€” issues, design discussions, and PRs all happen here on GitHub. Contributions are welcome from day one.

The highest-impact contribution is an agent adapter: a few hundred lines implementing the AgentBackend protocol for your favorite CLI agent, with no need to touch the search core. Use the Claude Code adapter as the reference, and the fake-binary pattern in its tests to keep CI free of API calls. See CONTRIBUTING.md for setup.

Also welcome: bug reports, docs, benchmark tasks, and arguing with our UCT constants.

License

MIT โ€” do whatever you want, just keep the notice.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent_mcts-0.1.0.tar.gz (180.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent_mcts-0.1.0-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file agent_mcts-0.1.0.tar.gz.

File metadata

  • Download URL: agent_mcts-0.1.0.tar.gz
  • Upload date:
  • Size: 180.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for agent_mcts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f4b8064c712163b480a3f3dc6c8f0d07b4d7327322b722d920d9d5f14afa0fd
MD5 ea1bae63e50c0063c348d1a562da20d9
BLAKE2b-256 bef680e00bc0a59b3e9fe5bf376f6ca030c76fef0d898d88a5d04e1b7b62ecda

See more details on using hashes here.

File details

Details for the file agent_mcts-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: agent_mcts-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for agent_mcts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b897403fca709c9a49fe24c1cb5158326825a29107c50a7996561e34f340a59d
MD5 1dd0dd6b7ce5e3111990684eed982de8
BLAKE2b-256 b369e2066ed934258075879f4c5f55c7a944037c00acccd18952f72644b0d15a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page