# modelscope/ms-agent architecture

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

- Interactive diagram: https://gitdiagram.com/modelscope/ms-agent
- Repository: https://github.com/modelscope/ms-agent

## Overview

MS-Agent is a Python agent framework with multiple packaged application workflows on top. The reusable core lives in `ms_agent/`, while end-user scenarios are implemented under `projects/`, and demonstrations/examples live in `examples/` and `tests/`. The repository also includes a separate `webui/` frontend/backend pair for interactive use.

The lowest common runtime boundary is the agent layer in `ms_agent/agent/`. `LLMAgent`, `CodeAgent`, and the loader/runtime classes turn a user query into an agent session, wire in configuration, and execute the loop that decides when to call models, tools, memory, or workflows. Most higher-level features are built by composing these agent primitives rather than introducing a separate framework.

Model access is abstracted in `ms_agent/llm/`, which contains provider-specific adapters for OpenAI-style APIs, Anthropic, DashScope, DeepSeek, ModelScope, and related helpers/mappings. This keeps the rest of the system model-agnostic and lets workflows switch providers through configuration rather than code changes.

Tooling is a major subsystem. `ms_agent/tools/` contains search, filesystem, code execution, document parsing, data fetching, image/video/audio generation, MCP client support, and task helpers. `ms_agent/capabilities/` and `ms_agent/capabilities/wrappers/` expose these tools as callable capabilities, including MCP server integration and wrappers for agent delegation, web search, filesystem, and LSP-based code server access.

Execution safety and code generation are isolated through `ms_agent/sandbox/` and `ms_agent/tools/code/`. The framework supports local executors and container/sandbox-backed execution, which is important for Code Genesis, FinResearch, and skills that run generated code or perform file modifications. This is one of the main architectural boundaries between “planning” and “unsafe execution.”

The skills subsystem in `ms_agent/skill/` is its own mini-platform. It loads Anthropic-style skills, resolves skill dependencies, builds execution DAGs, and uses containers plus prompts/specs to control what gets loaded into context. The `ms-agent-skills/` directory and the skill examples under `examples/skills/` show that skills are treated as portable artifacts with markdown instructions, scripts, and resource files.

Memory, retrieval, and context management are centralized in `ms_agent/memory/`, `ms_agent/rag/`, `ms_agent/retriever/`, and `ms_agent/knowledge_search/`. The framework supports persistent memory, context condensation/compression, hybrid retrieval, document extraction, and local knowledge search over codebases/docs. This layer sits between the conversational agent and long-horizon tasks, helping reduce token usage and preserve state across runs.

Workflow orchestration is another core boundary. `ms_agent/workflow/` provides base classes plus chain/DAG workflows, and `ms_agent/workflow/deep_research/` contains the shared deep-research logic. Workflows define task decomposition and sub-agent coordination, while the agent layer handles execution and tools. This separation is what allows specialized applications to share the same runtime.

The `projects/` folder contains domain-specific compositions of the framework:
- `projects/deep_research/` and `projects/deep_research/v2/` implement multi-agent research with searcher/researcher/reporter roles and filesystem-backed artifacts.
- `projects/code_genesis/` implements a code-generation pipeline with staged agents such as user story, architect, file design, file order, install, coding, and refine.
- `projects/fin_research/` combines financial data collection, analysis, and report writing, with structured workflows and finance-specific tools/specs.
- `projects/singularity_cinema/` breaks video generation into script, prompts, images, audio, subtitles, animation, rendering, and final composition.
- `projects/doc_research/` focuses on document ingestion and research.

The I/O model for these projects is largely file-centric. Many workflows persist intermediate state, generated artifacts, reports, and session data to disk so runs can be resumed, inspected, or post-processed. That makes the filesystem an explicit integration boundary, not just an implementation detail.

`webui/` is a separate product surface. The backend is FastAPI-based and manages sessions, project discovery, agent execution, worker coordination, and WebSocket streaming. The frontend is a React/Vite app that renders chat, workflow progress, logs, file progress, and deep-research views. Data flows from the browser to the backend, then into agent/workflow execution, and streams back as events and status updates.

Configuration is mostly YAML- and environment-driven. `ms_agent/config/`, the project-level `*.yaml` files, and `README` examples show that agents, workflows, tools, and model endpoints are assembled declaratively. This is important for diagramming because the same runtime can be pointed at very different applications without changing the core code.

The repo is supported by strong packaging and delivery tooling: `setup.py`, `requirements/*.txt`, pre-commit config, and GitHub Actions for linting, CI tests, and publishing. `docs/` contains the architecture- and usage-oriented documentation in English and Chinese, while `tests/` mirrors the main subsystems and gives a good map of the intended boundaries and integration points.

Main entry points: [`ms_agent/agent`](https://github.com/modelscope/ms-agent/tree/main/ms_agent/agent) (Agent loop), [`ms_agent/tools`](https://github.com/modelscope/ms-agent/tree/main/ms_agent/tools) (Tools), [`ms_agent/workflow`](https://github.com/modelscope/ms-agent/tree/main/ms_agent/workflow) (Workflows), [`webui/backend`](https://github.com/modelscope/ms-agent/tree/main/webui/backend) (WebUI backend)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core runtime"]
  node_ms_agent_agent["Agent loop<br/>runtime"]
  node_ms_agent_llm["Model adapters<br/>llm adapters"]
  node_ms_agent_config["Config<br/>settings"]
  node_ms_agent_workflow["Workflows<br/>orchestration"]
  node_ms_agent_skill["Skills<br/>skill platform"]
  node_ms_agent_memory[("Memory<br/>state")]
  node_ms_agent_rag["RAG<br/>retrieval"]
  node_ms_agent_retriever["Retrieval<br/>search"]
end

subgraph group_tools["Tools and execution"]
  node_ms_agent_tools["Tools<br/>tooling"]
  node_ms_agent_capabilities["Capabilities<br/>capability layer"]
  node_ms_agent_sandbox["Sandbox<br/>execution boundary"]
end

subgraph group_apps["Packaged projects"]
  node_projects_deep_research["Deep research<br/>project workflow"]
  node_projects_code_genesis["Code Genesis<br/>project workflow"]
  node_projects_fin_research["Fin research<br/>project workflow"]
  node_projects_singularity_cinema["Cinema pipeline<br/>project workflow"]
  node_projects_doc_research["Doc research<br/>project workflow"]
end

subgraph group_ui["Web UI"]
  node_webui_backend["WebUI backend<br/>api server"]
  node_webui_frontend["WebUI frontend<br/>react app"]
end

subgraph group_support["Support"]
  node_docs_tests_packaging["Docs and delivery<br/>support"]
end

node_ms_agent_config -->|"configure"| node_ms_agent_agent
node_ms_agent_config -->|"select provider"| node_ms_agent_llm
node_ms_agent_agent -->|"call models"| node_ms_agent_llm
node_ms_agent_agent -->|"use tools"| node_ms_agent_tools
node_ms_agent_agent -->|"persist state"| node_ms_agent_memory
node_ms_agent_agent -->|"run workflows"| node_ms_agent_workflow
node_ms_agent_workflow -->|"load skills"| node_ms_agent_skill
node_ms_agent_skill -->|"contain code"| node_ms_agent_sandbox
node_ms_agent_tools -->|"execute safely"| node_ms_agent_sandbox
node_ms_agent_tools -->|"expose capabilities"| node_ms_agent_capabilities
node_ms_agent_capabilities -->|"delegate"| node_ms_agent_tools
node_ms_agent_memory -->|"retrieve context"| node_ms_agent_retriever
node_ms_agent_rag -->|"hybrid search"| node_ms_agent_retriever
node_projects_deep_research -->|"compose"| node_ms_agent_workflow
node_projects_code_genesis -->|"specialize agents"| node_ms_agent_agent
node_projects_code_genesis -->|"generate code"| node_ms_agent_sandbox
node_projects_fin_research -->|"orchestrate"| node_ms_agent_workflow
node_projects_fin_research -->|"fetch data"| node_ms_agent_tools
node_projects_singularity_cinema -->|"pipeline"| node_ms_agent_workflow
node_projects_singularity_cinema -->|"generate media"| node_ms_agent_tools
node_projects_doc_research -->|"ingest docs"| node_ms_agent_rag
node_webui_backend -->|"dispatch"| node_ms_agent_agent
node_webui_backend -->|"control execution"| node_ms_agent_workflow
node_webui_backend -->|"load projects"| node_ms_agent_config
node_webui_frontend -->|"stream events"| node_webui_backend
node_docs_tests_packaging -.->|"cover"| node_ms_agent_agent
node_docs_tests_packaging -.->|"verify"| node_ms_agent_tools
node_docs_tests_packaging -.->|"package"| node_webui_backend

click node_ms_agent_agent "https://github.com/modelscope/ms-agent/tree/main/ms_agent/agent"
click node_ms_agent_llm "https://github.com/modelscope/ms-agent/tree/main/ms_agent/llm"
click node_ms_agent_config "https://github.com/modelscope/ms-agent/tree/main/ms_agent/config"
click node_ms_agent_workflow "https://github.com/modelscope/ms-agent/tree/main/ms_agent/workflow"
click node_ms_agent_skill "https://github.com/modelscope/ms-agent/tree/main/ms_agent/skill"
click node_ms_agent_memory "https://github.com/modelscope/ms-agent/tree/main/ms_agent/memory"
click node_ms_agent_rag "https://github.com/modelscope/ms-agent/tree/main/ms_agent/rag"
click node_ms_agent_retriever "https://github.com/modelscope/ms-agent/tree/main/ms_agent/retriever"
click node_ms_agent_tools "https://github.com/modelscope/ms-agent/tree/main/ms_agent/tools"
click node_ms_agent_capabilities "https://github.com/modelscope/ms-agent/tree/main/ms_agent/capabilities"
click node_ms_agent_sandbox "https://github.com/modelscope/ms-agent/tree/main/ms_agent/sandbox"
click node_projects_deep_research "https://github.com/modelscope/ms-agent/tree/main/projects/deep_research"
click node_projects_code_genesis "https://github.com/modelscope/ms-agent/tree/main/projects/code_genesis"
click node_projects_fin_research "https://github.com/modelscope/ms-agent/tree/main/projects/fin_research"
click node_projects_singularity_cinema "https://github.com/modelscope/ms-agent/tree/main/projects/singularity_cinema"
click node_projects_doc_research "https://github.com/modelscope/ms-agent/tree/main/projects/doc_research"
click node_webui_backend "https://github.com/modelscope/ms-agent/tree/main/webui/backend"
click node_webui_frontend "https://github.com/modelscope/ms-agent/tree/main/webui/frontend"

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_ms_agent_agent,node_ms_agent_llm,node_ms_agent_config,node_ms_agent_workflow,node_ms_agent_skill,node_ms_agent_memory,node_ms_agent_rag,node_ms_agent_retriever toneBlue
class node_ms_agent_tools,node_ms_agent_capabilities,node_ms_agent_sandbox toneAmber
class node_projects_deep_research,node_projects_code_genesis,node_projects_fin_research,node_projects_singularity_cinema,node_projects_doc_research toneMint
class node_webui_backend,node_webui_frontend toneRose
class node_docs_tests_packaging toneIndigo
```

## Components

### Core runtime

Agent, model, config, memory, retrieval, and workflow primitives.

- [Agent loop](https://github.com/modelscope/ms-agent/tree/main/ms_agent/agent): runtime · LLM-driven agent session/runtime boundary with loader and execution loop. · `ms_agent/agent`
- [Model adapters](https://github.com/modelscope/ms-agent/tree/main/ms_agent/llm): llm adapters · Provider-specific LLM integrations and model mapping helpers. · `ms_agent/llm`
- [Config](https://github.com/modelscope/ms-agent/tree/main/ms_agent/config): settings · YAML and environment configuration loading for agents, tools, and models. · `ms_agent/config`
- [Workflows](https://github.com/modelscope/ms-agent/tree/main/ms_agent/workflow): orchestration · Base workflows plus chain, DAG, and deep-research orchestration. · `ms_agent/workflow`
- [Skills](https://github.com/modelscope/ms-agent/tree/main/ms_agent/skill): skill platform · Portable skill loading, dependency resolution, containers, prompts, and specs. · `ms_agent/skill`
- [Memory](https://github.com/modelscope/ms-agent/tree/main/ms_agent/memory): state · Persistent memory, condensation, compression, and memory management. · `ms_agent/memory`
- [RAG](https://github.com/modelscope/ms-agent/tree/main/ms_agent/rag): retrieval · Document extraction, hybrid retrieval, and RAG helpers. · `ms_agent/rag`
- [Retrieval](https://github.com/modelscope/ms-agent/tree/main/ms_agent/retriever): search · Hybrid retriever used by memory and knowledge search flows. · `ms_agent/retriever`

### Tools and execution

Capability wrappers, tool adapters, and unsafe execution boundaries.

- [Tools](https://github.com/modelscope/ms-agent/tree/main/ms_agent/tools): tooling · Search, filesystem, code, parsing, media generation, and task tools. · `ms_agent/tools`
- [Capabilities](https://github.com/modelscope/ms-agent/tree/main/ms_agent/capabilities): capability layer · Callable wrappers and MCP/server capability registry. · `ms_agent/capabilities`
- [Sandbox](https://github.com/modelscope/ms-agent/tree/main/ms_agent/sandbox): execution boundary · Container and local sandbox execution boundary for generated code and file changes. · `ms_agent/sandbox`

### Packaged projects

Opinionated workflow compositions built on the shared runtime.

- [Deep research](https://github.com/modelscope/ms-agent/tree/main/projects/deep_research): project workflow · Multi-agent research composition with search, evidence, and report generation. · `projects/deep_research`
- [Code Genesis](https://github.com/modelscope/ms-agent/tree/main/projects/code_genesis): project workflow · Staged code-generation pipeline from story to architecture, files, install, coding, and refinement. · `projects/code_genesis`
- [Fin research](https://github.com/modelscope/ms-agent/tree/main/projects/fin_research): project workflow · Financial data collection, analysis, and reporting workflow. · `projects/fin_research`
- [Cinema pipeline](https://github.com/modelscope/ms-agent/tree/main/projects/singularity_cinema): project workflow · Video generation pipeline spanning script, prompts, images, audio, subtitles, animation, and rendering. · `projects/singularity_cinema`
- [Doc research](https://github.com/modelscope/ms-agent/tree/main/projects/doc_research): project workflow · Document ingestion and research composition. · `projects/doc_research`

### Web UI

Separate interactive frontend/backend product surface.

- [WebUI backend](https://github.com/modelscope/ms-agent/tree/main/webui/backend): api server · FastAPI backend for sessions, project discovery, execution, workers, and WebSockets. · `webui/backend`
- [WebUI frontend](https://github.com/modelscope/ms-agent/tree/main/webui/frontend): react app · React/Vite interface for chat, progress, logs, and deep-research views. · `webui/frontend`

### Support

Packaging, docs, and test scaffolding around the framework.

- **Docs and delivery**: support · Documentation, tests, requirements, CI, and packaging assets.

## Connections

- Config → Agent loop: configure · Agent sessions are assembled from declarative config and env values.
- Config → Model adapters: select provider · Model endpoints and providers are chosen through configuration.
- Agent loop → Model adapters: call models · The runtime turns user queries into model calls and decisions.
- Agent loop → Tools: use tools · Agents invoke search, filesystem, code, and media tools during the loop.
- Agent loop → Memory: persist state · Agent execution reads and writes long-horizon memory.
- Agent loop → Workflows: run workflows · Agents execute within workflow-defined task decompositions.
- Workflows → Skills: load skills · Workflows can activate portable skill artifacts and dependencies.
- Skills → Sandbox: contain code · Skills use containers/sandboxing to control loaded scripts and execution.
- Tools → Sandbox: execute safely · Code tools delegate unsafe execution to local or container-backed sandboxes.
- Tools → Capabilities: expose capabilities · Tools are surfaced as callable capabilities and wrappers.
- Capabilities → Tools: delegate · Capability wrappers dispatch into underlying tool implementations.
- Memory → Retrieval: retrieve context · Memory relies on retrievers to fetch relevant long-term context.
- RAG → Retrieval: hybrid search · RAG uses retrieval components for document and knowledge lookup.
- Deep research → Workflows: compose · Project-specific deep research logic builds on shared workflow primitives.
- Code Genesis → Agent loop: specialize agents · The pipeline is implemented as staged agent roles on the shared runtime.
- Code Genesis → Sandbox: generate code · Code generation and file editing depend on sandboxed execution.
- Fin research → Workflows: orchestrate · Financial research combines collection, analysis, and reporting workflows.
- Fin research → Tools: fetch data · Finance flows depend on data sources and analysis-oriented tools.
- Cinema pipeline → Workflows: pipeline · Video generation is staged as a multi-step workflow.
- Cinema pipeline → Tools: generate media · The pipeline uses image, audio, and video generation tools.
- Doc research → RAG: ingest docs · Document research is centered on extraction and retrieval.
- WebUI backend → Agent loop: dispatch · The backend starts and coordinates agent sessions and runs.
- WebUI backend → Workflows: control execution · Backend routes project/workflow execution and worker coordination.
- WebUI backend → Config: load projects · Backend uses configuration and project discovery to assemble runs.
- WebUI frontend → WebUI backend: stream events · UI sends chat and control actions to the backend and receives live updates.
- Docs and delivery → Agent loop: cover · Tests and docs mirror the runtime boundaries and expected behavior.
- Docs and delivery → Tools: verify · Tooling and capability integrations are exercised by tests.
- Docs and delivery → WebUI backend: package · Delivery tooling covers the backend and frontend product surface.
