#!/usr/bin/env python3
"""
Deployment script for Cost Explorer MCP Server to Agentcore Runtime.
"""
import subprocess
import sys
import os
def run_command(command, description):
"""Run a command and handle errors."""
print(f"๐ {description}...")
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
print(f"โ
{description} completed successfully")
if result.stdout:
print(f"Output: {result.stdout}")
return result
except subprocess.CalledProcessError as e:
print(f"โ {description} failed")
print(f"Error: {e.stderr}")
sys.exit(1)
def check_prerequisites():
"""Check if required tools are installed."""
print("๐ Checking prerequisites...")
# Check if agentcore CLI is installed
try:
subprocess.run(["agentcore", "--version"], check=True, capture_output=True)
print("โ
AgentCore CLI is installed")
except (subprocess.CalledProcessError, FileNotFoundError):
print("โ AgentCore CLI not found. Installing...")
run_command("pip install bedrock-agentcore-starter-toolkit", "Installing AgentCore CLI")
# Check AWS credentials
try:
subprocess.run(["aws", "sts", "get-caller-identity"], check=True, capture_output=True)
print("โ
AWS credentials are configured")
except (subprocess.CalledProcessError, FileNotFoundError):
print("โ AWS credentials not configured. Please run 'aws configure' first.")
sys.exit(1)
def configure_deployment():
"""Configure the deployment using agentcore CLI."""
print("โ๏ธ Configuring deployment for MCP server...")
# Check if configuration already exists
if os.path.exists(".bedrock_agentcore.yaml"):
print("๐ Existing configuration found. Skipping configuration step.")
return
print("๐ง Running agentcore configure for MCP protocol...")
try:
# Configure for MCP protocol deployment
subprocess.run([
"agentcore", "configure",
"--entrypoint", "agentcore_mcp_server.py",
"--protocol", "MCP",
"--non-interactive"
], check=True)
print("โ
Configuration completed for MCP protocol")
except subprocess.CalledProcessError as e:
print(f"โ Configuration failed: {e}")
sys.exit(1)
def deploy_to_agentcore():
"""Deploy the MCP server to Agentcore Runtime."""
print("๐ Deploying MCP server to Agentcore Runtime...")
result = run_command("agentcore launch", "Deploying to AWS")
# Extract ARN from output if possible
output = result.stdout
if "arn:aws:bedrock-agentcore" in output:
lines = output.split('\n')
for line in lines:
if "arn:aws:bedrock-agentcore" in line:
arn = line.strip()
print(f"๐ Deployment successful!")
print(f"๐ Agent Runtime ARN: {arn}")
# Save ARN to file for later use
with open("agent_arn.txt", "w") as f:
f.write(arn)
print("๐พ ARN saved to agent_arn.txt")
break
return result
def test_deployment():
"""Test the deployed MCP server."""
print("๐งช Testing deployed MCP server...")
try:
result = run_command(
'agentcore invoke \'{"prompt": "Hello MCP server!"}\'',
"Testing deployed MCP server"
)
print("โ
MCP server is responding correctly!")
return result
except Exception as e:
print(f"โ ๏ธ Testing failed, but deployment may still be successful: {e}")
print("You can test manually using: agentcore invoke '{\"prompt\": \"Hello!\"}'")
def main():
"""Main deployment function."""
print("๐ Starting Cost Explorer MCP Server deployment to Agentcore Runtime")
print("=" * 70)
# Check prerequisites
check_prerequisites()
# Configure deployment
configure_deployment()
# Deploy to Agentcore
deploy_to_agentcore()
# Test deployment
test_deployment()
print("\n" + "=" * 70)
print("๐ MCP Server deployment completed successfully!")
print("\n๐ Next steps:")
print("1. Your MCP server is now running on AgentCore Runtime")
print("2. The MCP endpoint will be available at port 8000 within the runtime")
print("3. Use the ARN from agent_arn.txt to connect MCP clients")
print("4. Test with: agentcore invoke '{\"prompt\": \"Hello!\"}'")
print("\n๐ For MCP client connection, see:")
print("https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-mcp.html")
print("\n๐ง To check status: agentcore status")
print("๐ To stop: agentcore destroy")
if __name__ == "__main__":
main()