Skip to main content

Model Context Protocol server wrapping the MetaTrader 5 Python library.

Project description

mt5-mcp

PyPI version Python License: MIT Tests

Model Context Protocol server wrapping the MetaTrader 5 Python library: exposes a logged-in MT5 terminal as a set of MCP tools an AI agent can call.

⚠️ 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.

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. It runs locally, in the same process tree as your agent, no cloud, no telemetry.

  • 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.

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

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.

Linux (the MT5 terminal runs in an all-in-one Docker image; the agent talks MCP over HTTP) and wiring to an agent are in docs/installation.md.

For AI agents

If you are an AI agent and have been handed this repository to install and run, follow this procedure.

  1. Detect the platform and pick a backend. Windows runs the MetaTrader5 library natively; Linux runs the terminal in an all-in-one Docker image (the agent talks MCP over HTTP). Full per-platform steps: docs/installation.md.
  2. Install from this checkout:
    • Windows: pip install . (or uv sync --extra dev). Tell the human to launch MetaTrader 5, log into their broker, and enable AlgoTrading.
    • Linux (recommended): bring up the Docker image — creds in deploy/.env, then docker compose -f deploy/docker-compose.yml up -d (it uses the published image; add --build only for local deploy/ changes). The server runs in the container and serves MCP over HTTP; the one-time VNC login + bridge alternative are in docs/installation.md.
  3. Verify before doing anything else: Windows → python -m mt5_mcp doctor (expect the backend + [PASS] lines); Docker image → after the human's one-time VNC login, your first ping returns ok: true. Do not proceed if verification fails — report it to the human and stop.
  4. Register / run the server: Docker image → it's already serving; register http://127.0.0.1:8765/mcp. Windows → python -m mt5_mcp serve (stdio) with a config under examples/clients/ (e.g. hermes.json) that scopes you to the read-only tools by default.
  5. Operate read-only unless explicitly asked to trade. The read tools are safe to call freely.
  6. ⚠️ Hard rule, if live account and using real money. place_order, modify_order, and close_position execute real, irreversible trades. When one returns an ApprovalPreview, show it verbatim to the human and only retry with approval_confirmed=true after they explicitly approve that specific order. Never auto-confirm, never batch-approve, never trade on a demo assumption — confirm it's a demo account first.

Tool semantics and the full consent flow: docs/tools.md. Under Claude Code, the skills in .claude/skills/ teach these directly.

Documentation

Guide What's in it
Installation & setup Requirements, Windows + Linux/Docker setup, wiring to an agent.
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.

Mutating actions above the configured auto_approve_notional (or that widen stops) require explicit human approval via the ApprovalPreview flow. Every mutating call is recorded in an append-only audit JSONL log. 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. On top of it: the MCP tools (src/mt5_mcp/tools/), subscribable resources (src/mt5_mcp/resources/), the consent / idempotency / audit layer (src/mt5_mcp/policy/), and the change-detection streaming subsystem (src/mt5_mcp/streaming/). The Pydantic models in src/mt5_mcp/types.py and src/mt5_mcp/config.py are the source of truth for the data and config schemas.

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.2.1.tar.gz (216.6 kB 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.2.1-py3-none-any.whl (72.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mt5_trading_mcp-1.2.1.tar.gz
  • Upload date:
  • Size: 216.6 kB
  • 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.2.1.tar.gz
Algorithm Hash digest
SHA256 0b4f07da9e3961120b5bf4ac50c0ac1b6fde5c3c058e9b7331f6edcf0feb5fb7
MD5 94127158e2f20717ae57f6021a3d7e1a
BLAKE2b-256 93d7090d0867e02a403e378df4e9431a89aa1f86f520b74424a75a5048b5390e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mt5_trading_mcp-1.2.1.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.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mt5_trading_mcp-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e6c6b30d23261925cfc48fcbcdfeb0a77e5647a1f70d3feb7e12c6694dc024b0
MD5 f7cdf51c5f4c2698d93e3f9bc094c3b4
BLAKE2b-256 574dc0437e97a58b82dd68d95957201ca76f89a16f9b8e8df615b22fca87f08b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mt5_trading_mcp-1.2.1-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