Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
20a8c4e
1
Parent(s): ba020b8
Add client doc
Browse files- docs/clients/auth/bearer.mdx +88 -0
docs/clients/auth/bearer.mdx
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Bearer Authentication
|
| 3 |
+
sidebarTitle: Bearer Auth
|
| 4 |
+
description: Authenticate your FastMCP client using pre-existing OAuth 2.0 Bearer tokens.
|
| 5 |
+
icon: key
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
import { VersionBadge } from "/snippets/version-badge.mdx"
|
| 9 |
+
|
| 10 |
+
<VersionBadge version="2.6.0" />
|
| 11 |
+
|
| 12 |
+
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.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
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:
|
| 16 |
+
|
| 17 |
+
```http
|
| 18 |
+
Authorization: Bearer <token>
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
## Client Usage
|
| 23 |
+
|
| 24 |
+
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.
|
| 25 |
+
|
| 26 |
+
<Tip>
|
| 27 |
+
If you're using a string token, do not include the `Bearer` prefix. FastMCP will add it for you.
|
| 28 |
+
</Tip>
|
| 29 |
+
|
| 30 |
+
```python {6}
|
| 31 |
+
from fastmcp import Client
|
| 32 |
+
|
| 33 |
+
async def main():
|
| 34 |
+
async with Client(
|
| 35 |
+
'https://fastmcp.cloud/mcp',
|
| 36 |
+
auth="<your-token>",
|
| 37 |
+
) as client:
|
| 38 |
+
await client.ping()
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
You can also supply a Bearer token to a transport instance, such as `StreamableHttpTransport` or `SSETransport`:
|
| 42 |
+
|
| 43 |
+
```python {6}
|
| 44 |
+
from fastmcp import Client
|
| 45 |
+
from fastmcp.client.transports import StreamableHttpTransport
|
| 46 |
+
|
| 47 |
+
transport = StreamableHttpTransport(
|
| 48 |
+
"http://fastmcp.cloud/mcp",
|
| 49 |
+
auth="<your-token>",
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
async def main():
|
| 53 |
+
async with Client(transport) as client:
|
| 54 |
+
await client.ping()
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
## `BearerAuth` Helper
|
| 58 |
+
|
| 59 |
+
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.
|
| 60 |
+
|
| 61 |
+
```python {7}
|
| 62 |
+
from fastmcp import Client
|
| 63 |
+
from fastmcp.client.auth import BearerAuth
|
| 64 |
+
|
| 65 |
+
async def main():
|
| 66 |
+
async with Client(
|
| 67 |
+
"https://fastmcp.cloud/mcp",
|
| 68 |
+
auth=BearerAuth(token="<your-token>"),
|
| 69 |
+
) as client:
|
| 70 |
+
await client.ping()
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
## Custom Headers
|
| 74 |
+
|
| 75 |
+
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:
|
| 76 |
+
|
| 77 |
+
```python {6-8}
|
| 78 |
+
from fastmcp import Client
|
| 79 |
+
|
| 80 |
+
async def main():
|
| 81 |
+
async with Client(
|
| 82 |
+
"https://fastmcp.cloud/mcp",
|
| 83 |
+
headers={
|
| 84 |
+
'X-API-Key': '<your-token>',
|
| 85 |
+
},
|
| 86 |
+
) as client:
|
| 87 |
+
await client.ping()
|
| 88 |
+
```
|