consume()
or validate_api_key()
methods, your MCP server first needs to extract the User API Key sent by the client. By convention, AgentPay User API Keys are sent in the HTTP header.
Standard Header
The standard HTTP header for transmitting the AgentPay User API Key is:X-AGENTPAY-API-KEY
Clients (like Cursor or Claude) should be configured to send the User API Key in this header when using remote MCP Servers.
Extraction in Python Web Frameworks
How you access this header depends on the web framework you are using to run your MCP server.Example Extraction using Starlette (Recommended)
We recommend using Starlette as your web framework, as it is simple, lightweight, and compatible with the official FastMCP framework. Example (Starlette with FastMCP):Other Frameworks (e.g. Flask, Django, etc.)
If you’re using other Python web frameworks like Flask or Django (or even a standard libraryhttp.server
), you’ll need to adapt the above Starlette + FastMCP pattern to work with your framework. Here are conceptual examples showing how to integrate User API Key extraction and validation with your framework’s routing:
Example (Conceptual - Flask with FastMCP):
Best Practices
- Case-Insensitivity: Remember that HTTP header names are generally case-insensitive. While
X-AGENTPAY-API-KEY
is the standard casing, your extraction logic should ideally handle variations if your framework doesn’t normalize them (most modern ones do). - Error Handling: Always check if the API key is present. If not, return an appropriate error response to the client (e.g. an appropriate error string, or HTTP 401 Unauthorized).
- Security: Do not log the full API keys unless absolutely necessary for debugging and ensure logs are secure. Remember, the User API Key you receive will be specific to usage with your Server.