Skip to content

Add region to CLI arguments#16

Merged
acmlau merged 11 commits into
aws:mainfrom
acmlau:update-cli-arguments
Oct 2, 2025
Merged

Add region to CLI arguments#16
acmlau merged 11 commits into
aws:mainfrom
acmlau:update-cli-arguments

Conversation

@acmlau

@acmlau acmlau commented Sep 30, 2025

Copy link
Copy Markdown
Contributor

Description of changes:

  • Added --region argument to specify region from command line
  • Added validation and determination for region
  • Updated tests to reflect changes
  • Moved environment variables fetching to main execution of script

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment thread aws_mcp_proxy/server.py Outdated
@anasstahr anasstahr requested review from anasstahr and removed request for anasstahr September 30, 2025 11:32
@acmlau acmlau force-pushed the update-cli-arguments branch from 8840e8f to 3058d7a Compare September 30, 2025 12:00
@kyoncal

kyoncal commented Sep 30, 2025

Copy link
Copy Markdown
Contributor

Even though the default was change, there are tests that didn't reflect the change (and are somehow still passing?)
https://github.com/aws/aws-mcp-proxy/blob/0bf5c98ba14bf1021995a9905d09a438525fa290/tests/test_sigv4_helper.py#L229C5-L248C1

@kyoncal

kyoncal commented Sep 30, 2025

Copy link
Copy Markdown
Contributor

Should the documentation also be changed to reflect the change in defaults? https://github.com/aws/aws-mcp-proxy/blob/0bf5c98ba14bf1021995a9905d09a438525fa290/tests/test_sigv4_helper.py#L246

Comment thread tests/test_sigv4_helper.py Fixed
@acmlau acmlau force-pushed the update-cli-arguments branch from 1a3be58 to 273a37e Compare September 30, 2025 14:54
@kyoncal kyoncal self-requested a review October 1, 2025 08:52

# Get region from parameter, environment variable, or default
if not region:
region = os.environ.get('AWS_REGION', 'us-west-2')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the region here, from the parsed args rather than fetching it again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a standalone function, it makes sense to check whether region is not set.
I did notice that we never passed the region as a parameter when creating the sigv4_auth https://github.com/aws/aws-mcp-proxy/blob/main/aws_mcp_proxy/server.py#L60, will create new PR to address that + removing some unused parameters

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the time we create sigv4 auth, region should have been resolved, and region should not be None

@wzxxing

wzxxing commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

There should not be a default region, region should come from the following order

  1. --region parameter
  2. parsed from url such as https://mcp.us-east-1.api.aws/mcp
  3. environment variable
  4. throw error if region cannot be resolved from 1, 2, 3

Comment thread tests/unit/test_utils.py Fixed
Comment thread tests/unit/test_utils.py Fixed
Comment thread tests/unit/test_utils.py Fixed
Comment thread tests/unit/test_utils.py Fixed
Comment thread tests/unit/test_utils.py Fixed
Comment thread tests/unit/test_utils.py Fixed
@acmlau acmlau force-pushed the update-cli-arguments branch from 077a66f to 1b1b447 Compare October 1, 2025 14:09
@acmlau acmlau force-pushed the update-cli-arguments branch from 1b1b447 to 075a918 Compare October 1, 2025 14:12
@acmlau acmlau force-pushed the update-cli-arguments branch from cfac71f to 89debe0 Compare October 1, 2025 14:31
Comment thread aws_mcp_proxy/utils.py

# Get region from parameter, environment variable, or default
if not region:
region = os.environ.get('AWS_REGION', 'us-west-2')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the time we create sigv4 auth, region should have been resolved, and region should not be None

Comment thread aws_mcp_proxy/sigv4_helper.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*, service: str, region: str, profile: Optional[str] = None,

Let's see if we can make region a required argument. create_sigv4_auth expect the region to be resolved lower in the call stack somewhere.

Also adding *, because the function take three str arguments, it is easy to mess up during dev.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ideally Region etc should be done during parse_args (or some new parent function like resolve_args that merges cli args with env args and defaults into a dataclass we can pass around)

Comment thread aws_mcp_proxy/server.py
Comment thread aws_mcp_proxy/utils.py Outdated
url: str,
service: str,
profile: Optional[str] = None,
region: Optional[str] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

region is not optional at this point.

Comment thread tests/unit/test_utils.py Fixed
@acmlau acmlau force-pushed the update-cli-arguments branch from 21a6e41 to f0e2a59 Compare October 2, 2025 08:49
@acmlau acmlau enabled auto-merge (squash) October 2, 2025 09:22
@acmlau acmlau disabled auto-merge October 2, 2025 09:22
@acmlau acmlau force-pushed the update-cli-arguments branch from f0e2a59 to a916495 Compare October 2, 2025 09:32
@acmlau acmlau enabled auto-merge (squash) October 2, 2025 11:32
@acmlau acmlau force-pushed the update-cli-arguments branch from fe99c9d to 82a3267 Compare October 2, 2025 11:41
Comment thread tests/unit/test_utils.py

result = determine_aws_region(endpoint, region)

assert result == region

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Comment thread tests/unit/test_utils.py

result = determine_aws_region(endpoint)

assert result == expected_region

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Comment thread tests/unit/test_utils.py

result = determine_aws_region(endpoint)

assert result == expected_region

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Comment thread tests/unit/test_utils.py
with pytest.raises(ValueError) as exc_info:
determine_aws_region(endpoint)

assert 'Could not determine AWS region' in str(exc_info.value)

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Comment thread tests/unit/test_utils.py
determine_aws_region(endpoint)

assert 'Could not determine AWS region' in str(exc_info.value)
assert endpoint in str(exc_info.value)

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Comment thread tests/unit/test_utils.py

assert 'Could not determine AWS region' in str(exc_info.value)
assert endpoint in str(exc_info.value)
assert '--region argument' in str(exc_info.value)

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
@acmlau acmlau force-pushed the update-cli-arguments branch from 82a3267 to 6bb0c5f Compare October 2, 2025 11:44
Comment thread tests/unit/test_sigv4_helper.py Fixed
assert isinstance(result, SigV4HTTPXAuth)
assert result.service == 'test-service'
assert result.region == 'eu-west-1' # from environment
assert result.region == 'test-region' # default region

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
@acmlau acmlau merged commit ad822ef into aws:main Oct 2, 2025
3 checks passed
@acmlau acmlau deleted the update-cli-arguments branch October 2, 2025 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants