Supercharge Your AWS Cost Analysis with the AWS Cost Explorer MCP Server

In today's cloud-first world, managing AWS costs effectively is crucial for any organization. While AWS provides excellent cost management tools, analyzing and querying cost data often requires switching between multiple interfaces or writing custom scripts. What if you could simply ask questions about your AWS costs in natural language and get instant, accurate answers?

Enter the AWS Cost Explorer MCP Server – a powerful Model Context Protocol (MCP) server that bridges the gap between AI assistants like Claude and AWS Cost Explorer, enabling you to query and analyze your AWS costs through conversational AI.

What is the Model Context Protocol (MCP)?

The Model Context Protocol (MCP) is an open standard that enables AI assistants to securely connect to external data sources and tools. It acts as a bridge between AI models and various services, allowing for seamless integration and data access. In this case, our MCP server connects Claude (or other AI assistants) directly to AWS Cost Explorer.

Key Features of the AWS Cost Explorer MCP Server

  • Natural Language Queries: Ask questions like "What were my AWS costs last month by service?" instead of navigating complex UIs
  • Comprehensive Cost Analysis: Access detailed cost breakdowns by time period, service, region, and other dimensions
  • Flexible Filtering: Filter costs by various AWS dimensions to get exactly the data you need
  • Multiple Metrics Support: Query different cost metrics (UnblendedCost, AmortizedCost, NetUnblendedCost, etc.)
  • Secure Authentication: Uses standard AWS credential methods for secure access
  • Real-time Data: Get up-to-date cost information directly from AWS Cost Explorer

Prerequisites

Before we dive into the setup, ensure you have:

  • Python 3.8 or higher installed on your system
  • An AWS account with Cost Explorer access
  • AWS credentials configured (we'll cover this in detail)
  • Claude Desktop or another MCP-compatible AI assistant

Step 1: Installation and Setup

Clone the Repository

First, let's get the AWS MCP server code from GitHub:

git clone https://github.com/finchtechnology/aws_mcp.git
cd aws_mcp

Install Dependencies

The server has minimal dependencies, making it lightweight and easy to deploy:

pip install -r requirements.txt

The required packages are:

  • boto3 - AWS SDK for Python
  • botocore - Low-level AWS interface
  • mcp - Model Context Protocol implementation
  • python-dotenv - Environment variable management

Step 2: Configure AWS Credentials

The MCP server needs access to your AWS account to query Cost Explorer data. You have several options for providing credentials:

Option A: AWS CLI Profile (Recommended)

If you already use the AWS CLI, this is the easiest method:

aws configure --profile your-profile-name

You'll be prompted to enter:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region (e.g., us-east-1)
  • Default output format (e.g., json)

Option B: Environment Variables

Set your credentials as environment variables:

export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
export AWS_DEFAULT_REGION=us-east-1

Option C: .env File

Create a .env file in the project directory:

AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_DEFAULT_REGION=us-east-1
AWS_PROFILE=your-profile-name

Step 3: Set Up Required AWS Permissions

Your AWS credentials need specific permissions to access Cost Explorer. Create an IAM policy with the following permissions:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ce:GetCostAndUsage",
        "ce:GetDimensionValues"
      ],
      "Resource": "*"
    }
  ]
}

Important Security Note: These permissions provide read-only access to cost data. Follow the principle of least privilege and only grant these permissions to users who need cost analysis capabilities.

Step 4: Configure Claude Desktop

To connect the MCP server with Claude Desktop, you need to update Claude's configuration file:

macOS Configuration

Edit the file at: ~/Library/Application Support/Claude/claude_desktop_config.json

Windows Configuration

Edit the file at: %APPDATA%\Claude\claude_desktop_config.json

Add the following configuration:

{
  "mcpServers": {
    "aws-cost-explorer": {
            "command": "uv",
            "args": [
                "--directory",
                "/absolute/path/to/aws_mcp",
                "run",
                "aws-cost-explorer",
                "--profile",
                "oms-dev"
            ],
    }
  }
}

Important: Replace /absolute/path/to/aws_mcp/ with the actual absolute path to your cloned repository, and your-profile-name with your AWS profile name.

Step 5: Test the Installation

Before connecting to Claude, let's verify that everything is working correctly:

python test_server.py

If successful, you should see output like:

Initializing connection...
Available tools: ['get_cost_and_usage', 'get_dimension_values']
Testing get_cost_and_usage tool...
✓ Cost data retrieved successfully

If you see "Unable to locate credentials", double-check your AWS credential configuration.

Understanding the Available Tools

The MCP server provides two powerful tools for cost analysis:

1. get_cost_and_usage

This tool retrieves AWS costs for specified time periods with flexible grouping and filtering options.

Key Parameters:

  • start and end: Define the time period (YYYY-MM-DD format)
  • granularity: "DAILY" or "MONTHLY" aggregation
  • group_by: Group results by dimensions like SERVICE, REGION, LINKED_ACCOUNT
  • metrics: Choose from UnblendedCost, AmortizedCost, NetUnblendedCost, etc.
  • filter_config: Filter by specific dimension values
  • max_results: Limit the number of results (1-100)

2. get_dimension_values

This tool discovers available values for Cost Explorer dimensions, helping you understand what services, regions, or accounts you can analyze.

Key Parameters:

  • dimension: The dimension to explore (SERVICE, REGION, LINKED_ACCOUNT, etc.)
  • time_period_start/end: Time range for the search
  • search_string: Filter dimension values
  • max_results: Limit results (1-1000)

Practical Usage Examples

Once configured, you can start asking Claude natural language questions about your AWS costs:

Basic Cost Queries

  • "What were my total AWS costs last month?"
  • "Show me daily costs for the last 30 days"
  • "What are my costs this year compared to last year?"

Service-Specific Analysis

  • "What were my EC2 costs by region last month?"
  • "Show me my top 10 most expensive AWS services"
  • "How much did I spend on S3 storage this quarter?"

Advanced Filtering

  • "What are my costs for production workloads in us-east-1?"
  • "Show me costs for my development account only"
  • "What services am I using in the Asia Pacific regions?"

Dimension Exploration

  • "What AWS services am I currently using?"
  • "List all regions where I have resources"
  • "Show me all my linked AWS accounts"

Best Practices and Tips

1. Start with Broad Queries

Begin with high-level questions to understand your cost patterns before diving into specific details:

"What's my overall AWS spend trend for the last 6 months?"

2. Use Appropriate Time Granularity

  • Use MONTHLY granularity for long-term trends and high-level analysis
  • Use DAILY granularity for short-term analysis and anomaly detection

3. Leverage Grouping Effectively

Group by relevant dimensions to get actionable insights:

"Show me last month's costs grouped by service and region"

4. Combine Multiple Metrics

Different metrics provide different insights:

  • UnblendedCost: Actual charges
  • AmortizedCost: Includes Reserved Instance and Savings Plan benefits
  • NetUnblendedCost: Costs after credits and refunds

5. Use Filters for Focused Analysis

Filter by specific dimensions to analyze particular aspects of your infrastructure:

"Show me costs for only my production workloads"

Troubleshooting Common Issues

"No credentials found"

  • Verify AWS credentials are properly configured
  • Check that the profile name in Claude's config matches your AWS profile
  • Ensure environment variables are set correctly

"Access denied"

  • Verify IAM permissions for Cost Explorer access
  • Ensure billing access is enabled for your AWS account
  • Check that the user/role has the required Cost Explorer permissions

"Invalid date format"

  • Always use YYYY-MM-DD format for dates
  • Ensure the end date is after the start date
  • Remember that the end date is exclusive

Connection Issues

  • Restart Claude Desktop after configuration changes
  • Check that the absolute path to server.py is correct
  • Verify that Python is in your system PATH

Advanced Use Cases

1. Cost Anomaly Detection

Use daily granularity to identify unusual spending patterns:

"Show me daily costs for the last 30 days to identify any spending spikes"

2. Multi-Account Analysis

For organizations with multiple AWS accounts:

"Compare costs across all my linked accounts for this month"

3. Resource Optimization

Identify optimization opportunities:

"What are my top 5 most expensive services and their costs by region?"

4. Budget Planning

Use historical data for future planning:

"What's my average monthly spend by service for the last 6 months?"

Security Considerations

When deploying this MCP server, keep these security best practices in mind:

  1. Least Privilege: Only grant the minimum required permissions (ce:GetCostAndUsage, ce:GetDimensionValues)
  2. Credential Management: Use IAM roles when possible, especially for production deployments
  3. Network Security: Consider running the MCP server in a secure network environment
  4. Audit Logging: Monitor access to cost data through CloudTrail
  5. Regular Review: Periodically review and rotate credentials

Future Enhancements

The AWS Cost Explorer MCP server is actively maintained and could be extended with additional features:

  • Budget Integration: Connect with AWS Budgets for threshold monitoring
  • Cost Forecasting: Integrate with Cost Explorer's forecasting capabilities
  • Reserved Instance Analysis: Tools for RI utilization and recommendations
  • Savings Plans: Analysis of Savings Plans effectiveness
  • Custom Visualizations: Generate charts and graphs from cost data

Conclusion

The AWS Cost Explorer MCP server transforms how you interact with your AWS cost data. By enabling natural language queries through AI assistants like Claude, it makes cost analysis more accessible, intuitive, and efficient.

Whether you're a cloud architect looking to optimize costs, a finance team member tracking budgets, or a DevOps engineer monitoring resource usage, this tool provides immediate insights into your AWS spending patterns.

The combination of AWS's comprehensive Cost Explorer data with the conversational interface of AI assistants creates a powerful platform for cost management and optimization. As cloud costs continue to grow in complexity, tools like this MCP server become essential for maintaining visibility and control over your AWS spending.

Ready to get started? Clone the repository from GitHub, follow the setup instructions, and start exploring your AWS costs in a whole new way!

Next
Next

Hidden Cloud Costs and How to Avoid Them