Skip to content

Commit 28911aa

Browse files
committed
Fix OAuth2 client_id type errors
Add explicit None checks for client_id fields before passing to functions that expect str. This fixes type errors where str | None was being passed to parameters that require str. Changes: - simple_auth_provider.py: Add client_id validation in exchange_client_credentials and exchange_token - oauth2.py: Add client_id check at start of _apply_client_auth method - test_auth_integration.py: Add assertions for client_id not being None in test mock methods This ensures proper type safety and prevents potential None dereference errors.
1 parent 394a0a0 commit 28911aa

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

examples/servers/simple-auth/mcp_simple_auth/simple_auth_provider.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ async def exchange_authorization_code(
244244

245245
async def exchange_client_credentials(self, client: OAuthClientInformationFull, scopes: list[str]) -> OAuthToken:
246246
"""Exchange client credentials for an MCP access token."""
247+
if not client.client_id:
248+
raise ValueError("No client_id provided")
247249
mcp_token = f"mcp_{secrets.token_hex(32)}"
248250
self.tokens[mcp_token] = AccessToken(
249251
token=mcp_token,
@@ -272,6 +274,8 @@ async def exchange_token(
272274
"""Exchange an external token for an MCP access token."""
273275
if not subject_token:
274276
raise ValueError("Invalid subject token")
277+
if not client.client_id:
278+
raise ValueError("No client_id provided")
275279

276280
mcp_token = f"mcp_{secrets.token_hex(32)}"
277281
self.tokens[mcp_token] = AccessToken(

src/mcp/client/auth/oauth2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ def _apply_client_auth(
256256
headers: dict[str, str],
257257
client_info: OAuthClientInformationFull,
258258
) -> None:
259+
if not client_info.client_id:
260+
raise OAuthFlowError("Client ID is required")
259261
auth_method = "client_secret_post"
260262
if self._metadata and self._metadata.token_endpoint_auth_methods_supported:
261263
supported = self._metadata.token_endpoint_auth_methods_supported

tests/server/fastmcp/auth/test_auth_integration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ async def exchange_refresh_token(
160160
)
161161

162162
async def exchange_client_credentials(self, client: OAuthClientInformationFull, scopes: list[str]) -> OAuthToken:
163+
assert client.client_id is not None
163164
access_token = f"access_{secrets.token_hex(32)}"
164165
self.tokens[access_token] = AccessToken(
165166
token=access_token,
@@ -188,6 +189,7 @@ async def exchange_token(
188189
if subject_token == "bad_token":
189190
raise TokenError("invalid_grant", "invalid subject token")
190191

192+
assert client.client_id is not None
191193
access_token = f"exchanged_{secrets.token_hex(32)}"
192194
self.tokens[access_token] = AccessToken(
193195
token=access_token,

0 commit comments

Comments
 (0)