test_mcp_container.pyβ’7.13 kB
#!/usr/bin/env python3
"""
Test containerized MCP service deployment (example helper)
"""
import json
import os
import subprocess
import time
import httpx
BASE_URL = os.environ.get("MCP_BASE_URL", "http://localhost:8000").rstrip("/")
ASSUMED_MODE = os.environ.get("MCP_MODE", "http-stream").strip() # http | http-stream
def test_container_local():
"""Test the container running locally"""
print("π³ Testing Local Container Deployment")
print("=" * 50)
try:
# In http mode, check /sse existence (200 or 405 is acceptable)
if ASSUMED_MODE == "http":
print("π₯ Checking SSE endpoint...")
response = httpx.get(f"{BASE_URL}/sse", timeout=5)
if response.status_code in (200, 405):
print("β
SSE route reachable (/sse)")
else:
print(f"β SSE route check failed: {response.status_code}")
return False
except httpx.RequestError as e:
print(f"β Cannot connect to container: {e}")
print("π‘ Make sure container is running:")
print(
" docker run -p 8000:8000 -e GOOGLE_API_KEY=your_key -e GOOGLE_CX=your_cx google-search-mcp:latest"
)
return False
try:
# Basic check: messages route present (OPTIONS preflight OK via CORS)
print("π§ Checking MCP messages route...")
r = httpx.options(
f"{BASE_URL}/messages",
headers={
"Origin": "http://localhost",
"Access-Control-Request-Method": "POST",
},
timeout=5,
)
if r.status_code in (200, 204):
print("β
Messages route reachable (/messages)")
else:
print(f"β Messages route check failed: {r.status_code}")
return False
except httpx.RequestError as e:
print(f"β Tools endpoint error: {e}")
return False
try:
# Note: actual tool execution is over MCP messages; keep example non-invasive.
print("π Skipping direct tool call (use an MCP client for full flow)")
return True
except httpx.RequestError as e:
print(f"β Search test error: {e}")
return False
def test_container_modes():
"""Test different MCP modes in the container"""
print("\nπ Testing Different MCP Modes")
print("=" * 50)
modes = [
("http", 8000, "HTTP (SSE capable)"),
("http-stream", 8001, "HTTP Streaming (no SSE route)"),
]
for mode, port, description in modes:
print(f"\nπ§ͺ Testing {description} mode...")
# This would require running containers on different ports
# For now, just document the approach
print(f" Mode: {mode}")
print(f" Port: {port}")
print(
f" Command: docker run -p {port}:8000 -e MCP_MODE={mode} --env-file .env google-search-mcp:latest"
)
if mode == "http":
print(f" Routes: http://localhost:{port}/sse and /messages")
else:
print(f" Routes: http://localhost:{port}/messages (no /sse)")
def get_ecs_service_info():
"""Get information about ECS deployment"""
print("\nβοΈ ECS Service Information")
print("=" * 50)
try:
# Get ECS service status
result = subprocess.run(
[
"aws",
"ecs",
"describe-services",
"--cluster",
"google-search-mcp-cluster",
"--services",
"google-search-mcp-service",
"--region",
"us-east-1",
],
capture_output=True,
text=True,
)
if result.returncode == 0:
services = json.loads(result.stdout)
if services.get("services"):
service = services["services"][0]
print(f"β
Service Status: {service.get('status')}")
print(f" Desired Count: {service.get('desiredCount')}")
print(f" Running Count: {service.get('runningCount')}")
print(f" Pending Count: {service.get('pendingCount')}")
# Get task ARNs
if service.get("taskArns"):
print(f" Active Tasks: {len(service['taskArns'])}")
return True
else:
print(" No active tasks")
return False
else:
print("β No services found")
return False
else:
print(f"β Failed to get service info: {result.stderr}")
return False
except Exception as e:
print(f"β Error getting ECS info: {e}")
return False
def show_deployment_summary():
"""Show summary of all deployment options"""
print("\nπ MCP Deployment Options Summary")
print("=" * 60)
deployments = [
("1. stdio Mode", "β
Working", "Direct MCP protocol", "Local/dev"),
("2. HTTP REST", "β
Working", "REST API endpoints", "Web integration"),
("3. HTTP Streaming", "β
Working", "Server-Sent Events", "Real-time apps"),
(
"4. Lambda + Gateway",
"β
Working",
"Serverless (388ms tested)",
"AWS managed",
),
(
"5. Container (ECS)",
"π§ Deployed",
"Containerized MCP service",
"Scalable cloud",
),
]
print(f"{'Option':<20} {'Status':<12} {'Protocol':<20} {'Use Case'}")
print("-" * 60)
for option, status, protocol, use_case in deployments:
print(f"{option:<20} {status:<12} {protocol:<20} {use_case}")
print(
"\nπ― All deployment patterns demonstrate the same Google Search MCP functionality"
)
print(" - No AI models needed")
print(" - Pure MCP tool service")
print(" - Different transport/deployment methods")
print(" - Same search tool API across all patterns")
if __name__ == "__main__":
print("π MCP Container Deployment Test Suite")
print("=" * 60)
# Test local container if available
local_success = test_container_local()
# Show different modes
test_container_modes()
# Check ECS deployment
ecs_success = get_ecs_service_info()
# Show summary
show_deployment_summary()
print("\nπ Test Results:")
print(
f" Local Container: {'β
Working' if local_success else 'β Not available'}"
)
print(f" ECS Deployment: {'β
Deployed' if ecs_success else 'β Not deployed'}")
if local_success or ecs_success:
print("\nπ Containerized MCP service deployment successful!")
print(" This completes our 5th deployment pattern demonstration.")
else:
print("\nπ‘ To test locally, run:")
print(
" ./deploy/deploy_mcp_container.sh google-search-mcp us-east-1 standalone"
)
print(
" docker run -p 8000:8000 -e GOOGLE_API_KEY=key -e GOOGLE_CX=cx google-search-mcp:latest"
)