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
Conversation
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.
There was a problem hiding this comment.
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_KEYoption to themcpCLI command and resolve API key from CLI/env/*_FILE/config before importingmcp_server. - Document MCP server usage in
README.md. - Ignore
AGENTS.mdand remove the repo’s.python-versionfile.
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 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 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, | ||
| ): |
| 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 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 | ||
| ``` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes Bug 1 from #115: the
mcpcommand always sent an emptyAuthorization: Bearerheader, causing every internal FastAPI tool call to return 401.Root Cause
env.pycapturesAPI_KEYat module-import time. ThemcpCLI command had no--api-keyoption and never setOPEN_TERMINAL_API_KEYin the environment before importingmcp_server, soAPI_KEYwas always"".The
runcommand works correctly because it explicitly setsos.environ["OPEN_TERMINAL_API_KEY"]before anything reads it. Themcpcommand had no equivalent step.Fix
--api-key/OPEN_TERMINAL_API_KEYClick option to themcpcommand, mirroring theruncommand._FILEDocker secret → config file.os.environ["OPEN_TERMINAL_API_KEY"]before the deferredfrom open_terminal.mcp_server import mcpsoenv.pycaptures the correct value at import time.Testing
Verified locally on Windows:
All MCP tool calls succeed with exit code 0.
Authorization: Bearer 12345678confirmed in the httpx client headers.Also works via env var and config file.
Closes #115 (Bug 1)