Skip to content

Commit a878e11

Browse files
committed
Fix MCP auth header failure signaling
1 parent 05b4499 commit a878e11

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

peak_assistant/utils/mcp_config.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ async def _connect_http_server(self, server_name: str, config: MCPServerConfig,
10031003

10041004
# Create HTTP client with authentication
10051005
headers = await self._get_auth_headers(config, user_id)
1006-
if headers is False:
1006+
if headers is None:
10071007
return False
10081008

10091009
# Store the connection (user-specific or system-level)
@@ -1018,19 +1018,19 @@ async def _connect_http_server(self, server_name: str, config: MCPServerConfig,
10181018
logger.info(f"Connected to HTTP server: {server_name}" + (f" for user {user_id}" if user_id else ""))
10191019
return True
10201020

1021-
async def _get_auth_headers(self, config: MCPServerConfig, user_id: Optional[str] = None) -> Dict[str, str]:
1021+
async def _get_auth_headers(self, config: MCPServerConfig, user_id: Optional[str] = None) -> Optional[Dict[str, str]]:
10221022
"""Get authentication headers for a server"""
10231023
headers = {}
10241024
if config.auth:
10251025
if config.auth.type == AuthType.BEARER:
10261026
if not config.auth.token:
10271027
logger.error(f"No token specified for bearer auth on {config.name}")
1028-
return {}
1028+
return None
10291029
headers["Authorization"] = f"Bearer {config.auth.token}"
10301030
elif config.auth.type == AuthType.API_KEY:
10311031
if not config.auth.api_key or not config.auth.header_name:
10321032
logger.error(f"API key or header name not specified for {config.name}")
1033-
return {}
1033+
return None
10341034
headers[config.auth.header_name] = config.auth.api_key
10351035
elif config.auth.type in [AuthType.OAUTH2_CLIENT_CREDENTIALS, AuthType.OAUTH2_AUTHORIZATION_CODE]:
10361036
# Priority 1: Check Streamlit session state (for web UI)
@@ -1067,19 +1067,19 @@ async def _get_auth_headers(self, config: MCPServerConfig, user_id: Optional[str
10671067
# Check if user ID is required
10681068
if config.auth.requires_user_auth:
10691069
logger.error(f"User ID is required for user-based OAuth on {config.name}")
1070-
return {}
1070+
return None
10711071
else:
10721072
logger.error(f"No access token in Streamlit session state for {config.name}")
1073-
return {}
1073+
return None
10741074
else:
10751075
logger.error(f"No OAuth data found in Streamlit session state for {config.name}")
1076-
return {}
1076+
return None
10771077
else:
10781078
logger.error(f"Streamlit session state not available")
1079-
return {}
1079+
return None
10801080
except Exception as e:
10811081
logger.error(f"Failed to get Streamlit OAuth headers for {config.name}: {e}")
1082-
return {}
1082+
return None
10831083
else:
10841084
# Priority 2: Check environment variables (for CLI/automation)
10851085
# Only used when NOT running in Streamlit
@@ -1111,7 +1111,7 @@ async def _get_auth_headers(self, config: MCPServerConfig, user_id: Optional[str
11111111
f" Alternatively, authenticate via Streamlit web interface.\n"
11121112
f" Server will be skipped."
11131113
)
1114-
return {}
1114+
return None
11151115

11161116
return headers
11171117

@@ -1137,7 +1137,7 @@ async def _get_auth_headers(self, config: MCPServerConfig, user_id: Optional[str
11371137
f" Alternatively, authenticate via Streamlit web interface.\n"
11381138
f" Server will be skipped."
11391139
)
1140-
return {}
1140+
return None
11411141

11421142
return headers
11431143

tests/unit_tests/test_mcp_oauth_env_vars.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async def test_oauth_with_token_and_user_id_env_vars(monkeypatch, client_manager
9696

9797
@pytest.mark.asyncio
9898
async def test_oauth_missing_required_user_id(monkeypatch, client_manager):
99-
"""Test that missing user ID returns empty headers with warning"""
99+
"""Test that missing user ID fails auth header generation"""
100100
monkeypatch.setenv("PEAK_MCP_TEST_SERVER_TOKEN", "test_token_123")
101101
# Don't set USER_ID
102102

@@ -112,8 +112,8 @@ async def test_oauth_missing_required_user_id(monkeypatch, client_manager):
112112

113113
headers = await client_manager._get_auth_headers(config)
114114

115-
# Should return empty when user ID required but missing
116-
assert headers == {}
115+
# Should fail when user ID required but missing
116+
assert headers is None
117117

118118

119119
@pytest.mark.asyncio
@@ -133,7 +133,7 @@ async def test_oauth_no_env_vars_no_streamlit(client_manager):
133133

134134
headers = await client_manager._get_auth_headers(config)
135135

136-
assert headers == {}
136+
assert headers is None
137137

138138

139139
@pytest.mark.asyncio

0 commit comments

Comments
 (0)