diff --git a/V0/agent_memory_server/config.py b/V0/agent_memory_server/config.py index 95464d01..5af59abd 100644 --- a/V0/agent_memory_server/config.py +++ b/V0/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/V0/agent_memory_server/main.py b/V0/agent_memory_server/main.py index b1b3cb51..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,6 +130,22 @@ async def lifespan(app: FastAPI): app.include_router(memory_router) +@app.get("/.well-known/oauth-protected-resource") +async def oauth_protected_resource_metadata(): + """RFC 9728 Protected Resource Metadata for MCP OIDC discovery.""" + 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}", + "authorization_servers": [settings.oauth2_issuer_url], + } + + def on_start_logger(port: int): """Log startup information""" print("\n-----------------------------------") diff --git a/V0/agent_memory_server/mcp.py b/V0/agent_memory_server/mcp.py index 10e7c084..1a333f28 100644 --- a/V0/agent_memory_server/mcp.py +++ b/V0/agent_memory_server/mcp.py @@ -1,8 +1,12 @@ +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 from agent_memory_server import working_memory as working_memory_core @@ -15,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 ( @@ -180,22 +185,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 @@ -213,14 +212,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.""" @@ -256,12 +250,53 @@ 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: + try: + user_info = await asyncio.to_thread(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 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.disable_auth + or 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(), ) 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")