Skip to main content

MCP server for MetaTrader 5: let an AI agent read market data, watch trades, and place real trades - behind a consent/audit safety layer. Windows + Linux (Docker).

Project description

mt5-mcp

Let an AI agent manage your MetaTrader 5 account over Model Context Protocol - with configurable human approval gate.

PyPI version PyPI downloads Python License: MIT Tests GitHub stars

mt5-mcp demo: an AI agent places and closes a live trade over MCP
A Hermes agent placing then closing a real 0.01-lot trade on a demo account, end-to-end over MCP on Linux.

The same round-trip in MetaTrader 5's History tab
Not a mock-up - the same round-trip in MetaTrader 5's own History tab; the tickets and balance match the recording.

⚠️ This software places real trades through your MetaTrader 5 terminal with real orders and irreversible fills. Read DISCLAIMER.md and SECURITY.md before connecting it to a live account. Always test using your demo account first.

Runs locally - in the same process tree as your agent, no cloud, no telemetry. Windows (native) or Linux (via Docker); Python 3.10+.

What it is

mt5-mcp lets an AI agent read your MetaTrader 5 account and place trades through it, over the Model Context Protocol.

  • 11 read-only tools: account, quotes, positions, orders, history, OHLC bars, and broker-authoritative margin estimates. No consent gate.
  • 4 mutating tools: place_order, modify_order, cancel_order, close_position, each behind a preflight + human-consent + idempotency + audit layer.
  • 3 subscribable resources: live account://, positions://, and quotes://{symbol} snapshots that push change notifications.
  • 2 ready-to-use Claude Code skills ship in .claude/skills/: mt5-market-data and mt5-trading teach an agent how to read the account and run the consent flow safely.

Full catalogue and the consent flow: docs/tools.md.

Why mt5-mcp

  • A safety layer, not just an API wrapper. Every mutating call routes through preflight checks → a fail-closed human-consent gate (every order needs approval by default) → idempotency → an append-only audit log, so an agent can't quietly fire-and-forget irreversible orders.
  • An honest threat model. It treats an LLM wired to place_order as a live attack surface and says so plainly - the MCP is explicitly not the security boundary (see SECURITY.md).
  • Verifiable proof, not a mock-up. The demo above is a real round-trip; the tickets and balance match MetaTrader 5's own History tab.
  • Local-first. No cloud, no telemetry; runs beside your agent. Windows-native or Linux via an all-in-one Docker image (no rpyc version-matching).

Quickstart (Windows, native)

pip install mt5-trading-mcp
  1. Launch MetaTrader 5 and log into your broker. Enable AlgoTrading (toolbar button green).
  2. Verify the terminal is reachable: python -m mt5_mcp doctor: expect [INFO] backend: native and [PASS] lines.
  3. Run it: python -m mt5_mcp serve.

Quickstart (Linux, Docker)

The MT5 terminal + the MCP run headless in an all-in-one image; your agent talks MCP over HTTP. No host Python, no bridge.

cp deploy/.env.example deploy/.env   # add MT5_LOGIN / MT5_PASSWORD / MT5_SERVER
docker compose -f deploy/docker-compose.yml up -d

Log the terminal in once via the KasmVNC web UI at http://127.0.0.1:3001 (File → Login to Trade Account; persists across restarts), then point your agent at http://127.0.0.1:8765/mcp. Full walkthrough: docs/installation.md.

For AI agents

If you've been handed this repository to install and run, follow the runbook in docs/agents.md. It covers platform detection, install, verification, registering the server, and the hard safety rules for trades - read it before calling any mutating tool.

Documentation

Guide What's in it
Installation & setup Requirements, Windows + Linux/Docker setup, wiring to an agent.
For AI agents Step-by-step runbook for an agent installing and running the server.
Configuration config.toml schema, storage paths, hot-reload.
Tools & resources Read tools, mutating tools + consent flow, subscribable resources.
MCP client setup Per-client config snippets and Claude Code usage.
Transports & deployment stdio/HTTP transports and Windows VPS patterns.
Contributing How to contribute and run the tests.
Changelog Release history and known limitations.

Safety

mt5-mcp is not the security boundary, the broker's MT5 server enforces the hard limits (margin, max-lot, symbol permissions). Pre-flight checks in the policy engine are UX guardrails to catch agent mistakes early, not security controls.

The human-consent gate is fail-closed by default: auto_approve_notional defaults to 0, so every mutating call requires explicit human approval (an ApprovalPreview you confirm) before it executes. Raise the threshold to auto-approve orders below a notional you trust; orders that widen stops always require approval. Every mutating call is recorded in an append-only audit JSONL log regardless. For vulnerability disclosure, see SECURITY.md.

Architecture

mt5-mcp wraps the MetaTrader 5 Python library behind a FastMCP server. A single MT5Client (src/mt5_mcp/adapter/) owns the terminal connection, broker-timezone inference, and type conversions; everything else sits on top of it. The Pydantic models in src/mt5_mcp/types.py / src/mt5_mcp/config.py are the source of truth for the data and config schemas.

     Agent / MCP client  (Hermes, OpenClaw, Claude Code, Claude Desktop, …)
                               │
                               │   stdio  ·  loopback HTTP
                               ▼
 ┌──────────────────────────────────────────────────────────┐
 │                      FastMCP server                      │
 │                                                          │
 │   tools/        resources/        policy/                │
 │   read +        subscribable      consent · idempotency  │
 │   mutating      account/quotes    · audit (JSONL)        │
 │                                                          │
 │   streaming/  - change-detection poller + dispatcher     │
 │   types.py · config.py - Pydantic schemas: source of     │
 │                          truth for data + config         │
 │                                                          │
 └──────────────────────────────────────────────────────────┘
                               │
                               ▼
 ┌──────────────────────────────────────────────────────────┐
 │                                                          │
 │   adapter/  MT5Client                                    │
 │   one terminal connection · broker-TZ inference ·        │
 │   type conversions · transparent reinit                  │
 │                                                          │
 └──────────────────────────────────────────────────────────┘
                               │
                               ▼
MetaTrader 5 Python library  →  broker terminal  →  broker server

The module paths shown (tools/, resources/, policy/, streaming/, adapter/, types.py, config.py) all live under src/mt5_mcp/.

Contributing

Contributions are welcome, see CONTRIBUTING.md for the dev setup, test workflow, and project principles.

License

MIT - see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mt5_trading_mcp-1.3.0.tar.gz (3.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mt5_trading_mcp-1.3.0-py3-none-any.whl (74.4 kB view details)

Uploaded Python 3

File details

Details for the file mt5_trading_mcp-1.3.0.tar.gz.

File metadata

  • Download URL: mt5_trading_mcp-1.3.0.tar.gz
  • Upload date:
  • Size: 3.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mt5_trading_mcp-1.3.0.tar.gz
Algorithm Hash digest
SHA256 6274637599077123e51aeaea0e84b7077d029a61a00fd5300d4b17bfb7095523
MD5 47137465905558c0d11d0279c55a94ef
BLAKE2b-256 b8f7cccc6860e75d07d03befc7a627bb81c178317620e721f2120467649dbdf4

See more details on using hashes here.

Provenance

The following attestation bundles were made for mt5_trading_mcp-1.3.0.tar.gz:

Publisher: publish.yml on vincentwongso/mt5-trading-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mt5_trading_mcp-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mt5_trading_mcp-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4239acebb28d661385c5c69fd623218e49ab7143c05b2a04db6791df0ca186c
MD5 db0d3281fbbd03ab4bc66b64dcfeea8c
BLAKE2b-256 35dead670456345dcef1ef2bae18cf0aa40c0f39974ef08b6e18146d95be06cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for mt5_trading_mcp-1.3.0-py3-none-any.whl:

Publisher: publish.yml on vincentwongso/mt5-trading-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page