Coverage for src/mcpadapt/auth/__init__.py: 100%

5 statements  

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

1"""Authentication module for MCPAdapt. 

2 

3This module provides OAuth, API Key, and Bearer token authentication support 

4for MCP servers. 

5 

6Example usage with OAuth: 

7 

8```python 

9from mcp.client.auth import OAuthClientProvider 

10from mcp.shared.auth import OAuthClientMetadata 

11from pydantic import HttpUrl 

12 

13from mcpadapt.auth import ( 

14 InMemoryTokenStorage,  

15 LocalBrowserOAuthHandler 

16) 

17from mcpadapt.core import MCPAdapt 

18from mcpadapt.smolagents_adapter import SmolAgentsAdapter 

19 

20# Create OAuth provider directly 

21client_metadata = OAuthClientMetadata( 

22 client_name="My App", 

23 redirect_uris=[HttpUrl("http://localhost:3030/callback")], 

24 grant_types=["authorization_code", "refresh_token"], 

25 response_types=["code"], 

26 token_endpoint_auth_method="client_secret_post", 

27) 

28 

29oauth_handler = LocalBrowserOAuthHandler(callback_port=3030) 

30token_storage = InMemoryTokenStorage() 

31 

32oauth_provider = OAuthClientProvider( 

33 server_url="https://example.com", 

34 client_metadata=client_metadata, 

35 storage=token_storage, 

36 redirect_handler=oauth_handler.handle_redirect, 

37 callback_handler=oauth_handler.handle_callback, 

38) 

39 

40# Use with MCPAdapt 

41with MCPAdapt( 

42 serverparams={"url": "https://example.com/mcp", "transport": "streamable-http"}, 

43 adapter=SmolAgentsAdapter(), 

44 auth_provider=oauth_provider, 

45) as tools: 

46 print(f"Connected with {len(tools)} tools") 

47``` 

48 

49Example usage with API Key: 

50 

51```python 

52from mcpadapt.auth import ApiKeyAuthProvider 

53from mcpadapt.core import MCPAdapt 

54from mcpadapt.smolagents_adapter import SmolAgentsAdapter 

55 

56# Create API Key provider 

57api_key_provider = ApiKeyAuthProvider( 

58 header_name="X-API-Key", 

59 header_value="your-api-key-here" 

60) 

61 

62with MCPAdapt( 

63 serverparams={"url": "https://example.com/mcp", "transport": "streamable-http"}, 

64 adapter=SmolAgentsAdapter(), 

65 auth_provider=api_key_provider, 

66) as tools: 

67 print(f"Connected with {len(tools)} tools") 

68``` 

69 

70For custom implementations, extend BaseOAuthHandler: 

71 

72```python 

73from mcpadapt.auth import BaseOAuthHandler 

74 

75class CustomOAuthHandler(BaseOAuthHandler): 

76 async def handle_redirect(self, authorization_url: str) -> None: 

77 # Custom redirect logic (e.g., print URL for headless environments) 

78 print(f"Please open: {authorization_url}") 

79  

80 async def handle_callback(self) -> tuple[str, str | None]: 

81 # Custom callback logic (e.g., manual code input) 

82 auth_code = input("Enter authorization code: ") 

83 return auth_code, None 

84``` 

85""" 

86 

87from .oauth import InMemoryTokenStorage 

88from .handlers import ( 

89 BaseOAuthHandler, 

90 LocalBrowserOAuthHandler, 

91 LocalCallbackServer, 

92) 

93from .providers import ( 

94 ApiKeyAuthProvider, 

95 BearerAuthProvider, 

96 get_auth_headers, 

97) 

98from .exceptions import ( 

99 OAuthError, 

100 OAuthTimeoutError, 

101 OAuthCancellationError, 

102 OAuthNetworkError, 

103 OAuthConfigurationError, 

104 OAuthServerError, 

105 OAuthCallbackError, 

106) 

107 

108__all__ = [ 

109 # Handler classes 

110 "BaseOAuthHandler", 

111 "LocalBrowserOAuthHandler", 

112 "LocalCallbackServer", 

113 # Provider classes 

114 "ApiKeyAuthProvider", 

115 "BearerAuthProvider", 

116 # Default implementations 

117 "InMemoryTokenStorage", 

118 # Provider functions 

119 "get_auth_headers", 

120 # Exception classes 

121 "OAuthError", 

122 "OAuthTimeoutError", 

123 "OAuthCancellationError", 

124 "OAuthNetworkError", 

125 "OAuthConfigurationError", 

126 "OAuthServerError", 

127 "OAuthCallbackError", 

128]