fix: address remote MCP auth review feedback

This commit is contained in:
Christian Krakau-Louis
2026-05-21 19:00:19 +02:00
parent b98020e07f
commit 0295613578
2 changed files with 44 additions and 9 deletions
+32 -1
View File
@@ -8,7 +8,7 @@ from starlette.testclient import TestClient
os.environ.setdefault("SECRET_KEY", "test-secret-key-for-testing-only")
os.environ.setdefault("AUTOMATION_TOKEN", "test-automation-token-for-testing")
from musicround.mcp_http import BearerAuthMiddleware, build_app # noqa: E402
from musicround.mcp_http import BearerAuthMiddleware, build_app, mcp # noqa: E402
async def _dummy_app(scope, receive, send):
@@ -34,5 +34,36 @@ def test_mcp_requires_bearer_token(monkeypatch):
accepted = client.get("/mcp", headers={"Authorization": "Bearer test-mcp-token"})
assert missing.status_code == 401
assert missing.headers["WWW-Authenticate"] == "Bearer"
assert wrong.status_code == 401
assert accepted.status_code != 401
def test_mcp_falls_back_to_automation_token(monkeypatch):
monkeypatch.delenv("MCP_BEARER_TOKEN", raising=False)
monkeypatch.setenv("AUTOMATION_TOKEN", "automation-token")
client = TestClient(BearerAuthMiddleware(_dummy_app))
accepted = client.get("/mcp", headers={"Authorization": "Bearer automation-token"})
assert accepted.status_code == 204
def test_mcp_reports_missing_bearer_configuration(monkeypatch):
monkeypatch.delenv("MCP_BEARER_TOKEN", raising=False)
monkeypatch.delenv("AUTOMATION_TOKEN", raising=False)
client = TestClient(BearerAuthMiddleware(_dummy_app))
response = client.get("/mcp", headers={"Authorization": "Bearer anything"})
assert response.status_code == 500
def test_allowed_hosts_are_replaced_between_builds(monkeypatch):
monkeypatch.setenv("MCP_ALLOWED_HOSTS", "first.example")
build_app()
monkeypatch.setenv("MCP_ALLOWED_HOSTS", "second.example")
build_app()
assert "first.example" not in mcp.settings.transport_security.allowed_hosts
assert "second.example" in mcp.settings.transport_security.allowed_hosts