ME

MetaTrader 5 MCP Server for AI Trading

Enable AI LLMs to execute live trades on MetaTrader 5 via an MCP server for secure, low-latency automated trading.

Quick Install
npx -y @ariadng/metatrader-mcp-server

Overview

This MCP (Model Context Protocol) server connects large language models (LLMs) or other AI agents to a MetaTrader 5 (MT5) trading terminal, enabling secure, low-latency execution of live trades and market queries. The server implements an MCP-compliant HTTP interface that exposes trading “tools” — actions such as querying prices, opening or closing positions, and reading account data — so an LLM can reason about the market and then call specific, auditable operations.

The server is useful when you want to add programmatic trading capabilities to an AI workflow without embedding trading credentials directly in the model. It keeps trading logic and permissions on an isolated endpoint, provides structured inputs/outputs for safer automation, and reduces latency compared with more complex bridging solutions. Typical deployments run the MCP server alongside an MT5 terminal on a secure host or container.

Features

  • MCP-compliant HTTP API for LLM tool calling
  • Exposure of common MT5 operations as discrete tools (market data, orders, positions, account)
  • Authentication and request validation to limit what an LLM can execute
  • Low-latency local connection to a MetaTrader 5 terminal (via official or community MT5 Python bindings)
  • JSON-based inputs/outputs for easy integration with LLM tool call formats
  • Optional Docker support for isolated deployment
  • Logging and basic audit trail for executed trades

Installation / Configuration

Prerequisites:

  • A machine with MetaTrader 5 installed and a logged-in trading account
  • Python 3.8+ and pip, or Docker

Install from source (example):

# clone the repo
git clone https://github.com/ariadng/metatrader-mcp-server.git
cd metatrader-mcp-server

# create a venv and install dependencies
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

# run the server
python server.py --host 127.0.0.1 --port 8080

Environment configuration (example .env or config.yaml):

# .env
MT5_LOGIN=1234567
MT5_PASSWORD=your_password
MT5_SERVER=YourBroker-Server
MT5_PATH=/path/to/terminal64.exe
MCP_API_KEY=supersecretapikey
HOST=0.0.0.0
PORT=8080
TLS_CERT=/path/to/cert.pem    # optional
TLS_KEY=/path/to/key.pem      # optional

Docker (example):

# docker-compose.yml
version: "3.8"
services:
  mcp-server:
    image: ariadng/metatrader-mcp-server:latest
    environment:
      - MT5_LOGIN=1234567
      - MT5_PASSWORD=your_password
      - MT5_SERVER=YourBroker-Server
      - MCP_API_KEY=supersecretapikey
    volumes:
      - /host/path/to/mt5:/mt5
    network_mode: "host" # often required for local MT5 connectivity

Start:

docker compose up -d

Note: Running with Docker may require mounting the MetaTrader terminal and using host networking for local terminal connectivity.

Available Tools / Resources

Typical toolset exposed by the server (tool names and expected behavior):

  • get_market_data
    • Inputs: symbol, timeframe, bars (count)
    • Returns: OHLCV bars or tick snapshot
  • get_account_info
    • Inputs: none
    • Returns: balance, equity, margin, free_margin, leverage, account_id
  • list_positions
    • Inputs: optional symbol
    • Returns: open positions with ticket, type, lots, price, profit
  • place_order
    • Inputs: symbol, side (buy/sell), volume, order_type (market/limit), price, sl, tp
    • Returns: order ticket, status, executed_price
  • close_position
    • Inputs: ticket or symbol
    • Returns: closed ticket, profit, status
  • get_symbol_info
    • Inputs: symbol
    • Returns: tick_size, contract_size, margin_requirements

API endpoints (example):

EndpointMethodPurpose
/mcp/toolsGETList available tools and signatures
/mcp/executePOSTExecute a named tool with arguments
/healthGETServer health and MT5 connection status

Authentication:

  • Bearer token via Authorization header (MCP_API_KEY) or TLS client certs
  • Request signing or IP allowlist recommended for production

Use Cases

  • LLM-driven strategy execution
    • An LLM analyzes market context, composes a trade plan (entry, SL/TP), and calls place_order to open a market position. The server enforces allowed symbols and maximum volume.
  • Alert-to-action automation
    • A monitoring agent detects a pattern and asks the MCP server to fetch live price data and optionally place a hedge or close a position.
  • Secure human-in-the-loop trading
    • The model proposes trades; the MCP server executes only after authenticated human approval, using the server as an auditable gatekeeper.
  • Rapid prototyping of automated strategies
    • Developers can iterate on NLP-based decision logic locally without exposing broker credentials or MT5 internals to the model.

Example: Calling the Server

Curl example to list tools:

curl -H "Authorization: Bearer supersecretapikey" \
  http://127.0.0.1:8080/mcp/tools

Place a market buy via curl:

curl -X POST http://127.0.0.1:8080/mcp/execute \
  -H "Authorization: Bearer supersecretapikey" \
  -H "Content-Type: application/json" \
  -d '{
    "tool": "place_order",
    "args": {"symbol":"EURUSD", "side":"buy", "volume":0.01}
  }'

Python example using requests:

import requests

API = "http://127.0.0.1:8080/mcp/execute"
headers = {"Authorization": "Bearer supersecretapikey"}
payload = {"tool": "get_account_info"}

r = requests.post(API, json=payload, headers=headers)
print(r.json())

Security and Best Practices

  • Run the MCP server on a secure host with limited network exposure; prefer a private network segment.
  • Use TLS and API keys; consider mTLS for production.
  • Limit allowed tools and enforce validation of arguments (max volume, allowed symbols).
  • Keep an audit log of requests and responses to trace automated actions.
  • Test thoroughly in demo accounts before using live funds.

Resources

  • Repository: https://github.com/ariadng/metatrader-mcp-server
  • MetaTrader 5 Python package and official MT5 docs for terminal integration
  • MCP (Model Context Protocol) spec for tool-calling conventions

This server is designed to be a lightweight, auditable bridge between AI agents and a live MT5 terminal — useful for prototyping and production deployments where controlled, programmatic trading is required.

Tags:ai-ml