# cosmicstack-labs/mercury-agent architecture

> Architecture diagram of the GitHub repository cosmicstack-labs/mercury-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cosmicstack-labs/mercury-agent
- Repository: https://github.com/cosmicstack-labs/mercury-agent

## Overview

Mercury is a local, persistent AI-agent runtime: a user sends a request through a terminal, messaging channel, or web dashboard; the agent uses a configured model and permission-checked capabilities to act, while memory and sessions preserve context. Its wider product includes Mercury Code, scheduled specialist bots, task boards, installable skills, and Spotify integration. The graph combines documented runtime flows with relationships visible in the supplied source index; unsampled implementation details are not inferred.

Main entry points: [`src/core/agent.ts`](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/agent.ts) (Agent loop), [`src/index.ts`](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/index.ts) (Runtime bootstrap), [`src/bots/bot-manager.ts`](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/bots/bot-manager.ts) (Bot fleet), [`src/providers/registry.ts`](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/providers/registry.ts) (Model providers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["User entry points"]
  node_terminal["Terminal UI<br/>[cli.ts]"]
  node_messaging["Messaging channels"]
  node_webui["Web dashboard<br/>[App.tsx]"]
  node_webserver["Web server and API<br/>[server.ts]"]
end

subgraph group_runtime["Agent runtime"]
  node_runtime_entry["Runtime bootstrap<br/>[index.ts]"]
  node_agent["Agent loop<br/>[agent.ts]"]
  node_permissions["Permissions and tools<br/>[registry.ts]"]
  node_agent_types["Agent contracts<br/>[agent.ts]"]
  node_code_mode["Mercury Code"]
end

subgraph group_work["Work and automation"]
  node_bots["Bot fleet<br/>[bot-manager.ts]"]
  node_boards["Task boards<br/>[board-manager.ts]"]
  node_scheduler["Scheduler<br/>[scheduler.ts]"]
  node_skills["Skills<br/>[loader.ts]"]
end

subgraph group_state["Memory and state"]
  node_memory[("Second Brain<br/>[second-brain-db.ts]")]
  node_user_memory["Memory retrieval<br/>[user-memory.ts]"]
  node_sessions[("Sessions<br/>[repository.ts]")]
  node_config["Configuration<br/>[config.ts]"]
  node_sqlite[("SQLite driver<br/>[sqlite-driver.ts]")]
end

subgraph group_integrations["Providers and integrations"]
  node_providers["Model providers<br/>[registry.ts]"]
  node_spotify["Spotify integration<br/>[client.ts]"]
  node_cloud["Mercury Cloud<br/>[client.ts]"]
end

node_user(("User"))

node_user -->|"sends request"| node_terminal
node_user -->|"sends request"| node_messaging
node_user -->|"sends request"| node_webui
node_terminal -->|"reaches same agent"| node_agent
node_messaging -->|"reaches same agent"| node_agent
node_webui -.->|"uses dashboard API"| node_webserver
node_webserver -.->|"routes requests"| node_agent
node_runtime_entry -->|"starts"| node_agent
node_runtime_entry -->|"starts"| node_bots
node_runtime_entry -->|"starts"| node_scheduler
node_agent -->|"checks actions"| node_permissions
node_agent -->|"requests completions"| node_providers
node_agent -->|"recalls and saves"| node_user_memory
node_user_memory -->|"reads and writes"| node_memory
node_runtime_entry -->|"loads sessions"| node_sessions
node_runtime_entry -->|"loads configuration"| node_config
node_runtime_entry -->|"initializes"| node_memory
node_runtime_entry -->|"configures"| node_providers
node_bots -->|"runs bot turns"| node_providers
node_bots -->|"loads skills"| node_skills
node_scheduler -->|"schedules work"| node_agent
node_scheduler -->|"schedules bots"| node_bots
node_agent -.->|"manages task work"| node_boards
node_agent -->|"enters coding mode"| node_code_mode
node_agent -.->|"uses music tools"| node_spotify
node_cloud -.->|"provides model access"| node_providers
node_memory -->|"uses storage driver"| node_sqlite

click node_terminal "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/channels/cli.ts"
click node_messaging "https://github.com/cosmicstack-labs/mercury-agent/tree/main/src/channels"
click node_webui "https://github.com/cosmicstack-labs/mercury-agent/blob/main/ui/src/App.tsx"
click node_webserver "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/web/server.ts"
click node_runtime_entry "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/index.ts"
click node_agent "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/agent.ts"
click node_permissions "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/capabilities/registry.ts"
click node_agent_types "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/types/agent.ts"
click node_code_mode "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/programming-mode.ts"
click node_bots "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/bots/bot-manager.ts"
click node_boards "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/board-manager.ts"
click node_scheduler "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/scheduler.ts"
click node_skills "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/skills/loader.ts"
click node_memory "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/memory/second-brain-db.ts"
click node_user_memory "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/memory/user-memory.ts"
click node_sessions "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/sessions/repository.ts"
click node_config "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/utils/config.ts"
click node_providers "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/providers/registry.ts"
click node_spotify "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/spotify/client.ts"
click node_cloud "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/cloud/client.ts"
click node_sqlite "https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/utils/sqlite-driver.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_terminal,node_messaging,node_webui,node_webserver,node_user toneBlue
class node_runtime_entry,node_agent,node_permissions,node_agent_types,node_code_mode toneAmber
class node_bots,node_boards,node_scheduler,node_skills toneMint
class node_memory,node_user_memory,node_sessions,node_config,node_sqlite toneRose
class node_providers,node_spotify,node_cloud toneIndigo
```

## Components

### User entry points

- [Terminal UI](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/channels/cli.ts): `src/channels/cli.ts`
- [Messaging channels](https://github.com/cosmicstack-labs/mercury-agent/tree/main/src/channels): `src/channels`
- [Web dashboard](https://github.com/cosmicstack-labs/mercury-agent/blob/main/ui/src/App.tsx): `ui/src/App.tsx`
- [Web server and API](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/web/server.ts): `src/web/server.ts`

### Agent runtime

- [Runtime bootstrap](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/index.ts): `src/index.ts`
- [Agent loop](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/agent.ts): `src/core/agent.ts`
- [Permissions and tools](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/capabilities/registry.ts): `src/capabilities/registry.ts`
- [Agent contracts](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/types/agent.ts): `src/types/agent.ts`
- [Mercury Code](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/programming-mode.ts): `src/core/programming-mode.ts`

### Work and automation

- [Bot fleet](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/bots/bot-manager.ts): `src/bots/bot-manager.ts`
- [Task boards](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/board-manager.ts): `src/core/board-manager.ts`
- [Scheduler](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/core/scheduler.ts): `src/core/scheduler.ts`
- [Skills](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/skills/loader.ts): `src/skills/loader.ts`

### Memory and state

- [Second Brain](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/memory/second-brain-db.ts): `src/memory/second-brain-db.ts`
- [Memory retrieval](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/memory/user-memory.ts): `src/memory/user-memory.ts`
- [Sessions](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/sessions/repository.ts): `src/sessions/repository.ts`
- [Configuration](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/utils/config.ts): `src/utils/config.ts`
- [SQLite driver](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/utils/sqlite-driver.ts): `src/utils/sqlite-driver.ts`

### Providers and integrations

- [Model providers](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/providers/registry.ts): `src/providers/registry.ts`
- [Spotify integration](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/spotify/client.ts): `src/spotify/client.ts`
- [Mercury Cloud](https://github.com/cosmicstack-labs/mercury-agent/blob/main/src/cloud/client.ts): `src/cloud/client.ts`

### Other components

- **User**

## Connections

- User → Terminal UI: sends request (evidence: `README.md`)
- User → Messaging channels: sends request (evidence: `README.md`)
- User → Web dashboard: sends request (evidence: `README.md`)
- Terminal UI → Agent loop: reaches same agent (evidence: `README.md`)
- Messaging channels → Agent loop: reaches same agent (evidence: `README.md`)
- Web dashboard → Web server and API: uses dashboard API
- Web server and API → Agent loop: routes requests
- Runtime bootstrap → Agent loop: starts (evidence: `src/index.ts`)
- Runtime bootstrap → Bot fleet: starts (evidence: `src/index.ts`)
- Runtime bootstrap → Scheduler: starts (evidence: `src/index.ts`)
- Agent loop → Permissions and tools: checks actions (evidence: `README.md`)
- Agent loop → Model providers: requests completions (evidence: `README.md`)
- Agent loop → Memory retrieval: recalls and saves (evidence: `README.md`)
- Memory retrieval → Second Brain: reads and writes (evidence: `src/memory/user-memory.ts`)
- Runtime bootstrap → Sessions: loads sessions (evidence: `src/index.ts`)
- Runtime bootstrap → Configuration: loads configuration (evidence: `src/index.ts`)
- Runtime bootstrap → Second Brain: initializes (evidence: `src/index.ts`)
- Runtime bootstrap → Model providers: configures (evidence: `src/index.ts`)
- Bot fleet → Model providers: runs bot turns (evidence: `src/bots/bot-manager.ts`)
- Bot fleet → Skills: loads skills (evidence: `src/bots/bot-manager.ts`)
- Scheduler → Agent loop: schedules work (evidence: `README.md`)
- Scheduler → Bot fleet: schedules bots (evidence: `README.md`)
- Agent loop → Task boards: manages task work
- Agent loop → Mercury Code: enters coding mode (evidence: `README.md`)
- Agent loop → Spotify integration: uses music tools
- Mercury Cloud → Model providers: provides model access
- Second Brain → SQLite driver: uses storage driver (evidence: `src/memory/second-brain-db.ts`)
