Skip to content
Architecture
This page is the docs-site companion to the root ARCHITECTURE.md.
If you are reading the repo directly, ARCHITECTURE.md is the fuller source of truth. This page is the shorter walkthrough for when you mostly want to understand how the pieces fit together.
For the companion behavior reference, see /reference/spec or the root SPEC.md.
1. High-level Structure
text
protoagent CLI
-> App (Ink)
-> Agentic Loop
-> OpenAI SDK client
-> Built-in tools
-> Dynamic tools from MCP and skills
-> Special sub-agent execution pathAt runtime, the user interacts with the Ink app, while the agent loop handles model/tool orchestration and emits events back to the UI.
2. Module Map
Main implementation areas:
src/cli.tsxsrc/App.tsxsrc/agentic-loop.tssrc/system-prompt.tssrc/sub-agent.tssrc/config.tsxsrc/providers.tssrc/sessions.tssrc/skills.tssrc/mcp.tssrc/tools/index.tssrc/tools/*src/components/*src/utils/*
3. Startup Flow
Current startup path:
src/cli.tsxparses arguments.Appinitializes logging and approval handling.- Config is loaded or inline setup is shown.
- The OpenAI client is created from provider metadata.
- MCP is initialized, which may register dynamic tools.
- A saved session is resumed or a new session is created.
- The initial system prompt is generated.
4. Turn Execution Flow
For a normal user message:
Appappends the user message immediately.runAgenticLoop()refreshes the system prompt and compacts history if needed.- The model streams assistant text and/or tool calls.
- Tool calls are executed through the tool system, except
sub_agent, which is handled specially. - Tool results are appended to history.
- The loop repeats until plain assistant text is returned.
Appsaves the session and TODO state.
5. Message and Session Model
The core app state centers on:
completionMessages- the current session object
- per-session TODO state
- a live
assistantMessageRefused during streaming updates
Sessions persist messages, provider/model metadata, timestamps, title, and TODOs.
6. Tool Architecture
Static built-ins come from src/tools/index.ts and src/tools/*.
Dynamic tools can be registered by:
src/mcp.tssrc/skills.ts
sub_agent is a special-case tool exposed by the loop rather than the normal registry.
7. Safety Model
The current safety model combines:
- path validation for file tools
- approval prompts for writes, edits, and non-safe shell commands
- hard-blocked dangerous shell patterns
Approved shell commands are not sandboxed.
8. Skills Architecture
The skills system discovers validated SKILL.md directories, may register activate_skill, and extends allowed file roots to skill directories.
Because skill initialization happens during system-prompt generation, it has runtime side effects on both tool registration and path-access roots.
9. MCP Architecture
src/mcp.ts reads merged protoagent.jsonc runtime config, connects stdio or HTTP MCP servers, discovers their tools, and registers them dynamically.
10. Sub-Agent Architecture
src/sub-agent.ts runs isolated child loops with a fresh prompt and message history. Children use the normal built-in and dynamic tools, but do not recursively expose sub_agent.
11. Conversation Compaction and Cost Tracking
ProtoAgent estimates token usage, tracks context-window usage, and compacts old conversation history at high utilization while preserving protected skill payloads.
12. Terminal UI
src/App.tsx is both the visible UI layer and the runtime coordinator for:
- slash commands
- session lifecycle
- approvals
- config flows
- MCP lifecycle
- event-driven rendering
The UI also includes collapsible message boxes, grouped tool rendering, formatted assistant output, and usage display.
13. Important Implementation Nuances
These are the details that are easy to miss if you only skim the file tree:
App.tsxis not just presentation- the system prompt is regenerated repeatedly
- skills initialization mutates runtime state
sub_agentis not part ofgetAllTools()- some tool failures flow back as tool-result strings rather than thrown errors
14. Shutdown and Lifecycle Boundaries
Graceful quit saves the session and shows a resume command. Immediate Ctrl-C exits without that quit flow. App cleanup clears approval handlers and closes MCP connections.
15. Extension Points
The main extension surfaces are:
src/providers.tssrc/tools/*src/skills.tssrc/mcp.tssrc/sub-agent.tssrc/components/*