From 16e73f86df44a9b0a94b08b2c28338a74b8b1ef7 Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Sun, 1 Jun 2025 20:45:04 -0400 Subject: [PATCH] Add client doc --- docs/clients/auth/bearer.mdx | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 docs/clients/auth/bearer.mdx diff --git a/docs/clients/auth/bearer.mdx b/docs/clients/auth/bearer.mdx new file mode 100644 index 000000000..656bd9ee7 --- /dev/null +++ b/docs/clients/auth/bearer.mdx @@ -0,0 +1,88 @@ +--- +title: Bearer Authentication +sidebarTitle: Bearer Auth +description: Authenticate your FastMCP client using pre-existing OAuth 2.0 Bearer tokens. +icon: key +--- + +import { VersionBadge } from "/snippets/version-badge.mdx" + + + +You can configure your FastMCP client to use **bearer authentication** by supplying a valid access token. This is most appropriate for service accounts, long-lived API keys, CI/CD, applications where authentication is managed separately, or other non-interactive authentication methods. + + +A Bearer token is a JSON Web Token (JWT) that is used to authenticate a request. It is most commonly used in the `Authorization` header of an HTTP request, using the `Bearer` scheme: + +```http +Authorization: Bearer +``` + + +## Client Usage + +The most straightforward way to use a pre-existing Bearer token is to provide it as a string to the `auth` parameter of the `fastmcp.Client` or transport instance. FastMCP will automatically format it correctly for the `Authorization` header and bearer scheme. + + +If you're using a string token, do not include the `Bearer` prefix. FastMCP will add it for you. + + +```python {6} +from fastmcp import Client + +async def main(): + async with Client( + 'https://fastmcp.cloud/mcp', + auth="", + ) as client: + await client.ping() +``` + +You can also supply a Bearer token to a transport instance, such as `StreamableHttpTransport` or `SSETransport`: + +```python {6} +from fastmcp import Client +from fastmcp.client.transports import StreamableHttpTransport + +transport = StreamableHttpTransport( + "http://fastmcp.cloud/mcp", + auth="", +) + +async def main(): + async with Client(transport) as client: + await client.ping() +``` + +## `BearerAuth` Helper + +If you prefer to be more explicit and not rely on FastMCP to transform your string token, you can use the `BearerAuth` class yourself, which implements the `httpx.Auth` interface. + +```python {7} +from fastmcp import Client +from fastmcp.client.auth import BearerAuth + +async def main(): + async with Client( + "https://fastmcp.cloud/mcp", + auth=BearerAuth(token=""), + ) as client: + await client.ping() +``` + +## Custom Headers + +If the MCP server expects a custom header or token scheme, you can manually set the client's `headers` instead of using the `auth` parameter: + +```python {6-8} +from fastmcp import Client + +async def main(): + async with Client( + "https://fastmcp.cloud/mcp", + headers={ + 'X-API-Key': '', + }, + ) as client: + await client.ping() +```