fix(catalog): wire api_key auth headers for http MCP servers

When an optional-mcps manifest declares transport.type=http with
auth.type=api_key, install_entry() prompts for the key and saves it to
.env, but _build_server_config() only handled the oauth case — the
api_key case produced a bare url entry with no headers, so every
request to the server was unauthenticated (-> 401).

Reuse _bearer_auth_headers(entry.name) from mcp_config.py so the
catalog path emits the same 'Authorization: Bearer ${MCP_..._API_KEY}'
template as the manual 'hermes mcp add --url' path.

Salvaged from #70782 (production hunk applied clean; tests re-anchored
onto current main). Credit: JonthanaHanh.
This commit is contained in:
JonthanaHanh
2026-08-03 10:07:37 +05:30
committed by kshitij
parent df9dbba2ba
commit 861ca18c67
2 changed files with 42 additions and 0 deletions
+4
View File
@@ -506,6 +506,10 @@ def _build_server_config(
cfg["url"] = t.url
if entry.auth.type == "oauth":
cfg["auth"] = "oauth"
elif entry.auth.type == "api_key":
from hermes_cli.mcp_config import _bearer_auth_headers
cfg["headers"] = _bearer_auth_headers(entry.name)
return cfg
+38
View File
@@ -143,6 +143,21 @@ class TestManifestParsing:
assert e.auth.env[1].required is False
assert e.auth.env[1].secret is False
def test_http_api_key_builds_bearer_headers_template(self, catalog_dir):
body = _basic_manifest(
transport={"type": "http", "url": "https://mcp.example.com/sse"},
auth={
"type": "api_key",
"env": [{"name": "MCP_DEMO_API_KEY", "prompt": "key", "secret": True}],
},
)
_write_manifest(catalog_dir, "demo", body)
from hermes_cli.mcp_catalog import _build_server_config
cfg = _build_server_config(_entry("demo"), None)
assert cfg["url"] == "https://mcp.example.com/sse"
assert cfg["headers"] == {"Authorization": "Bearer ${MCP_DEMO_API_KEY}"}
@@ -193,6 +208,29 @@ class TestInstall:
assert get_env_value("DEMO_KEY") == "secret-val"
assert "demo" in load_config()["mcp_servers"]
def test_install_http_api_key_writes_bearer_headers(self, catalog_dir, monkeypatch):
body = _basic_manifest(
transport={"type": "http", "url": "https://mcp.example.com/sse"},
auth={
"type": "api_key",
"env": [{"name": "MCP_DEMO_API_KEY", "prompt": "key", "secret": True}],
},
)
_write_manifest(catalog_dir, "demo", body)
from hermes_cli import mcp_catalog
monkeypatch.setattr(mcp_catalog, "_prompt_input", lambda *a, **kw: "secret-val")
from hermes_cli.mcp_catalog import install_entry
from hermes_cli.config import load_config
install_entry(_entry("demo"), enable=True)
server = load_config()["mcp_servers"]["demo"]
assert server["url"] == "https://mcp.example.com/sse"
assert server["headers"] == {"Authorization": "Bearer secret-val"}