Skip to content

Architecture Overview Developer

ChatAI Plugin uses modular layered architecture design, implementing tool calling system based on MCP standard.

Before Reading

It's recommended to understand MCP (Model Context Protocol) basics first.

Architecture Diagram

Core Concepts

Three-Layer Architecture

Layered Design Principle

Each layer only depends on lower layers, not upper layers, ensuring module decoupling and testability.

LayerDescriptionMain ModulesResponsibility
ApplicationMessage handling, command responseapps/, ChatListenerReceive Yunzai message events, route to handlers
ServiceBusiness logic, API servicesservices/, agent/LLM calls, context management, tool orchestration
CoreInfrastructure, adapterscore/, mcp/Multi-model adaptation, MCP protocol, cache storage

MCP System

MCP Protocol

MCP (Model Context Protocol) is an open standard by Anthropic for defining AI-tool interactions.

ComponentFileResponsibility
McpManagersrc/mcp/McpManager.jsUnified management of all tool sources, provides registration, query, call interfaces
McpClientsrc/mcp/McpClient.jsMCP protocol client, supports stdio/npm/SSE/HTTP transports
BuiltinMcpServersrc/mcp/BuiltinMcpServer.jsBuilt-in tool server, manages 22 tool categories and custom JS tools

Skills Agent

Skills Agent is the business abstraction layer above MCP, defined in src/services/agent/SkillsAgent.js:

FunctionDescription
Unified Skill InterfaceIntegrates all tool sources into unified "skill" concept
Permission FilteringFilters available tools based on user/group permissions
Auto Parameter InjectionAuto-injects user_id, group_id and other context parameters
Preset Level ControlSupports preset-level tool whitelist/blacklist

Directory Structure

Full Directory Structure (click to expand)
chatgpt-plugin/
├── apps/                    # Application modules (Yunzai plugin entry)
│   ├── chat.js              # Main chat handler
│   ├── Commands.js          # Command handler
│   ├── Management.js        # Management commands
│   └── ...                  # Other functional modules
├── config/                  # Configuration management
├── data/                    # Runtime data
│   ├── presets/             # Preset files
│   ├── tools/               # Custom JS tools
│   ├── mcp-servers.json     # MCP server configuration
│   └── chatai.db            # SQLite database
├── src/
│   ├── core/                # Core layer
│   │   ├── adapters/        # LLM adapters
│   │   │   ├── AbstractClient.js  # Abstract base class
│   │   │   ├── openai/      # OpenAI client
│   │   │   ├── claude/      # Claude client
│   │   │   └── gemini/      # Gemini client
│   │   ├── types/           # Type definitions
│   │   └── utils/           # Utility functions
│   ├── mcp/                 # MCP system
│   │   ├── McpManager.js    # MCP manager
│   │   ├── McpClient.js     # MCP client
│   │   ├── BuiltinMcpServer.js  # Built-in tool server
│   │   └── tools/           # Built-in tools (22 categories)
│   └── services/            # Service layer
│       ├── agent/           # Skills Agent
│       ├── llm/             # LLM service
│       ├── storage/         # Storage service
│       ├── routes/          # API routes
│       └── webServer.js     # Web service
└── index.js                 # Plugin entry

Core Directory Reference:

DirectoryDescriptionKey Files
apps/Yunzai plugin entrychat.js main chat handler
src/core/adapters/LLM adaptersAbstractClient.js base class
src/mcp/MCP systemMcpManager.js tool management
src/services/Service layerllm/, agent/, storage/
data/tools/Custom toolsUser JS tool scripts

Core Components

Component Dependencies

Components listed in dependency order, upper components depend on lower ones.

ComponentFileResponsibilityLayer
AbstractClientsrc/core/adapters/AbstractClient.jsLLM adapter base class, tool call parsingCore
OpenAIClientsrc/core/adapters/openai/OpenAIClient.jsOpenAI/compatible API clientCore
McpManagersrc/mcp/McpManager.jsTool registration, query, call managementCore
McpClientsrc/mcp/McpClient.jsMCP protocol client (stdio/npm/SSE/HTTP)Core
BuiltinMcpServersrc/mcp/BuiltinMcpServer.jsBuilt-in tool server, context management, hot reloadCore
SkillsAgentsrc/services/agent/SkillsAgent.jsSkill agent, permission control, parameter injectionService
ToolFilterServicesrc/services/tools/ToolFilterService.jsTool filtering, blacklist/whitelistService

Design Principles

PrincipleDescriptionImplementation
🧩 ModularDecoupled features, independent maintenanceEach module in separate directory with clear exports
🔌 ExtensibleSupport custom tools and adaptersAbstract base classes + plugin mechanism
📋 StandardizedFollow MCP protocol specImplement MCP standard interfaces
🔒 SecureComplete permission control mechanismMulti-layer permission filtering, dangerous command interception

Detailed Documentation

📚 Deep Dive into Each Module

DocumentContentRecommended
Layered ArchitectureDetailed layer structure explanation⭐⭐
MCP SystemMCP protocol implementation details⭐⭐⭐
Skills AgentSkill agent system⭐⭐
Data FlowRequest processing flow⭐⭐⭐
LLM AdaptersMulti-model adaptation implementation⭐⭐⭐
Storage SystemData persistence solution⭐⭐

Released under the MIT License