-
Notifications
You must be signed in to change notification settings - Fork 0
Pass external API tokens to MCP tools via X-External-Token header using ADK session #21
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4fcc1e7
Initial plan
Copilot aec4d84
Implement X-External-Token header passing to MCP tools
Copilot 1739724
Fix linting and type checking issues
Copilot ca6dd0f
Add documentation for external API token passing feature
Copilot 279b4ed
Address code review feedback
Copilot 23ddb1e
Reimplement token passing using ADK session and hooks
Copilot 20bc6e8
Fix linting and type checking issues
Copilot 1d0fec4
Address code review feedback
Copilot 0fcd4f5
Remove unused type: ignore comments
Copilot 34e5cc2
Fix mypy type checking errors
Copilot 71512aa
Fix bandit security linting false positives
Copilot a6178e2
Merge main branch and resolve conflicts
Copilot d2992a2
Add integration test for external token passing to MCP tools
Copilot 1fca353
Address code review feedback on external token implementation
Copilot 78a2f88
Use ADK's proper session state update mechanism
Copilot bcde875
Simplify header retrieval and add sub-agent token injection
Copilot f82d56a
Remove non-functional sub-agent token injection code
Copilot 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,4 @@ | ||
| """Constants shared across the agenticlayer package.""" | ||
|
|
||
| # Key used to store the external token in the ADK session state | ||
| EXTERNAL_TOKEN_SESSION_KEY = "__external_token__" # nosec B105 |
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,68 @@ | ||
| """Tests for external token passing to MCP tools via ADK session.""" | ||
|
|
||
| from agenticlayer.agent import _get_mcp_headers_from_session | ||
| from agenticlayer.constants import EXTERNAL_TOKEN_SESSION_KEY | ||
| from google.adk.sessions.session import Session | ||
|
|
||
|
|
||
| def test_header_provider_retrieves_token_from_session() -> None: | ||
| """Test that the header provider function can retrieve token from session state.""" | ||
| # Given: A session with an external token stored | ||
| test_token = "test-api-token-xyz" # nosec B105 | ||
| session = Session( | ||
| id="test-session", | ||
| app_name="test-app", | ||
| user_id="test-user", | ||
| state={EXTERNAL_TOKEN_SESSION_KEY: test_token}, | ||
| events=[], | ||
| last_update_time=0.0, | ||
| ) | ||
|
|
||
| # Create a mock readonly context | ||
| class MockReadonlyContext: | ||
| def __init__(self, session: Session) -> None: | ||
| self.session = session | ||
|
|
||
| readonly_context = MockReadonlyContext(session) | ||
|
|
||
| # When: Calling the header provider function | ||
| headers = _get_mcp_headers_from_session(readonly_context) # type: ignore[arg-type] | ||
|
|
||
| # Then: The headers should include the X-External-Token | ||
| assert headers == {"X-External-Token": test_token} | ||
|
|
||
|
|
||
| def test_header_provider_returns_empty_when_no_token() -> None: | ||
| """Test that the header provider returns empty dict when no token is present.""" | ||
| # Given: A session without an external token | ||
| session = Session( | ||
| id="test-session", | ||
| app_name="test-app", | ||
| user_id="test-user", | ||
| state={}, # No token | ||
| events=[], | ||
| last_update_time=0.0, | ||
| ) | ||
|
|
||
| # Create a mock readonly context | ||
| class MockReadonlyContext: | ||
| def __init__(self, session: Session) -> None: | ||
| self.session = session | ||
|
|
||
| readonly_context = MockReadonlyContext(session) | ||
|
|
||
| # When: Calling the header provider function | ||
| headers = _get_mcp_headers_from_session(readonly_context) # type: ignore[arg-type] | ||
|
|
||
| # Then: The headers should be empty | ||
| assert headers == {} | ||
|
|
||
|
|
||
| def test_header_provider_handles_none_context() -> None: | ||
| """Test that the header provider safely handles None context.""" | ||
| # When: Calling the header provider with None | ||
| headers = _get_mcp_headers_from_session(None) # type: ignore[arg-type] | ||
|
|
||
| # Then: The headers should be empty (no exception) | ||
| assert headers == {} | ||
|
|
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.