|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +import pytest_asyncio |
| 5 | +from .mcp.simple_mcp_client import build_mcp_client |
| 6 | +from typing import TypedDict |
| 7 | + |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +class RemoteMCPServerConfiguration(TypedDict): |
| 13 | + """Remote MCP Server endpoint config.""" |
| 14 | + |
| 15 | + endpoint: str |
| 16 | + region_name: str |
| 17 | + |
| 18 | + |
| 19 | +@pytest_asyncio.fixture(loop_scope='module', scope='module') |
| 20 | +async def mcp_client( |
| 21 | + remote_mcp_server_configuration: RemoteMCPServerConfiguration, |
| 22 | +): |
| 23 | + """Create MCP Client fixture for using in Integ tests.""" |
| 24 | + client = build_mcp_client( |
| 25 | + endpoint=remote_mcp_server_configuration['endpoint'], |
| 26 | + region_name=remote_mcp_server_configuration['region_name'], |
| 27 | + ) |
| 28 | + |
| 29 | + async with client: |
| 30 | + yield client |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope='module') |
| 34 | +async def is_using_agentcore(): |
| 35 | + """Boolean param if we are currently running against AgentCore Runtime.""" |
| 36 | + if os.environ.get('AGENTCORE_RUNTIME_ARN'): |
| 37 | + return True |
| 38 | + else: |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture(scope='module') |
| 43 | +def remote_mcp_server_configuration(is_using_agentcore: bool): |
| 44 | + """Configuration to connect to remotely hosted MCP Server.""" |
| 45 | + if is_using_agentcore: |
| 46 | + logger.info('Will use AgentCore MCP Server') |
| 47 | + return _build_agent_core_remote_configuration() |
| 48 | + else: |
| 49 | + logger.info('Will use remote MCP server defined with ENV variables') |
| 50 | + return _build_endpoint_environment_remote_configuration() |
| 51 | + |
| 52 | + |
| 53 | +def _build_agent_core_remote_configuration(): |
| 54 | + logger.info('Using AgentCore runtime ARN for remote MCP Server') |
| 55 | + |
| 56 | + runtime_arn = os.environ.get('AGENTCORE_RUNTIME_ARN') |
| 57 | + if not runtime_arn: |
| 58 | + raise RuntimeError('AGENTCORE_RUNTIME_ARN env variable not found') |
| 59 | + |
| 60 | + agent_core_runtime_url_format = 'https://bedrock-agentcore.{region_name}.amazonaws.com/runtimes/{encoded_arn}/invocations?qualifier=DEFAULT' |
| 61 | + region_name = runtime_arn.split(':')[3] |
| 62 | + encoded_arn = runtime_arn.replace(':', '%3A').replace('/', '%2F') |
| 63 | + |
| 64 | + endpoint = agent_core_runtime_url_format.format( |
| 65 | + region_name=region_name, encoded_arn=encoded_arn |
| 66 | + ) |
| 67 | + |
| 68 | + return RemoteMCPServerConfiguration(endpoint=endpoint, region_name=region_name) |
| 69 | + |
| 70 | + |
| 71 | +def _build_endpoint_environment_remote_configuration(): |
| 72 | + logger.info('Using Endpoint environment variable for remote MCP Server') |
| 73 | + |
| 74 | + remote_endpoint_url = os.environ.get('REMOTE_ENDPOINT_URL') |
| 75 | + if not remote_endpoint_url: |
| 76 | + raise RuntimeError('REMOTE_ENDPOINT_URL env variable not found') |
| 77 | + |
| 78 | + region_name = os.environ.get('AWS_REGION') |
| 79 | + if not region_name: |
| 80 | + logger.warn('AWS_REGION param not set. Defaulting to us-east-1') |
| 81 | + region_name = 'us-east-1' |
| 82 | + |
| 83 | + logger.info(f'Starting server with config - {remote_endpoint_url=} and {region_name=}') |
| 84 | + |
| 85 | + return RemoteMCPServerConfiguration( |
| 86 | + endpoint=remote_endpoint_url, |
| 87 | + region_name=region_name, |
| 88 | + ) |
0 commit comments