pi-subagent

pi-subagent

Enables MCP hosts to delegate coding tasks to Pi CLI as a programmable sub-agent with session tracking and process management.

Category
访问服务器

README

pi-subagent

Turn the Pi CLI (@earendil-works/pi-coding-agent) into a programmable coding sub-agent that any MCP host (ZCode, Claude Code, Cursor, …) can delegate tasks to, track sessions, and kill processes.

pi-subagent is a thin MCP server that wraps pi -p --mode json into 7 structured tools: delegate tasks, harvest results, make scheduling decisions, manage named sessions, and abort runs. Process-isolated, fully session-based, sync/async dual-mode.

Why

Pi is a minimal terminal coding agent. Rather than teaching Pi methodology, this project treats Pi as a delegatable worker: a host agent (ZCode / Claude Code) decides when to delegate, fires off a self-contained task, and harvests the result. One Pi process = one isolated sub-agent run.

  • Process isolation — each delegation spawns one pi -p child process. A Pi crash only affects that run.
  • Fully session-based — every task binds to a named session (e.g. feat-auth); subsequent calls auto-continue.
  • Sync / async — defaults to async (avoids host tool-call timeouts); harvest with pi_status long-poll.
  • Schedulable — pi_plan is a pure 5-stage decision function (reject / capacity / reuse / modify / mode), fully unit-tested.
  • Universal MCP — any standard MCP client can load it.

Architecture

┌─────────────────────────────────────────────────────────────┐
│  MCP Host (ZCode / Claude Code / Pi / Cursor …)              │
└───────────────────────────┬─────────────────────────────────┘
                            │ MCP (JSON-RPC over stdio)
                            ▼
┌─────────────────────────────────────────────────────────────┐
│  pi-subagent-server  (Node/TS)                                │
│  ┌────────────┐  ┌──────────────┐  ┌────────────────────┐   │
│  │ Tool layer │  │ Session      │  │ Pi runner          │   │
│  │ (7 tools)  │─▶│ registry     │─▶│ (spawn pi -p)      │   │
│  │ + plan()   │  │ + persist    │  │ parse agent_end    │   │
│  └─────┬──────┘  │ + _snapshot  │  │ + tool_execution   │   │
│        │         └──────────────┘  └─────────┬──────────┘   │
│        │                           ┌────────▼─────────┐     │
│        └───────────────────────────│ Run registry     │     │
│           (kill)                   │ + process-table  │     │
│                                   └──────────────────┘     │
└─────────────────────────────────────────────────────────────┘
                            │ child_process.spawn({ cwd })
                            ▼
                   ┌─────────────────────┐
                   │  pi CLI (0.77+)     │
                   └─────────────────────┘

Three layers with clear boundaries: Tool layer (MCP schema + plan() pure function) / Session registry (state + persistence + redaction) / Runner (spawn pi, parse NDJSON, process table).

Tools

Tool Purpose
pi_plan Decide: should-delegate, sync/async, how many sessions
pi_delegate Dispatch a task (default async; new sessions wait for handshake)
pi_status Harvest a run's result (long-poll)
pi_session_list List sessions (omit cwd for the full set pi_plan needs)
pi_session_snapshot Inspect one session
pi_session_fork Branch a session to try another path
pi_kill Abort a run

Session model

  • Each session has a human-readable name + Pi's UUID + cwd + goal.
  • First pi_delegate creates the session (goal required); later calls auto-continue.
  • The registry persists to ~/.pi-subagent/registry.json (atomic write; on restart, interrupted running records are corrected to error).
  • Concurrency cap: 4 running runs; a single session is never run concurrently.

Install

git clone <this-repo> && cd pi-subagent
npm install

Prerequisite: the pi CLI is installed (npm i -g @earendil-works/pi-coding-agent) and on PATH.

Configure an MCP host

Add to your MCP client config:

{
  "mcpServers": {
    "pi-subagent": {
      "command": "npx",
      "args": ["tsx", "/abs/path/to/pi-subagent/src/server.ts"]
    }
  }
}

Optional env vars:

  • PI_SUBAGENT_REGISTRY — registry path (default ~/.pi-subagent/registry.json)
  • PI_BIN — override the pi executable (used by tests)

Test

npm test           # full suite (79 tests)
npm run test:fast  # dot reporter

Tests use a fake pi (test/fixtures/fake-pi.sh) and cover: async/sync, timeout, kill, session-create-failure, multi-waiter, progress cap, scheduling rules (table-driven + 100-iteration property tests), registry persistence, redaction, etc.

Project layout

src/
├── types.ts                 # all shared types + error codes
├── errors.ts                # ToolError helpers
├── runner/                  # parse.ts, argv.ts, spawn.ts, process-table.ts
├── registry/                # session.ts, run.ts, persist.ts, redact.ts
├── scheduler/               # keywords.ts, plan.ts (5-stage pure function)
├── tools/                   # delegate, status, plan-tool, session, kill
└── server.ts                # MCP entry (stdio)
skills/pi-subagent/          # SKILL.md + delegation-patterns (strategy layer)
test/                        # fixtures/ + *.test.ts
docs/                        # design.md (spec) + implementation-plan.md

Design & process

This project went through collaborative design + 4 rounds of external review before implementation. The spec and plan are committed under docs/:

  • docs/design.md — full design spec (architecture, tool contracts, error handling, scheduler rules, testing strategy). Every contract is traceable to a review note (R1–R4).
  • docs/implementation-plan.md — 19 TDD tasks (write failing test → implement → pass → commit).

Key design decisions, all backed by real probing of pi -p output and external review:

  • cwd ≠ session storage — spawn({ cwd }) controls the working dir; Pi's session files use their default location (doesn't pollute the project).
  • async default + handshake — new sessions wait for Pi's session event before returning (with a sessionStartTimeoutMs), so the host always gets a real piSessionId.
  • Multi-stage scheduler — plan() is reject → capacity → reuse → modify → mode, where modifiers stack rather than first-match (a lesson from review round 1).
  • Progress redaction — tool results are truncated + scrubbed for tokens/keys before being stored.

Status

Working implementation, 79 passing tests. Not yet published to npm — run from source via tsx.

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选