|
35 | 35 | import httpx |
36 | 36 | from mcp import ClientSession |
37 | 37 | from mcp.client.auth import OAuthClientProvider, TokenStorage |
38 | | -from mcp.client.auth.extensions.client_credentials import JWTParameters, RFC7523OAuthClientProvider |
| 38 | +from mcp.client.auth.extensions.client_credentials import ( |
| 39 | + ClientCredentialsOAuthProvider, |
| 40 | + PrivateKeyJWTOAuthProvider, |
| 41 | + SignedJWTParameters, |
| 42 | +) |
39 | 43 | from mcp.client.streamable_http import streamablehttp_client |
40 | 44 | from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken |
41 | 45 | from pydantic import AnyUrl |
@@ -198,32 +202,21 @@ async def run_client_credentials_jwt_client(server_url: str) -> None: |
198 | 202 | if not private_key_pem: |
199 | 203 | raise RuntimeError("MCP_CONFORMANCE_CONTEXT missing 'private_key_pem'") |
200 | 204 |
|
201 | | - # Create JWT parameters for private_key_jwt authentication |
202 | | - jwt_params = JWTParameters( |
| 205 | + # Create JWT parameters for SDK-signed assertions |
| 206 | + jwt_params = SignedJWTParameters( |
203 | 207 | issuer=client_id, |
204 | 208 | subject=client_id, |
205 | | - jwt_signing_algorithm=signing_algorithm, |
206 | | - jwt_signing_key=private_key_pem, |
| 209 | + signing_algorithm=signing_algorithm, |
| 210 | + signing_key=private_key_pem, |
207 | 211 | ) |
208 | 212 |
|
209 | | - # Create OAuth authentication handler for client_credentials flow |
210 | | - # Note: redirect_uris is required by the model but not used in client_credentials flow |
211 | | - import warnings |
212 | | - |
213 | | - with warnings.catch_warnings(): |
214 | | - warnings.simplefilter("ignore", DeprecationWarning) |
215 | | - oauth_auth = RFC7523OAuthClientProvider( |
216 | | - server_url=server_url, |
217 | | - client_metadata=OAuthClientMetadata( |
218 | | - client_name=client_id, |
219 | | - redirect_uris=[AnyUrl("http://localhost:0/unused")], # Required but unused |
220 | | - grant_types=["client_credentials"], |
221 | | - response_types=[], |
222 | | - token_endpoint_auth_method="private_key_jwt", |
223 | | - ), |
224 | | - storage=InMemoryTokenStorage(), |
225 | | - jwt_parameters=jwt_params, |
226 | | - ) |
| 213 | + # Create OAuth provider for client_credentials with private_key_jwt |
| 214 | + oauth_auth = PrivateKeyJWTOAuthProvider( |
| 215 | + server_url=server_url, |
| 216 | + storage=InMemoryTokenStorage(), |
| 217 | + client_id=client_id, |
| 218 | + assertion_provider=jwt_params.create_assertion_provider(), |
| 219 | + ) |
227 | 220 |
|
228 | 221 | await _run_session(server_url, oauth_auth) |
229 | 222 |
|
@@ -251,34 +244,15 @@ async def run_client_credentials_basic_client(server_url: str) -> None: |
251 | 244 | if not client_secret: |
252 | 245 | raise RuntimeError("MCP_CONFORMANCE_CONTEXT missing 'client_secret'") |
253 | 246 |
|
254 | | - # Create storage pre-populated with client credentials |
255 | | - storage = InMemoryTokenStorage() |
256 | | - await storage.set_client_info( |
257 | | - OAuthClientInformationFull( |
258 | | - client_id=client_id, |
259 | | - client_secret=client_secret, |
260 | | - redirect_uris=[AnyUrl("http://localhost:0/unused")], |
261 | | - token_endpoint_auth_method="client_secret_basic", |
262 | | - ) |
| 247 | + # Create OAuth provider for client_credentials with client_secret_basic |
| 248 | + oauth_auth = ClientCredentialsOAuthProvider( |
| 249 | + server_url=server_url, |
| 250 | + storage=InMemoryTokenStorage(), |
| 251 | + client_id=client_id, |
| 252 | + client_secret=client_secret, |
| 253 | + token_endpoint_auth_method="client_secret_basic", |
263 | 254 | ) |
264 | 255 |
|
265 | | - # Create OAuth authentication handler for client_credentials flow with basic auth |
266 | | - import warnings |
267 | | - |
268 | | - with warnings.catch_warnings(): |
269 | | - warnings.simplefilter("ignore", DeprecationWarning) |
270 | | - oauth_auth = RFC7523OAuthClientProvider( |
271 | | - server_url=server_url, |
272 | | - client_metadata=OAuthClientMetadata( |
273 | | - client_name=client_id, |
274 | | - redirect_uris=[AnyUrl("http://localhost:0/unused")], # Required but unused |
275 | | - grant_types=["client_credentials"], |
276 | | - response_types=[], |
277 | | - token_endpoint_auth_method="client_secret_basic", |
278 | | - ), |
279 | | - storage=storage, |
280 | | - ) |
281 | | - |
282 | 256 | await _run_session(server_url, oauth_auth) |
283 | 257 |
|
284 | 258 |
|
|
0 commit comments