|
| 1 | +# API Key Authentication |
| 2 | + |
| 3 | +API Key authentication allows you to pass in an API Key as a header to the MCP server for authentication |
| 4 | + |
| 5 | +## Basic Usage |
| 6 | + |
| 7 | +```python |
| 8 | +from mcpadapt.auth import ApiKeyAuthProvider |
| 9 | +from mcpadapt.core import MCPAdapt |
| 10 | +from mcpadapt.smolagents_adapter import SmolAgentsAdapter |
| 11 | + |
| 12 | +# Create API Key provider |
| 13 | +api_key_provider = ApiKeyAuthProvider( |
| 14 | + header_name="X-API-Key", |
| 15 | + header_value="your-api-key-here" |
| 16 | +) |
| 17 | + |
| 18 | +with MCPAdapt( |
| 19 | + serverparams={"url": "https://api.example.com/mcp", "transport": "streamable-http"}, |
| 20 | + adapter=SmolAgentsAdapter(), |
| 21 | + auth_provider=api_key_provider, |
| 22 | +) as tools: |
| 23 | + print(f"Connected with {len(tools)} tools") |
| 24 | +``` |
| 25 | + |
| 26 | +## Custom Header Names |
| 27 | + |
| 28 | +Different APIs use different header names for API keys: |
| 29 | + |
| 30 | +```python |
| 31 | +from mcpadapt.auth import ApiKeyAuthProvider |
| 32 | + |
| 33 | +# Common API key header variations |
| 34 | +providers = [ |
| 35 | + ApiKeyAuthProvider("X-API-Key", "key123"), # Most common |
| 36 | + ApiKeyAuthProvider("Authorization", "key123"), # Simple auth header |
| 37 | + ApiKeyAuthProvider("X-Auth-Token", "key123"), # Auth token variant |
| 38 | + ApiKeyAuthProvider("X-Custom-Auth", "key123"), # Custom header |
| 39 | +] |
| 40 | +``` |
| 41 | + |
| 42 | +## Environment Variables |
| 43 | + |
| 44 | +Store API keys securely using environment variables: |
| 45 | + |
| 46 | +```python |
| 47 | +import os |
| 48 | +from mcpadapt.auth import ApiKeyAuthProvider |
| 49 | + |
| 50 | +# Load API key from environment |
| 51 | +api_key = os.getenv("MY_API_KEY") |
| 52 | +if not api_key: |
| 53 | + raise ValueError("MY_API_KEY environment variable is required") |
| 54 | + |
| 55 | +api_key_provider = ApiKeyAuthProvider( |
| 56 | + header_name="X-API-Key", |
| 57 | + header_value=api_key |
| 58 | +) |
| 59 | +``` |
| 60 | + |
| 61 | +## Multiple APIs with Different Keys |
| 62 | + |
| 63 | +Use different API keys for different MCP servers: |
| 64 | + |
| 65 | +```python |
| 66 | +import os |
| 67 | +from mcpadapt.auth import ApiKeyAuthProvider |
| 68 | +from mcpadapt.core import MCPAdapt |
| 69 | +from mcpadapt.smolagents_adapter import SmolAgentsAdapter |
| 70 | + |
| 71 | +# Different API keys for different services |
| 72 | +auth_providers = [ |
| 73 | + ApiKeyAuthProvider("X-API-Key", os.getenv("SERVICE_A_KEY")), |
| 74 | + ApiKeyAuthProvider("X-Auth-Token", os.getenv("SERVICE_B_KEY")), |
| 75 | + None, # No authentication for third service |
| 76 | +] |
| 77 | + |
| 78 | +server_configs = [ |
| 79 | + {"url": "https://service-a.com/mcp", "transport": "streamable-http"}, |
| 80 | + {"url": "https://service-b.com/mcp", "transport": "streamable-http"}, |
| 81 | + {"url": "http://localhost:8000/sse"}, |
| 82 | +] |
| 83 | + |
| 84 | +with MCPAdapt( |
| 85 | + serverparams=server_configs, |
| 86 | + adapter=SmolAgentsAdapter(), |
| 87 | + auth_provider=auth_providers, |
| 88 | +) as tools: |
| 89 | + print(f"Connected to {len(server_configs)} servers") |
| 90 | +``` |
| 91 | + |
| 92 | +## API Key Formats |
| 93 | + |
| 94 | +### Simple API Key |
| 95 | + |
| 96 | +```python |
| 97 | +ApiKeyAuthProvider("X-API-Key", "abc123def456") |
| 98 | +``` |
| 99 | + |
| 100 | +### Prefixed API Key |
| 101 | + |
| 102 | +```python |
| 103 | +ApiKeyAuthProvider("X-API-Key", "Bearer abc123def456") |
| 104 | +ApiKeyAuthProvider("Authorization", "API-Key abc123def456") |
| 105 | +``` |
| 106 | + |
| 107 | +### Base64 Encoded Credentials |
| 108 | + |
| 109 | +```python |
| 110 | +import base64 |
| 111 | + |
| 112 | +credentials = base64.b64encode(b"username:password").decode() |
| 113 | +ApiKeyAuthProvider("Authorization", f"Basic {credentials}") |
| 114 | +``` |
| 115 | + |
| 116 | +## Best Practices |
| 117 | + |
| 118 | +### Security |
| 119 | +- Never hard-code API keys in source code |
| 120 | +- Use environment variables or secure configuration files |
| 121 | +- Rotate API keys regularly |
| 122 | +- Use the principle of least privilege for API key permissions |
| 123 | + |
| 124 | +### Configuration |
| 125 | +- Use descriptive environment variable names |
| 126 | +- Document required API keys in your README |
| 127 | +- Provide clear error messages for missing keys |
| 128 | +- Validate API key format before using |
| 129 | + |
| 130 | +## Integration Examples |
| 131 | + |
| 132 | +### With Different Frameworks |
| 133 | + |
| 134 | +```python |
| 135 | +# SmolAgents |
| 136 | +from mcpadapt.smolagents_adapter import SmolAgentsAdapter |
| 137 | +adapter = SmolAgentsAdapter() |
| 138 | + |
| 139 | +# CrewAI |
| 140 | +from mcpadapt.crewai_adapter import CrewAIAdapter |
| 141 | +adapter = CrewAIAdapter() |
| 142 | + |
| 143 | +# LangChain |
| 144 | +from mcpadapt.langchain_adapter import LangChainAdapter |
| 145 | +adapter = LangChainAdapter() |
| 146 | + |
| 147 | +# All use the same API key provider |
| 148 | +api_key_provider = ApiKeyAuthProvider("X-API-Key", os.getenv("API_KEY")) |
| 149 | +``` |
0 commit comments