Coverage for src/mcpadapt/auth/authenticate.py: 0%

6 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-06 19:35 +0530

1"""Authentication utilities for pre-authenticating providers.""" 

2 

3from typing import Any 

4 

5from mcp.client.auth import OAuthClientProvider 

6 

7from .providers import ApiKeyAuthProvider, BearerAuthProvider, create_auth_provider 

8from .models import AuthConfig, OAuthConfig 

9 

10 

11async def authenticate( 

12 auth_config: AuthConfig, 

13 server_url: str 

14) -> OAuthClientProvider | ApiKeyAuthProvider | BearerAuthProvider: 

15 """ 

16 Create and prepare an auth provider for use with MCPAdapt. 

17  

18 For OAuth: Creates a configured OAuth provider that will perform the OAuth flow 

19 (browser redirect, callback, token exchange) when first used by MCPAdapt 

20 For API Key/Bearer: Creates a ready-to-use provider (no additional flow needed) 

21  

22 Args: 

23 auth_config: Authentication configuration 

24 server_url: Server URL (needed for OAuth server endpoint discovery) 

25  

26 Returns: 

27 Auth provider ready to use with MCPAdapt 

28  

29 Example: 

30 >>> # Prepare OAuth provider 

31 >>> oauth_config = OAuthConfig(client_metadata={...}) 

32 >>> auth_provider = await authenticate(oauth_config, "https://mcp.canva.com/mcp") 

33 >>>  

34 >>> # Use with MCPAdapt - OAuth flow will happen during connection 

35 >>> with MCPAdapt(server_config, adapter, auth_provider=auth_provider) as tools: 

36 >>> print(tools) 

37  

38 Note: 

39 For OAuth, the actual authentication flow (browser redirect, token exchange)  

40 occurs when MCPAdapt makes its first connection to the MCP server. This function 

41 prepares the OAuth provider with all necessary configuration. 

42 """ 

43 return await create_auth_provider(auth_config, server_url)