# qu3ai/qu3-app architecture

> Architecture diagram of the GitHub repository qu3ai/qu3-app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/qu3ai/qu3-app
- Repository: https://github.com/qu3ai/qu3-app

## Overview

##### Project Type and Purpose
- This is a specialized client–server application written in Python.
- On the client side, it’s a CLI tool (`src/main.py`) that implements post-quantum cryptography (PQC) protocols to communicate securely with an MCP server.
- A mock server (`scripts/mock_mcp_server.py`) is provided for development and testing.
- Purpose: Establish a quantum-safe session, send encrypted inference or policy-update requests, and verify server attestations.

##### File Structure
- Top-level:
- README.md, config.yaml, requirements.txt
- scripts/ — contains the FastAPI mock server (`mock_mcp_server.py`)
- src/ — main application modules: `main.py`, `mcp_client.py`, `pqc_utils.py`, `config_utils.py`
- tests/ — unit tests for each core module
- Key libraries & configs:
- `config.yaml` — holds `key_directory` and `server_url`
- Requirements include `liboqs-python`, `cryptography`, `requests`, `typer`, and `fastapi` (for mock)

##### Pull Insights from the README
- Graph of module dependencies on the client
- Key patterns:
- Separation of concerns: CLI orchestration (`src/main.py`), cryptographic operations (`pqc_utils`), configuration & key management (`config_utils`), networking (`mcp_client` using requests), mock server for testing.

a. Main Components
- Client Side
1. CLI Layer (Typer) – Entry point: `src/main.py`
2. Business Logic / Client SDK – `src/mcp_client.py`
3. Crypto Utilities – `src/pqc_utils.py`
4. Config & Key Management – `src/config_utils.py` + `config.yaml` + filesystem (`~/.qu3/keys/`)
5. Networking – HTTP via `requests.Session`
6. External Libraries – `liboqs_python`, `cryptography`, `PyYAML`, `Typer`
- Server Side (Mock)
1. FastAPI App – `scripts/mock_mcp_server.py`
2. Config & Key Management – Reuses `src/config_utils.py`
3. Endpoints – `/keys`, `/kem-handshake/initiate`, `/inference`, `/policy-update`
4. In-memory model stubs – `model_caps`, `model_reverse`
5. External Libraries – `fastapi`, `uvicorn`, same PQC libs

b. Relationships & Interactions
- CLI → Client SDK (`mcp_client`) → Config Utils + PQC Utils → Network Layer → mock server → back to client
- File System: both client & server read/write key files
- Network calls: GET /keys → POST /kem-handshake/initiate → POST /inference or /policy-update
- Crypto workloads: KEM handshake, AES-GCM encryption, SPHINCS+ signing/verification, HKDF

c. Architectural Patterns & Principles
- Layered architecture: CLI / orchestration, SDK/core logic, utilities (crypto, config), transport (HTTP)
- Single-responsibility modules (clear separation of key management, crypto ops, network calls)
- Dependency injection via configuration (server URL, key directory)
- Testability: mock server in scripts/, unit tests in tests/

d. Key Technologies
- Python 3, Typer (CLI), FastAPI (mock server)
- liboqs-python (post-quantum KEM & signature)
- cryptography (AES-GCM, HKDF)
- requests (HTTP client)
- PyYAML (config file parsing)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Side
    subgraph "Client"
        direction TB
        CLI["CLI (Typer)"]:::client
        MCP["MCPClient"]:::client
        Crypto["PQC Utils"]:::crypto
        Config["Config Utils"]:::config
        ConfigYAML["config.yaml"]:::config
        HTTP["HTTP Layer\nrequests.Session"]:::network
        KeyStore[(Key Store\n~/.qu3/keys)]:::storage
        subgraph "External Libraries"
            direction TB
            LibOQS["liboqs-python"]:::extlib
            CryptoLib["cryptography"]:::extlib
            YAMLLib["PyYAML"]:::extlib
            TyperLib["Typer"]:::extlib
        end
    end

    %% Server Side (Mock)
    subgraph "Server (Mock MCP)"
        direction TB
        ServerApp["Mock MCP Server\nFastAPI"]:::server
        Endpoints["Endpoints\n/keys\n/kem-handshake/initiate\n/inference\n/policy-update"]:::server
        subgraph "Server External Libraries"
            direction TB
            FastAPI["fastapi"]:::extlib
            Uvicorn["uvicorn"]:::extlib
            SrvLibOQS["liboqs-python"]:::extlib
            SrvCryptoLib["cryptography"]:::extlib
        end
    end

    %% Tests
    subgraph "Unit Tests"
        direction TB
        TestConfig["test_config_utils.py"]:::test
        TestClient["test_mcp_client.py"]:::test
        TestPQC["test_pqc_utils.py"]:::test
    end

    %% Client Interactions
    CLI -->|"invokes"| MCP
    MCP -->|"uses"| Config
    MCP -->|"uses"| Crypto
    Config -->|"reads"| ConfigYAML
    Config -->|"manages keys"| KeyStore
    Crypto -->|"loads/saves keys"| KeyStore
    MCP -->|"HTTP calls"| HTTP

    %% Network Flow
    HTTP -->|"GET /keys"| Endpoints
    HTTP -->|"POST /kem-handshake/initiate"| Endpoints
    HTTP -->|"POST /inference"| Endpoints
    HTTP -->|"POST /policy-update"| Endpoints

    %% Server Interactions
    Endpoints -->|"handled by"| ServerApp
    ServerApp -->|"uses"| Config
    ServerApp -->|"uses"| Crypto
    ServerApp -->|"accesses"| KeyStore

    %% Tests Dependencies
    TestConfig -->|tests| Config
    TestClient -->|tests| MCP
    TestPQC -->|tests| Crypto

    %% Click Events
    click CLI "https://github.com/qu3ai/qu3-app/blob/main/src/main.py"
    click MCP "https://github.com/qu3ai/qu3-app/blob/main/src/mcp_client.py"
    click Crypto "https://github.com/qu3ai/qu3-app/blob/main/src/pqc_utils.py"
    click Config "https://github.com/qu3ai/qu3-app/blob/main/src/config_utils.py"
    click ConfigYAML "https://github.com/qu3ai/qu3-app/blob/main/config.yaml"
    click ServerApp "https://github.com/qu3ai/qu3-app/blob/main/scripts/mock_mcp_server.py"
    click TestConfig "https://github.com/qu3ai/qu3-app/blob/main/tests/test_config_utils.py"
    click TestClient "https://github.com/qu3ai/qu3-app/blob/main/tests/test_mcp_client.py"
    click TestPQC "https://github.com/qu3ai/qu3-app/blob/main/tests/test_pqc_utils.py"

    %% Styles
    classDef client fill:#E0F7FA,stroke:#006064,color:#004D40
    classDef crypto fill:#F3E5F5,stroke:#6A1B9A,color:#4A148C
    classDef config fill:#E8F5E9,stroke:#2E7D32,color:#1B5E20
    classDef network fill:#ECEFF1,stroke:#546E7A,color:#263238
    classDef storage fill:#FFF3E0,stroke:#EF6C00,color:#E65100
    classDef server fill:#FFEBEE,stroke:#C62828,color:#B71C1C
    classDef extlib fill:#F5F5F5,stroke:#9E9E9E,color:#424242
    classDef test fill:#FFFDE7,stroke:#F9A825,color:#F57F17
```
