> ## Documentation Index
> Fetch the complete documentation index at: https://docs.alpic.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# API Key authentication

> Protect your MCP server with API Keys

Alpic also supports API key Authentication via the *x-api-key* header.
You can get the value of the api key headers in your tool callback:

```ts Typescript theme={null}

server.tool(
    "greet",
    "A simple greeting tool",
    {
      name: z.string().describe("Name to greet"),
    },
    async (
      args: { name: string },
      extra: RequestHandlerExtra<ServerRequest, ServerNotification>,
    ): Promise<CallToolResult> => {
      const apiKey = extra.requestInfo?.headers["x-api-key"];
      if (!apiKey) {
        return {
          content: [
            isError: true,
            {
              type: "text",
              text: `You need to have a valid api key to use this tool.`,
            },
          ],
        };
      }
      const yourApiClient = new yourApiClient(apiKey);
      const yourApiResponse = await yourApiClient.greet(name);
      return {
        content: [
          {
            type: "text",
            text: `${yourApiResponse}`,
          },
        ],
      };
    },
  );

```

```python Python theme={null}
from mcp.server.fastmcp import Context, FastMCP
from pydantic import Field

mcp = FastMCP("Echo Server", stateless_http=True)

@mcp.tool(
    title="Echo Tool",
    description="Echo the input text",
)
async def echo(ctx: Context, text: str = Field(description="The text to echo")) -> str:
    api_key = ctx.request_context.request.get("x-api-key")
    if not api_key:
        raise ValueError("API key is required")
    your_api_client = YourApiClient(api_key)
    your_api_response = await your_api_client.echo(text)
    return your_api_response

```

You can then instruct your users to add this custom headers when adding your MCP server to their MCP Client.

```json Cursor theme={null}
{
  "mcpServers": {
    "yourMcpServer": {
      "url": "https://your-mcp-server.alpic.live",
      "headers": {
        "x-api-key": "<USER_API_KEY>"
      }
    }
  }
}
```

```json Claude Desktop theme={null}
{
  "mcpServers": {
    "yourMcpServer": {
      "command": "npx",
      "args": ["mcp-remote", "https://your-mcp-server.alpic.live", "--header", "x-api-key: <USER_API_KEY>"]
    }
  }
}
```

```bash VSCode theme={null}
code --add-mcp '{"type":"http","name":"yourMcpServer","version":"0.0.1","description":"your amazing server","url":"https://your-mcp-server.alpic.live","author":"You","categories":["mcp"],"headers":{"x-api-key":"<USER_API_KEY>"}}'
```

```bash Claude Code theme={null}
claude mcp add --transport http yourMcpServer https://your-mcp-server.alpic.live \
  --header "x-api-key: <USER_API_KEY>"
```

```md Goose theme={null}
Go to Extensions
Click on Add custom extension
Fill the following information:

- Extension Name: yourMcpServer
- Type: Streamable HTTP
- Description: Your amazing MCP server
- Endpoint: https://your-mcp-server.alpic.live

Enter authentication headers:
Scroll down to the Request Headers section
Click the + Add button
Enter Header name: x-api-key
Enter Value: <USER_API_KEY>
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.