How modelscope/ms-agent works
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: agent Agent loop · tools Tools · workflow Workflows · backend WebUI backend
Components 19
Core runtime
Agent, model, config, memory, retrieval, and workflow primitives.
- Agent loop · LLM-driven agent session/runtime boundary with loader and execution loop.
ms_agent/agent - Model adapters · Provider-specific LLM integrations and model mapping helpers.
ms_agent/llm - Config · YAML and environment configuration loading for agents, tools, and models.
ms_agent/config - Workflows · Base workflows plus chain, DAG, and deep-research orchestration.
ms_agent/workflow - Skills · Portable skill loading, dependency resolution, containers, prompts, and specs.
ms_agent/skill - Memory · Persistent memory, condensation, compression, and memory management.
ms_agent/memory - RAG · Document extraction, hybrid retrieval, and RAG helpers.
ms_agent/rag - Retrieval · Hybrid retriever used by memory and knowledge search flows.
ms_agent/retriever
Tools and execution
Capability wrappers, tool adapters, and unsafe execution boundaries.
- Tools · Search, filesystem, code, parsing, media generation, and task tools.
ms_agent/tools - Capabilities · Callable wrappers and MCP/server capability registry.
ms_agent/capabilities - Sandbox · 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 · Multi-agent research composition with search, evidence, and report generation.
projects/deep_research - Code Genesis · Staged code-generation pipeline from story to architecture, files, install, coding, and refinement.
projects/code_genesis - Fin research · Financial data collection, analysis, and reporting workflow.
projects/fin_research - Cinema pipeline · Video generation pipeline spanning script, prompts, images, audio, subtitles, animation, and rendering.
projects/singularity_cinema - Doc research · Document ingestion and research composition.
projects/doc_research
Web UI
Separate interactive frontend/backend product surface.
- WebUI backend · FastAPI backend for sessions, project discovery, execution, workers, and WebSockets.
webui/backend - WebUI frontend · 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 · Documentation, tests, requirements, CI, and packaging assets.
Connections 28
- 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.