From 1f5dc326440b4444d6a1d4abcfcc1d29c2bac967 Mon Sep 17 00:00:00 2001 From: Piotr Zaniewski Date: Thu, 16 Apr 2026 17:58:19 +0200 Subject: [PATCH 1/3] feat(mcp): wire up oauth2 token verification for streamable-http transport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the mcp sdk (v1.26.0+) has built-in oauth 2.1 support via AuthSettings and TokenVerifier, but the agent-memory-server never wired it up for the streamable-http transport. without it, unauthenticated requests to /mcp return 406 instead of 401 + WWW-Authenticate header, which prevents mcp clients (like claude) from discovering the oauth flow. changes: - add JWTTokenVerifier that implements the sdk's TokenVerifier protocol by wrapping the existing verify_jwt() function - conditionally pass AuthSettings + token_verifier to FastMCP when AUTH_MODE=oauth2 and OAUTH2_RESOURCE_HOST are set - add /.well-known/oauth-authorization-server endpoint (RFC 9728) to the REST API for protected resource metadata discovery - add OAUTH2_RESOURCE_HOST config setting for the server's public hostname when enabled, the mcp transport now: - returns 401 with WWW-Authenticate: Bearer resource_metadata="..." header - serves /.well-known/oauth-protected-resource automatically (sdk built-in) - validates jwt tokens on all mcp requests via jwks when AUTH_MODE != oauth2 or OAUTH2_RESOURCE_HOST is unset, behavior is unchanged — no auth kwargs are passed to FastMCP. tested with ory hydra as the oidc provider and claude as the mcp client. --- agent_memory_server/config.py | 1 + agent_memory_server/main.py | 11 ++++++ agent_memory_server/mcp.py | 72 +++++++++++++++++++++++++---------- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/agent_memory_server/config.py b/agent_memory_server/config.py index 95464d01..5af59abd 100644 --- a/agent_memory_server/config.py +++ b/agent_memory_server/config.py @@ -427,6 +427,7 @@ class Settings(BaseSettings): oauth2_audience: str | None = None oauth2_jwks_url: str | None = None oauth2_algorithms: list[str] = ["RS256"] + oauth2_resource_host: str | None = None # Token Authentication settings token_auth_enabled: bool = False diff --git a/agent_memory_server/main.py b/agent_memory_server/main.py index b1b3cb51..163595e9 100644 --- a/agent_memory_server/main.py +++ b/agent_memory_server/main.py @@ -130,6 +130,17 @@ async def lifespan(app: FastAPI): app.include_router(memory_router) +@app.get("/.well-known/oauth-authorization-server") +async def oauth_authorization_server_metadata(): + """RFC 9728 Protected Resource Metadata for MCP OIDC discovery.""" + if not settings.oauth2_issuer_url: + return {"error": "OIDC not configured"} + return { + "resource": f"https://{settings.oauth2_resource_host or 'localhost'}", + "authorization_servers": [settings.oauth2_issuer_url], + } + + def on_start_logger(port: int): """Log startup information""" print("\n-----------------------------------") diff --git a/agent_memory_server/mcp.py b/agent_memory_server/mcp.py index 5bf55e04..e416a257 100644 --- a/agent_memory_server/mcp.py +++ b/agent_memory_server/mcp.py @@ -3,6 +3,8 @@ from typing import Any import ulid +from mcp.server.auth.provider import AccessToken, TokenVerifier +from mcp.server.auth.settings import AuthSettings from mcp.server.fastmcp import FastMCP as _FastMCPBase from agent_memory_server import working_memory as working_memory_core @@ -179,22 +181,16 @@ async def run_sse_async(self): ).serve() def streamable_http_app(self): - """Return a Starlette app for streamable-http with namespace routing.""" - from mcp.server.streamable_http_manager import StreamableHTTPSessionManager - from starlette.applications import Starlette + """Return a Starlette app for streamable-http with namespace routing. + + Extends the parent's streamable_http_app() by adding a namespace-prefixed + route while preserving auth middleware and protected resource routes. + """ from starlette.requests import Request from starlette.routing import Route - if self._session_manager is None: - self._session_manager = StreamableHTTPSessionManager( - app=self._mcp_server, - event_store=self._event_store, - retry_interval=self._retry_interval, - json_response=self.settings.json_response, - stateless=self.settings.stateless_http, - security_settings=self.settings.transport_security, - ) - + # Get the parent's app which includes auth middleware and well-known routes + app = super().streamable_http_app() session_manager = self._session_manager mcp_instance = self @@ -212,14 +208,9 @@ async def __call__(self, scope, receive, send): handler = _NamespaceAwareHandler() path = self.settings.streamable_http_path - return Starlette( - debug=self.settings.debug, - routes=[ - Route(path, endpoint=handler), - Route(f"/{{namespace}}{path}", endpoint=handler), - ], - lifespan=lambda app: session_manager.run(), - ) + # Add namespace-prefixed route to the existing app + app.routes.append(Route(f"/{{namespace}}{path}", endpoint=handler)) + return app async def run_streamable_http_async(self): """Start streamable HTTP MCP server.""" @@ -255,12 +246,51 @@ async def run_stdio_async(self): """ +class JWTTokenVerifier(TokenVerifier): + """Verify JWT tokens from Hydra using the existing auth module.""" + + async def verify_token(self, token: str) -> AccessToken | None: + from agent_memory_server.auth import verify_jwt + + try: + user_info = verify_jwt(token) + scopes = user_info.scope.split() if user_info.scope else [] + return AccessToken( + token=token, + client_id=user_info.sub, + scopes=scopes, + expires_at=user_info.exp, + ) + except Exception: + return None + + +def _build_mcp_auth_kwargs() -> dict[str, Any]: + """Build auth kwargs for FastMCP if OAuth2 is configured.""" + if ( + settings.auth_mode != "oauth2" + or not settings.oauth2_issuer_url + or not settings.oauth2_resource_host + ): + return {} + + resource_url = f"https://{settings.oauth2_resource_host}" + return { + "auth": AuthSettings( + issuer_url=settings.oauth2_issuer_url, + resource_server_url=resource_url, + ), + "token_verifier": JWTTokenVerifier(), + } + + mcp_app = FastMCP( "Redis Agent Memory Server", host=settings.mcp_host, port=settings.mcp_port, instructions=INSTRUCTIONS, default_namespace=settings.default_mcp_namespace, + **_build_mcp_auth_kwargs(), ) From 9649e6bacedbe2e25b2ba44d7544e0ca7a3a78dc Mon Sep 17 00:00:00 2001 From: Piotr Zaniewski Date: Mon, 9 Nov 2026 20:40:01 +0100 Subject: [PATCH 2/3] ci: retrigger flaky search index test test_list_sessions_returns_indexed_sessions failed on redis:latest only (expected 3 sessions, got 2). The PR does not touch working memory, and a sibling search index test flaked the same way on main in run 31640102621. Claude-Session: https://claude.ai/code/session_017edg9j5gLvdZkp32i7vPVD From d77fa9f19cf0a39ff949e3a4416434bd1441117f Mon Sep 17 00:00:00 2001 From: Piotr Zaniewski Date: Wed, 11 Nov 2026 17:55:00 +0100 Subject: [PATCH 3/3] fix(mcp): correct oauth discovery and verification MCP clients can receive invalid discovery metadata, miss authentication configuration changes, or see service failures reported as invalid tokens. Synchronous key retrieval can also block other requests. --- V0/agent_memory_server/main.py | 17 ++++-- V0/agent_memory_server/mcp.py | 17 ++++-- V0/tests/test_mcp_oauth.py | 101 +++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 V0/tests/test_mcp_oauth.py diff --git a/V0/agent_memory_server/main.py b/V0/agent_memory_server/main.py index 163595e9..a86ff9c4 100644 --- a/V0/agent_memory_server/main.py +++ b/V0/agent_memory_server/main.py @@ -3,7 +3,7 @@ from contextlib import asynccontextmanager import uvicorn -from fastapi import FastAPI +from fastapi import FastAPI, HTTPException from agent_memory_server import __version__ from agent_memory_server.api import router as memory_router @@ -130,13 +130,18 @@ async def lifespan(app: FastAPI): app.include_router(memory_router) -@app.get("/.well-known/oauth-authorization-server") -async def oauth_authorization_server_metadata(): +@app.get("/.well-known/oauth-protected-resource") +async def oauth_protected_resource_metadata(): """RFC 9728 Protected Resource Metadata for MCP OIDC discovery.""" - if not settings.oauth2_issuer_url: - return {"error": "OIDC not configured"} + if ( + settings.disable_auth + or settings.auth_mode != "oauth2" + or not settings.oauth2_issuer_url + or not settings.oauth2_resource_host + ): + raise HTTPException(status_code=404, detail="OIDC not configured") return { - "resource": f"https://{settings.oauth2_resource_host or 'localhost'}", + "resource": f"https://{settings.oauth2_resource_host}", "authorization_servers": [settings.oauth2_issuer_url], } diff --git a/V0/agent_memory_server/mcp.py b/V0/agent_memory_server/mcp.py index 17c2a444..1a333f28 100644 --- a/V0/agent_memory_server/mcp.py +++ b/V0/agent_memory_server/mcp.py @@ -1,8 +1,10 @@ +import asyncio import logging from datetime import datetime from typing import Any import ulid +from fastapi import HTTPException from mcp.server.auth.provider import AccessToken, TokenVerifier from mcp.server.auth.settings import AuthSettings from mcp.server.fastmcp import FastMCP as _FastMCPBase @@ -17,6 +19,7 @@ search_long_term_memory as core_search_long_term_memory, update_long_term_memory as core_update_long_term_memory, ) +from agent_memory_server.auth import verify_jwt from agent_memory_server.config import settings from agent_memory_server.dependencies import get_background_tasks from agent_memory_server.filters import ( @@ -251,10 +254,8 @@ class JWTTokenVerifier(TokenVerifier): """Verify JWT tokens from Hydra using the existing auth module.""" async def verify_token(self, token: str) -> AccessToken | None: - from agent_memory_server.auth import verify_jwt - try: - user_info = verify_jwt(token) + user_info = await asyncio.to_thread(verify_jwt, token) scopes = user_info.scope.split() if user_info.scope else [] return AccessToken( token=token, @@ -262,14 +263,18 @@ async def verify_token(self, token: str) -> AccessToken | None: scopes=scopes, expires_at=user_info.exp, ) - except Exception: - return None + except HTTPException as exc: + if exc.status_code == 401: + logger.info("JWT verification rejected token: %s", exc.detail) + return None + raise def _build_mcp_auth_kwargs() -> dict[str, Any]: """Build auth kwargs for FastMCP if OAuth2 is configured.""" if ( - settings.auth_mode != "oauth2" + settings.disable_auth + or settings.auth_mode != "oauth2" or not settings.oauth2_issuer_url or not settings.oauth2_resource_host ): diff --git a/V0/tests/test_mcp_oauth.py b/V0/tests/test_mcp_oauth.py new file mode 100644 index 00000000..445e759c --- /dev/null +++ b/V0/tests/test_mcp_oauth.py @@ -0,0 +1,101 @@ +import threading +from unittest.mock import patch + +import httpx +import pytest +from fastapi import HTTPException + +from agent_memory_server.auth import UserInfo +from agent_memory_server.config import settings +from agent_memory_server.main import app +from agent_memory_server.mcp import JWTTokenVerifier, _build_mcp_auth_kwargs + + +@pytest.fixture +def oauth_settings(monkeypatch): + monkeypatch.setattr(settings, "disable_auth", False) + monkeypatch.setattr(settings, "auth_mode", "oauth2") + monkeypatch.setattr(settings, "oauth2_issuer_url", "https://issuer.example") + monkeypatch.setattr(settings, "oauth2_resource_host", "memory.example") + + +@pytest.mark.asyncio +async def test_resource_metadata(oauth_settings): + async with httpx.AsyncClient( + transport=httpx.ASGITransport(app=app), base_url="http://test" + ) as client: + response = await client.get("/.well-known/oauth-protected-resource") + assert response.status_code == 200 + assert response.json() == { + "resource": "https://memory.example", + "authorization_servers": ["https://issuer.example"], + } + assert ( + await client.get("/.well-known/oauth-authorization-server") + ).status_code == 404 + + +@pytest.mark.parametrize( + ("field", "value"), + [ + ("disable_auth", True), + ("auth_mode", "disabled"), + ("auth_mode", "token"), + ("oauth2_issuer_url", None), + ("oauth2_resource_host", None), + ], +) +@pytest.mark.asyncio +async def test_metadata_and_mcp_auth_disabled_together( + oauth_settings, monkeypatch, field, value +): + monkeypatch.setattr(settings, field, value) + assert _build_mcp_auth_kwargs() == {} + async with httpx.AsyncClient( + transport=httpx.ASGITransport(app=app), base_url="http://test" + ) as client: + assert ( + await client.get("/.well-known/oauth-protected-resource") + ).status_code == 404 + + +@pytest.mark.asyncio +async def test_verifier_runs_in_thread_and_returns_claims(oauth_settings): + event_loop_thread = threading.get_ident() + + def verify(token): + assert threading.get_ident() != event_loop_thread + return UserInfo(sub="client", scope="read write", exp=123) + + with patch("agent_memory_server.mcp.verify_jwt", side_effect=verify): + result = await JWTTokenVerifier().verify_token("token") + assert result.client_id == "client" + assert result.scopes == ["read", "write"] + assert result.expires_at == 123 + assert _build_mcp_auth_kwargs()["token_verifier"] is not None + + +@pytest.mark.parametrize("status", [401, 500, 503]) +@pytest.mark.asyncio +async def test_verifier_only_swallows_auth_failures(status): + with patch( + "agent_memory_server.mcp.verify_jwt", + side_effect=HTTPException(status_code=status), + ): + if status == 401: + assert await JWTTokenVerifier().verify_token("invalid") is None + else: + with pytest.raises(HTTPException) as error: + await JWTTokenVerifier().verify_token("token") + assert error.value.status_code == status + + +@pytest.mark.asyncio +async def test_verifier_propagates_unexpected_errors(): + with ( + patch( + "agent_memory_server.mcp.verify_jwt", side_effect=RuntimeError("failure") + ), + pytest.raises(RuntimeError, match="failure"), + ): + await JWTTokenVerifier().verify_token("token")