Repository navigation
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR wires OAuth2 token verification into the MCP streamable-http transport so MCP clients receive proper 401 + WWW-Authenticate challenges and can complete OIDC discovery/login flows, aligning MCP auth behavior with the server’s existing JWT verification logic.
Changes:
- Adds an MCP SDK
TokenVerifierimplementation that wraps the server’s existingverify_jwt()logic. - Conditionally enables MCP SDK auth middleware via
AuthSettingswhen OAuth2 is configured. - Adds a well-known metadata endpoint and a new
OAUTH2_RESOURCE_HOSTsetting for constructing resource URLs.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| agent_memory_server/mcp.py | Adds JWTTokenVerifier and conditional auth wiring into FastMCP initialization. |
| agent_memory_server/main.py | Adds a well-known OAuth discovery/metadata endpoint for clients. |
| agent_memory_server/config.py | Introduces oauth2_resource_host configuration used for resource URL construction. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…sport 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.
Piotr1215
force-pushed
the
feat/mcp-oauth-token-verifier
branch
from
April 16, 2026 16:05
2e0b5c7 to
1f5dc32
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 4 total unresolved issues (including 3 from previous reviews).
Reviewed by Cursor Bugbot for commit 1f5dc32. Configure here.
Upstream redis#319 moved the project into V0/, so the PR's edits to agent_memory_server/{config,main,mcp}.py conflicted on GitHub. Git rename detection carries them to V0/agent_memory_server/. The only other upstream change to these files since the branch point is the summary extraction fix in mcp.py (redis#316), which does not overlap the auth changes; both are kept. Claude-Session: https://claude.ai/code/session_017edg9j5gLvdZkp32i7vPVD
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
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
The MCP SDK (v1.26.0+) has built-in OAuth 2.1 support via
AuthSettings,TokenVerifier, andRequireAuthMiddleware. The agent-memory-server's existingauth.pyhandles JWT validation for the REST API (/v1/*endpoints), but the MCP streamable-http transport (/mcpendpoint) never wires this up to the SDK's auth layer.When an MCP client (e.g., Claude) connects to
/mcpwithout a Bearer token, the transport returns a406 Not Acceptableerror instead of401with aWWW-Authenticateheader. Without that header, clients cannot discover the OAuth authorization server and the login flow never triggers.The REST API and MCP transport are two separate ASGI apps — the FastAPI auth middleware on the REST side does not protect the MCP side.
Fix
Three small changes:
1.
JWTTokenVerifierinmcp.pyImplements the SDK's
TokenVerifierprotocol by wrapping the existingverify_jwt()function fromauth.py. ConvertsUserInfo(the REST API's auth model) toAccessToken(the SDK's auth model).2. Conditional
AuthSettingsinmcp.py_build_mcp_auth_kwargs()returnsauth+token_verifierkwargs forFastMCPonly whenAUTH_MODE=oauth2andOAUTH2_RESOURCE_HOSTare set. When disabled, no kwargs are passed and behavior is unchanged.When enabled, the SDK's built-in middleware:
401withWWW-Authenticate: Bearer resource_metadata="..."for unauthenticated requests/.well-known/oauth-protected-resourceautomatically3.
/.well-known/oauth-authorization-serverendpoint inmain.pyRFC 9728 Protected Resource Metadata endpoint on the REST API side. Returns the resource server URL and authorization server URL so clients can discover where to authenticate.
4.
OAUTH2_RESOURCE_HOSTconfig settingNew optional setting for the server's public hostname (e.g.,
agent-memory.loft.rocks). Used to construct theresourcefield in metadata responses.Configuration
When
AUTH_MODEis notoauth2orOAUTH2_RESOURCE_HOSTis unset, the MCP transport runs without auth — same as before this change.Test plan
_build_mcp_auth_kwargs()returns correct settings when configured_build_mcp_auth_kwargs()returns empty dict when unconfiguredcurl -H "Accept: text/event-stream" https://server/mcpreturns 401 +WWW-AuthenticateheaderTested with Ory Hydra as the OIDC provider and Claude as the MCP client.
Note
Medium Risk
Touches authentication and request routing for the MCP transport; misconfiguration or verifier/middleware integration issues could cause unexpected 401/406 behavior or block clients from connecting.
Overview
Wires the MCP
streamable-httptransport into the MCP SDK’s OAuth2 auth layer by conditionally passingAuthSettingsand a newJWTTokenVerifierthat adapts the server’s existingverify_jwt()validation to the SDK’sTokenVerifierinterface.Adds a new
/.well-known/oauth-authorization-serverendpoint on the REST app to publish protected-resource/authorization-server metadata (using newoauth2_resource_hostconfig), and updates the MCPstreamable_http_app()override to extend the parent app so auth middleware and well-known routes are preserved while still supporting namespace-prefixed routing.Reviewed by Cursor Bugbot for commit 1f5dc32. Bugbot is set up for automated code reviews on this repo. Configure here.