# huayaoai/fingenius architecture

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

- Interactive diagram: https://gitdiagram.com/huayaoai/fingenius
- Repository: https://github.com/huayaoai/fingenius

## Overview

##### Project type and purpose
- FinGenius is an open-source, command-line AI-powered financial analysis tool for A-share markets.
- It orchestrates a “Research–Battle” multi-agent architecture: six specialized AI agents collaborate to analyze market data (Research) then engage in a structured debate (Battle) and produce an HTML report.

##### File structure
- Top level
- main.py – entry point
- config/ – example configuration for LLM API and MCP protocol
- src/ – source code
- report/ – generated HTML templates and assets
- requirements.txt, LICENSE, README.md
- src/
- agent/ – BaseAgent, ReActAgent, ToolCallAgent, MCPAgent, and six concrete agents (sentiment, risk_control, hot_money, technical_analysis, chip_analysis, big_deal_analysis, report)
- environment/ – BaseEnvironment, ResearchEnvironment, BattleEnvironment, BattleState, EnvironmentFactory
- mcp/ – MCP servers (battle_server, sentiment_server, risk_control_server, hot_money_server, technical_analysis_server, server.py)
- prompt/ – system and next-step prompts for each agent
- tool/ – Tool abstraction and concrete tools (LLM calls, HTML generation, web/financial search, text-to-speech)
- ollama_client.py – wrapper for local LLM hosting
- llm.py, logger.py, schema.py, exceptions.py, console.py
- utils/ – report manager and cleanup scripts

##### README for additional insights
- Describes twin-environment flow:
1. ResearchEnvironment: parallel/sequential deep analysis by 6 professional agents, each can invoke tools via MCP protocol.
2. BattleEnvironment: structured multi-round debate where agents speak in sequence, vote, and reach consensus.

##### Essential elements to capture
- Entry point (main.py → EnvironmentFactory)
- Dual-environment sequence (Research → Battle)
- Agent hierarchy and tool invocation flow
- MCP protocol servers as separate processes
- Report generation path

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Orchestration Layer
    subgraph "Orchestration"
        direction TB
        Main["main.py"]:::orchestration
        Console["Console Interface"]:::orchestration
        ConfigApp["config/config.example.toml"]:::orchestration
        ConfigMCP["config/mcp.example.json"]:::orchestration
        ConfigLoader["Runtime Config Loader"]:::orchestration
    end

    %% Environment Layer
    subgraph "Environments"
        direction TB
        EnvFactory["EnvironmentFactory"]:::environment
        ResearchEnv["ResearchEnvironment"]:::environment
        BattleEnv["BattleEnvironment"]:::environment
    end

    %% Agents Layer
    subgraph "Agents"
        direction TB
        BaseAgent["BaseAgent"]:::agent
        ReActAgent["ReActAgent"]:::agent
        ToolCallAgent["ToolCallAgent"]:::agent
        MCPAgent["MCPAgent"]:::agent
        SentimentAgent["Sentiment Analysis Agent"]:::agent
        RiskAgent["Risk Control Agent"]:::agent
        HotMoneyAgent["Hot Money Agent"]:::agent
        TechnicalAgent["Technical Analysis Agent"]:::agent
        ChipAgent["Chip Analysis Agent"]:::agent
        BigDealAgent["Big Deal Analysis Agent"]:::agent
        ReportAgent["Report Aggregator Agent"]:::agent
    end

    %% Tools & MCP Servers Layer
    subgraph "Tools & MCP Servers"
        direction TB
        subgraph "Tools"
            direction TB
            ToolBase["Tool Base"]:::tools
            ToolCollection["Tool Collection"]:::tools
            ChatTool["Chat Completion Tool"]:::tools
            HTMLTool["HTML Generation Tool"]:::tools
            TTSTool["TTS Tool"]:::tools
            StockTool["Stock Info Request Tool"]:::tools
            WebSearch["Web Search Wrapper"]:::tools
            MCPClient["MCP Client"]:::tools
            FinancialTools["Financial Deep-Search"]:::tools
            SentimentTool["Sentiment Tool"]:::tools
            RiskTool["Risk Control Tool"]:::tools
            HotMoneyTool["Hot Money Tool"]:::tools
            TechnicalTool["Technical Analysis Tool"]:::tools
        end
        subgraph "MCP Servers"
            direction TB
            MCPServerCore["MCP Server"]:::tools
            BattleServer["Battle Server"]:::tools
            SentimentServer["Sentiment Server"]:::tools
            RiskServer["Risk Control Server"]:::tools
            HotMoneyServer["Hot Money Server"]:::tools
            TechnicalServer["Technical Analysis Server"]:::tools
        end
    end

    %% LLM & External Services Layer
    subgraph "LLM & External Services"
        direction TB
        LLMWrapper["LLM Wrapper"]:::external
        Ollama["Ollama Client"]:::external
        ExternalLLM["External LLM API"]:::external
    end

    %% Reporting & Utilities Layer
    subgraph "Reporting & Utilities"
        direction TB
        ReportManager["Report Manager"]:::report
        Cleanup["Cleanup Reports"]:::report
        ReportFolder["Report Templates & Assets"]:::report
    end

    %% Connections
    Main -->|invokes| Console
    Main -->|loads config| ConfigLoader
    ConfigLoader --> ConfigApp
    ConfigLoader --> ConfigMCP
    Main -->|initializes| EnvFactory
    EnvFactory -->|creates| ResearchEnv
    ResearchEnv -->|register Agents| BaseAgent
    ResearchEnv --> BattleEnv
    BattleEnv -->|starts debate| BaseAgent
    BaseAgent -->|uses| ToolCallAgent
    BaseAgent -->|uses| MCPAgent
    BaseAgent -->|delegates to| ReActAgent
    BaseAgent -->|delegates to| ToolCallAgent
    BaseAgent -->|delegates to| MCPAgent
    BaseAgent -->|delegates to| SentimentAgent
    BaseAgent -->|delegates to| RiskAgent
    BaseAgent -->|delegates to| HotMoneyAgent
    BaseAgent -->|delegates to| TechnicalAgent
    BaseAgent -->|delegates to| ChipAgent
    BaseAgent -->|delegates to| BigDealAgent
    BaseAgent -->|delegates to| ReportAgent
    BaseAgent -->|calls tool| ChatTool
    BaseAgent -->|calls tool| HTMLTool
    BaseAgent -->|calls tool| TTSTool
    BaseAgent -->|calls tool| StockTool
    BaseAgent -->|calls tool| WebSearch
    ChatTool -->|via client| MCPClient
    MCPClient --> MCPServerCore
    SentimentTool --> SentimentServer
    RiskTool --> RiskServer
    HotMoneyTool --> HotMoneyServer
    TechnicalTool --> TechnicalServer
    ChatTool --> LLMWrapper
    LLMWrapper --> Ollama
    Ollama --> ExternalLLM
    BattleEnv -->|produces report data| ReportManager
    ReportManager --> ReportFolder
    ReportManager --> Cleanup

    %% Click Events
    click Main "https://github.com/huayaoai/fingenius/blob/main/main.py"
    click Console "https://github.com/huayaoai/fingenius/blob/main/src/console.py"
    click ConfigApp "https://github.com/huayaoai/fingenius/blob/main/config/config.example.toml"
    click ConfigMCP "https://github.com/huayaoai/fingenius/blob/main/config/mcp.example.json"
    click ConfigLoader "https://github.com/huayaoai/fingenius/blob/main/src/config.py"
    click EnvFactory "https://github.com/huayaoai/fingenius/blob/main/src/environment/base.py"
    click ResearchEnv "https://github.com/huayaoai/fingenius/blob/main/src/environment/research.py"
    click BattleEnv "https://github.com/huayaoai/fingenius/blob/main/src/environment/battle.py"
    click BaseAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/base.py"
    click ReActAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/react.py"
    click ToolCallAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/toolcall.py"
    click MCPAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/mcp.py"
    click SentimentAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/sentiment.py"
    click RiskAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/risk_control.py"
    click HotMoneyAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/hot_money.py"
    click TechnicalAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/technical_analysis.py"
    click ChipAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/chip_analysis.py"
    click BigDealAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/big_deal_analysis.py"
    click ReportAgent "https://github.com/huayaoai/fingenius/blob/main/src/agent/report.py"
    click LLMWrapper "https://github.com/huayaoai/fingenius/blob/main/src/llm.py"
    click Ollama "https://github.com/huayaoai/fingenius/blob/main/src/ollama_client.py"
    click MCPServerCore "https://github.com/huayaoai/fingenius/blob/main/src/mcp/server.py"
    click BattleServer "https://github.com/huayaoai/fingenius/blob/main/src/mcp/battle_server.py"
    click SentimentServer "https://github.com/huayaoai/fingenius/blob/main/src/mcp/sentiment_server.py"
    click RiskServer "https://github.com/huayaoai/fingenius/blob/main/src/mcp/risk_control_server.py"
    click HotMoneyServer "https://github.com/huayaoai/fingenius/blob/main/src/mcp/hot_money_srver.py"
    click TechnicalServer "https://github.com/huayaoai/fingenius/blob/main/src/mcp/technical_analysis_server.py"
    click ToolBase "https://github.com/huayaoai/fingenius/blob/main/src/tool/base.py"
    click ToolCollection "https://github.com/huayaoai/fingenius/blob/main/src/tool/tool_collection.py"
    click ChatTool "https://github.com/huayaoai/fingenius/blob/main/src/tool/create_chat_completion.py"
    click HTMLTool "https://github.com/huayaoai/fingenius/blob/main/src/tool/create_html.py"
    click TTSTool "https://github.com/huayaoai/fingenius/blob/main/src/tool/tts_tool.py"
    click StockTool "https://github.com/huayaoai/fingenius/blob/main/src/tool/stock_info_request.py"
    click WebSearch "https://github.com/huayaoai/fingenius/blob/main/src/tool/web_search.py"
    click MCPClient "https://github.com/huayaoai/fingenius/blob/main/src/tool/mcp_client.py"
    click FinancialTools "https://github.com/huayaoai/fingenius/tree/main/src/tool/financial_deep_search/"
    click SentimentTool "https://github.com/huayaoai/fingenius/blob/main/src/tool/sentiment.py"
    click RiskTool "https://github.com/huayaoai/fingenius/blob/main/src/tool/risk_control.py"
    click HotMoneyTool "https://github.com/huayaoai/fingenius/blob/main/src/tool/hot_money.py"
    click TechnicalTool "https://github.com/huayaoai/fingenius/blob/main/src/tool/technical_analysis.py"
    click ReportManager "https://github.com/huayaoai/fingenius/blob/main/src/utils/report_manager.py"
    click Cleanup "https://github.com/huayaoai/fingenius/blob/main/src/utils/cleanup_reports.py"
    click ReportFolder "https://github.com/huayaoai/fingenius/tree/main/report/"

    %% Styles
    classDef orchestration fill:#cce5ff,stroke:#000,stroke-width:1px;
    classDef environment fill:#d4edda,stroke:#000,stroke-width:1px;
    classDef agent fill:#ffe5b4,stroke:#000,stroke-width:1px;
    classDef tools fill:#e2d6f7,stroke:#000,stroke-width:1px;
    classDef external fill:#f0f0f0,stroke:#000,stroke-width:1px;
    classDef report fill:#fff3cd,stroke:#000,stroke-width:1px;
```
