1515import boto3
1616import logging
1717from anyio .streams .memory import MemoryObjectReceiveStream , MemoryObjectSendStream
18+ from botocore .credentials import Credentials
1819from contextlib import _AsyncGeneratorContextManager
1920from datetime import timedelta
2021from mcp .client .streamable_http import GetSessionIdCallback , streamablehttp_client
@@ -32,6 +33,7 @@ def aws_iam_streamablehttp_client(
3233 aws_service : str ,
3334 aws_region : Optional [str ] = None ,
3435 aws_profile : Optional [str ] = None ,
36+ credentials : Optional [Credentials ] = None ,
3537 headers : Optional [dict [str , str ]] = None ,
3638 timeout : float | timedelta = 30 ,
3739 sse_read_timeout : float | timedelta = 60 * 5 ,
@@ -55,6 +57,7 @@ def aws_iam_streamablehttp_client(
5557 aws_service: The name of the AWS service the MCP server is hosted on, e.g. "bedrock-agentcore".
5658 aws_region: The AWS region name of the MCP server, e.g. "us-west-2".
5759 aws_profile: The AWS profile to use for authentication.
60+ credentials: Optional AWS credentials from boto3/botocore. If provided, takes precedence over aws_profile.
5861 headers: Optional additional HTTP headers to include in requests.
5962 timeout: Request timeout in seconds or timedelta object. Defaults to 30 seconds.
6063 sse_read_timeout: Server-sent events read timeout in seconds or timedelta object.
@@ -78,28 +81,37 @@ def aws_iam_streamablehttp_client(
7881 """
7982 logger .debug ('Preparing AWS IAM MCP client for endpoint: %s' , endpoint )
8083
81- kwargs = {}
82- if aws_profile is not None :
83- kwargs ['profile_name' ] = aws_profile
84- if aws_region is not None :
85- kwargs ['region_name' ] = aws_region
84+ if credentials is not None :
85+ creds = credentials
86+ region = aws_region
87+ if not region :
88+ raise ValueError (
89+ 'AWS region must be specified via aws_region parameter when using credentials.'
90+ )
91+ logger .debug ('Using provided AWS credentials' )
92+ else :
93+ kwargs = {}
94+ if aws_profile is not None :
95+ kwargs ['profile_name' ] = aws_profile
96+ if aws_region is not None :
97+ kwargs ['region_name' ] = aws_region
98+
99+ session = boto3 .Session (** kwargs )
100+ creds = session .get_credentials ()
101+ region = session .region_name
102+
103+ if not region :
104+ raise ValueError (
105+ 'AWS region must be specified via aws_region parameter, AWS_REGION environment variable, or AWS config.'
106+ )
107+
108+ logger .debug ('AWS profile: %s' , session .profile_name )
86109
87- session = boto3 .Session (** kwargs )
88-
89- profile = session .profile_name
90- region = session .region_name
91-
92- if not region :
93- raise ValueError (
94- 'AWS region must be specified via aws_region parameter, AWS_PROFILE environment variable, or AWS config.'
95- )
96-
97- logger .debug ('AWS profile: %s' , profile )
98110 logger .debug ('AWS region: %s' , region )
99111 logger .debug ('AWS service: %s' , aws_service )
100112
101113 # Create a SigV4 authentication handler with AWS credentials
102- auth = SigV4HTTPXAuth (session . get_credentials () , aws_service , region )
114+ auth = SigV4HTTPXAuth (creds , aws_service , region )
103115
104116 # Return the streamable HTTP client context manager with AWS IAM authentication
105117 return streamablehttp_client (
0 commit comments