generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add gateway auth and multi-gateway support to agent templates #427
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
aidandaly24
merged 6 commits into
aws:feat/gateway-integration
from
aidandaly24:feat/batch-5-agent-gateway-auth
Feb 25, 2026
+511
−64
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5342658
feat: add gateway auth support to agent templates
aidandaly24 faca1a5
feat: add multi-gateway support and fix template rendering
aidandaly24 1e3f58a
fix: update main.py files with gateway conditional imports
aidandaly24 9dfe27b
style: fix formatting
aidandaly24 4cb8d1c
refactor: use mcp-proxy-for-aws for gateway auth, remove AutoGen gate…
aidandaly24 6a1696c
fix: pass AWS region to aws_iam_streamablehttp_client in Strands temp…
aidandaly24 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
251 changes: 226 additions & 25 deletions
251
src/assets/__tests__/__snapshots__/assets.snapshot.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,15 +1,45 @@ | ||
| import os | ||
| import logging | ||
| from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset | ||
| from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| {{#if hasGateway}} | ||
| {{#if (includes gatewayAuthTypes "AWS_IAM")}} | ||
| import httpx | ||
| from mcp_proxy_for_aws.sigv4_helper import SigV4HTTPXAuth, create_aws_session | ||
| {{/if}} | ||
|
|
||
| def get_all_gateway_mcp_toolsets() -> list[MCPToolset]: | ||
| """Returns MCP Toolsets for all configured gateways.""" | ||
| toolsets = [] | ||
| {{#each gatewayProviders}} | ||
| url = os.environ.get("{{envVarName}}") | ||
| if url: | ||
| {{#if (eq authType "AWS_IAM")}} | ||
| session = create_aws_session() | ||
| auth = SigV4HTTPXAuth(session.get_credentials(), "bedrock-agentcore", session.region_name) | ||
| toolsets.append(MCPToolset(connection_params=StreamableHTTPConnectionParams( | ||
| url=url, | ||
| httpx_client_factory=lambda **kwargs: httpx.AsyncClient(auth=auth, **kwargs) | ||
| ))) | ||
| {{else}} | ||
| toolsets.append(MCPToolset(connection_params=StreamableHTTPConnectionParams(url=url))) | ||
| {{/if}} | ||
| else: | ||
| logger.warning("{{envVarName}} not set — {{name}} gateway tools unavailable") | ||
| {{/each}} | ||
| return toolsets | ||
| {{else}} | ||
| # ExaAI provides information about code through web searches, crawling and code context searches through their platform. Requires no authentication | ||
| EXAMPLE_MCP_ENDPOINT = "https://mcp.exa.ai/mcp" | ||
|
|
||
|
|
||
| def get_streamable_http_mcp_client() -> MCPToolset: | ||
| """ | ||
| Returns an MCP Toolset compatible with Google ADK. | ||
| """ | ||
| """Returns an MCP Toolset compatible with Google ADK.""" | ||
| # to use an MCP server that supports bearer authentication, add headers={"Authorization": f"Bearer {access_token}"} | ||
| return MCPToolset( | ||
| connection_params=StreamableHTTPConnectionParams(url=EXAMPLE_MCP_ENDPOINT) | ||
| ) | ||
| {{/if}} |
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
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 |
|---|---|---|
|
|
@@ -2,13 +2,22 @@ | |
| from agents import Agent, Runner, function_tool | ||
| from bedrock_agentcore.runtime import BedrockAgentCoreApp | ||
| from model.load import load_model | ||
| {{#if hasGateway}} | ||
| from mcp_client.client import get_all_gateway_mcp_servers | ||
| {{else}} | ||
| from mcp_client.client import get_streamable_http_mcp_client | ||
| {{/if}} | ||
|
|
||
| app = BedrockAgentCoreApp() | ||
| log = app.logger | ||
|
|
||
| # Get MCP Server | ||
| {{#if hasGateway}} | ||
| mcp_servers = get_all_gateway_mcp_servers() | ||
| {{else}} | ||
| mcp_server = get_streamable_http_mcp_client() | ||
| mcp_servers = [mcp_server] if mcp_server else [] | ||
| {{/if}} | ||
|
|
||
| _credentials_loaded = False | ||
|
|
||
|
|
@@ -30,16 +39,47 @@ def add_numbers(a: int, b: int) -> int: | |
| async def main(query): | ||
| ensure_credentials_loaded() | ||
| try: | ||
| async with mcp_server as server: | ||
| active_servers = [server] if server else [] | ||
| {{#if hasGateway}} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gateway and non-gateway branches duplicate a lot of lines of agent creation. We could factor out the shared logic |
||
| if mcp_servers: | ||
| agent = Agent( | ||
| name="{{ name }}", | ||
| model="gpt-4.1", | ||
| mcp_servers=active_servers, | ||
| mcp_servers=mcp_servers, | ||
| tools=[add_numbers] | ||
| ) | ||
| result = await Runner.run(agent, query) | ||
| return result | ||
| else: | ||
| agent = Agent( | ||
| name="{{ name }}", | ||
| model="gpt-4.1", | ||
| mcp_servers=[], | ||
| tools=[add_numbers] | ||
| ) | ||
| result = await Runner.run(agent, query) | ||
| return result | ||
| {{else}} | ||
| if mcp_servers: | ||
| async with mcp_servers[0] as server: | ||
| active_servers = [server] | ||
| agent = Agent( | ||
| name="{{ name }}", | ||
| model="gpt-4.1", | ||
| mcp_servers=active_servers, | ||
| tools=[add_numbers] | ||
| ) | ||
| result = await Runner.run(agent, query) | ||
| return result | ||
| else: | ||
| agent = Agent( | ||
| name="{{ name }}", | ||
| model="gpt-4.1", | ||
| mcp_servers=[], | ||
| tools=[add_numbers] | ||
| ) | ||
| result = await Runner.run(agent, query) | ||
| return result | ||
| {{/if}} | ||
| except Exception as e: | ||
| log.error(f"Error during agent execution: {e}", exc_info=True) | ||
| raise e | ||
|
|
||
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 |
|---|---|---|
| @@ -1,14 +1,44 @@ | ||
| import os | ||
| import logging | ||
| from agents.mcp import MCPServerStreamableHttp | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| {{#if hasGateway}} | ||
| {{#if (includes gatewayAuthTypes "AWS_IAM")}} | ||
| import httpx | ||
| from mcp_proxy_for_aws.sigv4_helper import SigV4HTTPXAuth, create_aws_session | ||
| {{/if}} | ||
|
|
||
| def get_all_gateway_mcp_servers() -> list[MCPServerStreamableHttp]: | ||
| """Returns MCP servers for all configured gateways.""" | ||
| servers = [] | ||
| {{#each gatewayProviders}} | ||
| url = os.environ.get("{{envVarName}}") | ||
| if url: | ||
| {{#if (eq authType "AWS_IAM")}} | ||
| session = create_aws_session() | ||
| auth = SigV4HTTPXAuth(session.get_credentials(), "bedrock-agentcore", session.region_name) | ||
| servers.append(MCPServerStreamableHttp( | ||
| name="{{name}}", | ||
| params={"url": url, "httpx_client_factory": lambda **kwargs: httpx.AsyncClient(auth=auth, **kwargs)} | ||
| )) | ||
| {{else}} | ||
| servers.append(MCPServerStreamableHttp(name="{{name}}", params={"url": url})) | ||
| {{/if}} | ||
| else: | ||
| logger.warning("{{envVarName}} not set — {{name}} gateway tools unavailable") | ||
| {{/each}} | ||
| return servers | ||
| {{else}} | ||
| # ExaAI provides information about code through web searches, crawling and code context searches through their platform. Requires no authentication | ||
| EXAMPLE_MCP_ENDPOINT = "https://mcp.exa.ai/mcp" | ||
|
|
||
|
|
||
| def get_streamable_http_mcp_client() -> MCPServerStreamableHttp: | ||
| """ | ||
| Returns an MCP Client compatible with OpenAI Agents SDK. | ||
| """ | ||
| """Returns an MCP Client compatible with OpenAI Agents SDK.""" | ||
| # to use an MCP server that supports bearer authentication, add headers={"Authorization": f"Bearer {access_token}"} | ||
| return MCPServerStreamableHttp( | ||
| name="AgentCore Gateway MCP", params={"url": EXAMPLE_MCP_ENDPOINT} | ||
| ) | ||
| {{/if}} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MultiServerMCPClient is normally used as async with. Calling .get_tools() without entering the context manager may not initialize connections.