# thinkwatchproject/thinkwatch architecture

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

- Interactive diagram: https://gitdiagram.com/thinkwatchproject/thinkwatch
- Repository: https://github.com/thinkwatchproject/thinkwatch

## Overview

ThinkWatch is a self-hosted organizational gateway for AI model APIs and MCP tools, with an admin API and web console. The main request path authenticates callers, applies access, budget and rate-limit checks, inspects and transforms traffic, selects an upstream, then records usage and audit events. The README documents major capabilities such as identity, guards, routing, cost reporting and MCP; source excerpts show parts of the AI gateway wiring. Unsampled subsystems and integrations are represented only where the README supports their role.

Main entry points: [`crates/gateway/src/proxy/generate.rs`](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/generate.rs) (AI request pipeline), [`crates/server/src/app.rs`](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/app.rs) (HTTP application), [`crates/mcp-gateway/src/proxy.rs`](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/mcp-gateway/src/proxy.rs) (MCP request proxy), [`crates/common/src/audit/logger.rs`](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/common/src/audit/logger.rs) (Audit logging)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry and identity"]
  node_server["HTTP application<br/>[app.rs]"]
  node_auth_middleware["Request authentication<br/>[auth_guard.rs]"]
  node_auth_core["Identity and tokens<br/>[oidc.rs]"]
  node_session["Session issuance<br/>[session_service.rs]"]
end

subgraph group_ai["AI gateway"]
  node_ai_pipeline["AI request pipeline<br/>[generate.rs]"]
  node_preflight["Budget and limits<br/>[pipeline.rs]"]
  node_guarding["Security guards<br/>[content_filter.rs]"]
  node_redaction["Secrets and privacy<br/>[redaction.rs]"]
  node_model_routing["Model routing<br/>[routing.rs]"]
  node_route_table["Route registry<br/>[router.rs]"]
  node_upstream_transport["Provider transport<br/>[transport.rs]"]
end

subgraph group_mcp["MCP gateway"]
  node_mcp_proxy["MCP request proxy<br/>[proxy.rs]"]
  node_mcp_registry["MCP server registry<br/>[registry.rs]"]
  node_mcp_transport["MCP transport<br/>[streamable_http.rs]"]
end

subgraph group_ops["Administration and reporting"]
  node_web_console["Management console<br/>[App.tsx]"]
  node_admin_handlers["Admin API<br/>[mod.rs]"]
  node_accounting["Usage and cost accounting<br/>[usage_estimate.rs]"]
  node_analytics["Analytics and chargeback<br/>[analytics.rs]"]
end

subgraph group_state["State and audit"]
  node_repositories["Application repositories"]
  node_postgres[("PostgreSQL")]
  node_redis[("Redis")]
  node_audit["Audit logging<br/>[logger.rs]"]
  node_clickhouse[("ClickHouse audit store<br/>[clickhouse.rs]")]
  node_forwarders["Audit forwarders<br/>[forwarders.rs]"]
end

node_ai_clients(("AI clients"))
node_admin_user(("Admin browser"))
node_providers["AI providers"]
node_mcp_servers["MCP servers"]

node_ai_clients -->|"send requests"| node_server
node_admin_user -->|"manage gateway"| node_web_console
node_web_console -->|"call admin API"| node_server
node_server -->|"authenticates routes"| node_auth_middleware
node_auth_middleware -->|"validates identity"| node_auth_core
node_server -->|"issues sessions"| node_session
node_server -->|"routes AI surfaces"| node_ai_pipeline
node_ai_pipeline -->|"checks access and limits"| node_preflight
node_ai_pipeline -->|"filters content"| node_guarding
node_ai_pipeline -->|"redacts payloads"| node_redaction
node_ai_pipeline -->|"selects route"| node_model_routing
node_model_routing -->|"chooses candidates"| node_route_table
node_model_routing -->|"reads affinity"| node_redis
node_ai_pipeline -->|"sends request"| node_upstream_transport
node_upstream_transport -->|"forwards request"| node_providers
node_ai_pipeline -.->|"estimates usage"| node_accounting
node_server -.->|"routes MCP calls"| node_mcp_proxy
node_mcp_proxy -.->|"resolves servers"| node_mcp_registry
node_mcp_proxy -.->|"dispatches calls"| node_mcp_transport
node_mcp_transport -->|"forwards tool calls"| node_mcp_servers
node_web_console -->|"manages configuration"| node_admin_handlers
node_admin_handlers -.->|"reads and writes"| node_repositories
node_repositories -->|"persists records"| node_postgres
node_preflight -->|"checks counters"| node_redis
node_accounting -->|"records usage"| node_audit
node_mcp_proxy -->|"records tool calls"| node_audit
node_audit -->|"writes events"| node_clickhouse
node_audit -->|"forwards events"| node_forwarders
node_admin_handlers -.->|"serves reports"| node_analytics
node_analytics -->|"queries audit data"| node_clickhouse

click node_server "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/app.rs"
click node_auth_middleware "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/middleware/auth_guard.rs"
click node_auth_core "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/auth/src/oidc.rs"
click node_session "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/services/session_service.rs"
click node_web_console "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/web/src/App.tsx"
click node_admin_handlers "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/handlers/mod.rs"
click node_ai_pipeline "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/generate.rs"
click node_preflight "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/pipeline.rs"
click node_guarding "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/content_filter.rs"
click node_redaction "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/redaction.rs"
click node_model_routing "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/routing.rs"
click node_route_table "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/router.rs"
click node_upstream_transport "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/transport.rs"
click node_mcp_proxy "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/mcp-gateway/src/proxy.rs"
click node_mcp_registry "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/mcp-gateway/src/registry.rs"
click node_mcp_transport "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/mcp-gateway/src/transport/streamable_http.rs"
click node_accounting "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/usage_estimate.rs"
click node_analytics "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/handlers/analytics.rs"
click node_repositories "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/services/model_repository.rs"
click node_audit "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/common/src/audit/logger.rs"
click node_clickhouse "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/common/src/audit/clickhouse.rs"
click node_forwarders "https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/common/src/audit/forwarders.rs"

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_server,node_auth_middleware,node_auth_core,node_session,node_ai_clients,node_admin_user toneBlue
class node_ai_pipeline,node_preflight,node_guarding,node_redaction,node_model_routing,node_route_table,node_upstream_transport toneAmber
class node_mcp_proxy,node_mcp_registry,node_mcp_transport,node_mcp_servers toneMint
class node_web_console,node_admin_handlers,node_accounting,node_analytics toneRose
class node_repositories,node_postgres,node_redis,node_audit,node_clickhouse,node_forwarders,node_providers toneIndigo
```

## Components

### Entry and identity

- [HTTP application](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/app.rs): `crates/server/src/app.rs`
- [Request authentication](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/middleware/auth_guard.rs): `crates/server/src/middleware/auth_guard.rs`
- [Identity and tokens](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/auth/src/oidc.rs): `crates/auth/src/oidc.rs`
- [Session issuance](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/services/session_service.rs): `crates/server/src/services/session_service.rs`

### AI gateway

- [AI request pipeline](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/generate.rs): `crates/gateway/src/proxy/generate.rs`
- [Budget and limits](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/pipeline.rs): `crates/gateway/src/proxy/pipeline.rs`
- [Security guards](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/content_filter.rs): `crates/gateway/src/content_filter.rs`
- [Secrets and privacy](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/redaction.rs): `crates/gateway/src/redaction.rs`
- [Model routing](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/routing.rs): `crates/gateway/src/proxy/routing.rs`
- [Route registry](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/router.rs): `crates/gateway/src/router.rs`
- [Provider transport](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/proxy/transport.rs): `crates/gateway/src/proxy/transport.rs`

### MCP gateway

- [MCP request proxy](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/mcp-gateway/src/proxy.rs): `crates/mcp-gateway/src/proxy.rs`
- [MCP server registry](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/mcp-gateway/src/registry.rs): `crates/mcp-gateway/src/registry.rs`
- [MCP transport](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/mcp-gateway/src/transport/streamable_http.rs): `crates/mcp-gateway/src/transport/streamable_http.rs`

### Administration and reporting

- [Management console](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/web/src/App.tsx): `web/src/App.tsx`
- [Admin API](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/handlers/mod.rs): `crates/server/src/handlers/mod.rs`
- [Usage and cost accounting](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/gateway/src/usage_estimate.rs): `crates/gateway/src/usage_estimate.rs`
- [Analytics and chargeback](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/handlers/analytics.rs): `crates/server/src/handlers/analytics.rs`

### State and audit

- [Application repositories](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/server/src/services/model_repository.rs): `crates/server/src/services/model_repository.rs`
- **PostgreSQL**
- **Redis**
- [Audit logging](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/common/src/audit/logger.rs): `crates/common/src/audit/logger.rs`
- [ClickHouse audit store](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/common/src/audit/clickhouse.rs): `crates/common/src/audit/clickhouse.rs`
- [Audit forwarders](https://github.com/ThinkWatchProject/ThinkWatch/blob/main/crates/common/src/audit/forwarders.rs): `crates/common/src/audit/forwarders.rs`

### Other components

- **AI clients**
- **Admin browser**
- **AI providers**
- **MCP servers**

## Connections

- AI clients → HTTP application: send requests (evidence: `README.md`)
- Admin browser → Management console: manage gateway (evidence: `README.md`)
- Management console → HTTP application: call admin API (evidence: `README.md`)
- HTTP application → Request authentication: authenticates routes (evidence: `crates/server/src/app.rs`)
- Request authentication → Identity and tokens: validates identity (evidence: `crates/server/src/middleware/auth_guard.rs`)
- HTTP application → Session issuance: issues sessions (evidence: `crates/server/src/services/session_service.rs`)
- HTTP application → AI request pipeline: routes AI surfaces (evidence: `crates/server/src/app.rs`)
- AI request pipeline → Budget and limits: checks access and limits (evidence: `crates/gateway/src/proxy/pipeline.rs`)
- AI request pipeline → Security guards: filters content (evidence: `crates/gateway/src/proxy/generate.rs`)
- AI request pipeline → Secrets and privacy: redacts payloads (evidence: `crates/gateway/src/proxy/generate.rs`)
- AI request pipeline → Model routing: selects route (evidence: `crates/gateway/src/proxy/generate.rs`)
- Model routing → Route registry: chooses candidates (evidence: `crates/gateway/src/proxy/routing.rs`)
- Model routing → Redis: reads affinity (evidence: `crates/gateway/src/proxy/routing.rs`)
- AI request pipeline → Provider transport: sends request (evidence: `crates/gateway/src/proxy/generate.rs`)
- Provider transport → AI providers: forwards request (evidence: `README.md`)
- AI request pipeline → Usage and cost accounting: estimates usage (evidence: `crates/gateway/src/proxy/generate.rs`)
- HTTP application → MCP request proxy: routes MCP calls
- MCP request proxy → MCP server registry: resolves servers
- MCP request proxy → MCP transport: dispatches calls
- MCP transport → MCP servers: forwards tool calls (evidence: `README.md`)
- Management console → Admin API: manages configuration (evidence: `README.md`)
- Admin API → Application repositories: reads and writes
- Application repositories → PostgreSQL: persists records (evidence: `README.md`)
- Budget and limits → Redis: checks counters (evidence: `crates/gateway/src/proxy/pipeline.rs`)
- Usage and cost accounting → Audit logging: records usage (evidence: `README.md`)
- MCP request proxy → Audit logging: records tool calls (evidence: `README.md`)
- Audit logging → ClickHouse audit store: writes events (evidence: `README.md`)
- Audit logging → Audit forwarders: forwards events (evidence: `README.md`)
- Admin API → Analytics and chargeback: serves reports
- Analytics and chargeback → ClickHouse audit store: queries audit data (evidence: `README.md`)
