MetaTrader 5 MCP Server
Enables comprehensive access to the MetaTrader 5 trading platform for retrieving market data, managing accounts, and executing trading operations. It provides 32 specialized tools for interacting with MT5 functionalities, including historical OHLCV data access, real-time position monitoring, and automated order management.
README
MetaTrader 5 MCP Server
A Model Context Protocol (MCP) server that provides comprehensive access to MetaTrader 5 trading platform functionality through Python.
📖 New to this project? Check out the Quick Start Guide for a condensed reference!
🚀 Quick Start
Prerequisites
- MetaTrader 5 terminal installed and running on Windows
- Python 3.10+
Installation (Choose One Method)
Method 1: Automated Installation (Recommended)
Windows:
# Clone the repository
git clone https://github.com/amirkhonov/metatrader5-mcp.git
cd metatrader5-mcp
# Run installation script
install.bat
Linux/Mac:
# Clone the repository
git clone https://github.com/amirkhonov/metatrader5-mcp.git
cd metatrader5-mcp
# Run installation script
chmod +x install.sh
./install.sh
Method 2: Using pip
# Install from source
git clone https://github.com/amirkhonov/metatrader5-mcp.git
cd metatrader5-mcp
pip install -r requirements.txt
pip install -e .
Method 3: Using Poetry
git clone https://github.com/amirkhonov/metatrader5-mcp.git
cd metatrader5-mcp
poetry install
Configuration
-
Copy the environment template:
cp .env.example .env -
Edit
.envwith your MT5 credentials:MT5_LOGIN=12345678 MT5_PASSWORD=your_password MT5_SERVER=MetaQuotes-Demo MT5_PATH=C:\Program Files\MetaTrader 5\terminal64.exe -
Run the server:
# Using environment variables from .env metatrader5-mcp # Or with command line arguments metatrader5-mcp --login 12345 --password secret --server MetaQuotes-Demo
Quick Test
# Check if the server starts correctly
metatrader5-mcp --help
Features
This MCP server exposes 32 tools for interacting with MT5, organized into the following categories:
🔌 Connection & Initialization
- Initialize/shutdown MT5 connection
- Get terminal version and information
- Login to trading accounts
- Get last error information
💰 Account Information
- Retrieve account balance, equity, margin, profit
- Get account settings and leverage
📊 Market Data
- Access symbol information and current prices
- Retrieve historical bar data (OHLCV) by date or position
- Get tick data with various filters and date ranges
- Manage Market Watch symbols
📈 Trading Operations
- Send market and pending orders
- Check order validity before execution
- Calculate required margin
- Calculate potential profit/loss
📋 Position & Order Management
- View open positions
- Monitor pending orders
- Access trading history (orders and deals)
- Filter by symbol, date range, or ticket
Prerequisites
- MetaTrader 5 terminal installed and running on Windows
- Python 3.10+
Installation Methods
Choose the method that works best for you:
Option 1: Quick Install Script (Easiest)
Windows:
install.bat
Linux/Mac:
./install.sh
The script will:
- Check Python version
- Create a virtual environment
- Install all dependencies
- Create a
.envconfiguration file - Guide you through setup
Option 2: Manual Installation with pip
# Install dependencies
pip install -r requirements.txt
# Install the package
pip install -e .
# Copy environment template
cp .env.example .env
# Edit .env with your credentials
Option 3: Using Poetry
poetry install
cp .env.example .env
# Edit .env with your credentials
Option 4: Using pipx (Global Install)
pipx install .
Option 5: Docker (Experimental)
# Build and run
docker-compose up -d
# View logs
docker-compose logs -f
Note: Docker support is experimental as MT5 requires Windows. Best for development/testing.
Configuration
For Claude Desktop
Add this configuration to your Claude Desktop config file:
Windows: %APPDATA%\Claude\claude_desktop_config.json
With Auto-Initialization (Recommended)
{
"mcpServers": {
"metatrader": {
"command": "metatrader5-mcp",
"args": [
"--login", "YOUR_MT5_LOGIN",
"--password", "YOUR_MT5_PASSWORD",
"--server", "YOUR_MT5_SERVER",
"--path", "C:\\Program Files\\MetaTrader 5\\terminal64.exe"
]
}
}
}
Command Line Arguments
The server supports the following optional arguments:
--login LOGIN- Trading account number--password PASSWORD- Trading account password--server SERVER- Trading server name (e.g., "MetaQuotes-Demo")--path PATH- Path to MT5 terminal executable
Running Locally
# With auto-initialization
metatrader5-mcp --login 12345 --password secret --server MetaQuotes-Demo
# Without initialization (manual connection via tools)
metatrader5-mcp
Troubleshooting
Having issues? Check our comprehensive Troubleshooting Guide for:
- Installation problems
- Connection issues
- Authentication errors
- Trading problems
- Configuration issues
- Common error codes and solutions
Or run the validation script:
python validate.py
Security Considerations
⚠️ Important Security Notes:
- Real Money Trading: This server can execute real trades. Always test with a demo account first.
- Credentials: Never hardcode passwords. Use environment variables or secure credential storage.
- Access Control: Limit access to this MCP server to trusted clients only.
- Network: MT5 terminal must be running on the same machine as the server.
📖 For comprehensive security guidance, see SECURITY.md
Troubleshooting
"initialize() failed"
- Ensure MT5 terminal is installed and running
- Check that the terminal path is correct (if specified)
- Verify you have the correct login credentials
"No data returned"
- Symbol might not be available in Market Watch
- Use
mt5_symbol_selectto add symbols - Check date ranges for historical data
Connection timeout
- Check MT5 terminal is responsive
- Verify no firewall blocking
Development
Running the Server Directly
python -m metatrader5_mcp
The server uses stdio transport and communicates via JSON-RPC over stdin/stdout.
Testing Tools
You can test individual MT5 functions in Python:
import MetaTrader5 as mt5
# Initialize
if not mt5.initialize():
print("Failed:", mt5.last_error())
quit()
# Get account info
account = mt5.account_info()
print(account._asdict())
# Cleanup
mt5.shutdown()
Resources
Documentation
- 📖 Quick Start Guide - Quick reference for common tasks
- 🔧 Troubleshooting Guide - Solve common issues
- 🔒 Security Best Practices - Keep your trading secure
- 🤝 Contributing Guide - How to contribute
External Links
- MetaTrader 5 Python Documentation
- MCP Protocol Specification
- MetaTrader 5 Terminal
- GitHub Repository
- Issue Tracker
License
MIT License - feel free to use and modify as needed. See LICENSE for details.
Contributing
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
Changelog
See CHANGELOG.md for version history and updates.
⚠️ Disclaimer:
- This software is for educational and informational purposes. Trading involves financial risk. Always test with demo accounts first. The authors are not responsible for any financial losses incurred through use of this software.
- MetaTrader is a trademark of METAQUOTES LTD. This project is an unofficial Model Context Protocol (MCP) implementation for interacting with MetaTrader 5 and is not affiliated with or endorsed by MetaQuotes.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。