MetaTrader 5 build 6140 - MCP internal server returns 401 Unauthorized with generated API key

 
Hello,

I am seeing a reproducible MCP authentication issue in MetaTrader 5 build 6140.

Environment:
- MetaTrader 5 build 6140
- Windows
- Portable MT5 installations
- Internal MCP server enabled
- Localhost endpoint only
- Tested with multiple independent MT5 instances
- Also reproduced with a completely clean temporary MT5 instance

Expected behavior:
An external MCP client should be able to connect to the MT5 internal MCP server using:

Authorization: Bearer <API key generated by MetaTrader 5>

Actual behavior:
The MCP server returns:

HTTP 401 Unauthorized

or, when tested through Codex:

Auth required

Reproduction steps:

1. Start a fresh MT5 build 6140 instance.

2. Go to:
   Tools -> Options -> MCP

3. Enable:
   "Enable internal server"

4. Configure a local endpoint, for example:
   http://127.0.0.1:23100/mcp

5. Click "Generate" to generate an API key.

6. Click OK.

7. Use the API key shown directly in the MT5 MCP UI.

8. Send an MCP initialize request to:
   http://127.0.0.1:23100/mcp

   with:
   Authorization: Bearer <the API key shown by MT5>

9. Result:
   HTTP 401 Unauthorized

I also tested the configuration generated by the MT5 "Copy" button.

The copied configuration contains separate MCP definitions for MetaEditor and Terminal, including:

[mcp_servers.terminal]
url = "http://127.0.0.1:23100/mcp"
enabled = true
http_headers = { "Authorization" = "Bearer <REDACTED>" }

Using the exact Terminal Authorization header generated by MT5 also results in:

HTTP 401 Unauthorized

Additional checks performed:
- Verified the correct terminal64.exe process owns the configured TCP port.
- Verified there is only one [MCP.MetaTrader] section.
- Verified the endpoint is reachable.
- Verified no CR/LF characters are present in the GUI API key.
- Verified the key was copied directly from the MT5 API Key field.
- Restarted the MT5 terminal after key generation.
- Tested multiple ports.
- Tested FTMO and Vantage demo instances.
- Reproduced the same behavior on another machine.
- Reproduced the same behavior on a completely clean MT5 instance.

This does not appear to be broker-specific or related to an existing MT5 configuration.

The issue seems to be between the API key generated/shown by MT5 build 6140 and the authentication performed by the internal MCP server.

Could you please confirm whether this is a known issue in build 6140, or whether the authentication procedure for the internal MCP server has changed?

Thank you.
 

I managed to connect to MCP server using Github Copilot from VS Code.

This is the configuration:

 - Connection is defined in mcp.json in .vscode folder

- I copied server definitions from MT5:

{
  "servers": {
    "metaeditor": {
      "type": "http",
      "url": "http://127.0.0.1:22345/mcp",
      "headers": {
        "Authorization": "Bearer ****"
      }
    },
    "mt5terminal": {
      "type": "http",
      "url": "http://127.0.0.1:22346/mcp",
      "headers": {
        "Authorization": "Bearer ***"
      }
    },
    "marketdata": {
      "type": "http",
      "url": "https://www.metatrader.com/mcp",
      "headers": {
        "Authorization": "Bearer mq-***"
      }
    }
  }
}


First, I tried to manually add definition using Github Copilot GUI, but I got 401 same as you.  Then I copied configuration and it worked.

Using above configuration, I managed to open and close charts and to start and run strategy tester.

I'm also using portable MT5 setup.


Now I see that you are mentioning Codex. I don't have experience with it, but I think that I saw that it uses `toml` file. Did you try that? 

 
Drazen Penic #:

I managed to connect to MCP server using Github Copilot from VS Code.

This is the configuration:

 - Connection is defined in mcp.json in .vscode folder

- I copied server definitions from MT5:


First, I tried to manually add definition using Github Copilot GUI, but I got 401 same as you.  Then I copied configuration and it worked.

Using above configuration, I managed to open and close charts and to start and run strategy tester.

I'm also using portable MT5 setup.


Now I see that you are mentioning Codex. I don't have experience with it, but I think that I saw that it uses `toml` file. Did you try that? 

Did you generated a new API Key ?

As the problem with it newly generated key, not connecting with the default one.

Connecting to Claude or Codex with the default key is working just fine.

 
Alain Verleyen #:

Did you generated a new API Key ?

As the problem with it newly generated key, not connecting with the default one.

Connecting to Claude or Codex with the default key is working just fine.

I used the default key that was already generated and I didn't have any problems connecting.

The OP - @CoralFish reported problems connecting.


I have a problem retrieving information from the strategy tester: 

Forum on trading, automated trading systems and testing trading strategies

Problem with running strategy tester through MCP server

Drazen Penic, 2026.08.22 14:45

I successfully configured an MCP server and I managed to start a local indicator tester session. I could see a correct indicator being tested in the strategy tester

MT5 returned tester run_id: 7676818232884649938 

While the test was still running, I asked for the tester status, but the MCP server couldn't find that run_id: “tester_get_status requested run_id does not match the active tester job”

When the tester finished, I asked for the test report, but it answered with: "tester_get_report requested run_id does not match the last tester job" 

I didn't run any other test.

The tester log file doesn't contain "run_id", nor could I find it in the MT5.


MT5 v6140

I tried this from VS Code, using Github Copilot.


Does this work for testing indicators?

Am I missing something?


 
Drazen Penic #:

I used the default key that was already generated and I didn't have any problems connecting.

The OP - @CoralFish reported problems connecting.

Yes with a new generated api key.

I have a problem retrieving information from the strategy tester: 

Post without enough information may be ignored or forgot, I don't have time to guess and I am sometimes tired to fight to get all the needed information.