Skip to content

fix: mcp command always sends empty Authorization header, causing 401 on all tool calls#116

Open
Varshith-JV-1410 wants to merge 2 commits into
open-webui:mainfrom
Varshith-JV-1410:fix/mcp-empty-auth-header-issue-115
Open

fix: mcp command always sends empty Authorization header, causing 401 on all tool calls#116
Varshith-JV-1410 wants to merge 2 commits into
open-webui:mainfrom
Varshith-JV-1410:fix/mcp-empty-auth-header-issue-115

Conversation

@Varshith-JV-1410
Copy link
Copy Markdown

@Varshith-JV-1410 Varshith-JV-1410 commented Apr 23, 2026

Summary

Fixes Bug 1 from #115: the mcp command always sent an empty Authorization: Bearer header, causing every internal FastAPI tool call to return 401.

Root Cause

env.py captures API_KEY at module-import time. The mcp CLI command had no --api-key option and never set OPEN_TERMINAL_API_KEY in the environment before importing mcp_server, so API_KEY was always "".

The run command works correctly because it explicitly sets os.environ["OPEN_TERMINAL_API_KEY"] before anything reads it. The mcp command had no equivalent step.

Fix

  • Added --api-key / OPEN_TERMINAL_API_KEY Click option to the mcp command, mirroring the run command.
  • Resolves the key through the full priority chain: CLI flag → env var → _FILE Docker secret → config file.
  • Sets os.environ["OPEN_TERMINAL_API_KEY"] before the deferred from open_terminal.mcp_server import mcp so env.py captures the correct value at import time.

Testing

Verified locally on Windows:

open-terminal mcp --transport streamable-http --host 0.0.0.0 --port 9000 --api-key 12345678

All MCP tool calls succeed with exit code 0. Authorization: Bearer 12345678 confirmed in the httpx client headers.

Also works via env var and config file.

Closes #115 (Bug 1)

The `mcp` CLI command had no `--api-key` option and never set
`OPEN_TERMINAL_API_KEY` in the environment before importing
`mcp_server`. Because `env.py` captures `API_KEY` at module-import
time, the header was always `Bearer ` (empty), causing every internal
FastAPI call to return 401.

Fix: add `--api-key` / `OPEN_TERMINAL_API_KEY` option to the `mcp`
command (mirroring the `run` command) and resolve it through the full
chain (CLI flag -> env var -> `_FILE` Docker secret -> config file),
setting `os.environ["OPEN_TERMINAL_API_KEY"]` before the deferred
import of `mcp_server` so `env.py` picks up the correct value.

Also add MCP server usage to README.md and exclude AGENTS.md from git.
Copilot AI review requested due to automatic review settings May 7, 2026 20:57
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses MCP tool-call authentication failures by ensuring the MCP server sees the resolved API key before importing modules that capture it at import time.

Changes:

  • Add --api-key / OPEN_TERMINAL_API_KEY option to the mcp CLI command and resolve API key from CLI/env/*_FILE/config before importing mcp_server.
  • Document MCP server usage in README.md.
  • Ignore AGENTS.md and remove the repo’s .python-version file.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.

File Description
open_terminal/cli.py Adds API key option + early API key resolution for mcp startup.
README.md Adds an “MCP Server” section with usage guidance.
.gitignore Ignores AGENTS.md.
.python-version Removes pinned local Python version file.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread open_terminal/cli.py
Comment on lines +184 to +195
# Resolve API key: CLI flag > env var > Docker secret file > config file.
# Must be set in os.environ BEFORE importing mcp_server so that env.py
# picks it up at module-import time (it captures API_KEY at import).
if not api_key:
file_path = os.environ.get("OPEN_TERMINAL_API_KEY_FILE")
if file_path:
with open(file_path) as f:
api_key = f.read().strip()
if not api_key:
api_key = cfg.get("api_key", "")
if api_key:
os.environ["OPEN_TERMINAL_API_KEY"] = api_key
Comment thread open_terminal/cli.py
Comment on lines 165 to 172
def mcp(
transport: str,
host: str | None,
port: int | None,
config_path: str | None,
cwd: str | None,
api_key: str,
):
Comment thread README.md
Open Terminal ships with an [MCP](https://modelcontextprotocol.io) server that exposes every API endpoint as an MCP tool. Install the optional dependency first:

```bash
pip install -e ".[mcp]"
Comment thread README.md
Comment on lines +192 to +199
### streamable-http (remote)

To run the MCP server as a network service and connect from another machine:

```bash
open-terminal mcp --transport streamable-http --host 0.0.0.0 --port 8000 --api-key your-secret-key
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

open-terminal mcp always sends empty Authorization header, causing 401 on all tool calls — plus stdio transport crashes with unexpected host kwarg

2 participants