Skip to content

Commit adaa55d

Browse files
authored
fix(auth-for-mcp): prevent caching of PRM endpoints in python examples (#68)
1 parent 6ca8a9d commit adaa55d

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

auth-for-mcp/fastmcp-mcp-customtokenexchange-python/src/auth0/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from mcp.server.auth.routes import create_protected_resource_routes
1414
from mcp.server.fastmcp import FastMCP
1515
from starlette.middleware import Middleware
16+
from starlette.middleware.base import BaseHTTPMiddleware
1617
from starlette.requests import Request
1718
from starlette.responses import JSONResponse, Response
1819
from starlette.routing import Route, Router
@@ -80,7 +81,14 @@ def auth_metadata_router(self) -> Router:
8081
resource_name=self.name,
8182
)
8283

83-
return Router(routes=routes)
84+
# Middleware to override cache headers
85+
class NoCacheMiddleware(BaseHTTPMiddleware):
86+
async def dispatch(self, request: Request, call_next: Callable) -> Response:
87+
response = await call_next(request)
88+
response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
89+
return response
90+
91+
return Router(routes=routes, middleware=[Middleware(NoCacheMiddleware)])
8492

8593
def auth_middleware(self) -> list[Middleware]:
8694
return [Middleware(

auth-for-mcp/fastmcp-mcp-customtokenexchange-python/src/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def lifespan(app: Starlette) -> AsyncIterator[None]:
3838
debug=config.debug,
3939
routes=[
4040
# Add discovery metadata route
41-
*auth0_mcp.auth_metadata_router().routes,
41+
Mount("/", app=auth0_mcp.auth_metadata_router()),
4242

4343
# Main MCP app route with authentication middleware
4444
Mount(

auth-for-mcp/fastmcp-mcp-python/src/auth0/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from mcp.server.auth.routes import create_protected_resource_routes
1414
from mcp.server.fastmcp import FastMCP
1515
from starlette.middleware import Middleware
16+
from starlette.middleware.base import BaseHTTPMiddleware
1617
from starlette.requests import Request
1718
from starlette.responses import JSONResponse, Response
1819
from starlette.routing import Route, Router
@@ -67,7 +68,14 @@ def auth_metadata_router(self) -> Router:
6768
resource_name=self.name,
6869
)
6970

70-
return Router(routes=routes)
71+
# Middleware to override cache headers
72+
class NoCacheMiddleware(BaseHTTPMiddleware):
73+
async def dispatch(self, request: Request, call_next: Callable) -> Response:
74+
response = await call_next(request)
75+
response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
76+
return response
77+
78+
return Router(routes=routes, middleware=[Middleware(NoCacheMiddleware)])
7179

7280
def auth_middleware(self) -> list[Middleware]:
7381
return [Middleware(Auth0Middleware, domain=self.domain, audience=self.audience)]

auth-for-mcp/fastmcp-mcp-python/src/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def lifespan(app: Starlette) -> AsyncIterator[None]:
3636
debug=config.debug,
3737
routes=[
3838
# Add discovery metadata route
39-
*auth0_mcp.auth_metadata_router().routes,
39+
Mount("/", app=auth0_mcp.auth_metadata_router()),
4040

4141
# Main MCP app route with authentication middleware
4242
Mount(

0 commit comments

Comments
 (0)