Multiple Authentication Headersยถ
Overviewยถ
ContextForge supports multiple custom authentication headers for both gateway connections and REST tools. This feature allows you to configure multiple header key-value pairs that will be sent with every request to your MCP servers or REST endpoints.
Admin UI URL
- Direct installs (
uvx, pip, ordocker run):http://localhost:4444/admin/ - Docker Compose (nginx proxy):
http://localhost:8080/admin/ - Dev server (
make dev):http://localhost:8000/admin/
Use Casesยถ
Multiple authentication headers are useful when:
- Your MCP server requires multiple API keys or tokens
- You need to send client identification along with authentication
- Your service uses region-specific or version-specific headers
- You're integrating with services that require complex header-based authentication
Configurationยถ
Via Admin UIยถ
- Navigate to the Admin Panel (see Admin UI URL above)
- Click on the "Gateways" tab
-
When adding or editing a gateway:
-
Select "Custom Headers" as the Authentication Type
- Click "Add Header" to add multiple header pairs
- Enter the header key (e.g.,
X-API-Key) and value for each header - Click "Remove" next to any header to delete it
- Submit the form to save your configuration
Via APIยถ
Send a POST request to /admin/gateways with the auth_headers field as a JSON array:
{
"name": "My Gateway",
"url": "http://mcp-server.example.com",
"auth_type": "authheaders",
"auth_headers": [
{"key": "X-API-Key", "value": "secret-key-123"},
{"key": "X-Client-ID", "value": "client-456"},
{"key": "X-Region", "value": "us-east-1"}
]
}
Via Python SDKยถ
from mcpgateway.schemas import GatewayCreate
gateway = GatewayCreate(
name="My Gateway",
url="http://mcp-server.example.com",
auth_type="authheaders",
auth_headers=[
{"key": "X-API-Key", "value": "secret-key-123"},
{"key": "X-Client-ID", "value": "client-456"},
{"key": "X-Region", "value": "us-east-1"}
]
)
Toolsยถ
REST tools accept the same auth_headers array on POST /tools and PUT /tools/{tool_id}, with identical validation and precedence rules:
{
"name": "my-tool",
"url": "https://api.example.com/endpoint",
"request_type": "POST",
"auth_type": "authheaders",
"auth_headers": [
{"key": "X-API-Key", "value": "secret"},
{"key": "X-Tenant", "value": "acme"}
]
}
The same array is used by the "Custom Headers" authentication type in the Tools tab of the Admin UI.
Backward Compatibilityยถ
Gateways and tools still support the legacy single-header format for backward compatibility:
{
"name": "My Gateway",
"url": "http://mcp-server.example.com",
"auth_type": "authheaders",
"auth_header_key": "X-API-Key",
"auth_header_value": "secret-key-123"
}
If both auth_headers (multi) and auth_header_key/auth_header_value (single) are provided, the multi-header format takes precedence.
Security Considerationsยถ
Encryptionยถ
All authentication headers are encrypted before being stored in the database using AES-256-GCM encryption. The encryption key is derived from the AUTH_ENCRYPTION_SECRET environment variable.
Header Validationยถ
Gateways, tools and A2A agents share a single validator, so the same rules apply everywhere:
- Empty header keys are ignored; if no entry has a key, the request is rejected with a 422
- Header keys may contain only alphanumeric characters, hyphens and underscores โ anything else (including embedded spaces) is rejected with a 422
- Surrounding whitespace on a header key is trimmed before storage
- Duplicate header keys will use the last provided value
- Header keys and values must be strings; any other JSON type is rejected with a 422
- Header values can be empty strings if required by your authentication scheme, and may contain special characters
- A maximum of 100 header entries may be submitted. The cap applies to the entries you send, before duplicates collapse โ so 101 entries are rejected even if they resolve to fewer unique keys
Best Practicesยถ
- Use HTTPS: Always use HTTPS URLs for your MCP servers to prevent header interception
- Rotate Keys: Regularly rotate your API keys and update them in the gateway configuration
- Minimal Headers: Only include headers that are strictly necessary for authentication
- Environment Variables: Store sensitive values in environment variables when deploying
Common Patternsยถ
Multiple API Keysยถ
{
"auth_headers": [
{"key": "X-Primary-Key", "value": "primary-secret"},
{"key": "X-Secondary-Key", "value": "secondary-secret"}
]
}
API Key with Client Identificationยถ
{
"auth_headers": [
{"key": "X-API-Key", "value": "api-secret"},
{"key": "X-Client-ID", "value": "client-123"},
{"key": "X-Client-Secret", "value": "client-secret"}
]
}
Regional Configurationยถ
{
"auth_headers": [
{"key": "X-API-Key", "value": "api-secret"},
{"key": "X-Region", "value": "eu-west-1"},
{"key": "X-Environment", "value": "production"}
]
}
Troubleshootingยถ
Headers Not Being Sentยถ
- Check that your gateway is using
auth_type: "authheaders" - Verify headers are properly formatted in the JSON array
- Ensure the gateway is enabled and reachable
- Check server logs to confirm headers are being received
Case Sensitivityยถ
HTTP headers are case-insensitive by specification. Some HTTP clients or servers may normalize header names to lowercase. Your MCP server should handle headers in a case-insensitive manner.
Validation Errorsยถ
If you receive validation errors when saving:
- Ensure at least one header is provided when using "Custom Headers" authentication
- Check that your JSON is properly formatted if using the API
- Verify that header keys don't contain invalid characters
Testing Your Configurationยถ
Use the "Test" button in the Admin UI to verify your gateway connection with the configured headers. The test will attempt to connect to your MCP server and validate that authentication is working correctly.
Migration from Single Headersยถ
If you have existing gateways using single header authentication, they will continue to work without modification. To migrate to multi-headers:
- Edit your gateway in the Admin UI
- Your existing single header will be displayed
- Add additional headers as needed
- Save the configuration
The system will automatically convert your configuration to the multi-header format while preserving your existing authentication.
API Referenceยถ
GatewayCreate Schemaยถ
{
"name": str,
"url": str,
"auth_type": "authheaders",
"auth_headers": [
{"key": str, "value": str},
...
]
}
GatewayUpdate Schemaยถ
ToolCreate / ToolUpdate Schemaยถ
{
"name": str, # ToolCreate only
"url": str, # ToolCreate only
"auth_type": "authheaders",
"auth_headers": [
{"key": str, "value": str},
...
]
}