#!/usr/bin/env python3
"""
Cost Explorer MCP Server for Agentcore Runtime with Streamable HTTP support.
This server provides tools for analyzing AWS costs and usage data through the AWS Cost Explorer API.
Converted from STDIO to streamable HTTP for deployment to Agentcore Runtime.
"""
import os
import sys
from awslabs.cost_explorer_mcp_server.comparison_handler import (
get_cost_and_usage_comparisons,
get_cost_comparison_drivers,
)
from awslabs.cost_explorer_mcp_server.cost_usage_handler import get_cost_and_usage
from awslabs.cost_explorer_mcp_server.forecasting_handler import get_cost_forecast
from awslabs.cost_explorer_mcp_server.metadata_handler import (
get_dimension_values,
get_tag_values,
)
from awslabs.cost_explorer_mcp_server.utility_handler import get_today_date
from loguru import logger
from mcp.server.fastmcp import FastMCP
# Configure Loguru logging
logger.remove()
logger.add(sys.stderr, level=os.getenv('FASTMCP_LOG_LEVEL', 'WARNING'))
# Define server instructions
SERVER_INSTRUCTIONS = """
# AWS Cost Explorer MCP Server
## IMPORTANT: Each API call costs $0.01 - use filters and specific date ranges to minimize charges.
## Critical Rules
- Comparison periods: exactly 1 month, start on day 1 (e.g., "2025-04-01" to "2025-05-01")
- UsageQuantity: Recommended to filter by USAGE_TYPE, USAGE_TYPE_GROUP or results are meaningless
- When user says "last X months": Use complete calendar months, not partial periods
- get_cost_comparison_drivers: returns only top 10 most significant drivers
## Query Pattern Mapping
| User Query Pattern | Recommended Tool | Notes |
|-------------------|-----------------|-------|
| "What were my costs for..." | get_cost_and_usage | Use for historical cost analysis |
| "How much did I spend on..." | get_cost_and_usage | Filter by service/region as needed |
| "Show me costs by..." | get_cost_and_usage | Set group_by parameter accordingly |
| "Compare costs between..." | get_cost_and_usage_comparisons | Ensure exactly 1 month periods |
| "Why did my costs change..." | get_cost_comparison_drivers | Returns top 10 drivers only |
| "What caused my bill to..." | get_cost_comparison_drivers | Good for root cause analysis |
| "Predict/forecast my costs..." | get_cost_forecast | Works best with specific services |
| "What will I spend on..." | get_cost_forecast | Can filter by dimension |
## Cost Optimization Tips
- Always use specific date ranges rather than broad periods
- Filter by specific services when possible to reduce data processed
- For usage metrics, always filter by USAGE_TYPE or USAGE_TYPE_GROUP to get meaningful results
- Combine related questions into a single query where possible
"""
# Create FastMCP server with streamable HTTP support for Agentcore Runtime
app = FastMCP(
name='Cost Explorer MCP Server',
instructions=SERVER_INSTRUCTIONS,
host="0.0.0.0",
port=8000,
stateless_http=True
)
# Register all tools with the app
app.tool('get_today_date')(get_today_date)
app.tool('get_dimension_values')(get_dimension_values)
app.tool('get_tag_values')(get_tag_values)
app.tool('get_cost_forecast')(get_cost_forecast)
app.tool('get_cost_and_usage_comparisons')(get_cost_and_usage_comparisons)
app.tool('get_cost_comparison_drivers')(get_cost_comparison_drivers)
app.tool('get_cost_and_usage')(get_cost_and_usage)
def main():
"""Run the MCP server with streamable HTTP transport for Agentcore Runtime."""
logger.info("Starting Cost Explorer MCP Server on port 8000 with streamable HTTP")
app.run(transport="streamable-http")
if __name__ == '__main__':
main()