# daodao97/chatmcp architecture

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

- Interactive diagram: https://gitdiagram.com/daodao97/chatmcp
- Repository: https://github.com/daodao97/chatmcp

## Overview

##### Main Components
- Frontend/UI Layer:
- This is built primarily with Flutter (see the “lib” directory containing UI pages under “lib/page”, widgets under “lib/widgets”, and various layouts).
- It includes components such as chat interfaces, setting pages (for configuring LLM API keys and MCP server settings) and preview screens (as evidenced by the assets preview images).
- Business Logic and Service Layer:
- This covers providers (in “lib/provider”) managing state for chat, settings, and MCP server interactions.
- There is also a “dao” (data access objects) layer (in “lib/dao”) to handle database operations and persistence (for example, storing chat history).
- LLM & MCP Communication:
- The “lib/llm” directory contains different clients for various LLM providers (OpenAI, Claude, OLLama, DeepSeek) alongside utilities and models.
- The “lib/mcp” directory shows integration with MCP protocol components, including client interfaces, models, and both SSE and stdio based communications.
- Data and Configuration Management:
- Use of SQLite (via sqflite or sqflite_common_ffi) is indicated by database files (e.g., “assets/test.db”) and initialization scripts in “lib/dao/init_db.dart”.
- There is also configuration management for the MCP server found in assets and documented in the README (“mcp_server.json”).
- Native Platform Integrations:
- Separate directories for Android, iOS, macOS, Linux, Windows highlight native configurations needed by Flutter to deploy on each platform. These include native build configurations, platform-specific assets, and project files.

##### Relationships and Interactions
- The UI components (chat pages, settings pages) interact with provider classes to mediate state changes and user inputs.
- These providers depend on the service layer (LLM and MCP clients) to send chat requests, retrieve responses, and update the UI.
- When user interactions require database operations (e.g., saving chat history), the provider layer makes calls to the DAO components.
- The LLM clients interact over network or local process using the MCP protocol (which could be via SSE or standard IO channels) to coordinate with external or embedded servers.
- Underlying configuration files and assets are loaded at startup to provide localization (from “lib/l10n”), UI theming (light/dark mode support), and visual previews.

##### Architectural Patterns and Design Principles
- Separation of concerns: clear division between UI, state management (provider), backend services (LLM/MCP clients), and persistence (DAO).
- Use of a modular architecture: subdirectories group corresponding functionalities (e.g., “llm”, “dao”, “mcp”).
- Cross-platform abstraction: Flutter code in “lib” abstracts the UI and business logic so that platform-specific folders only contain configuration and native code necessary for deployment.
- Extensibility: the layered design allows for adding new LLM providers or MCP server functionalities without major refactoring.

##### Relevant Technologies, Frameworks, & Libraries
- Flutter (for cross-platform UI development).
- Dart (programming language used in the “lib” directory).
- SQLite (as the database via sqflite or sqflite ffi on Linux).
- Provider or similar state management solutions in Dart.
- Native configuration files and build systems for Android (Gradle), iOS (Xcode), macOS, Linux, and Windows.
- External API integrations (e.g., OpenAI, Claude, OLLama, DeepSeek).

- UI Layer (Chat, Settings, Widgets)
- Provider Layer (State Management for Chat, Settings, MCP Server)
- Service Layer (LLM Clients, MCP Client using SSE/STDIO)
- Data Access Layer (DAO components and database)
- External Configurations/Assets and Localization Modules
- Native Integration Components (group these by Android, iOS, Desktop)
- Data flow: from the UI to providers, from providers to back-end services, and vice versa.
- Dependencies: i.e., the UI depends on providers; providers depend on DAO and network modules.
- For example, use blue rectangles for UI, green for business logic/providers, orange for service/network components, and yellow for persistence.
- Critical integration points like:
- MCP Server communication (potentially highlighting protocols like SSE).
- LLM API management and key configuration settings.
- Database interactions (via SQL and SQLite files).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        UI1["Chat Page (chat_page.dart)"]:::uiStyle
        UI2["Settings Page"]:::uiStyle
        UI3["Widgets"]:::uiStyle
        UI4["Localization & Theming"]:::uiStyle
        UI5["Asset Previews"]:::uiStyle
    end

    %% Business Logic / Provider Layer
    subgraph "Business Logic and Service Layer"
        PROV1["Chat Provider"]:::providerStyle
        PROV2["Settings Provider"]:::providerStyle
        PROV3["MCP Server Provider"]:::providerStyle
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        DAO1["Init DB"]:::daoStyle
        DAO2["Chat DAO"]:::daoStyle
    end

    %% LLM & MCP Communication Layer
    subgraph "LLM & MCP Communication"
        LLM1["OpenAI Client"]:::serviceStyle
        LLM2["Claude Client"]:::serviceStyle
        MCP1["MCP Client (Common)"]:::serviceStyle
        MCP2["MCP SSE"]:::serviceStyle
        MCP3["MCP STDIO"]:::serviceStyle
    end

    %% Data & Configuration Management
    subgraph "Data and Configuration Management"
        CONF1["SQLite DB"]:::configStyle
        CONF2["MCP Server Config"]:::configStyle
        CONF3["MCP Market Config"]:::configStyle
    end

    %% Native Platform Integrations
    subgraph "Native Platform Integrations"
        NATIVE1["Android Config"]:::nativeStyle
        NATIVE2["iOS Config"]:::nativeStyle
        NATIVE3["Linux"]:::nativeStyle
        NATIVE4["macOS"]:::nativeStyle
        NATIVE5["Windows"]:::nativeStyle
    end

    %% Relationships
    UI1 -->|"emit chat-events"| PROV1
    UI2 -->|"update settings"| PROV2
    UI3 -->|"used by UI"| UI1
    UI4 -->|"provides localization"| UI1
    UI4 -->|"provides theming"| UI2
    UI5 -->|"visual assets"| UI1

    PROV1 -->|"handle persistence"| DAO2
    PROV1 -->|"trigger network calls"| LLM1
    PROV1 -->|"trigger network calls"| LLM2
    PROV1 -->|"trigger MCP comms"| MCP1
    PROV1 -->|"trigger MCP comms"| MCP2
    PROV1 -->|"trigger MCP comms"| MCP3

    PROV2 -->|"store settings"| DAO1
    PROV3 -->|"manage MCP server"| DAO1

    DAO1 -->|"init SQLite"| CONF1
    DAO2 -->|"read/write chat history"| CONF1

    LLM1 ---|"utilizes"| CONF2
    LLM2 ---|"utilizes"| CONF2
    MCP1 ---|"uses protocol"| CONF2
    MCP2 ---|"uses SSE"| CONF2
    MCP3 ---|"uses STDIO"| CONF2

    PROV3 ---|"reads MCP config"| CONF3

    %% Native integrations support UI Layer and Providers for deployment
    UI1 ---|"platform-specific optimization"| NATIVE1
    UI1 ---|"platform-specific optimization"| NATIVE2
    UI1 ---|"platform-specific optimization"| NATIVE3
    UI1 ---|"platform-specific optimization"| NATIVE4
    UI1 ---|"platform-specific optimization"| NATIVE5

    %% Click Events for UI Layer
    click UI1 "https://github.com/daodao97/chatmcp/blob/main/lib/page/layout/chat_page/chat_page.dart"
    click UI2 "https://github.com/daodao97/chatmcp/tree/main/lib/page/setting"
    click UI3 "https://github.com/daodao97/chatmcp/tree/main/lib/widgets"
    click UI4 "https://github.com/daodao97/chatmcp/tree/main/lib/l10n"
    click UI4 "https://github.com/daodao97/chatmcp/tree/main/lib/generated"
    click UI5 "https://github.com/daodao97/chatmcp/tree/main/assets/preview"

    %% Click Events for Business Logic and Service Layer
    click PROV1 "https://github.com/daodao97/chatmcp/blob/main/lib/provider/chat_provider.dart"
    click PROV2 "https://github.com/daodao97/chatmcp/blob/main/lib/provider/settings_provider.dart"
    click PROV3 "https://github.com/daodao97/chatmcp/blob/main/lib/provider/mcp_server_provider.dart"

    %% Click Events for Data Access Layer
    click DAO1 "https://github.com/daodao97/chatmcp/blob/main/lib/dao/init_db.dart"
    click DAO2 "https://github.com/daodao97/chatmcp/blob/main/lib/dao/chat.dart"

    %% Click Events for LLM & MCP Communication Layer
    click LLM1 "https://github.com/daodao97/chatmcp/blob/main/lib/llm/openai_client.dart"
    click LLM2 "https://github.com/daodao97/chatmcp/blob/main/lib/llm/claude_client.dart"
    click MCP1 "https://github.com/daodao97/chatmcp/tree/main/lib/mcp/client"
    click MCP2 "https://github.com/daodao97/chatmcp/tree/main/lib/mcp/sse"
    click MCP3 "https://github.com/daodao97/chatmcp/tree/main/lib/mcp/stdio"

    %% Click Events for Data & Configuration Management
    click CONF1 "https://github.com/daodao97/chatmcp/blob/main/assets/test.db"
    click CONF2 "https://github.com/daodao97/chatmcp/blob/main/assets/mcp_server.json"
    click CONF3 "https://github.com/daodao97/chatmcp/blob/main/assets/mcp_server_market.json"

    %% Click Events for Native Platform Integrations
    click NATIVE1 "https://github.com/daodao97/chatmcp/blob/main/android/app/src/main/AndroidManifest.xml"
    click NATIVE1 "https://github.com/daodao97/chatmcp/blob/main/build.gradle.kts"
    click NATIVE2 "https://github.com/daodao97/chatmcp/blob/main/ios/Runner/AppDelegate.swift"
    click NATIVE2 "https://github.com/daodao97/chatmcp/blob/main/ios/Runner/Info.plist"
    click NATIVE3 "https://github.com/daodao97/chatmcp/tree/main/linux"
    click NATIVE4 "https://github.com/daodao97/chatmcp/tree/main/macos"
    click NATIVE5 "https://github.com/daodao97/chatmcp/tree/main/windows"

    %% Styles
    classDef uiStyle fill:#b3cde0,stroke:#03396c,stroke-width:2px;
    classDef providerStyle fill:#ccebc5,stroke:#006400,stroke-width:2px;
    classDef serviceStyle fill:#fdb462,stroke:#ff7f00,stroke-width:2px;
    classDef daoStyle fill:#ffedbc,stroke:#e6b800,stroke-width:2px;
    classDef configStyle fill:#fadadd,stroke:#ff69b4,stroke-width:2px;
    classDef nativeStyle fill:#d9d9d9,stroke:#808080,stroke-width:2px;
```
