Skip to content

Commit de97d8a

Browse files
fix: Disable audience validation by default in Keycloak example
Keycloak tokens don't include 'aud' claim by default. Changed example to disable audience validation for development (audience=None). Production deployments should configure Keycloak audience mappers and set the FASTMCP_SERVER_AUTH_KEYCLOAK_AUDIENCE environment variable.
1 parent 8506312 commit de97d8a

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

docs/integrations/keycloak.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ If you prefer using Docker Compose instead, you may want to have a look at the [
111111
### Step 2: FastMCP Configuration
112112

113113
<Warning>
114-
**Security Best Practice**: Always configure the `audience` parameter in production environments. Without audience validation, your server will accept tokens issued for *any* audience, including tokens meant for completely different services. Set `audience` to your resource server identifier (typically your server's base URL) to ensure tokens are specifically intended for your server.
114+
**Security Best Practice**: For production environments, always configure the `audience` parameter. Without audience validation, your server will accept tokens issued for *any* audience, including tokens meant for completely different services.
115+
116+
**Important**: Keycloak doesn't include the `aud` claim in tokens by default. For the example below to work out-of-the-box, audience validation is disabled. For production, configure Keycloak audience mappers and set `audience` to your resource server identifier (typically your server's base URL) to ensure tokens are specifically intended for your server.
115117
</Warning>
116118

117119
Create your FastMCP server file and use the KeycloakAuthProvider to handle all the OAuth integration automatically:
@@ -127,7 +129,7 @@ auth_provider = KeycloakAuthProvider(
127129
realm_url="http://localhost:8080/realms/fastmcp", # Your Keycloak realm URL
128130
base_url="http://localhost:8000", # Your server's public URL
129131
required_scopes=["openid", "profile"], # Required OAuth scopes
130-
audience="http://localhost:8000", # Recommended: validate token audience
132+
# audience="http://localhost:8000", # For production: configure Keycloak audience mappers first
131133
)
132134

133135
# Create FastMCP server with auth

examples/auth/keycloak_auth/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Manually import the realm:
3838
```bash
3939
export FASTMCP_SERVER_AUTH_KEYCLOAK_REALM_URL="http://localhost:8080/realms/fastmcp"
4040
export FASTMCP_SERVER_AUTH_KEYCLOAK_BASE_URL="http://localhost:8000"
41-
# Optional: Set audience for token validation (defaults to base_url if not set)
41+
# Optional: Set audience for token validation (disabled by default)
42+
# For production, configure Keycloak audience mappers first, then uncomment:
4243
# export FASTMCP_SERVER_AUTH_KEYCLOAK_AUDIENCE="http://localhost:8000"
4344
```
4445

examples/auth/keycloak_auth/server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
Optional environment variables:
1010
- FASTMCP_SERVER_AUTH_KEYCLOAK_REQUIRED_SCOPES: Required OAuth scopes (default: "openid,profile")
11-
- FASTMCP_SERVER_AUTH_KEYCLOAK_AUDIENCE: Audience for JWT validation (default: base_url)
11+
- FASTMCP_SERVER_AUTH_KEYCLOAK_AUDIENCE: Audience for JWT validation (default: None for development)
1212
1313
To run:
1414
python server.py
@@ -36,13 +36,15 @@
3636
required_scopes = os.getenv(
3737
"FASTMCP_SERVER_AUTH_KEYCLOAK_REQUIRED_SCOPES", "openid,profile"
3838
)
39-
audience = os.getenv("FASTMCP_SERVER_AUTH_KEYCLOAK_AUDIENCE", base_url)
39+
# Note: Audience validation is disabled by default for this development example.
40+
# For production, configure Keycloak audience mappers and set FASTMCP_SERVER_AUTH_KEYCLOAK_AUDIENCE
41+
audience = os.getenv("FASTMCP_SERVER_AUTH_KEYCLOAK_AUDIENCE")
4042

4143
auth = KeycloakAuthProvider(
4244
realm_url=realm_url,
4345
base_url=base_url,
4446
required_scopes=required_scopes,
45-
audience=audience, # Validate token audience for security
47+
audience=audience, # None by default for development
4648
)
4749

4850
mcp = FastMCP("Keycloak OAuth Example Server", auth=auth)

0 commit comments

Comments
 (0)