# oneiroi/aegir architecture

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

- Interactive diagram: https://gitdiagram.com/oneiroi/aegir
- Repository: https://github.com/oneiroi/aegir

## Overview

#### Purpose and runtime
Aegir is a Go MCP security gateway: a transport-layer proxy between MCP clients and upstream MCP servers. It authenticates requests, inspects MCP traffic, redacts regulated data, records tamper-evident audit events, and blocks or judges suspicious content before forwarding. Main executable: `cmd/server/main.go`; the proxy/runtime implementation is centered in `internal/server/server.go`.

#### Process entry and configuration
`cmd/server/main.go` loads configuration, initializes security subsystems, and starts the gateway. Configuration is represented and validated in `internal/config/config.go`, with deployment defaults/examples in `aegir.yaml` and `docker-compose.yml`. The service can run with TLS and is containerized through `Dockerfile`; local development can disable TLS.

#### MCP ingress, protocol handling, and upstream boundary
`internal/server/server.go` owns HTTP server routing and the request security pipeline. MCP transport-specific forwarding is split across `internal/server/mcp_proxy.go`, `internal/server/mcp_sse.go`, and `internal/server/mcp_stdio.go`, supporting proxy, SSE, and stdio-related flows. The outbound trust boundary is managed by `internal/upstream/manager.go`, including secure dialing/mTLS concerns. Aegir treats both client input and upstream tool results as untrusted.

#### Identity and request admission
Authentication and identity management are implemented in `internal/auth/manager.go`; administrative WebAuthn/FIDO2 support is under `internal/auth/webauthn/handler.go`. Admission controls in `internal/server/ratelimit.go` enforce per-identity limits, while `internal/server/header_scan.go` and server-level handlers apply protocol/header validation. This is the first fail-closed boundary before payload forwarding.

#### Detection and semantic escalation
Content detection is built around IOC/prompt-injection patterns in `internal/detection/patterns.go` and scanning orchestration in `internal/detection/detection.go`. `internal/anomaly/detector.go` adds entropy and encoding-evasion signals. Suspicious traffic is synchronously sent to `internal/judge/judge.go`; this layer supports local-first Ollama plus opt-in OpenAI-compatible and Anthropic-style backends, with fail-closed behavior on judge failure or timeout.

#### Sanitization, compliance, and response protection
`internal/sanitizer/manager.go` coordinates request and response safety checks, including suspicious routing and compliance payload handling. `internal/sanitizer/compliance.go` and `internal/redaction/redactor.go` identify/redact GDPR/CCPA PII, HIPAA PHI, and PCI data. The proxy also scans upstream tool-call results for indirect prompt injection before they re-enter an MCP client/model context.

#### State, auditability, and operations
Sensitive state protection is provided by `internal/crypto/encryption.go` and signed-state support in `internal/statesign/statesign.go`. `internal/session/analyzer.go` evaluates session-level behavior; `internal/tasks/registry.go` supports tracked task/status work. Observability is supplied through `internal/logging/logger.go`, `internal/metrics/collector.go`, and OpenTelemetry integration in `internal/telemetry/otel.go`; Prometheus deployment configuration is in `prometheus.yml`. An optional administrative dashboard is implemented in `internal/dashboard/web.go` and `internal/dashboard/handlers.go`.

#### Key architectural constraints
The hot path keeps clean traffic off the LLM judge to preserve low latency. Detection is intentionally bounded by transport-layer visibility; semantic and cross-session threats remain partial or external-control concerns. DNS-rebinding-resistant egress enforcement is not a proxy-only guarantee, so production deployments need network-level controls in addition to `internal/upstream/manager.go`.

Main entry points: [`internal/server/server.go`](https://github.com/oneiroi/aegir/blob/public/internal/server/server.go) (HTTP security gateway), [`internal/detection/detection.go`](https://github.com/oneiroi/aegir/blob/public/internal/detection/detection.go) (Content detection), [`internal/sanitizer/manager.go`](https://github.com/oneiroi/aegir/blob/public/internal/sanitizer/manager.go) (Safety sanitizer), [`internal/auth/manager.go`](https://github.com/oneiroi/aegir/blob/public/internal/auth/manager.go) (Identity authentication)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Gateway Runtime"]
  node_entrypoint["Server entrypoint<br/>Go executable<br/>[main.go]"]
  node_config["Configuration<br/>validated config<br/>[config.go]"]
  node_gateway{{"HTTP security gateway<br/>proxy runtime<br/>[server.go]"}}
  node_mcp_transports["MCP transports<br/>[mcp_proxy.go]"]
end

subgraph group_admission["Admission &amp; Detection"]
  node_auth["Identity authentication<br/>auth manager<br/>[manager.go]"]
  node_webauthn["Admin WebAuthn<br/>FIDO2 handler<br/>[handler.go]"]
  node_admission_controls["Admission controls<br/>rate and header validation<br/>[ratelimit.go]"]
  node_detection["Content detection<br/>IOC and injection scanner<br/>[detection.go]"]
  node_anomaly["Evasion anomaly detector<br/>entropy / encoding analysis<br/>[detector.go]"]
  node_judge{{"Semantic judge<br/>LLM decision layer<br/>[judge.go]"}}
end

subgraph group_protection["Data Protection"]
  node_sanitizer["Safety sanitizer<br/>request/response coordinator<br/>[manager.go]"]
  node_redaction["PII/PHI/PCI redaction<br/>compliance redactor<br/>[redactor.go]"]
end

subgraph group_egress["Upstream Boundary"]
  node_upstream_manager{{"Secure upstream egress<br/>connection manager<br/>[manager.go]"}}
end

subgraph group_operations["State &amp; Operations"]
  node_audit_crypto[("Protected audit state<br/>encryption and signatures<br/>[encryption.go]")]
  node_session_analysis["Session analysis<br/>behavior analyzer<br/>[analyzer.go]"]
  node_observability["Observability<br/>logs, metrics, telemetry<br/>[otel.go]"]
  node_dashboard["Admin dashboard<br/>operations UI<br/>[web.go]"]
end

node_mcp_clients(("MCP clients<br/>external client"))
node_upstream_servers(("Upstream MCP servers<br/>external service"))

node_entrypoint -->|"loads"| node_config
node_entrypoint -->|"starts"| node_gateway
node_config -->|"configures"| node_gateway
node_mcp_clients -->|"MCP traffic"| node_gateway
node_gateway -->|"authenticates"| node_auth
node_auth -.->|"admin auth"| node_webauthn
node_auth -->|"identity"| node_admission_controls
node_admission_controls -->|"admitted payloads"| node_detection
node_detection -->|"evasion signals"| node_anomaly
node_detection -->|"suspicious only"| node_judge
node_anomaly -->|"escalates"| node_judge
node_detection -->|"clean or approved"| node_sanitizer
node_judge -->|"allow decision"| node_sanitizer
node_sanitizer -->|"regulated data"| node_redaction
node_redaction -->|"safe payload"| node_mcp_transports
node_mcp_transports -->|"forwards"| node_upstream_manager
node_upstream_manager -->|"secure connection"| node_upstream_servers
node_upstream_servers -->|"tool results rescanned"| node_sanitizer
node_gateway -->|"audit events"| node_audit_crypto
node_gateway -->|"session signals"| node_session_analysis
node_gateway -->|"telemetry"| node_observability
node_dashboard -.->|"admin operations"| node_gateway

click node_entrypoint "https://github.com/oneiroi/aegir/blob/public/cmd/server/main.go"
click node_config "https://github.com/oneiroi/aegir/blob/public/internal/config/config.go"
click node_gateway "https://github.com/oneiroi/aegir/blob/public/internal/server/server.go"
click node_mcp_transports "https://github.com/oneiroi/aegir/blob/public/internal/server/mcp_proxy.go"
click node_auth "https://github.com/oneiroi/aegir/blob/public/internal/auth/manager.go"
click node_webauthn "https://github.com/oneiroi/aegir/blob/public/internal/auth/webauthn/handler.go"
click node_admission_controls "https://github.com/oneiroi/aegir/blob/public/internal/server/ratelimit.go"
click node_detection "https://github.com/oneiroi/aegir/blob/public/internal/detection/detection.go"
click node_anomaly "https://github.com/oneiroi/aegir/blob/public/internal/anomaly/detector.go"
click node_judge "https://github.com/oneiroi/aegir/blob/public/internal/judge/judge.go"
click node_sanitizer "https://github.com/oneiroi/aegir/blob/public/internal/sanitizer/manager.go"
click node_redaction "https://github.com/oneiroi/aegir/blob/public/internal/redaction/redactor.go"
click node_upstream_manager "https://github.com/oneiroi/aegir/blob/public/internal/upstream/manager.go"
click node_audit_crypto "https://github.com/oneiroi/aegir/blob/public/internal/crypto/encryption.go"
click node_session_analysis "https://github.com/oneiroi/aegir/blob/public/internal/session/analyzer.go"
click node_observability "https://github.com/oneiroi/aegir/blob/public/internal/telemetry/otel.go"
click node_dashboard "https://github.com/oneiroi/aegir/blob/public/internal/dashboard/web.go"

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_entrypoint,node_config,node_gateway,node_mcp_transports toneBlue
class node_auth,node_webauthn,node_admission_controls,node_detection,node_anomaly,node_judge toneAmber
class node_sanitizer,node_redaction toneMint
class node_upstream_manager toneRose
class node_audit_crypto,node_session_analysis,node_observability,node_dashboard toneIndigo
class node_mcp_clients,node_upstream_servers toneNeutral
```

## Components

### Gateway Runtime

- [Server entrypoint](https://github.com/oneiroi/aegir/blob/public/cmd/server/main.go): Go executable · `cmd/server/main.go`
- [Configuration](https://github.com/oneiroi/aegir/blob/public/internal/config/config.go): validated config · `internal/config/config.go`
- [HTTP security gateway](https://github.com/oneiroi/aegir/blob/public/internal/server/server.go): proxy runtime · Owns routing and the security pipeline. · `internal/server/server.go`
- [MCP transports](https://github.com/oneiroi/aegir/blob/public/internal/server/mcp_proxy.go): proxy / SSE / stdio · `internal/server/mcp_proxy.go`

### Admission & Detection

- [Identity authentication](https://github.com/oneiroi/aegir/blob/public/internal/auth/manager.go): auth manager · `internal/auth/manager.go`
- [Admin WebAuthn](https://github.com/oneiroi/aegir/blob/public/internal/auth/webauthn/handler.go): FIDO2 handler · `internal/auth/webauthn/handler.go`
- [Admission controls](https://github.com/oneiroi/aegir/blob/public/internal/server/ratelimit.go): rate and header validation · Fail-closed per-identity and protocol checks. · `internal/server/ratelimit.go`
- [Content detection](https://github.com/oneiroi/aegir/blob/public/internal/detection/detection.go): IOC and injection scanner · `internal/detection/detection.go`
- [Evasion anomaly detector](https://github.com/oneiroi/aegir/blob/public/internal/anomaly/detector.go): entropy / encoding analysis · `internal/anomaly/detector.go`
- [Semantic judge](https://github.com/oneiroi/aegir/blob/public/internal/judge/judge.go): LLM decision layer · Invoked only for suspicious traffic; failures block. · `internal/judge/judge.go`

### Data Protection

- [Safety sanitizer](https://github.com/oneiroi/aegir/blob/public/internal/sanitizer/manager.go): request/response coordinator · `internal/sanitizer/manager.go`
- [PII/PHI/PCI redaction](https://github.com/oneiroi/aegir/blob/public/internal/redaction/redactor.go): compliance redactor · `internal/redaction/redactor.go`

### Upstream Boundary

- [Secure upstream egress](https://github.com/oneiroi/aegir/blob/public/internal/upstream/manager.go): connection manager · Applies dialing and mTLS boundary controls. · `internal/upstream/manager.go`

### State & Operations

- [Protected audit state](https://github.com/oneiroi/aegir/blob/public/internal/crypto/encryption.go): encryption and signatures · Secures sensitive and tamper-evident state. · `internal/crypto/encryption.go`
- [Session analysis](https://github.com/oneiroi/aegir/blob/public/internal/session/analyzer.go): behavior analyzer · `internal/session/analyzer.go`
- [Observability](https://github.com/oneiroi/aegir/blob/public/internal/telemetry/otel.go): logs, metrics, telemetry · `internal/telemetry/otel.go`
- [Admin dashboard](https://github.com/oneiroi/aegir/blob/public/internal/dashboard/web.go): operations UI · `internal/dashboard/web.go`

### Other components

- **MCP clients**: external client · Untrusted request and tool-result context.
- **Upstream MCP servers**: external service · Untrusted tool-result source.

## Connections

- Server entrypoint → Configuration: loads
- Server entrypoint → HTTP security gateway: starts
- Configuration → HTTP security gateway: configures
- MCP clients → HTTP security gateway: MCP traffic
- HTTP security gateway → Identity authentication: authenticates
- Identity authentication → Admin WebAuthn: admin auth
- Identity authentication → Admission controls: identity
- Admission controls → Content detection: admitted payloads
- Content detection → Evasion anomaly detector: evasion signals
- Content detection → Semantic judge: suspicious only
- Evasion anomaly detector → Semantic judge: escalates
- Content detection → Safety sanitizer: clean or approved
- Semantic judge → Safety sanitizer: allow decision
- Safety sanitizer → PII/PHI/PCI redaction: regulated data
- PII/PHI/PCI redaction → MCP transports: safe payload
- MCP transports → Secure upstream egress: forwards
- Secure upstream egress → Upstream MCP servers: secure connection
- Upstream MCP servers → Safety sanitizer: tool results rescanned
- HTTP security gateway → Protected audit state: audit events
- HTTP security gateway → Session analysis: session signals
- HTTP security gateway → Observability: telemetry
- Admin dashboard → HTTP security gateway: admin operations
