|
| 1 | +"""Integration tests for AWS MCP Server at https://aws-mcp.us-east-1.api.aws/mcp.""" |
| 2 | + |
| 3 | +import fastmcp |
| 4 | +import logging |
| 5 | +import pytest |
| 6 | +from fastmcp.client.client import CallToolResult |
| 7 | + |
| 8 | +from tests.integ.test_proxy_simple_mcp_server import get_text_content |
| 9 | +import json |
| 10 | + |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.asyncio(loop_scope="module") |
| 16 | +async def test_aws_mcp_ping(aws_mcp_client: fastmcp.Client): |
| 17 | + """Test ping to AWS MCP Server.""" |
| 18 | + await aws_mcp_client.ping() |
| 19 | + |
| 20 | +@pytest.mark.asyncio(loop_scope="module") |
| 21 | +async def test_aws_mcp_list_tools(aws_mcp_client: fastmcp.Client): |
| 22 | + """Test list tools from AWS MCP Server.""" |
| 23 | + tools = await aws_mcp_client.list_tools() |
| 24 | + |
| 25 | + assert len(tools) > 0, f"AWS MCP Server should have tools (got {len(tools)})" |
| 26 | + |
| 27 | + |
| 28 | +def verify_response_content(response: CallToolResult): |
| 29 | + """Verify that a tool call response is successful and contains text content. |
| 30 | +
|
| 31 | + Args: |
| 32 | + response: The CallToolResult from an MCP tool call |
| 33 | +
|
| 34 | + Returns: |
| 35 | + str: The extracted text content from the response |
| 36 | +
|
| 37 | + Raises: |
| 38 | + AssertionError: If response indicates an error or has empty content |
| 39 | + """ |
| 40 | + assert ( |
| 41 | + response.is_error is False |
| 42 | + ), f"is_error returned true. Returned response body: {response}." |
| 43 | + assert len(response.content) > 0, f"Empty result list in response. Response: {response}" |
| 44 | + |
| 45 | + response_text = get_text_content(response) |
| 46 | + assert len(response_text) > 0, f"Empty response text. Response: {response}" |
| 47 | + |
| 48 | + return response_text |
| 49 | + |
| 50 | +def verify_json_response(response: CallToolResult): |
| 51 | + """Verify that a tool call response is successful and contains valid JSON data. |
| 52 | +
|
| 53 | + Args: |
| 54 | + response: The CallToolResult from an MCP tool call |
| 55 | +
|
| 56 | + Raises: |
| 57 | + AssertionError: If response indicates an error, has empty content, |
| 58 | + contains invalid JSON, or JSON data is empty |
| 59 | + """ |
| 60 | + response_text = verify_response_content(response) |
| 61 | + |
| 62 | + # Verify response_text is valid JSON |
| 63 | + try: |
| 64 | + response_data = json.loads(response_text) |
| 65 | + except json.JSONDecodeError: |
| 66 | + raise AssertionError(f"Response text is not valid JSON. Response text: {response_text}") |
| 67 | + |
| 68 | + assert len(response_data) > 0, f"Empty JSON content in response. Response: {response}" |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize( |
| 72 | + "tool_name,params", |
| 73 | + [ |
| 74 | + ("aws___list_regions", {}), |
| 75 | + ("aws___suggest_aws_commands", {"query": "how to list my lambda functions"}), |
| 76 | + ("aws___search_documentation", {"search_phrase": "S3 bucket versioning"}), |
| 77 | + ( |
| 78 | + "aws___recommend", |
| 79 | + {"url": "bad_url"}, |
| 80 | + ), |
| 81 | + ( |
| 82 | + "aws___read_documentation", |
| 83 | + {"url": "https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html"}, |
| 84 | + ), |
| 85 | + ( |
| 86 | + "aws___get_regional_availability", |
| 87 | + {"resource_type": "cfn", "region": "us-east-1"}, |
| 88 | + ), |
| 89 | + ("aws___call_aws", {"cli_command": "aws s3 ls", "max_results": 50}), |
| 90 | + ], |
| 91 | + ids=[ |
| 92 | + "list_regions", |
| 93 | + "suggest_aws_commands", |
| 94 | + "search_documentation", |
| 95 | + "recommend", |
| 96 | + "read_documentation", |
| 97 | + "get_regional_availability", |
| 98 | + "call_aws", |
| 99 | + ], |
| 100 | +) |
| 101 | +@pytest.mark.asyncio(loop_scope="module") |
| 102 | +async def test_aws_mcp_tools(aws_mcp_client: fastmcp.Client, tool_name: str, params: dict): |
| 103 | + """Test AWS MCP tools with minimal valid params.""" |
| 104 | + response = await aws_mcp_client.call_tool(tool_name, params) |
| 105 | + verify_json_response(response) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio(loop_scope="module") |
| 109 | +async def test_aws_mcp_tools_retrieve_agent_sop(aws_mcp_client: fastmcp.Client): |
| 110 | + """Test aws___retrieve_agent_sop by retrieving the list of available SOPs.""" |
| 111 | + |
| 112 | + # Step 1: Call retrieve_agent_sop with empty params to get list of available SOPs |
| 113 | + list_sops_response = await aws_mcp_client.call_tool("aws___retrieve_agent_sop") |
| 114 | + |
| 115 | + list_sops_response_text = verify_response_content(list_sops_response) |
| 116 | + |
| 117 | + # Parse SOP names from text (format: "* sop_name : description") |
| 118 | + sop_names = [] |
| 119 | + for line in list_sops_response_text.split("\n"): |
| 120 | + line = line.strip() |
| 121 | + if line.startswith("*") and ":" in line: |
| 122 | + # Extract the SOP name between '*' and ':' |
| 123 | + sop_name = line.split("*", 1)[1].split(":", 1)[0].strip() |
| 124 | + if sop_name: |
| 125 | + sop_names.append(sop_name) |
| 126 | + |
| 127 | + assert ( |
| 128 | + len(sop_names) > 0 |
| 129 | + ), f"No SOPs found in response. Response: {list_sops_response_text[:200]}..." |
| 130 | + logger.info(f"Found {len(sop_names)} SOPs: {sop_names}") |
| 131 | + |
| 132 | + # Step 2: Test retrieving the first SOP |
| 133 | + test_script = sop_names[0] |
| 134 | + logger.info(f"Testing with SOP: {test_script}") |
| 135 | + |
| 136 | + response = await aws_mcp_client.call_tool("aws___retrieve_agent_sop", {"sop_name": test_script}) |
| 137 | + |
| 138 | + verify_response_content(response) |
0 commit comments