MASFactory is a VibeGraphing oriented framework for building agentic systems.
Project description
ใEnglish | Chineseใ
๐ Overview
MASFactory is a graph-first Multi-Agent Orchestration framework built for VibeGraphing workflows: start from a natural-language intent to sketch a Graph, iteratively converge the structure with visual preview/editing, compile it into an executable workflow, and observe runtime states/messages/shared-attributes end-to-end.
Key capabilities:
- VibeGraphing (intent โ graph): turn โwhat you want to buildโ into an initial graph draft, then refine it into a runnable and reusable workflow.
- Graph contract & composability: model control flow and field contracts explicitly with
Node/Edge, and scale with subgraphs, loops, switches, and composite components. - Full-path visualization & white-box debugging: MASFactory Visualizer provides topology preview, runtime tracing, and human-in-the-loop interactions.
- Context protocol (ContextBlock): unify Memory / RAG / MCP as structured
ContextBlocks with passive injection and active on-demand retrieval for controllable, auditable context.
โก๏ธQuick Start
Install MASFactory Environment
Install using UV
# If uv is not installed yet, install uv first
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone repository
git clone <repository-url>
cd <repo>
# Install dependencies
uv sync
# Install masfactory as python lib
uv pip install -e .
Install using conda
# Clone repository
git clone <repository-url>
cd <repo>
# Create conda virtual environment
conda create -n <your-conda-env-name> python=3.10
# Activate virtual environment
conda activate <your-conda-env-name>
# Install dependencies
pip install -r requirements.txt
# Install masfactory as python lib (recommended)
pip install -e .
Preview docs locally (VitePress)
npm --prefix docs install
npm --prefix docs run docs:dev
Run Multi-Agent Frameworks Reproduced with MASFactory
# ChatDev
python applications/chatdev/run.py --task "a simple pingpong game" --name "pingpong"
# ChatDev Lite (a lighter workflow example)
python applications/chatdev_lite/workflow/main.py --task "Develop a basic Gomoku game." --name "Gomoku"
# AgentVerse.Tasksolving
python applications/agentverse/tasksolving/main.py --config python_calculator --task "Write a function to calculate the factorial of a number"
Easy Learning
MASFactory keeps the core primitives small (Node/Edge/Graph) while remaining scalable. Recommended entry points:
- Docs site (VitePress):
docs/(English entry:docs/src/en/start/introduction.md) - Agent runtime (Observe/Think/Act):
docs/src/en/guide/agent_runtime.md - Context adapters (RAG/Memory/MCP via ContextBlock):
docs/src/en/guide/context_adapters.md - Examples:
applications/(ChatDev, AgentVerse, etc.) - Graph patterns library:
examples/(imperative vs declarative, batch runnable)
Project Directory Structure
.
โโโ masfactory/ # MASFactory Framework
โ โโโ core/ # Foundation: Node / Edge / Gate / Message
โ โโโ components/ # Components (Agents / Graphs / Controls / CustomNode)
โ โ โโโ agents/ # Agent, DynamicAgent, SingleAgent
โ โ โโโ graphs/ # BaseGraph, Graph, RootGraph, Loop
โ โ โโโ controls/ # LogicSwitch, AgentSwitch
โ โโโ adapters/ # Adapters (Model / Tool / Memory / Retrieval / MCP)
โ โ โโโ context/ # Context pipeline (ContextBlock / policy / renderer / composer)
โ โโโ integrations/ # 3rd-party integrations (MemoryOS / UltraRAG, etc.)
โ โโโ utils/ # Utilities (config, hook, Embedding, etc.)
โ โโโ resources/ # Resources and static files
โ โโโ visualizer/ # MASFactory Visualizer runtime integration
โโโ masfactory-visualizer/ # VSCode extension: MASFactory Visualizer
โโโ applications/ # Examples and reproductions based on MASFactory
โ โโโ chatdev_lite/
โ โโโ chatdev/
โ โโโ agentverse/
โ โโโ camel/
โ โโโ number_off_demo.py
โโโ docs/ # VitePress docs
โ โโโ .vitepress/
โ โโโ src/
โ โโโ zh/
โ โโโ en/
โโโ examples/ # Graph patterns (imperative vs declarative)
โโโ README.md # English (default)
โโโ README.zh.md # Chinese
โโโ pyproject.toml
โโโ requirements.txt
โโโ uv.lock
Simple Example
This example shows a minimal two-Agent workflow (declarative). For more patterns, see applications/ and the docs site.
# A minimal runnable example: two Agents collaborating to complete a task (declarative)
from masfactory import RootGraph, Agent, NodeTemplate, OpenAIModel
"""
Diagram:
+----------------------------------------------+
| RootGraph (demo) |
| |
| +-------------+ |
| | entry | |
| +-------------+ |
| | user_question |
| v |
| +-------------+ |
| | analyser | |
| +-------------+ |
| | requirements_report |
| v |
| +-------------+ |
| | coder | |
| +-------------+ |
| | codes |
| v |
| +-------------+ |
| | exit | |
| +-------------+ |
| |
+----------------------------------------------+
"""
# 1) Create model; model parameters can be loaded from config/environment variables
model = OpenAIModel(
api_key="your api key",
base_url="your api base url",
model_name="gpt-4o-mini",
)
# 2) Reuse a base Agent template
BaseAgent = NodeTemplate(Agent, model=model)
# 3) Declarative graph (requirement analysis -> code writing)
graph = RootGraph(
name="demo",
nodes=[
(
"analyser",
BaseAgent(
instructions=[
"You are a software product manager.",
"Analyze user requirements and produce a short requirement report.",
],
prompt_template="User request: {user_question}",
),
),
(
"coder",
BaseAgent(
instructions=[
"You are a senior software engineer.",
"Write runnable Python code based on the requirement report.",
],
prompt_template="Requirement report: {requirements_report}",
),
),
],
edges=[
("entry", "analyser", {"user_question": "User request"}),
("analyser", "coder", {"requirements_report": "Requirement analysis report"}),
("coder", "exit", {"codes": "Program code"}),
],
)
# 4) Build and run
graph.build()
output, _attrs = graph.invoke({"user_question": "Write a Python function that adds two numbers"})
print(output) # => {"codes": "..."}
๐งOverall Architecture
MASFactory has 3 layers: core engine, component layer, and adapter layer, unified around the Node/Edge graph abstraction for control flow and data flow.
Core Components (Core)
- Node: Abstract computational units on the Graph, with Graphs, Agents, control logic, etc. all being derived classes of Node.
- Edge: Connects two nodes, used for process control and automatic message forwarding.
- Message: Unified message parsing and distribution mechanism.
Functional Components (Components):
Top-level Components:
Top-level components refer to objects that can be directly instantiated and run independently by users. They are at the outermost layer, do not belong to any Graph, and will not serve as Node or subgraph of other Graph.
MASFactory provides two types of top-level components: SingleAgent and RootGraph.
-
RootGraph: Top-level executable workflow container. Can be directly instantiated as a "canvas" to host nodes and edges, organize the overall DAG, and handle one-time execution and result output. Main methods:
create_node(cls:Node, *args, **kwargs) ->Node: Create a node onRootGraph(such asAgent,DynamicAgent,AgentSwitch,LogicSwitch,Graph,Loop,CustomNode, etc.). cls must be aNodesubclass (prohibitsSingleAgentandRootGraphas in-graph nodes). Other parameters are passed as-is to the corresponding constructor, returning the node instance.create_edge(sender:Node, receiver:Node, keys: dict | None = None) ->Edge: Create a directed edge between two nodes;keysdefine fields to be passed and their natural language descriptions.build() -> None: Recursively buildRootGraphand its subgraphs/nodes and complete consistency checks; must be executed before callinginvoke.invoke(input: dict, attributes: dict | None = None) ->(dict, dict): Start workflow execution; returns(output, attributes_snapshot).
-
SingleAgent: Lightweight agent detached from graphs for single-step/linear tasks.
__init__(name: str, model: Model adapter, instructions: str | list[str], prompt_template: str | list[str] | None = None, ...)invoke(input: dict) -> dict: Takes structured dict input and returns structured dict output.- Use cases: Quick Q&A, simple tool calls, scripted batch processing, and other tasks that don't require complete workflow orchestration.
In-Graph Components
In-graph components are created by Graph instances through create_node, serving as computational nodes in workflows; then connected through create_edge to form DAG.
-
Graph: Subworkflow node, reusable and nestable.- Features: Built-in
Entry/Exit, serves as "stage" hosting multiple nodes; inheritsBaseGraph's node/edge management and consistency validation. - Related methods:
edge_from_entry(receiver, keys: dict | None = None)edge_to_exit(sender, keys: dict | None = None)- Use case: Divide complex processes into several sub-stages for reuse and debugging.
RootGraphis a derived class ofGraph, with all methods ofGraph.
- Features: Built-in
-
Loop: Loop subgraph, encapsulating iteration control and optional LLM termination judgment.- Parameters:
name,max_iterations,model(optional),terminate_condition_prompt(optional),pull_keys/push_keys - Internal:
Controllercontrols iteration and termination;TerminateNodesupports early exit within loop body - Related methods:
edge_from_controller(receiver, keys[, ...]): Start each iteration from controlleredge_to_controller(sender, keys[, ...]): Feed intermediate results back to controller to form next round inputedge_to_terminate_node(sender, keys[, ...]): Trigger forced termination within loop body (like break)- Termination conditions: Reach
max_iterationsor determined by LLM to meetterminate_condition_prompt
- Parameters:
-
Agents: Agent nodes.
Agent: Standard agent. Parameters includename,model,instructions,prompt_template?,tools?,memories?,pull_keys?,push_keys?,model_settings?,role_name?DynamicAgent: When incoming edge message containsinstruction_key(default value is "instructions"), it will dynamically overrideAgent'sinstructions, with other behaviors identical toAgent.- Features: Core nodes of MASFactory; can inject memory/retrieval context; call tools and RAG, support different model APIs.
-
Controls: Routing and control flow nodes.
LogicSwitch: Conditional routing based on callback functions; usecondition_binding(callback, out_edge) to bind routing conditions.AgentSwitch: Semantic routing based on LLM; usecondition_binding(prompt, out_edge) to bind routing semantics.
-
CustomNode: Lightweight node with custom forward logic through callback functions, convenient for integrating external computation/rules; supports setting logic during initialization or subsequently throughset_forward. -
Unified Constraints (provided by
BaseGraph):- Loop check: Except for legal loops formed by
Loop'sController, other loops will be judged as errors. - Duplicate edge check: Duplicate edges with same
senderโreceiverwill be judged as errors. - Key conflict check: Duplicate
keys(including nested) of incoming edges to the same receiving node will be judged as errors. - Cannot create
SingleAgentandRootGraphobjects throughcreate_nodeinterface.
- Loop check: Except for legal loops formed by
Extension Adaptation Interfaces (Adapters)
The adaptation layer provides unified interfaces for models, tools, memory, and retrieval, ensuring different implementations are pluggable and composable.
Model
- Interface: invoke(messages, tools, settings) -> dict, returns CONTENT or TOOL_CALL.
- Implementations: OpenAIModel / AnthropicModel / GeminiModel; automatically adapts message and tool declaration formats of various providers, supports common generation parameters like temperature, max_tokens, top_p, stop, etc.
ToolAdapter
- Interface: details provides JSON Schema description of functions; call(name, arguments) executes tools.
- Purpose: Works with Agent to complete the closed loop of "model initiates tool call โ execution โ return results".
Context Adapters (RAG / Memory / MCP)
MASFactory models โLLM-visible contextโ as structured ContextBlocks (driven by ContextQuery) and injects them
into the CONTEXT field during the Agent Observe phase.
- Memory (writable):
insert/update/delete/reset+get_blocks(ContextQuery) -> list[ContextBlock]HistoryMemorykeeps chat history (carried as messages, not emitted as ContextBlocks)VectorMemorystores semantic memory and emits ContextBlocks by similarity
- Retrieval (read-only RAG):
get_blocks(ContextQuery) -> list[ContextBlock] - MCP: map external tool results into ContextBlocks via
MCP(name, call=...)
Two modes are supported:
- passive: auto-injection (default)
- active: on-demand retrieval exposed to the model as a tool (
retrieve_context)
See: docs/src/en/guide/context_adapters.md and docs/src/en/guide/agent_runtime.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file masfactory-1.0.0.post1.tar.gz.
File metadata
- Download URL: masfactory-1.0.0.post1.tar.gz
- Upload date:
- Size: 130.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cda224f446e7feb7ba8497254424851071980c0a778859f560eea6a64dcef8b7
|
|
| MD5 |
8a74e6cbcedc1a786e1606b0da44612b
|
|
| BLAKE2b-256 |
714d74e3783e09566049a42dbb1fb60c0e9ca830bbf8b453619e92c9965dd474
|
File details
Details for the file masfactory-1.0.0.post1-py3-none-any.whl.
File metadata
- Download URL: masfactory-1.0.0.post1-py3-none-any.whl
- Upload date:
- Size: 165.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c074335e64da4e13dafacc7bd39e5c9dabbae678eb29b1a5385519f6df1235a0
|
|
| MD5 |
b436daa76565f2a303c2196fb3c6a0d7
|
|
| BLAKE2b-256 |
bb7e41e79f2988245f5001ecaa056bd9ef4eb5971acece115929e1ea7a4a5615
|