Repository navigation
feat(mcp): wire up oauth2 token verification for streamable-http transport #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Piotr1215
wants to merge
4
commits into
redis:main
Choose a base branch
from
Piotr1215:feat/mcp-oauth-token-verifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1f5dc32
feat(mcp): wire up oauth2 token verification for streamable-http tran…
Piotr1215 c38dedd
Merge upstream/main into feat/mcp-oauth-token-verifier
Piotr1215 9649e6b
ci: retrigger flaky search index test
Piotr1215 d77fa9f
fix(mcp): correct oauth discovery and verification
Piotr1215 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.