@@ -918,11 +918,7 @@ from autogen_agentchat.agents import AssistantAgent
918918from autogen_core.tools import FunctionTool
919919from bedrock_agentcore.runtime import BedrockAgentCoreApp
920920from model.load import load_model
921- { {#if hasGateway }}
922- from mcp_client.client import get_all_gateway_mcp_tools
923- { {else }}
924921from mcp_client.client import get_streamable_http_mcp_tools
925- { {/if }}
926922
927923app = BedrockAgentCoreApp()
928924log = app.logger
@@ -947,13 +943,7 @@ async def invoke(payload, context):
947943 log.info("Invoking Agent.....")
948944
949945 # Get MCP Tools
950- { {#if hasGateway }}
951- mcp_tools = await get_all_gateway_mcp_tools()
952- { {else }}
953946 mcp_tools = await get_streamable_http_mcp_tools()
954- { {/if }}
955- if mcp_tools is None:
956- mcp_tools = []
957947
958948 # Define an AssistantAgent with the model and tools
959949 agent = AssistantAgent(
@@ -984,41 +974,24 @@ exports[`Assets Directory Snapshots > Python framework assets > python/python/au
984974` ;
985975
986976exports [` Assets Directory Snapshots > Python framework assets > python/python/autogen/base/mcp_client/client.py should match snapshot 1` ] = `
987- "import os
988- import logging
989- from typing import List
977+ "from typing import List
990978from autogen_ext.tools.mcp import (
991979 StreamableHttpMcpToolAdapter,
992980 StreamableHttpServerParams,
993981 mcp_server_tools,
994982)
995983
996- logger = logging.getLogger(__name__)
997-
998- { {#if hasGateway }}
999- async def get_all_gateway_mcp_tools() -> List[StreamableHttpMcpToolAdapter]:
1000- """Returns MCP Tools from all configured gateways."""
1001- tools = []
1002- { {#each gatewayProviders }}
1003- url = os.environ.get("{ {envVarName }} ")
1004- if url:
1005- server_params = StreamableHttpServerParams(url=url)
1006- tools.extend(await mcp_server_tools(server_params))
1007- else:
1008- logger.warning("{ {envVarName }} not set — { {name }} gateway tools unavailable")
1009- { {/each }}
1010- return tools
1011- { {else }}
1012984# ExaAI provides information about code through web searches, crawling and code context searches through their platform. Requires no authentication
1013985EXAMPLE_MCP_ENDPOINT = "https://mcp.exa.ai/mcp"
1014986
1015987
1016988async def get_streamable_http_mcp_tools() -> List[StreamableHttpMcpToolAdapter]:
1017- """Returns MCP Tools compatible with AutoGen."""
989+ """
990+ Returns MCP Tools compatible with AutoGen.
991+ """
1018992 # to use an MCP server that supports bearer authentication, add headers={ " Authorization" : f " Bearer {access_token}" }
1019993 server_params = StreamableHttpServerParams(url=EXAMPLE_MCP_ENDPOINT)
1020994 return await mcp_server_tools(server_params)
1021- { {/if }}
1022995"
1023996` ;
1024997
@@ -1735,13 +1708,27 @@ from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnecti
17351708logger = logging.getLogger(__name__)
17361709
17371710{ {#if hasGateway }}
1711+ { {#if (includes gatewayAuthTypes "AWS_IAM ")}}
1712+ import httpx
1713+ from mcp_proxy_for_aws.sigv4_helper import SigV4HTTPXAuth, create_aws_session
1714+ { {/if }}
1715+
17381716def get_all_gateway_mcp_toolsets() -> list[MCPToolset]:
17391717 """Returns MCP Toolsets for all configured gateways."""
17401718 toolsets = []
17411719 { {#each gatewayProviders }}
17421720 url = os.environ.get("{ {envVarName }} ")
17431721 if url:
1722+ { {#if (eq authType "AWS_IAM ")}}
1723+ session = create_aws_session()
1724+ auth = SigV4HTTPXAuth(session.get_credentials(), "bedrock-agentcore", session.region_name)
1725+ toolsets.append(MCPToolset(connection_params=StreamableHTTPConnectionParams(
1726+ url=url,
1727+ httpx_client_factory=lambda **kwargs: httpx.AsyncClient(auth=auth, **kwargs)
1728+ )))
1729+ { {else }}
17441730 toolsets.append(MCPToolset(connection_params=StreamableHTTPConnectionParams(url=url)))
1731+ { {/if }}
17451732 else:
17461733 logger.warning("{ {envVarName }} not set — { {name }} gateway tools unavailable")
17471734 { {/each }}
@@ -1828,6 +1815,8 @@ dependencies = [
18281815 "google-adk >= 1.17.0",
18291816 "bedrock-agentcore >= 1.0.3",
18301817 "botocore[crt] >= 1.35.0",
1818+ { {#if hasGateway }} { {#if (includes gatewayAuthTypes "AWS_IAM ")}} "mcp-proxy-for-aws >= 1.1.0",
1819+ { {/if }} { {/if }}
18311820]
18321821
18331822[tool.hatch.build.targets.wheel]
@@ -2007,44 +1996,19 @@ logger = logging.getLogger(__name__)
20071996
20081997{ {#if hasGateway }}
20091998{ {#if (includes gatewayAuthTypes "AWS_IAM ")}}
2010- import boto3
2011- import httpx
2012- from botocore.auth import SigV4Auth
2013- from botocore.awsrequest import AWSRequest
2014-
2015-
2016- class SigV4HTTPXAuth(httpx.Auth):
2017- """Signs HTTP requests with AWS SigV4 for Lambda function URL authentication."""
2018-
2019- def __init__(self):
2020- session = boto3.Session()
2021- credentials = session.get_credentials().get_frozen_credentials()
2022- region = session.region_name or os.environ.get("AWS_REGION", "us-east-1")
2023- self.signer = SigV4Auth(credentials, "lambda", region)
2024-
2025- def auth_flow(self, request):
2026- headers = dict(request.headers)
2027- headers.pop("connection", None)
2028- aws_request = AWSRequest(
2029- method=request.method,
2030- url=str(request.url),
2031- data=request.content,
2032- headers=headers,
2033- )
2034- self.signer.add_auth(aws_request)
2035- request.headers.update(dict(aws_request.headers))
2036- yield request
1999+ from mcp_proxy_for_aws.sigv4_helper import SigV4HTTPXAuth, create_aws_session
20372000{ {/if }}
20382001
2039-
20402002def get_all_gateway_mcp_client() -> MultiServerMCPClient | None:
20412003 """Returns an MCP Client connected to all configured gateways."""
20422004 servers = { }
20432005 { {#each gatewayProviders }}
20442006 url = os.environ.get("{ {envVarName }} ")
20452007 if url:
20462008 { {#if (eq authType "AWS_IAM ")}}
2047- servers["{ {name }} "] = { " transport" : " streamable_http" , " url" : url , " http_client" : httpx .AsyncClient (auth = SigV4HTTPXAuth ())}
2009+ session = create_aws_session()
2010+ auth = SigV4HTTPXAuth(session.get_credentials(), "bedrock-agentcore", session.region_name)
2011+ servers["{ {name }} "] = { " transport" : " streamable_http" , " url" : url , " auth" : auth }
20482012 { {else }}
20492013 servers["{ {name }} "] = { " transport" : " streamable_http" , " url" : url }
20502014 { {/if }}
@@ -2239,6 +2203,8 @@ dependencies = [
22392203{ {#if (eq modelProvider "Gemini ")}}
22402204 "langchain-google-genai >= 3.0.3",
22412205{ {/if }}
2206+ { {#if hasGateway }} { {#if (includes gatewayAuthTypes "AWS_IAM ")}} "mcp-proxy-for-aws >= 1.1.0",
2207+ { {/if }} { {/if }}
22422208]
22432209
22442210[tool.hatch.build.targets.wheel]
@@ -2455,44 +2421,23 @@ logger = logging.getLogger(__name__)
24552421
24562422{ {#if hasGateway }}
24572423{ {#if (includes gatewayAuthTypes "AWS_IAM ")}}
2458- import boto3
24592424import httpx
2460- from botocore.auth import SigV4Auth
2461- from botocore.awsrequest import AWSRequest
2462-
2463-
2464- class SigV4HTTPXAuth(httpx.Auth):
2465- """Signs HTTP requests with AWS SigV4 for Lambda function URL authentication."""
2466-
2467- def __init__(self):
2468- session = boto3.Session()
2469- credentials = session.get_credentials().get_frozen_credentials()
2470- region = session.region_name or os.environ.get("AWS_REGION", "us-east-1")
2471- self.signer = SigV4Auth(credentials, "lambda", region)
2472-
2473- def auth_flow(self, request):
2474- headers = dict(request.headers)
2475- headers.pop("connection", None)
2476- aws_request = AWSRequest(
2477- method=request.method,
2478- url=str(request.url),
2479- data=request.content,
2480- headers=headers,
2481- )
2482- self.signer.add_auth(aws_request)
2483- request.headers.update(dict(aws_request.headers))
2484- yield request
2425+ from mcp_proxy_for_aws.sigv4_helper import SigV4HTTPXAuth, create_aws_session
24852426{ {/if }}
24862427
2487-
24882428def get_all_gateway_mcp_servers() -> list[MCPServerStreamableHttp]:
24892429 """Returns MCP servers for all configured gateways."""
24902430 servers = []
24912431 { {#each gatewayProviders }}
24922432 url = os.environ.get("{ {envVarName }} ")
24932433 if url:
24942434 { {#if (eq authType "AWS_IAM ")}}
2495- servers.append(MCPServerStreamableHttp(name="{ {name }} ", params={ " url" : url , " http_client" : httpx .AsyncClient (auth = SigV4HTTPXAuth ())} ))
2435+ session = create_aws_session()
2436+ auth = SigV4HTTPXAuth(session.get_credentials(), "bedrock-agentcore", session.region_name)
2437+ servers.append(MCPServerStreamableHttp(
2438+ name="{ {name }} ",
2439+ params={ " url" : url , " httpx_client_factory" : lambda ** kwargs : httpx .AsyncClient (auth = auth , ** kwargs )}
2440+ ))
24962441 { {else }}
24972442 servers.append(MCPServerStreamableHttp(name="{ {name }} ", params={ " url" : url } ))
24982443 { {/if }}
@@ -2577,6 +2522,8 @@ dependencies = [
25772522 "openai-agents >= 0.4.2",
25782523 "bedrock-agentcore >= 1.0.3",
25792524 "botocore[crt] >= 1.35.0",
2525+ { {#if hasGateway }} { {#if (includes gatewayAuthTypes "AWS_IAM ")}} "mcp-proxy-for-aws >= 1.1.0",
2526+ { {/if }} { {/if }}
25802527]
25812528
25822529[tool.hatch.build.targets.wheel]
@@ -2786,33 +2733,7 @@ logger = logging.getLogger(__name__)
27862733
27872734{ {#if hasGateway }}
27882735{ {#if (includes gatewayAuthTypes "AWS_IAM ")}}
2789- import boto3
2790- import httpx
2791- from botocore.auth import SigV4Auth
2792- from botocore.awsrequest import AWSRequest
2793-
2794-
2795- class SigV4HTTPXAuth(httpx.Auth):
2796- """Signs HTTP requests with AWS SigV4 for Lambda function URL authentication."""
2797-
2798- def __init__(self):
2799- session = boto3.Session()
2800- credentials = session.get_credentials().get_frozen_credentials()
2801- region = session.region_name or os.environ.get("AWS_REGION", "us-east-1")
2802- self.signer = SigV4Auth(credentials, "lambda", region)
2803-
2804- def auth_flow(self, request):
2805- headers = dict(request.headers)
2806- headers.pop("connection", None)
2807- aws_request = AWSRequest(
2808- method=request.method,
2809- url=str(request.url),
2810- data=request.content,
2811- headers=headers,
2812- )
2813- self.signer.add_auth(aws_request)
2814- request.headers.update(dict(aws_request.headers))
2815- yield request
2736+ from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
28162737{ {/if }}
28172738
28182739{ {#each gatewayProviders }}
@@ -2823,8 +2744,7 @@ def get_{{snakeCase name}}_mcp_client() -> MCPClient | None:
28232744 logger.warning("{ {envVarName }} not set — { {name }} gateway tools unavailable")
28242745 return None
28252746 { {#if (eq authType "AWS_IAM ")}}
2826- http_client = httpx.AsyncClient(auth=SigV4HTTPXAuth())
2827- return MCPClient(lambda: streamablehttp_client(url, http_client=http_client))
2747+ return MCPClient(lambda: aws_iam_streamablehttp_client(url, aws_service="bedrock-agentcore"))
28282748 { {else }}
28292749 return MCPClient(lambda: streamablehttp_client(url))
28302750 { {/if }}
@@ -3003,6 +2923,8 @@ dependencies = [
30032923 { {/if }} "mcp >= 1.19.0",
30042924 { {#if (eq modelProvider "OpenAI ")}} "openai >= 1.0.0",
30052925 { {/if }} "strands-agents >= 1.13.0",
2926+ { {#if hasGateway }} { {#if (includes gatewayAuthTypes "AWS_IAM ")}} "mcp-proxy-for-aws >= 1.1.0",
2927+ { {/if }} { {/if }}
30062928]
30072929
30082930[tool.hatch.build.targets.wheel]
0 commit comments