# sgundala/gmail-agent architecture

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

- Interactive diagram: https://gitdiagram.com/sgundala/gmail-agent
- Repository: https://github.com/sgundala/gmail-agent
- Video tour (about a minute): https://gitdiagram.com/sgundala/gmail-agent/video

## Overview

Gmail Reply Agent is a web application that signs users in with Google, lets them browse email threads, generates editable replies using persona/rules/past-email memory, sends replies through Gmail, and learns from edits. The graph follows the documented frontend-to-router-to-agent flow and preserves the separate MCP, RAG, and shared-core subsystems. Several backend routers and RAG stages are represented from the README and source index; their internal implementation was not sampled.

Main entry points: [`frontend/src/api/client.ts`](https://github.com/sgundala/gmail-agent/blob/main/frontend/src/api/client.ts) (API client), [`frontend/src/App.tsx`](https://github.com/sgundala/gmail-agent/blob/main/frontend/src/App.tsx) (Web app routing), [`backend/app/agent/agent.py`](https://github.com/sgundala/gmail-agent/blob/main/backend/app/agent/agent.py) (Draft orchestrator), [`backend/app/agent/tools.py`](https://github.com/sgundala/gmail-agent/blob/main/backend/app/agent/tools.py) (Agent tools)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Web application"]
  node_app["Web app routing<br/>[App.tsx]"]
  node_auth_state["Auth state<br/>[authStore.ts]"]
  node_inbox["Inbox page<br/>[InboxPage.tsx]"]
  node_thread_page["Thread and draft page"]
  node_settings_page["Settings page<br/>[SettingsPage.tsx]"]
  node_api_client["API client<br/>[client.ts]"]
end

subgraph group_api["HTTP and auth"]
  node_auth_router["Authentication routes<br/>[auth.py]"]
  node_email_router["Email routes<br/>[emails.py]"]
  node_draft_router["Draft routes<br/>[drafts.py]"]
  node_settings_router["Settings routes<br/>[settings.py]"]
  node_core_auth["Token and identity<br/>[auth.py]"]
end

subgraph group_agent["Reply agent"]
  node_agent_orchestrator["Draft orchestrator<br/>[agent.py]"]
  node_agent_state["Prompt state<br/>[state.py]"]
  node_agent_tools["Agent tools<br/>[tools.py]"]
  node_llm["LLM provider chain<br/>[llm.py]"]
end

subgraph group_rag["RAG memory"]
  node_rag_index["Memory indexing<br/>[index.py]"]
  node_rag_retrieve["Memory retrieval<br/>[retrieve.py]"]
  node_rag_learn["Send and edit learning<br/>[learn.py]"]
end

subgraph group_mcp["Gmail MCP"]
  node_mcp_client["Gmail MCP client<br/>[client.py]"]
  node_mcp_transport["MCP transport<br/>[transport.py]"]
  node_mcp_server["Gmail tool server<br/>[server.py]"]
  node_gmail_service["Gmail capability<br/>[gmail.py]"]
end

node_user(("Email user"))
node_oauth{{"Google OAuth"}}
node_database[("Supabase and pgvector")]
node_gmail_api{{"Gmail API"}}

node_user -->|"uses"| node_app
node_app -->|"routes to"| node_inbox
node_app -->|"routes to"| node_thread_page
node_app -->|"routes to"| node_settings_page
node_app -->|"checks session"| node_auth_state
node_auth_state -->|"fetches profile"| node_api_client
node_inbox -->|"requests email data"| node_api_client
node_thread_page -->|"requests thread and draft"| node_api_client
node_settings_page -->|"requests settings"| node_api_client
node_api_client -->|"calls HTTP API"| node_auth_router
node_api_client -.->|"calls HTTP API"| node_email_router
node_api_client -.->|"calls HTTP API"| node_draft_router
node_api_client -.->|"calls HTTP API"| node_settings_router
node_user -->|"signs in"| node_oauth
node_auth_router -->|"handles OAuth"| node_core_auth
node_core_auth -->|"exchanges tokens"| node_oauth
node_email_router -->|"reads Gmail"| node_mcp_client
node_draft_router -.->|"requests draft"| node_agent_orchestrator
node_settings_router -->|"indexes persona"| node_rag_index
node_agent_orchestrator -->|"builds context"| node_agent_state
node_agent_orchestrator -->|"uses tools"| node_agent_tools
node_agent_orchestrator -->|"generates reply"| node_llm
node_agent_state -->|"loads memory"| node_rag_retrieve
node_agent_tools -->|"calls Gmail tools"| node_mcp_client
node_agent_tools -->|"records learning"| node_rag_learn
node_rag_index -->|"writes vectors"| node_database
node_rag_retrieve -->|"searches memory"| node_database
node_rag_learn -->|"indexes updates"| node_rag_index
node_rag_learn -->|"summarizes edits"| node_llm
node_mcp_client -->|"opens session"| node_mcp_transport
node_mcp_transport -->|"sends tool calls"| node_mcp_server
node_mcp_server -->|"delegates tools"| node_gmail_service
node_gmail_service -->|"reads and sends"| node_gmail_api

click node_app "https://github.com/sgundala/gmail-agent/blob/main/frontend/src/App.tsx"
click node_auth_state "https://github.com/sgundala/gmail-agent/blob/main/frontend/src/store/authStore.ts"
click node_inbox "https://github.com/sgundala/gmail-agent/blob/main/frontend/src/pages/InboxPage.tsx"
click node_thread_page "https://github.com/sgundala/gmail-agent/blob/main/frontend/src/pages/EmailThreadPage.tsx"
click node_settings_page "https://github.com/sgundala/gmail-agent/blob/main/frontend/src/pages/SettingsPage.tsx"
click node_api_client "https://github.com/sgundala/gmail-agent/blob/main/frontend/src/api/client.ts"
click node_auth_router "https://github.com/sgundala/gmail-agent/blob/main/backend/app/routers/auth.py"
click node_email_router "https://github.com/sgundala/gmail-agent/blob/main/backend/app/routers/emails.py"
click node_draft_router "https://github.com/sgundala/gmail-agent/blob/main/backend/app/routers/drafts.py"
click node_settings_router "https://github.com/sgundala/gmail-agent/blob/main/backend/app/routers/settings.py"
click node_core_auth "https://github.com/sgundala/gmail-agent/blob/main/backend/app/core/auth.py"
click node_agent_orchestrator "https://github.com/sgundala/gmail-agent/blob/main/backend/app/agent/agent.py"
click node_agent_state "https://github.com/sgundala/gmail-agent/blob/main/backend/app/agent/state.py"
click node_agent_tools "https://github.com/sgundala/gmail-agent/blob/main/backend/app/agent/tools.py"
click node_llm "https://github.com/sgundala/gmail-agent/blob/main/backend/app/core/llm.py"
click node_rag_index "https://github.com/sgundala/gmail-agent/blob/main/backend/app/rag/index.py"
click node_rag_retrieve "https://github.com/sgundala/gmail-agent/blob/main/backend/app/rag/retrieve.py"
click node_rag_learn "https://github.com/sgundala/gmail-agent/blob/main/backend/app/rag/learn.py"
click node_mcp_client "https://github.com/sgundala/gmail-agent/blob/main/backend/app/mcp/client.py"
click node_mcp_transport "https://github.com/sgundala/gmail-agent/blob/main/backend/app/mcp/transport.py"
click node_mcp_server "https://github.com/sgundala/gmail-agent/blob/main/backend/app/mcp/server.py"
click node_gmail_service "https://github.com/sgundala/gmail-agent/blob/main/backend/app/mcp/gmail.py"

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_app,node_auth_state,node_inbox,node_thread_page,node_settings_page,node_api_client,node_user toneBlue
class node_auth_router,node_email_router,node_draft_router,node_settings_router,node_core_auth,node_database toneAmber
class node_agent_orchestrator,node_agent_state,node_agent_tools,node_llm,node_gmail_api toneMint
class node_rag_index,node_rag_retrieve,node_rag_learn toneRose
class node_mcp_client,node_mcp_transport,node_mcp_server,node_gmail_service,node_oauth toneIndigo
```

## Components

### Web application

- [Web app routing](https://github.com/sgundala/gmail-agent/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Auth state](https://github.com/sgundala/gmail-agent/blob/main/frontend/src/store/authStore.ts): `frontend/src/store/authStore.ts`
- [Inbox page](https://github.com/sgundala/gmail-agent/blob/main/frontend/src/pages/InboxPage.tsx): `frontend/src/pages/InboxPage.tsx`
- [Thread and draft page](https://github.com/sgundala/gmail-agent/blob/main/frontend/src/pages/EmailThreadPage.tsx): `frontend/src/pages/EmailThreadPage.tsx`
- [Settings page](https://github.com/sgundala/gmail-agent/blob/main/frontend/src/pages/SettingsPage.tsx): `frontend/src/pages/SettingsPage.tsx`
- [API client](https://github.com/sgundala/gmail-agent/blob/main/frontend/src/api/client.ts): `frontend/src/api/client.ts`

### HTTP and auth

- [Authentication routes](https://github.com/sgundala/gmail-agent/blob/main/backend/app/routers/auth.py): `backend/app/routers/auth.py`
- [Email routes](https://github.com/sgundala/gmail-agent/blob/main/backend/app/routers/emails.py): `backend/app/routers/emails.py`
- [Draft routes](https://github.com/sgundala/gmail-agent/blob/main/backend/app/routers/drafts.py): `backend/app/routers/drafts.py`
- [Settings routes](https://github.com/sgundala/gmail-agent/blob/main/backend/app/routers/settings.py): `backend/app/routers/settings.py`
- [Token and identity](https://github.com/sgundala/gmail-agent/blob/main/backend/app/core/auth.py): `backend/app/core/auth.py`

### Reply agent

- [Draft orchestrator](https://github.com/sgundala/gmail-agent/blob/main/backend/app/agent/agent.py): `backend/app/agent/agent.py`
- [Prompt state](https://github.com/sgundala/gmail-agent/blob/main/backend/app/agent/state.py): `backend/app/agent/state.py`
- [Agent tools](https://github.com/sgundala/gmail-agent/blob/main/backend/app/agent/tools.py): `backend/app/agent/tools.py`
- [LLM provider chain](https://github.com/sgundala/gmail-agent/blob/main/backend/app/core/llm.py): `backend/app/core/llm.py`

### RAG memory

- [Memory indexing](https://github.com/sgundala/gmail-agent/blob/main/backend/app/rag/index.py): `backend/app/rag/index.py`
- [Memory retrieval](https://github.com/sgundala/gmail-agent/blob/main/backend/app/rag/retrieve.py): `backend/app/rag/retrieve.py`
- [Send and edit learning](https://github.com/sgundala/gmail-agent/blob/main/backend/app/rag/learn.py): `backend/app/rag/learn.py`

### Gmail MCP

- [Gmail MCP client](https://github.com/sgundala/gmail-agent/blob/main/backend/app/mcp/client.py): `backend/app/mcp/client.py`
- [MCP transport](https://github.com/sgundala/gmail-agent/blob/main/backend/app/mcp/transport.py): `backend/app/mcp/transport.py`
- [Gmail tool server](https://github.com/sgundala/gmail-agent/blob/main/backend/app/mcp/server.py): `backend/app/mcp/server.py`
- [Gmail capability](https://github.com/sgundala/gmail-agent/blob/main/backend/app/mcp/gmail.py): `backend/app/mcp/gmail.py`

### Other components

- **Email user**
- **Google OAuth**
- **Supabase and pgvector**
- **Gmail API**

## Connections

- Email user → Web app routing: uses
- Web app routing → Inbox page: routes to (evidence: `frontend/src/App.tsx`)
- Web app routing → Thread and draft page: routes to (evidence: `frontend/src/App.tsx`)
- Web app routing → Settings page: routes to (evidence: `frontend/src/App.tsx`)
- Web app routing → Auth state: checks session (evidence: `frontend/src/App.tsx`)
- Auth state → API client: fetches profile (evidence: `frontend/src/store/authStore.ts`)
- Inbox page → API client: requests email data (evidence: `frontend/src/pages/InboxPage.tsx`)
- Thread and draft page → API client: requests thread and draft (evidence: `frontend/src/pages/EmailThreadPage.tsx`)
- Settings page → API client: requests settings
- API client → Authentication routes: calls HTTP API
- API client → Email routes: calls HTTP API
- API client → Draft routes: calls HTTP API
- API client → Settings routes: calls HTTP API
- Email user → Google OAuth: signs in
- Authentication routes → Token and identity: handles OAuth (evidence: `backend/app/routers/auth.py`)
- Token and identity → Google OAuth: exchanges tokens (evidence: `backend/app/core/auth.py`)
- Email routes → Gmail MCP client: reads Gmail (evidence: `backend/app/routers/emails.py`)
- Draft routes → Draft orchestrator: requests draft
- Settings routes → Memory indexing: indexes persona (evidence: `backend/app/routers/settings.py`)
- Draft orchestrator → Prompt state: builds context (evidence: `backend/app/agent/agent.py`)
- Draft orchestrator → Agent tools: uses tools (evidence: `backend/app/agent/agent.py`)
- Draft orchestrator → LLM provider chain: generates reply (evidence: `backend/app/agent/agent.py`)
- Prompt state → Memory retrieval: loads memory (evidence: `backend/app/agent/state.py`)
- Agent tools → Gmail MCP client: calls Gmail tools (evidence: `backend/app/agent/tools.py`)
- Agent tools → Send and edit learning: records learning (evidence: `backend/app/agent/tools.py`)
- Memory indexing → Supabase and pgvector: writes vectors
- Memory retrieval → Supabase and pgvector: searches memory (evidence: `backend/app/rag/retrieve.py`)
- Send and edit learning → Memory indexing: indexes updates (evidence: `backend/app/rag/learn.py`)
- Send and edit learning → LLM provider chain: summarizes edits (evidence: `backend/app/rag/learn.py`)
- Gmail MCP client → MCP transport: opens session (evidence: `backend/app/mcp/client.py`)
- MCP transport → Gmail tool server: sends tool calls
- Gmail tool server → Gmail capability: delegates tools (evidence: `backend/app/mcp/server.py`)
- Gmail capability → Gmail API: reads and sends
