iflow-mcp_aws-samples-aws-cost-explorer-mcp 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,361 @@
1
+ Metadata-Version: 2.4
2
+ Name: iflow-mcp_aws-samples-aws-cost-explorer-mcp
3
+ Version: 0.1.0
4
+ Summary: Add your description here
5
+ Requires-Python: <3.13,>=3.11
6
+ Description-Content-Type: text/markdown
7
+ License-File: LICENSE
8
+ Requires-Dist: boto3>=1.37.9
9
+ Requires-Dist: botocore>=1.37.9
10
+ Requires-Dist: chainlit>=2.4.1
11
+ Requires-Dist: jmespath>=1.0.1
12
+ Requires-Dist: langchain>=0.3.20
13
+ Requires-Dist: langchain-anthropic>=0.3.9
14
+ Requires-Dist: langchain-aws>=0.2.15
15
+ Requires-Dist: langchain-mcp-adapters>=0.0.4
16
+ Requires-Dist: langgraph>=0.3.10
17
+ Requires-Dist: mcp>=1.3.0
18
+ Requires-Dist: pandas>=2.2.3
19
+ Requires-Dist: pydantic>=2.10.6
20
+ Requires-Dist: tabulate>=0.9.0
21
+ Requires-Dist: typing-extensions>=4.12.2
22
+ Dynamic: license-file
23
+
24
+ # A sample MCP server for understanding cloud spend
25
+
26
+ An MCP server for getting AWS spend data via Cost Explorer and Amazon Bedrock usage data via [`Model invocation logs`](https://docs.aws.amazon.com/bedrock/latest/userguide/model-invocation-logging.html) in Amazon Cloud Watch through [Anthropic's MCP (Model Control Protocol)](https://www.anthropic.com/news/model-context-protocol). See section on ["secure" remote MCP server](#secure-remote-mcp-server) to see how you can run your MCP server over HTTPS.
27
+
28
+ ```mermaid
29
+ flowchart LR
30
+ User([User]) --> UserApp[User Application]
31
+ UserApp --> |Queries| Host[Host]
32
+
33
+ subgraph "Claude Desktop"
34
+ Host --> MCPClient[MCP Client]
35
+ end
36
+
37
+ MCPClient --> |MCP Protocol over HTTPS| MCPServer[AWS Cost Explorer MCP Server]
38
+
39
+ subgraph "AWS Services"
40
+ MCPServer --> |API Calls| CostExplorer[(AWS Cost Explorer)]
41
+ MCPServer --> |API Calls| CloudWatchLogs[(AWS CloudWatch Logs)]
42
+ end
43
+ ```
44
+
45
+ You can run the MCP server locally and access it via the Claude Desktop or you could also run a Remote MCP server on Amazon EC2 and access it via a MCP client built into a LangGraph Agent.
46
+
47
+ ## Overview
48
+
49
+ This tool provides a convenient way to analyze and visualize AWS cloud spending data using Anthropic's Claude model as an interactive interface. It functions as an MCP server that exposes AWS Cost Explorer API functionality to Claude Desktop, allowing you to ask questions about your AWS spend in natural language.
50
+
51
+ ## Features
52
+
53
+ - **Amazon EC2 Spend Analysis**: View detailed breakdowns of EC2 spending for the last day
54
+ - **Amazon Bedrock Spend Analysis**: View breakdown by region, users and models over the last 30 days
55
+ - **Service Spend Reports**: Analyze spending across all AWS services for the last 30 days
56
+ - **Detailed Cost Breakdown**: Get granular cost data by day, region, service, and instance type
57
+ - **Interactive Interface**: Use Claude to query your cost data through natural language
58
+
59
+ ## Requirements
60
+
61
+ - Python 3.12
62
+ - AWS credentials with Cost Explorer access
63
+ - Anthropic API access (for Claude integration)
64
+ - [Optional] Amazon Bedrock access (for LangGraph Agent)
65
+ - [Optional] Amazon EC2 for running a remote MCP server
66
+
67
+ ## Installation
68
+
69
+ 1. Install `uv`:
70
+ ```bash
71
+ # On macOS and Linux
72
+ curl -LsSf https://astral.sh/uv/install.sh | sh
73
+ ```
74
+
75
+
76
+ ```powershell
77
+ # On Windows
78
+ powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
79
+ ```
80
+ Additional installation options are documented [here](https://docs.astral.sh/uv/getting-started/installation/)
81
+
82
+ 2. Clone this repository: (assuming this will be updated to point to aws-samples?)
83
+ ```
84
+ git clone https://github.com/aws-samples/sample-cloud-spend-mcp-server
85
+ cd aws-cost-explorer-mcp
86
+ ```
87
+
88
+ 3. Set up the Python virtual environment and install dependencies:
89
+ ```
90
+ uv venv --python 3.12 && source .venv/bin/activate && uv pip install --requirement pyproject.toml
91
+ ```
92
+
93
+ 4. Configure your AWS credentials:
94
+ ```
95
+ mkdir -p ~/.aws
96
+ # Set up your credentials in ~/.aws/credentials and ~/.aws/config
97
+ ```
98
+ If you use AWS IAM Identity Center, follow the [docs](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html) to configure your short-term credentials
99
+
100
+ ## Usage
101
+
102
+ ### Prerequisites
103
+
104
+ 1. Setup [model invocation logs](https://docs.aws.amazon.com/bedrock/latest/userguide/model-invocation-logging.html#setup-cloudwatch-logs-destination) in Amazon CloudWatch.
105
+ 1. Ensure that the IAM user/role being used has full read-only access to Amazon Cost Explorer and Amazon CloudWatch, this is required for the MCP server to retrieve data from these services.
106
+ See [here](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-example-policies.html) and [here](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/CloudWatchLogsReadOnlyAccess.html) for sample policy examples that you can use & modify as per your requirements.
107
+
108
+ ### Local setup
109
+
110
+ Uses `stdio` as a transport for MCP, both the MCP server and client are running on your local machine.
111
+
112
+ #### Starting the Server (local)
113
+
114
+ Run the server using:
115
+
116
+ ```
117
+ export MCP_TRANSPORT=stdio
118
+ export BEDROCK_LOG_GROUP_NAME=YOUR_BEDROCK_CW_LOG_GROUP_NAME
119
+ python server.py
120
+ ```
121
+
122
+ #### Claude Desktop Configuration
123
+
124
+ There are two ways to configure this tool with Claude Desktop:
125
+
126
+ ##### Option 1: Using Docker
127
+
128
+ Add the following to your Claude Desktop configuration file. The file can be found out these paths depending upon you operating system.
129
+
130
+ - macOS: ~/Library/Application Support/Claude/claude_desktop_config.json.
131
+ - Windows: %APPDATA%\Claude\claude_desktop_config.json.
132
+ - Linux: ~/.config/Claude/claude_desktop_config.json.
133
+
134
+ ```json
135
+ {
136
+ "mcpServers": {
137
+ "aws-cost-explorer": {
138
+ "command": "docker",
139
+ "args": [ "run", "-i", "--rm", "-e", "AWS_PROFILE", "-e", "AWS_REGION", "-e", "BEDROCK_LOG_GROUP_NAME", "-e", "MCP_TRANSPORT", "aws-cost-explorer-mcp:latest" ],
140
+ "env": {
141
+ "AWS_PROFILE": "YOUR_AWS_PROFILE_NAME",
142
+ "AWS_REGION": "us-east-1",
143
+ "BEDROCK_LOG_GROUP_NAME": "YOUR_CLOUDWATCH_BEDROCK_MODEL_INVOCATION_LOG_GROUP_NAME",
144
+ "MCP_TRANSPORT": "stdio"
145
+ }
146
+ }
147
+ }
148
+ }
149
+ ```
150
+
151
+ > **IMPORTANT**: Replace `YOUR_AWS_PROFILE_NAME` with your actual AWS profile name. This profile should be configured in your `~/.aws/credentials` and `~/.aws/config` files.
152
+
153
+ ##### Option 2: Using UV (without Docker)
154
+
155
+ If you prefer to run the server directly without Docker, you can use UV:
156
+
157
+ ```json
158
+ {
159
+ "mcpServers": {
160
+ "aws_cost_explorer": {
161
+ "command": "uv",
162
+ "args": [
163
+ "--directory",
164
+ "/path/to/aws-cost-explorer-mcp-server",
165
+ "run",
166
+ "server.py"
167
+ ],
168
+ "env": {
169
+ "AWS_PROFILE": "YOUR_AWS_PROFILE_NAME",
170
+ "AWS_REGION": "us-east-1",
171
+ "BEDROCK_LOG_GROUP_NAME": "YOUR_CLOUDWATCH_BEDROCK_MODEL_INVOCATION_LOG_GROUP_NAME",
172
+ "MCP_TRANSPORT": "stdio"
173
+ }
174
+ }
175
+ }
176
+ }
177
+ ```
178
+
179
+ Make sure to replace the directory path with the actual path to your repository on your system.
180
+
181
+ ### Remote setup
182
+
183
+ Uses `sse` as a transport for MCP, the MCP servers on EC2 and the client is running on your local machine. Note that Claude Desktop does not support remote MCP servers at this time (see [this](https://github.com/orgs/modelcontextprotocol/discussions/16) GitHub issue).
184
+
185
+ #### Starting the Server (remote)
186
+
187
+ You can start a remote MCP server on Amazon EC2 by following the same instructions as above. Make sure to set the `MCP_TRANSPORT` as `sse` (server side events) as shown below. **Note that the MCP uses JSON-RPC 2.0 as its wire format, therefore the protocol itself does not include authorization and authentication (see [this GitHub issue](https://github.com/modelcontextprotocol/specification/discussions/102)), do not send or receive sensitive data over MCP**.
188
+
189
+ Run the server using:
190
+
191
+ ```
192
+ export MCP_TRANSPORT=sse
193
+ export BEDROCK_LOG_GROUP_NAME=YOUR_BEDROCK_CW_LOG_GROUP_NAME
194
+ python server.py
195
+ ```
196
+
197
+ 1. The MCP server will start listening on TCP port 8000.
198
+ 1. Configure an ingress rule in the security group associated with your EC2 instance to allow access to TCP port 8000 from your local machine (where you are running the MCP client/LangGraph based app) to your EC2 instance.
199
+
200
+ >Also see section on running a ["secure" remote MCP server](#secure-remote-mcp-server) i.e. a server to which your MCP clients can connect over HTTPS.
201
+
202
+ #### Testing with a CLI MCP client
203
+
204
+ You can test your remote MCP server with the `mcp_sse_client.py` script. Running this script will print the list of tools available from the MCP server and an output for the `get_bedrock_daily_usage_stats` tool.
205
+
206
+ ```{.bashrc}
207
+ MCP_SERVER_HOSTNAME=YOUR_MCP_SERVER_EC2_HOSTNAME
208
+ python mcp_sse_client.py --host $MCP_SERVER_HOSTNAME
209
+ ```
210
+
211
+
212
+ #### Testing with Chainlit app
213
+
214
+ The `app.py` file in this repo provides a Chainlit app (chatbot) which creates a LangGraph agent that uses the [`LangChain MCP Adapter`](https://github.com/langchain-ai/langchain-mcp-adapters) to import the tools provided by the MCP server as tools in a LangGraph Agent. The Agent is then able to use an LLM to respond to user questions and use the tools available to it as needed. Thus if the user asks a question such as "_What was my Bedrock usage like in the last one week?_" then the Agent will use the tools available to it via the remote MCP server to answer that question. We use Claude 3.5 Haiku model available via Amazon Bedrock to power this agent.
215
+
216
+ Run the Chainlit app using:
217
+
218
+ ```{.bashrc}
219
+ chainlit run app.py --port 8080
220
+ ```
221
+
222
+ A browser window should open up on `localhost:8080` and you should be able to use the chatbot to get details about your AWS spend.
223
+
224
+ ### Available Tools
225
+
226
+ The server exposes the following tools that Claude can use:
227
+
228
+ 1. **`get_ec2_spend_last_day()`**: Retrieves EC2 spending data for the previous day
229
+ 1. **`get_detailed_breakdown_by_day(days=7)`**: Delivers a comprehensive analysis of costs by region, service, and instance type
230
+ 1. **`get_bedrock_daily_usage_stats(days=7, region='us-east-1', log_group_name='BedrockModelInvocationLogGroup')`**: Delivers a per-day breakdown of model usage by region and users.
231
+ 1. **`get_bedrock_hourly_usage_stats(days=7, region='us-east-1', log_group_name='BedrockModelInvocationLogGroup')`**: Delivers a per-day per-hour breakdown of model usage by region and users.
232
+
233
+ ### Example Queries
234
+
235
+ Once connected to Claude through an MCP-enabled interface, you can ask questions like:
236
+
237
+ - "Help me understand my Bedrock spend over the last few weeks"
238
+ - "What was my EC2 spend yesterday?"
239
+ - "Show me my top 5 AWS services by cost for the last month"
240
+ - "Analyze my spending by region for the past 14 days"
241
+ - "Which instance types are costing me the most money?"
242
+ - "Which services had the highest month-over-month cost increase?"
243
+
244
+ ## Docker Support
245
+
246
+ A Dockerfile is included for containerized deployment:
247
+
248
+ ```
249
+ docker build -t aws-cost-explorer-mcp .
250
+ docker run -v ~/.aws:/root/.aws aws-cost-explorer-mcp
251
+ ```
252
+
253
+ ## Development
254
+
255
+ ### Project Structure
256
+
257
+ - `server.py`: Main server implementation with MCP tools
258
+ - `pyproject.toml`: Project dependencies and metadata
259
+ - `Dockerfile`: Container definition for deployments
260
+
261
+ ### Adding New Cost Analysis Tools
262
+
263
+ To extend the functionality:
264
+
265
+ 1. Add new functions to `server.py`
266
+ 2. Annotate them with `@mcp.tool()`
267
+ 3. Implement the AWS Cost Explorer API calls
268
+ 4. Format the results for easy readability
269
+
270
+ ## Secure "remote" MCP server
271
+
272
+ We can use [`nginx`](https://nginx.org/) as a reverse-proxy so that it can provide an HTTPS endpoint for connecting to the MCP server. Remote MCP clients can connect to `nginx` over HTTPS and then it can proxy traffic internally to `http://localhost:8000`. The following steps describe how to do this.
273
+
274
+ 1. Enable access to TCP port 443 from the IP address of your MCP client (your laptop, or anywhere) in the inbound rules in the security group associated with your EC2 instance.
275
+
276
+ 1. You would need to have an HTTPS certificate and private key to proceed. Let's say you use `your-mcp-server-domain-name.com` as the domain for your MCP server then you will need an SSL cert for `your-mcp-server-domain-name.com` and it will be accessible to MCP clients as `https://your-mcp-server-domain-name.com/sse`. _While you can use a self-signed cert but it would require disabling SSL verification on the MCP client, we DO NOT recommend you do that_. If you are hosting your MCP server on EC2 then you could generate an SSL cert using [no-ip](https://www.noip.com/) or [Let's Encrypt](https://letsencrypt.org/) or other similar services. Place the SSL cert and private key files in `/etc/ssl/certs` and `/etc/ssl/privatekey` folders respectively on your EC2 machine.
277
+
278
+ 1. Install `nginx` on your EC2 machine using the following commands.
279
+
280
+ ```{.bashrc}
281
+ sudo apt-get install nginx
282
+ sudo nginx -t
283
+ sudo systemctl reload nginx
284
+ ```
285
+
286
+ 1. Get the hostname for your EC2 instance, this would be needed for configuring the `nginx` reverse proxy.
287
+
288
+ ```{.bashrc}
289
+ TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") && curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/public-hostname
290
+ ```
291
+
292
+ 1. Copy the following content into a new file `/etc/nginx/conf.d/ec2.conf`. Replace `YOUR_EC2_HOSTNAME`, `/etc/ssl/certs/cert.pem` and `/etc/ssl/privatekey/privkey.pem` with values appropriate for your setup.
293
+
294
+ ```{.bashrc}
295
+ server {
296
+ listen 80;
297
+ server_name YOUR_EC2_HOSTNAME;
298
+
299
+ # Optional: Redirect HTTP to HTTPS
300
+ return 301 https://$host$request_uri;
301
+ }
302
+
303
+ server {
304
+ listen 443 ssl;
305
+ server_name YOUR_EC2_HOSTNAME;
306
+
307
+ # Self-signed certificate paths
308
+ ssl_certificate /etc/ssl/certs/cert.pem;
309
+ ssl_certificate_key /etc/ssl/privatekey/privkey.pem;
310
+
311
+ # Optional: Good practice
312
+ ssl_protocols TLSv1.2 TLSv1.3;
313
+ ssl_ciphers HIGH:!aNULL:!MD5;
314
+
315
+ location / {
316
+ # Reverse proxy to your local app (e.g., port 8000)
317
+ proxy_pass http://127.0.0.1:8000;
318
+ proxy_http_version 1.1;
319
+ proxy_set_header Host $host;
320
+ proxy_set_header X-Real-IP $remote_addr;
321
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
322
+ }
323
+ }
324
+
325
+ ```
326
+
327
+ 1. Restart `nginx`.
328
+
329
+ ```{.bashrc}
330
+ sudo systemctl start nginx
331
+ ```
332
+
333
+ 1. Start your MCP server as usual as described in the [remote setup](#remote-setup) section.
334
+
335
+ 1. Your MCP server is now accessible over HTTPS as `https://your-mcp-server-domain-name.com/sse` to your MCP client.
336
+
337
+ 1. On the client side now (say on your laptop or in your Agent) configure your MCP client to communicate to your MCP server as follows.
338
+
339
+ ```{.bashrc}
340
+ MCP_SERVER_HOSTNAME=YOUR_MCP_SERVER_DOMAIN_NAME
341
+ python mcp_sse_client.py --host $MCP_SERVER_HOSTNAME --port 443
342
+ ```
343
+
344
+ Similarly you could run the chainlit app to talk to remote MCP server over HTTPS.
345
+
346
+ ```{.bashrc}
347
+ export MCP_SERVER_URL=YOUR_MCP_SERVER_DOMAIN_NAME
348
+ export MCP_SERVER_PORT=443
349
+ chainlit run app.py --port 8080
350
+ ```
351
+
352
+ Similarly you could run the LangGraph Agent to talk to remote MCP server over HTTPS.
353
+
354
+ ```{.bashrc}
355
+ python langgraph_agent_mcp_sse_client.py --host YOUR_MCP_SERVER_DOMAIN_NAME --port 443
356
+ ```
357
+
358
+ ## License
359
+
360
+ [MIT License](LICENSE)
361
+
@@ -0,0 +1,7 @@
1
+ server.py,sha256=mfJYeZuSx_rHLcklkp_7JUdMZB8UHzCxMLjz1zF29fg,33590
2
+ iflow_mcp_aws_samples_aws_cost_explorer_mcp-0.1.0.dist-info/licenses/LICENSE,sha256=UCXHu-28jahottziq2iSJbPzPEOz8DNosKMxgJtq2iY,946
3
+ iflow_mcp_aws_samples_aws_cost_explorer_mcp-0.1.0.dist-info/METADATA,sha256=ydRpesdz_KrYlLAROAzu2YON9mtaGFGVIbxHKFNgF3g,15102
4
+ iflow_mcp_aws_samples_aws_cost_explorer_mcp-0.1.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
5
+ iflow_mcp_aws_samples_aws_cost_explorer_mcp-0.1.0.dist-info/entry_points.txt,sha256=roffbUYMExb5EUZKKGTPZorXsmJgUpOiwUJQTBYGrOw,76
6
+ iflow_mcp_aws_samples_aws_cost_explorer_mcp-0.1.0.dist-info/top_level.txt,sha256=StKOSmRhvWS5IPcvhsDRbtxUTEofJgYFGOu5AAJdSWo,7
7
+ iflow_mcp_aws_samples_aws_cost_explorer_mcp-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ iflow-mcp_aws-samples-aws-cost-explorer-mcp = server:main
@@ -0,0 +1,16 @@
1
+ MIT No Attribution
2
+
3
+ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
13
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
15
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
server.py ADDED
@@ -0,0 +1,845 @@
1
+ """
2
+ AWS Cost Explorer MCP Server.
3
+
4
+ This server provides MCP tools to interact with AWS Cost Explorer API.
5
+ """
6
+ import os
7
+ import argparse
8
+ from collections import defaultdict
9
+ from datetime import datetime, timedelta
10
+ from typing import Any, Dict, List, Optional, Union
11
+
12
+ import boto3
13
+ import pandas as pd
14
+ import json
15
+ from mcp.server.fastmcp import FastMCP
16
+ from pydantic import BaseModel, Field
17
+ from tabulate import tabulate
18
+
19
+
20
+
21
+ class DaysParam(BaseModel):
22
+ """Parameters for specifying the number of days to look back."""
23
+
24
+ days: int = Field(
25
+ default=7,
26
+ description="Number of days to look back for cost data"
27
+ )
28
+
29
+
30
+
31
+ class BedrockLogsParams(BaseModel):
32
+ """Parameters for retrieving Bedrock invocation logs."""
33
+ days: int = Field(
34
+ default=7,
35
+ description="Number of days to look back for Bedrock logs",
36
+ ge=1,
37
+ le=90
38
+ )
39
+ region: str = Field(
40
+ default="us-east-1",
41
+ description="AWS region to retrieve logs from"
42
+ )
43
+ log_group_name: str = Field(
44
+ description="Bedrock Log Group Name",
45
+ default=os.environ.get('BEDROCK_LOG_GROUP_NAME', 'BedrockModelInvocationLogGroup')
46
+ )
47
+
48
+
49
+ def get_bedrock_logs(params: BedrockLogsParams) -> Optional[pd.DataFrame]:
50
+ """
51
+ Retrieve Bedrock invocation logs for the last n days in a given region as a dataframe
52
+
53
+ Args:
54
+ params: Pydantic model containing parameters:
55
+ - days: Number of days to look back (default: 7)
56
+ - region: AWS region to query (default: us-east-1)
57
+
58
+ Returns:
59
+ pd.DataFrame: DataFrame containing the log data with columns:
60
+ - timestamp: Timestamp of the invocation
61
+ - region: AWS region
62
+ - modelId: Bedrock model ID
63
+ - userId: User ARN
64
+ - inputTokens: Number of input tokens
65
+ - completionTokens: Number of completion tokens
66
+ - totalTokens: Total tokens used
67
+ """
68
+ # Initialize CloudWatch Logs client
69
+ client = boto3.client("logs", region_name=params.region)
70
+
71
+ # Calculate time range
72
+ end_time = datetime.now()
73
+ start_time = end_time - timedelta(days=params.days)
74
+
75
+ # Convert to milliseconds since epoch
76
+ start_time_ms = int(start_time.timestamp() * 1000)
77
+ end_time_ms = int(end_time.timestamp() * 1000)
78
+
79
+ filtered_logs = []
80
+
81
+ try:
82
+ paginator = client.get_paginator("filter_log_events")
83
+
84
+ # Parameters for the log query
85
+ query_params = {
86
+ "logGroupName": params.log_group_name, # Use the provided log group name
87
+ "logStreamNames": [
88
+ "aws/bedrock/modelinvocations"
89
+ ], # The specific log stream
90
+ "startTime": start_time_ms,
91
+ "endTime": end_time_ms,
92
+ }
93
+
94
+ # Paginate through results
95
+ for page in paginator.paginate(**query_params):
96
+ for event in page.get("events", []):
97
+ try:
98
+ # Parse the message as JSON
99
+ message = json.loads(event["message"])
100
+
101
+ # Get user prompt from the input messages
102
+ prompt = ""
103
+ if (
104
+ message.get("input", {})
105
+ .get("inputBodyJson", {})
106
+ .get("messages")
107
+ ):
108
+ for msg in message["input"]["inputBodyJson"]["messages"]:
109
+ if msg.get("role") == "user" and msg.get("content"):
110
+ for content in msg["content"]:
111
+ if content.get("text"):
112
+ prompt += content["text"] + " "
113
+ prompt = prompt.strip()
114
+
115
+ # Extract only the required fields
116
+ filtered_event = {
117
+ "timestamp": message.get("timestamp"),
118
+ "region": message.get("region"),
119
+ "modelId": message.get("modelId"),
120
+ "userId": message.get("identity", {}).get("arn"),
121
+ "inputTokens": message.get("input", {}).get("inputTokenCount"),
122
+ "completionTokens": message.get("output", {}).get(
123
+ "outputTokenCount"
124
+ ),
125
+ "totalTokens": (
126
+ message.get("input", {}).get("inputTokenCount", 0)
127
+ + message.get("output", {}).get("outputTokenCount", 0)
128
+ ),
129
+ }
130
+
131
+ filtered_logs.append(filtered_event)
132
+ except json.JSONDecodeError:
133
+ continue # Skip non-JSON messages
134
+ except KeyError:
135
+ continue # Skip messages missing required fields
136
+
137
+ # Create DataFrame if we have logs
138
+ if filtered_logs:
139
+ df = pd.DataFrame(filtered_logs)
140
+ df["timestamp"] = pd.to_datetime(df["timestamp"])
141
+ return df
142
+ else:
143
+ print("No logs found for the specified time period.")
144
+ return None
145
+
146
+ except client.exceptions.ResourceNotFoundException:
147
+ print(
148
+ f"Log group '{params.log_group_name}' or stream 'aws/bedrock/modelinvocations' not found"
149
+ )
150
+ return None
151
+ except Exception as e:
152
+ print(f"Error retrieving logs: {str(e)}")
153
+ return None
154
+
155
+
156
+
157
+ # Initialize FastMCP server
158
+ mcp = FastMCP("aws_cloudwatch_logs")
159
+
160
+ @mcp.tool()
161
+ def get_bedrock_daily_usage_stats(params: BedrockLogsParams) -> str:
162
+ """
163
+ Get daily usage statistics with detailed breakdowns.
164
+
165
+ Args:
166
+ params: Parameters specifying the number of days to look back and region
167
+
168
+ Returns:
169
+ str: Formatted string representation of daily usage statistics
170
+ """
171
+ df = get_bedrock_logs(params)
172
+
173
+ if df is None or df.empty:
174
+ return "No usage data found for the specified period."
175
+
176
+ # Initialize result string
177
+ result_parts = []
178
+
179
+ # Add header
180
+ result_parts.append(f"Bedrock Usage Statistics (Past {params.days} days - {params.region})")
181
+ result_parts.append("=" * 80)
182
+
183
+ # Add a date column for easier grouping
184
+ df['date'] = df['timestamp'].dt.date
185
+
186
+ # === REGION -> MODEL GROUPING ===
187
+ result_parts.append("\n=== Daily Region-wise -> Model-wise Analysis ===")
188
+
189
+ # Group by date, region, model and calculate metrics
190
+ region_model_stats = df.groupby(['date', 'region', 'modelId']).agg({
191
+ 'inputTokens': ['count', 'sum', 'mean', 'max', 'median'],
192
+ 'completionTokens': ['sum', 'mean', 'max', 'median'],
193
+ 'totalTokens': ['sum', 'mean', 'max', 'median']
194
+ })
195
+
196
+ # Flatten the column multi-index
197
+ region_model_stats.columns = [f"{col[0]}_{col[1]}" for col in region_model_stats.columns]
198
+
199
+ # Reset the index to get a flat dataframe
200
+ flattened_stats = region_model_stats.reset_index()
201
+
202
+ # Rename inputTokens_count to request_count
203
+ flattened_stats = flattened_stats.rename(columns={'inputTokens_count': 'request_count'})
204
+
205
+ # Add the flattened stats to result
206
+ result_parts.append(flattened_stats.to_string(index=False))
207
+
208
+ # Add summary statistics
209
+ result_parts.append("\n=== Summary Statistics ===")
210
+
211
+ # Total requests and tokens
212
+ total_requests = flattened_stats['request_count'].sum()
213
+ total_input_tokens = flattened_stats['inputTokens_sum'].sum()
214
+ total_completion_tokens = flattened_stats['completionTokens_sum'].sum()
215
+ total_tokens = flattened_stats['totalTokens_sum'].sum()
216
+
217
+ result_parts.append(f"Total Requests: {total_requests:,}")
218
+ result_parts.append(f"Total Input Tokens: {total_input_tokens:,}")
219
+ result_parts.append(f"Total Completion Tokens: {total_completion_tokens:,}")
220
+ result_parts.append(f"Total Tokens: {total_tokens:,}")
221
+
222
+ # === REGION SUMMARY ===
223
+ result_parts.append("\n=== Region Summary ===")
224
+ region_summary = df.groupby('region').agg({
225
+ 'inputTokens': ['count', 'sum'],
226
+ 'completionTokens': ['sum'],
227
+ 'totalTokens': ['sum']
228
+ })
229
+
230
+ # Flatten region summary columns
231
+ region_summary.columns = [f"{col[0]}_{col[1]}" for col in region_summary.columns]
232
+ region_summary = region_summary.reset_index()
233
+ region_summary = region_summary.rename(columns={'inputTokens_count': 'request_count'})
234
+
235
+ result_parts.append(region_summary.to_string(index=False))
236
+
237
+ # === MODEL SUMMARY ===
238
+ result_parts.append("\n=== Model Summary ===")
239
+ model_summary = df.groupby('modelId').agg({
240
+ 'inputTokens': ['count', 'sum'],
241
+ 'completionTokens': ['sum'],
242
+ 'totalTokens': ['sum']
243
+ })
244
+
245
+ # Flatten model summary columns
246
+ model_summary.columns = [f"{col[0]}_{col[1]}" for col in model_summary.columns]
247
+ model_summary = model_summary.reset_index()
248
+ model_summary = model_summary.rename(columns={'inputTokens_count': 'request_count'})
249
+
250
+ # Format model IDs to be more readable
251
+ model_summary['modelId'] = model_summary['modelId'].apply(
252
+ lambda model: model.split('.')[-1] if '.' in model else model.split('/')[-1]
253
+ )
254
+
255
+ result_parts.append(model_summary.to_string(index=False))
256
+
257
+ # === USER SUMMARY ===
258
+ if 'userId' in df.columns:
259
+ result_parts.append("\n=== User Summary ===")
260
+ user_summary = df.groupby('userId').agg({
261
+ 'inputTokens': ['count', 'sum'],
262
+ 'completionTokens': ['sum'],
263
+ 'totalTokens': ['sum']
264
+ })
265
+
266
+ # Flatten user summary columns
267
+ user_summary.columns = [f"{col[0]}_{col[1]}" for col in user_summary.columns]
268
+ user_summary = user_summary.reset_index()
269
+ user_summary = user_summary.rename(columns={'inputTokens_count': 'request_count'})
270
+
271
+ result_parts.append(user_summary.to_string(index=False))
272
+
273
+ # === REGION -> USER -> MODEL DETAILED SUMMARY ===
274
+ if 'userId' in df.columns:
275
+ result_parts.append("\n=== Region -> User -> Model Detailed Summary ===")
276
+ region_user_model_summary = df.groupby(['region', 'userId', 'modelId']).agg({
277
+ 'inputTokens': ['count', 'sum', 'mean'],
278
+ 'completionTokens': ['sum', 'mean'],
279
+ 'totalTokens': ['sum', 'mean']
280
+ })
281
+
282
+ # Flatten columns
283
+ region_user_model_summary.columns = [f"{col[0]}_{col[1]}" for col in region_user_model_summary.columns]
284
+ region_user_model_summary = region_user_model_summary.reset_index()
285
+ region_user_model_summary = region_user_model_summary.rename(columns={'inputTokens_count': 'request_count'})
286
+
287
+ # Format model IDs to be more readable
288
+ region_user_model_summary['modelId'] = region_user_model_summary['modelId'].apply(
289
+ lambda model: model.split('.')[-1] if '.' in model else model.split('/')[-1]
290
+ )
291
+
292
+ result_parts.append(region_user_model_summary.to_string(index=False))
293
+
294
+
295
+ # Combine all parts into a single string
296
+ result = "\n".join(result_parts)
297
+
298
+ return result
299
+
300
+ @mcp.tool()
301
+ def get_bedrock_hourly_usage_stats(params: BedrockLogsParams) -> str:
302
+ """
303
+ Get hourly usage statistics with detailed breakdowns.
304
+
305
+ Args:
306
+ params: Parameters specifying the number of days to look back and region
307
+
308
+ Returns:
309
+ str: Formatted string representation of hourly usage statistics
310
+ """
311
+ df = get_bedrock_logs(params)
312
+
313
+ if df is None or df.empty:
314
+ return "No usage data found for the specified period."
315
+
316
+ # Initialize result string
317
+ result_parts = []
318
+
319
+ # Add header
320
+ result_parts.append(f"Hourly Bedrock Usage Statistics (Past {params.days} days - {params.region})")
321
+ result_parts.append("=" * 80)
322
+
323
+ # Add date and hour columns for easier grouping
324
+ df['date'] = df['timestamp'].dt.date
325
+ df['hour'] = df['timestamp'].dt.hour
326
+ df['datetime'] = df['timestamp'].dt.strftime('%Y-%m-%d %H:00')
327
+
328
+ # === HOURLY USAGE ANALYSIS ===
329
+ result_parts.append("\n=== Hourly Usage Analysis ===")
330
+
331
+ # Group by datetime (date + hour)
332
+ hourly_stats = df.groupby('datetime').agg({
333
+ 'inputTokens': ['count', 'sum', 'mean'],
334
+ 'completionTokens': ['sum', 'mean'],
335
+ 'totalTokens': ['sum', 'mean']
336
+ })
337
+
338
+ # Flatten the column multi-index
339
+ hourly_stats.columns = [f"{col[0]}_{col[1]}" for col in hourly_stats.columns]
340
+
341
+ # Reset the index to get a flat dataframe
342
+ hourly_stats = hourly_stats.reset_index()
343
+
344
+ # Rename inputTokens_count to request_count
345
+ hourly_stats = hourly_stats.rename(columns={'inputTokens_count': 'request_count'})
346
+
347
+ # Add the hourly stats to result
348
+ result_parts.append(hourly_stats.to_string(index=False))
349
+
350
+ # === HOURLY REGION -> MODEL GROUPING ===
351
+ result_parts.append("\n=== Hourly Region-wise -> Model-wise Analysis ===")
352
+
353
+ # Group by datetime, region, model and calculate metrics
354
+ hourly_region_model_stats = df.groupby(['datetime', 'region', 'modelId']).agg({
355
+ 'inputTokens': ['count', 'sum', 'mean', 'max', 'median'],
356
+ 'completionTokens': ['sum', 'mean', 'max', 'median'],
357
+ 'totalTokens': ['sum', 'mean', 'max', 'median']
358
+ })
359
+
360
+ # Flatten the column multi-index
361
+ hourly_region_model_stats.columns = [f"{col[0]}_{col[1]}" for col in hourly_region_model_stats.columns]
362
+
363
+ # Reset the index to get a flat dataframe
364
+ hourly_region_model_stats = hourly_region_model_stats.reset_index()
365
+
366
+ # Rename inputTokens_count to request_count
367
+ hourly_region_model_stats = hourly_region_model_stats.rename(columns={'inputTokens_count': 'request_count'})
368
+
369
+ # Format model IDs to be more readable
370
+ hourly_region_model_stats['modelId'] = hourly_region_model_stats['modelId'].apply(
371
+ lambda model: model.split('.')[-1] if '.' in model else model.split('/')[-1]
372
+ )
373
+
374
+ # Add the hourly region-model stats to result
375
+ result_parts.append(hourly_region_model_stats.to_string(index=False))
376
+
377
+ # Add summary statistics
378
+ result_parts.append("\n=== Summary Statistics ===")
379
+
380
+ # Total requests and tokens
381
+ total_requests = hourly_stats['request_count'].sum()
382
+ total_input_tokens = hourly_stats['inputTokens_sum'].sum()
383
+ total_completion_tokens = hourly_stats['completionTokens_sum'].sum()
384
+ total_tokens = hourly_stats['totalTokens_sum'].sum()
385
+
386
+ result_parts.append(f"Total Requests: {total_requests:,}")
387
+ result_parts.append(f"Total Input Tokens: {total_input_tokens:,}")
388
+ result_parts.append(f"Total Completion Tokens: {total_completion_tokens:,}")
389
+ result_parts.append(f"Total Tokens: {total_tokens:,}")
390
+
391
+ # === REGION SUMMARY ===
392
+ result_parts.append("\n=== Region Summary ===")
393
+ region_summary = df.groupby('region').agg({
394
+ 'inputTokens': ['count', 'sum'],
395
+ 'completionTokens': ['sum'],
396
+ 'totalTokens': ['sum']
397
+ })
398
+
399
+ # Flatten region summary columns
400
+ region_summary.columns = [f"{col[0]}_{col[1]}" for col in region_summary.columns]
401
+ region_summary = region_summary.reset_index()
402
+ region_summary = region_summary.rename(columns={'inputTokens_count': 'request_count'})
403
+
404
+ result_parts.append(region_summary.to_string(index=False))
405
+
406
+ # === MODEL SUMMARY ===
407
+ result_parts.append("\n=== Model Summary ===")
408
+ model_summary = df.groupby('modelId').agg({
409
+ 'inputTokens': ['count', 'sum'],
410
+ 'completionTokens': ['sum'],
411
+ 'totalTokens': ['sum']
412
+ })
413
+
414
+ # Flatten model summary columns
415
+ model_summary.columns = [f"{col[0]}_{col[1]}" for col in model_summary.columns]
416
+ model_summary = model_summary.reset_index()
417
+ model_summary = model_summary.rename(columns={'inputTokens_count': 'request_count'})
418
+
419
+ # Format model IDs to be more readable
420
+ model_summary['modelId'] = model_summary['modelId'].apply(
421
+ lambda model: model.split('.')[-1] if '.' in model else model.split('/')[-1]
422
+ )
423
+
424
+ result_parts.append(model_summary.to_string(index=False))
425
+
426
+ # === USER SUMMARY ===
427
+ if 'userId' in df.columns:
428
+ result_parts.append("\n=== User Summary ===")
429
+ user_summary = df.groupby('userId').agg({
430
+ 'inputTokens': ['count', 'sum'],
431
+ 'completionTokens': ['sum'],
432
+ 'totalTokens': ['sum']
433
+ })
434
+
435
+ # Flatten user summary columns
436
+ user_summary.columns = [f"{col[0]}_{col[1]}" for col in user_summary.columns]
437
+ user_summary = user_summary.reset_index()
438
+ user_summary = user_summary.rename(columns={'inputTokens_count': 'request_count'})
439
+
440
+ result_parts.append(user_summary.to_string(index=False))
441
+
442
+ # === HOURLY REGION -> USER -> MODEL DETAILED SUMMARY ===
443
+ if 'userId' in df.columns:
444
+ result_parts.append("\n=== Hourly Region -> User -> Model Detailed Summary ===")
445
+ hourly_region_user_model_summary = df.groupby(['datetime', 'region', 'userId', 'modelId']).agg({
446
+ 'inputTokens': ['count', 'sum', 'mean'],
447
+ 'completionTokens': ['sum', 'mean'],
448
+ 'totalTokens': ['sum', 'mean']
449
+ })
450
+
451
+ # Flatten columns
452
+ hourly_region_user_model_summary.columns = [f"{col[0]}_{col[1]}" for col in hourly_region_user_model_summary.columns]
453
+ hourly_region_user_model_summary = hourly_region_user_model_summary.reset_index()
454
+ hourly_region_user_model_summary = hourly_region_user_model_summary.rename(columns={'inputTokens_count': 'request_count'})
455
+
456
+ # Format model IDs to be more readable
457
+ hourly_region_user_model_summary['modelId'] = hourly_region_user_model_summary['modelId'].apply(
458
+ lambda model: model.split('.')[-1] if '.' in model else model.split('/')[-1]
459
+ )
460
+
461
+ result_parts.append(hourly_region_user_model_summary.to_string(index=False))
462
+
463
+ # === HOURLY USAGE PATTERN ANALYSIS ===
464
+ result_parts.append("\n=== Hourly Usage Pattern Analysis ===")
465
+
466
+ # Group by hour of day (ignoring date) to see hourly patterns
467
+ hour_pattern = df.groupby(df['timestamp'].dt.hour).agg({
468
+ 'inputTokens': ['count', 'sum'],
469
+ 'totalTokens': ['sum']
470
+ })
471
+
472
+ # Flatten hour pattern columns
473
+ hour_pattern.columns = [f"{col[0]}_{col[1]}" for col in hour_pattern.columns]
474
+ hour_pattern = hour_pattern.reset_index()
475
+ hour_pattern = hour_pattern.rename(columns={
476
+ 'timestamp': 'hour_of_day',
477
+ 'inputTokens_count': 'request_count'
478
+ })
479
+
480
+ # Format the hour to be more readable
481
+ hour_pattern['hour_of_day'] = hour_pattern['hour_of_day'].apply(
482
+ lambda hour: f"{hour:02d}:00 - {hour:02d}:59"
483
+ )
484
+
485
+ result_parts.append(hour_pattern.to_string(index=False))
486
+
487
+ # Combine all parts into a single string
488
+ result = "\n".join(result_parts)
489
+
490
+ return result
491
+
492
+ @mcp.tool()
493
+ async def get_ec2_spend_last_day() -> Dict[str, Any]:
494
+ """
495
+ Retrieve EC2 spend for the last day using standard AWS Cost Explorer API.
496
+
497
+ Returns:
498
+ Dict[str, Any]: The raw response from the AWS Cost Explorer API, or None if an error occurs.
499
+ """
500
+ # Initialize the Cost Explorer client
501
+ ce_client = boto3.client('ce')
502
+
503
+ # Calculate the time period - last day
504
+ end_date = datetime.now().strftime('%Y-%m-%d')
505
+ start_date = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
506
+
507
+ try:
508
+ # Make the API call using get_cost_and_usage (standard API)
509
+ response = ce_client.get_cost_and_usage(
510
+ TimePeriod={
511
+ 'Start': start_date,
512
+ 'End': end_date
513
+ },
514
+ Granularity='DAILY',
515
+ Filter={
516
+ 'Dimensions': {
517
+ 'Key': 'SERVICE',
518
+ 'Values': [
519
+ 'Amazon Elastic Compute Cloud - Compute'
520
+ ]
521
+ }
522
+ },
523
+ Metrics=[
524
+ 'UnblendedCost',
525
+ 'UsageQuantity'
526
+ ],
527
+ GroupBy=[
528
+ {
529
+ 'Type': 'DIMENSION',
530
+ 'Key': 'INSTANCE_TYPE'
531
+ }
532
+ ]
533
+ )
534
+
535
+ # Process and print the results
536
+ print(f"EC2 Spend from {start_date} to {end_date}:")
537
+ print("-" * 50)
538
+
539
+ total_cost = 0.0
540
+
541
+ if 'ResultsByTime' in response and response['ResultsByTime']:
542
+ time_period_data = response['ResultsByTime'][0]
543
+
544
+ if 'Groups' in time_period_data:
545
+ for group in time_period_data['Groups']:
546
+ instance_type = group['Keys'][0]
547
+ cost = float(group['Metrics']['UnblendedCost']['Amount'])
548
+ currency = group['Metrics']['UnblendedCost']['Unit']
549
+ usage = float(group['Metrics']['UsageQuantity']['Amount'])
550
+
551
+ print(f"Instance Type: {instance_type}")
552
+ print(f"Cost: {cost:.4f} {currency}")
553
+ print(f"Usage: {usage:.2f}")
554
+ print("-" * 30)
555
+
556
+ total_cost += cost
557
+
558
+ # If no instance-level breakdown, show total
559
+ if not time_period_data.get('Groups'):
560
+ if 'Total' in time_period_data:
561
+ total = time_period_data['Total']
562
+ cost = float(total['UnblendedCost']['Amount'])
563
+ currency = total['UnblendedCost']['Unit']
564
+ print(f"Total EC2 Cost: {cost:.4f} {currency}")
565
+ else:
566
+ print("No EC2 costs found for this period")
567
+ else:
568
+ print(f"Total EC2 Cost: {total_cost:.4f} {currency if 'currency' in locals() else 'USD'}")
569
+
570
+ # Check if results are estimated
571
+ if 'Estimated' in time_period_data:
572
+ print(f"Note: These results are {'estimated' if time_period_data['Estimated'] else 'final'}")
573
+
574
+ return response
575
+
576
+ except Exception as e:
577
+ print(f"Error retrieving EC2 cost data: {str(e)}")
578
+ return None
579
+
580
+
581
+ @mcp.tool()
582
+ async def get_detailed_breakdown_by_day(params: DaysParam) -> str: #Dict[str, Any]:
583
+ """
584
+ Retrieve daily spend breakdown by region, service, and instance type.
585
+
586
+ Args:
587
+ params: Parameters specifying the number of days to look back
588
+
589
+ Returns:
590
+ Dict[str, Any]: A tuple containing:
591
+ - A nested dictionary with cost data organized by date, region, and service
592
+ - A string containing the formatted output report
593
+ or (None, error_message) if an error occurs.
594
+ """
595
+ # Initialize the Cost Explorer client
596
+ ce_client = boto3.client('ce')
597
+
598
+ # Get the days parameter
599
+ days = params.days
600
+
601
+ # Calculate the time period
602
+ end_date = datetime.now().strftime('%Y-%m-%d')
603
+ start_date = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
604
+
605
+ # Initialize output buffer
606
+ output_buffer = []
607
+
608
+ try:
609
+ output_buffer.append(f"\nDetailed Cost Breakdown by Region, Service, and Instance Type ({days} days):")
610
+ output_buffer.append("-" * 75)
611
+
612
+ # First get the daily costs by region and service
613
+ response = ce_client.get_cost_and_usage(
614
+ TimePeriod={
615
+ 'Start': start_date,
616
+ 'End': end_date
617
+ },
618
+ Granularity='DAILY',
619
+ Metrics=['UnblendedCost'],
620
+ GroupBy=[
621
+ {
622
+ 'Type': 'DIMENSION',
623
+ 'Key': 'REGION'
624
+ },
625
+ {
626
+ 'Type': 'DIMENSION',
627
+ 'Key': 'SERVICE'
628
+ }
629
+ ]
630
+ )
631
+
632
+ # Create data structure to hold the results
633
+ all_data = defaultdict(lambda: defaultdict(lambda: defaultdict(float)))
634
+
635
+ # Process the results
636
+ for time_data in response['ResultsByTime']:
637
+ date = time_data['TimePeriod']['Start']
638
+
639
+ output_buffer.append(f"\nDate: {date}")
640
+ output_buffer.append("=" * 50)
641
+
642
+ if 'Groups' in time_data and time_data['Groups']:
643
+ # Create data structure for this date
644
+ region_services = defaultdict(lambda: defaultdict(float))
645
+
646
+ # Process groups
647
+ for group in time_data['Groups']:
648
+ region, service = group['Keys']
649
+ cost = float(group['Metrics']['UnblendedCost']['Amount'])
650
+ currency = group['Metrics']['UnblendedCost']['Unit']
651
+
652
+ region_services[region][service] = cost
653
+ all_data[date][region][service] = cost
654
+
655
+ # Add the results for this date to the buffer
656
+ for region in sorted(region_services.keys()):
657
+ output_buffer.append(f"\nRegion: {region}")
658
+ output_buffer.append("-" * 40)
659
+
660
+ # Create a DataFrame for this region's services
661
+ services_df = pd.DataFrame({
662
+ 'Service': list(region_services[region].keys()),
663
+ 'Cost': list(region_services[region].values())
664
+ })
665
+
666
+ # Sort by cost descending
667
+ services_df = services_df.sort_values('Cost', ascending=False)
668
+
669
+ # Get top services by cost
670
+ top_services = services_df.head(5)
671
+
672
+ # Add region's services table to buffer
673
+ output_buffer.append(tabulate(top_services.round(2), headers='keys', tablefmt='pretty', showindex=False))
674
+
675
+ # If there are more services, indicate the total for other services
676
+ if len(services_df) > 5:
677
+ other_cost = services_df.iloc[5:]['Cost'].sum()
678
+ output_buffer.append(f"... and {len(services_df) - 5} more services totaling {other_cost:.2f} {currency}")
679
+
680
+ # For EC2, get instance type breakdown
681
+ if any(s.startswith('Amazon Elastic Compute') for s in region_services[region].keys()):
682
+ try:
683
+ instance_response = get_instance_type_breakdown(
684
+ ce_client,
685
+ date,
686
+ region,
687
+ 'Amazon Elastic Compute Cloud - Compute',
688
+ 'INSTANCE_TYPE'
689
+ )
690
+
691
+ if instance_response:
692
+ output_buffer.append("\n EC2 Instance Type Breakdown:")
693
+ output_buffer.append(" " + "-" * 38)
694
+
695
+ # Get table with indentation
696
+ instance_table = tabulate(instance_response.round(2), headers='keys', tablefmt='pretty', showindex=False)
697
+ for line in instance_table.split('\n'):
698
+ output_buffer.append(f" {line}")
699
+
700
+ except Exception as e:
701
+ output_buffer.append(f" Note: Could not retrieve EC2 instance type breakdown: {str(e)}")
702
+
703
+ # For SageMaker, get instance type breakdown
704
+ if any(s == 'Amazon SageMaker' for s in region_services[region].keys()):
705
+ try:
706
+ sagemaker_instance_response = get_instance_type_breakdown(
707
+ ce_client,
708
+ date,
709
+ region,
710
+ 'Amazon SageMaker',
711
+ 'INSTANCE_TYPE'
712
+ )
713
+
714
+ if sagemaker_instance_response is not None and not sagemaker_instance_response.empty:
715
+ output_buffer.append("\n SageMaker Instance Type Breakdown:")
716
+ output_buffer.append(" " + "-" * 38)
717
+
718
+ # Get table with indentation
719
+ sagemaker_table = tabulate(sagemaker_instance_response.round(2), headers='keys', tablefmt='pretty', showindex=False)
720
+ for line in sagemaker_table.split('\n'):
721
+ output_buffer.append(f" {line}")
722
+
723
+ # Also try to get usage type breakdown for SageMaker (notebooks, endpoints, etc.)
724
+ sagemaker_usage_response = get_instance_type_breakdown(
725
+ ce_client,
726
+ date,
727
+ region,
728
+ 'Amazon SageMaker',
729
+ 'USAGE_TYPE'
730
+ )
731
+
732
+ if sagemaker_usage_response is not None and not sagemaker_usage_response.empty:
733
+ output_buffer.append("\n SageMaker Usage Type Breakdown:")
734
+ output_buffer.append(" " + "-" * 38)
735
+
736
+ # Get table with indentation
737
+ usage_table = tabulate(sagemaker_usage_response.round(2), headers='keys', tablefmt='pretty', showindex=False)
738
+ for line in usage_table.split('\n'):
739
+ output_buffer.append(f" {line}")
740
+
741
+ except Exception as e:
742
+ output_buffer.append(f" Note: Could not retrieve SageMaker breakdown: {str(e)}")
743
+ else:
744
+ output_buffer.append("No data found for this date")
745
+
746
+ output_buffer.append("\n" + "-" * 75)
747
+
748
+ # Join the buffer into a single string
749
+ formatted_output = "\n".join(output_buffer)
750
+
751
+ # Return both the raw data and the formatted output
752
+ #return {"data": all_data, "formatted_output": formatted_output}
753
+ return formatted_output
754
+
755
+ except Exception as e:
756
+ error_message = f"Error retrieving detailed breakdown: {str(e)}"
757
+ #return {"data": None, "formatted_output": error_message}
758
+ return error_message
759
+
760
+ def get_instance_type_breakdown(ce_client, date, region, service, dimension_key):
761
+ """
762
+ Helper function to get instance type or usage type breakdown for a specific service.
763
+
764
+ Args:
765
+ ce_client: The Cost Explorer client
766
+ date: The date to query
767
+ region: The AWS region
768
+ service: The AWS service name
769
+ dimension_key: The dimension to group by (e.g., 'INSTANCE_TYPE' or 'USAGE_TYPE')
770
+
771
+ Returns:
772
+ DataFrame containing the breakdown or None if no data
773
+ """
774
+ tomorrow = (datetime.strptime(date, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y-%m-%d')
775
+
776
+ instance_response = ce_client.get_cost_and_usage(
777
+ TimePeriod={
778
+ 'Start': date,
779
+ 'End': tomorrow
780
+ },
781
+ Granularity='DAILY',
782
+ Filter={
783
+ 'And': [
784
+ {
785
+ 'Dimensions': {
786
+ 'Key': 'REGION',
787
+ 'Values': [region]
788
+ }
789
+ },
790
+ {
791
+ 'Dimensions': {
792
+ 'Key': 'SERVICE',
793
+ 'Values': [service]
794
+ }
795
+ }
796
+ ]
797
+ },
798
+ Metrics=['UnblendedCost'],
799
+ GroupBy=[
800
+ {
801
+ 'Type': 'DIMENSION',
802
+ 'Key': dimension_key
803
+ }
804
+ ]
805
+ )
806
+
807
+ if ('ResultsByTime' in instance_response and
808
+ instance_response['ResultsByTime'] and
809
+ 'Groups' in instance_response['ResultsByTime'][0] and
810
+ instance_response['ResultsByTime'][0]['Groups']):
811
+
812
+ instance_data = instance_response['ResultsByTime'][0]
813
+ instance_costs = []
814
+
815
+ for instance_group in instance_data['Groups']:
816
+ type_value = instance_group['Keys'][0]
817
+ cost_value = float(instance_group['Metrics']['UnblendedCost']['Amount'])
818
+
819
+ # Add a better label for the dimension used
820
+ column_name = 'Instance Type' if dimension_key == 'INSTANCE_TYPE' else 'Usage Type'
821
+
822
+ instance_costs.append({
823
+ column_name: type_value,
824
+ 'Cost': cost_value
825
+ })
826
+
827
+ # Create DataFrame and sort by cost
828
+ result_df = pd.DataFrame(instance_costs)
829
+ if not result_df.empty:
830
+ result_df = result_df.sort_values('Cost', ascending=False)
831
+ return result_df
832
+
833
+ return None
834
+
835
+ @mcp.resource("config://app")
836
+ def get_config() -> str:
837
+ """Static configuration data"""
838
+ return "App configuration here"
839
+
840
+ def main():
841
+ # Run the server with the specified transport
842
+ mcp.run(transport=os.environ.get('MCP_TRANSPORT', 'stdio'))
843
+
844
+ if __name__ == "__main__":
845
+ main()