Skip to main content
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:

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}`,
          },
        ],
      };
    },
  );

You can then instruct your users to add this custom headers when adding your MCP server to their MCP Client.
{
  "mcpServers": {
    "yourMcpServer": {
      "url": "https://your-mcp-server.alpic.live",
      "headers": {
        "x-api-key": "<USER_API_KEY>"
      }
    }
  }
}
I