We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/ariadng/metatrader-mcp-server'
If you have feedback or need assistance with the MCP directory API, please join our Discord server
import os
import pytest
from dotenv import load_dotenv
from metatrader_client.client_connection import MT5Connection, ConnectionError, LoginError, InitializationError
import platform
@pytest.fixture(scope="module")
def connection_config():
# Clear console for pretty output
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
print("\nπ§ͺ MetaTrader 5 MCP Connection Test Suite π§ͺ\n")
print("π Loading credentials and preparing connection config...")
load_dotenv()
login = os.getenv("LOGIN")
password = os.getenv("PASSWORD")
server = os.getenv("SERVER")
path = os.getenv("TERMINAL_PATH", None)
if not login or not password or not server:
print("β Error: Missing required environment variables!")
print("Please create a .env file with LOGIN, PASSWORD, and SERVER variables.")
pytest.skip("Missing environment variables for MetaTrader 5 connection")
config = {
"login": int(login),
"password": password,
"server": server,
}
if path:
config["path"] = path
return config
@pytest.fixture(scope="function")
def mt5_connection(connection_config):
conn = MT5Connection(connection_config)
yield conn
if conn.is_connected():
conn.disconnect()
def test_successful_connection(mt5_connection):
print("\nπ¦ Testing successful connection...")
assert not mt5_connection.is_connected(), "Should not be connected initially."
assert mt5_connection.connect() is True, "Connection should succeed."
assert mt5_connection.is_connected() is True, "Should be connected after connect()."
print("β
Connected successfully!")
mt5_connection.disconnect()
assert not mt5_connection.is_connected(), "Should be disconnected after disconnect()."
print("π Disconnected successfully!")
def test_duplicate_connection_handling(mt5_connection):
print("\nπ Testing duplicate connection handling...")
mt5_connection.connect()
assert mt5_connection.is_connected() is True
# Attempt to connect again
assert mt5_connection.connect() is True
assert mt5_connection.is_connected() is True
mt5_connection.disconnect()
print("β
Duplicate connection handled gracefully!")
def test_terminal_info_and_version(mt5_connection):
print("\nβΉοΈ Testing terminal info and version retrieval...")
mt5_connection.connect()
info = mt5_connection.get_terminal_info()
version = mt5_connection.get_version()
assert info is not None, "Terminal info should not be None."
assert isinstance(version, tuple) and len(version) == 4, "Version should be a tuple of length 4."
mt5_connection.disconnect()
print(f"β
Terminal info: {info}\nβ
Version: {version}")
def test_double_disconnection_handling(mt5_connection):
print("\nπ Testing double disconnection handling...")
mt5_connection.connect()
mt5_connection.disconnect()
# Attempt to disconnect again
mt5_connection.disconnect()
assert not mt5_connection.is_connected(), "Should remain disconnected."
print("β
Double disconnection handled gracefully!")
def test_error_handling_invalid_credentials(connection_config):
print("\nβ Testing error handling with invalid credentials...")
bad_config = connection_config.copy()
bad_config["login"] = 0 # Invalid login
bad_config["password"] = "wrongpassword"
bad_conn = MT5Connection(bad_config)
with pytest.raises((LoginError, ConnectionError)):
bad_conn.connect()
assert not bad_conn.is_connected(), "Should not be connected with invalid credentials."
print("β
Properly handled invalid credentials!")
def test_error_handling_invalid_terminal_path(connection_config):
print("\nβ Testing error handling with invalid terminal path...")
bad_config = connection_config.copy()
bad_config["path"] = "C:/nonexistent/path/terminal.exe"
bad_conn = MT5Connection(bad_config)
with pytest.raises((InitializationError, ConnectionError)):
bad_conn.connect()
print("β
Properly handled invalid terminal path!")
def test_cooldown_logic(mt5_connection):
print("\nβ³ Testing cooldown logic...")
mt5_connection.connect()
import time
start = time.time()
mt5_connection.disconnect()
mt5_connection.connect() # Should enforce cooldown if implemented
elapsed = time.time() - start
assert mt5_connection.is_connected() is True
mt5_connection.disconnect()
print(f"β
Cooldown logic tested (elapsed: {elapsed:.2f}s)")