Skip to content

Commit 8506312

Browse files
refactor: Remove manual cache clearing from Keycloak OAuth example
OAuth client now automatically handles stale credentials and re-registers with Keycloak when needed. Manual cache clearing is no longer required.
1 parent 443a12d commit 8506312

File tree

3 files changed

+8
-47
lines changed

3 files changed

+8
-47
lines changed

docs/integrations/keycloak.mdx

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,28 +181,13 @@ When you run the client for the first time:
181181
The client caches tokens locally, so you won't need to re-authenticate for subsequent runs unless the token expires or you explicitly clear the cache.
182182
</Info>
183183

184-
### Troubleshooting: "Client not found" Error
184+
### Automatic Client Re-registration
185185

186-
<Warning>
187-
If you restart Keycloak or change the realm configuration, you may end up seeing Keycloak showing a "Client not found" error instead of the login screen when running your client. This happens because FastMCP uses Dynamic Client Registration (DCR) and the client ID that was cached locally no longer exists on the Keycloak server.
188-
189-
**Keycloak error**: "We are sorry... Client not found."
190-
191-
**Solution**: Clear the local OAuth cache to force re-registration with Keycloak:
192-
193-
```python
194-
from fastmcp.client.auth.oauth import FileTokenStorage
195-
196-
# Clear OAuth cache for your specific MCP server
197-
storage = FileTokenStorage("http://localhost:8000/mcp/") # Use your MCP server URL
198-
storage.clear()
199-
200-
# Or clear all OAuth cache data for all MCP servers
201-
FileTokenStorage.clear_all()
202-
```
186+
<Info>
187+
If you restart Keycloak or change the realm configuration, your FastMCP client will automatically detect if the cached OAuth client credentials are no longer valid and will re-register with Keycloak automatically. You don't need to manually clear any caches - just run your client again and it will handle the re-registration process seamlessly.
203188

204-
After clearing the cache, run your client again. It will automatically re-register with Keycloak and obtain new credentials.
205-
</Warning>
189+
This automatic retry mechanism ensures a smooth developer experience when working with Dynamic Client Registration (DCR).
190+
</Info>
206191

207192
## Environment Variables
208193

examples/auth/keycloak_auth/client.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,11 @@
99
import asyncio
1010

1111
from fastmcp import Client
12-
from fastmcp.client.auth.oauth import FileTokenStorage
1312

1413
SERVER_URL = "http://localhost:8000/mcp"
1514

16-
# Set to True to clear any previously stored tokens.
17-
# This is useful if you have just restarted Keycloak and end up seeing
18-
# Keycloak showing this error: "We are sorry... Client not found."
19-
# instead of the login screen
20-
CLEAR_TOKEN_CACHE = False
21-
2215

2316
async def main():
24-
if CLEAR_TOKEN_CACHE:
25-
storage = FileTokenStorage(f"{SERVER_URL.rstrip('/')}/")
26-
storage.clear()
27-
print("🧹 Cleared cached OAuth tokens.")
28-
2917
try:
3018
async with Client(SERVER_URL, auth="oauth") as client:
3119
assert await client.ping()

examples/auth/keycloak_auth/keycloak/README.md

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,6 @@ docker-compose logs -f keycloak
114114
- Review client registration policies in the admin console
115115

116116
4. **"Client not found" error after Keycloak restart**
117-
- This happens because FastMCP uses Dynamic Client Registration (DCR) and the client ID that was cached locally no longer exists on the Keycloak server after restart
118-
- **Solution**: Clear the local OAuth cache to force re-registration:
119-
120-
```python
121-
from fastmcp.client.auth.oauth import FileTokenStorage
122-
123-
# Clear OAuth cache for your specific MCP server
124-
storage = FileTokenStorage("http://localhost:8000/mcp/")
125-
storage.clear()
126-
127-
# Or clear all OAuth cache data
128-
FileTokenStorage.clear_all()
129-
```
130-
131-
- After clearing the cache, run your client again to automatically re-register with Keycloak
117+
- This can happen when Keycloak is restarted and the previously registered OAuth client no longer exists
118+
- **No action needed**: The FastMCP OAuth client automatically detects this condition and re-registers with Keycloak
119+
- Simply run your client again and it will automatically handle the re-registration process

0 commit comments

Comments
 (0)