|
1 |
| -"""Authentication module for MCPAdapt.""" |
| 1 | +"""Authentication module for MCPAdapt. |
| 2 | +
|
| 3 | +This module provides OAuth, API Key, and Bearer token authentication support |
| 4 | +for MCP servers. |
| 5 | +
|
| 6 | +Example usage with OAuth: |
| 7 | +
|
| 8 | +```python |
| 9 | +from mcp.client.auth import OAuthClientProvider |
| 10 | +from mcp.shared.auth import OAuthClientMetadata |
| 11 | +from pydantic import HttpUrl |
| 12 | +
|
| 13 | +from mcpadapt.auth import ( |
| 14 | + InMemoryTokenStorage, |
| 15 | + LocalBrowserOAuthHandler |
| 16 | +) |
| 17 | +from mcpadapt.core import MCPAdapt |
| 18 | +from mcpadapt.smolagents_adapter import SmolAgentsAdapter |
| 19 | +
|
| 20 | +# Create OAuth provider directly |
| 21 | +client_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 | +
|
| 29 | +oauth_handler = LocalBrowserOAuthHandler(callback_port=3030) |
| 30 | +token_storage = InMemoryTokenStorage() |
| 31 | +
|
| 32 | +oauth_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 |
| 41 | +with 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 | +
|
| 49 | +Example usage with API Key: |
| 50 | +
|
| 51 | +```python |
| 52 | +from mcpadapt.auth import ApiKeyAuthProvider |
| 53 | +from mcpadapt.core import MCPAdapt |
| 54 | +from mcpadapt.smolagents_adapter import SmolAgentsAdapter |
| 55 | +
|
| 56 | +# Create API Key provider |
| 57 | +api_key_provider = ApiKeyAuthProvider( |
| 58 | + header_name="X-API-Key", |
| 59 | + header_value="your-api-key-here" |
| 60 | +) |
| 61 | +
|
| 62 | +with 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 | +
|
| 70 | +For custom implementations, extend BaseOAuthHandler: |
| 71 | +
|
| 72 | +```python |
| 73 | +from mcpadapt.auth import BaseOAuthHandler |
| 74 | +
|
| 75 | +class 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 | +""" |
2 | 86 |
|
3 |
| -from .handlers import default_callback_handler, default_redirect_handler |
4 | 87 | from .oauth import InMemoryTokenStorage
|
| 88 | +from .handlers import ( |
| 89 | + BaseOAuthHandler, |
| 90 | + LocalBrowserOAuthHandler, |
| 91 | + LocalCallbackServer, |
| 92 | +) |
5 | 93 | from .providers import (
|
6 | 94 | ApiKeyAuthProvider,
|
7 | 95 | BearerAuthProvider,
|
8 |
| - create_auth_provider, |
9 | 96 | get_auth_headers,
|
10 | 97 | )
|
11 |
| -from .models import ( |
12 |
| - ApiKeyConfig, |
13 |
| - AuthConfig, |
14 |
| - AuthConfigBase, |
15 |
| - BearerAuthConfig, |
16 |
| - CallbackHandler, |
17 |
| - OAuthConfig, |
18 |
| - RedirectHandler, |
| 98 | +from .exceptions import ( |
| 99 | + OAuthError, |
| 100 | + OAuthTimeoutError, |
| 101 | + OAuthCancellationError, |
| 102 | + OAuthNetworkError, |
| 103 | + OAuthConfigurationError, |
| 104 | + OAuthServerError, |
| 105 | + OAuthCallbackError, |
19 | 106 | )
|
20 | 107 |
|
21 | 108 | __all__ = [
|
22 |
| - # Types |
23 |
| - "AuthConfig", |
24 |
| - "AuthConfigBase", |
25 |
| - "OAuthConfig", |
26 |
| - "ApiKeyConfig", |
27 |
| - "BearerAuthConfig", |
28 |
| - "CallbackHandler", |
29 |
| - "RedirectHandler", |
30 |
| - # OAuth utilities |
31 |
| - "InMemoryTokenStorage", |
32 |
| - # Handlers |
33 |
| - "default_callback_handler", |
34 |
| - "default_redirect_handler", |
35 |
| - # Providers |
| 109 | + # Handler classes |
| 110 | + "BaseOAuthHandler", |
| 111 | + "LocalBrowserOAuthHandler", |
| 112 | + "LocalCallbackServer", |
| 113 | + # Provider classes |
36 | 114 | "ApiKeyAuthProvider",
|
37 | 115 | "BearerAuthProvider",
|
38 |
| - "create_auth_provider", |
| 116 | + # Default implementations |
| 117 | + "InMemoryTokenStorage", |
| 118 | + # Provider functions |
39 | 119 | "get_auth_headers",
|
| 120 | + # Exception classes |
| 121 | + "OAuthError", |
| 122 | + "OAuthTimeoutError", |
| 123 | + "OAuthCancellationError", |
| 124 | + "OAuthNetworkError", |
| 125 | + "OAuthConfigurationError", |
| 126 | + "OAuthServerError", |
| 127 | + "OAuthCallbackError", |
40 | 128 | ]
|
0 commit comments