Skip to main content

mainspring

mainspring is an open-source Python agent framework whose core depends on exactly two runtime packages: pydantic and httpx. MCP, workflow graphs, session stores, OpenTelemetry, sandboxing, and coding tools ship as separate packages that can be removed without touching the core.

English | 简体中文

CI PyPI Python License: MIT Docs

mainspring-coder terminal demo: tool calls, a diff, an approval prompt, and a passing test run

Project status

mainspring is at v0.1.2 and in developer preview. The framework, extensions, CLI, server, and web client are usable today, but APIs may change between minor versions while the design settles.

Known gap: cloud smoke tests for eight hosted providers still need to be re-run with real API keys. The locally reachable Ollama path and all zero-key replay paths are verified. See the release notes and the release and compatibility policy.

Design constraints

The core is kept deliberately small. Five constraints define what belongs in it:

Constraint What it means
Two dependencies pip install mainspring-agent pulls in pydantic and httpx. Provider SDKs are never imported; every vendor is spoken to over plain HTTP.
One switch for models 13 dialects: an OpenAI-compatible base plus native Anthropic and Gemini. Change provider and model; the Agent object does not change. Unsupported capabilities fail before the request instead of surfacing as a vendor error.
Control is declarative Turn, token, wall-clock, and no-progress budgets; tool allowlists; input and output guardrails; and human-in-the-loop approve/deny/resume are declaration slots on Agent, not if statements scattered through business code.
Isolation is enforced import-linter contracts prevent the core from importing an extra and prevent extras from importing each other. CI uninstalls each extra in turn and the core suite stays green.
Observable and replayable Every step emits a RunEvent. The console sink is built in; the OTel extra turns the same stream into a span tree. Recorded JSONL replays into an identical event sequence.

Run

From PyPI

pip install mainspring-agent

The import name is mainspring; the PyPI distribution is mainspring-agent because the name mainspring was already taken by another project.

Optional packages are installed separately:

pip install "mainspring-agent[config]"   # YAML config loader (pyyaml)
pip install mainspring-coding-tools      # 14 coding tools and a terminal REPL
pip install mainspring-mcp               # MCP client (stdio and Streamable HTTP)
pip install mainspring-otel              # OpenTelemetry export and replay CLI

Quickstart without an API key

Requires Python 3.11 or newer and Ollama:

ollama pull qwen3:14b
from mainspring import Agent, Runner, tool
from mainspring.providers.ollama import create_model

@tool
def add(a: int, b: int) -> int:
    """Add two integers."""
    return a + b

agent = Agent(
    name="assistant",
    instructions="Use tools when they help. Keep answers short.",
    model=create_model("qwen3:14b"),   # local, zero cost
    tools=[add],
)

result = Runner().run_sync(agent, "What is 2 + 3?")
print(result.status, result.finish_reason, result.message.text_content())

Cloud models use the same agent code; only the import and model name change:

from mainspring.providers.deepseek import create_model

model = create_model("deepseek-chat")   # reads DEEPSEEK_API_KEY

Keys enter only through environment variables. They are never written to YAML files, event payloads, or traces.

Terminal coding agent

pip install mainspring-coding-tools
mainspring-coder --root .

Every write or shell command produces a tool card, a unified diff where applicable, and a y/n approval prompt. Read-only tools run without asking. Three commands start the session: /new, /resume, and /model <name>.

From source: server and web UI

The application layer ships as artifacts rather than PyPI packages, so this part runs from a clone:

git clone https://github.com/chyong/mainspring
cd mainspring
uv sync
uv run uvicorn mainspring_server.app:app --port 8900

The server exposes an AG-UI event stream (SSE) plus REST endpoints for sessions and approvals. A React web client and a Tauri desktop shell live under apps/.

Repository layout

core/                  # framework (directory is core/, import name is mainspring)
extras/                # 11 independent packages: mcp, workflow, session-{sqlite,redis,postgres},
                       # otel, memory, sandbox, coding-tools, eval-online, langfuse
apps/                  # application layer: mainspring-server (HTTP), web (React), desktop (Tauri)
examples/              # runnable examples, all offline-capable by default
docs/                  # mkdocs documentation site
mainspring.yaml        # config layer: model, storage, budgets

Performance

Framework overhead is measured separately from model latency (local qwen3:14b, 2026-10-31):

Scenario End-to-end p50 Framework overhead p50
Single-step run (synthetic model) 0.044 ms 0.039 ms
Tool step (one tool call) 0.315 ms 0.301 ms
Replayed support-ticket run (hooks + 2 tools + structured output) 0.958 ms —
Live run (qwen3:14b) 18,334 ms 1.41 ms (< 0.01%)

tracemalloc peak per run is 25.2 KB. Reproduce with uv run python benchmarks/bench_run.py; the benchmark needs no API key and no network.

Documentation

The documentation site is built with mkdocs: uv sync --group docs && uv run mkdocs serve.

Development

uv sync            # installs the workspace, extras, and development tools
uv run pytest      # 700+ tests

These checks must pass on every commit:

uv run ruff check . && uv run ruff format --check .
uv run mypy                       # strict, core and server
uv run lint-imports               # dependency-direction contracts
uv run pytest                     # coverage gate: 85% (core is at 100%)

CI runs with no network access and no API keys: recorded fixtures replay the model calls. Every example runs offline by default for the same reason.

More detail is in CONTRIBUTING.md.

Community and support

Citation

@misc{mainspring2026,
  title={mainspring: A two-dependency Python agent framework},
  author={mainspring contributors},
  year={2026},
  publisher={GitHub},
  howpublished={\url{https://github.com/chyong/mainspring}}
}

License

MIT — see LICENSE.

Metadata

Release files for mainspring-agent 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mainspring-agent 0.1.2
File Size Uploaded Provenance Actions
mainspring_agent-0.1.2.tar.gz 413.7 kB With provenance Details for mainspring_agent-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mainspring-agent 0.1.2
File Interpreter ABI Platform Actions
mainspring_agent-0.1.2-py3-none-any.whl Python 3 none any Details for mainspring_agent-0.1.2-py3-none-any.whl

Total release size: 565.9 kB

Release files / mainspring_agent-0.1.2.tar.gz

Download URL mainspring_agent-0.1.2.tar.gz
Size 413.7 kB
Tags Source
SHA-256 checksum
How to use checksums
db619fe240e126b15257f449dc774bcf0100264f0487cbb11cadd213e4040c8d
BLAKE2b-256 checksum
How to use checksums
0f31109a069c5150016cb2f58a529038d26357d495251d15d52bcf394b2b7912
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Release files / mainspring_agent-0.1.2-py3-none-any.whl

Download URL mainspring_agent-0.1.2-py3-none-any.whl
Size 152.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a65be5eaf22aca6ded836ccdf04067160e1eb218a29b8113efc9458bcc62f9d
BLAKE2b-256 checksum
How to use checksums
be4d8dfb56c59968c2f91b1ec51010207cc1374263ed0639203d25be68292f15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Security for mainspring-agent 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mainspring_agent-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page