
Hands-on: Build a Slack Assistant with Amazon Bedrock AgentCore and MCP Servers
Deploy AWS Cost Assistant, which answers questions about your AWS costs in a Slack thread, as one CDK stack. Along the way, see how Welt handles Slack, how AWS MCP Server aggregates costs with scripts, and how a Cedar policy on AgentCore Gateway gives the agent only the tools it needs.
In this hands-on guide, you use Amazon Bedrock AgentCore and MCP servers to build AWS Cost Assistant, an assistant that answers questions about your AWS costs in Slack.

The following diagram shows the architecture of the assistant.

I put particular care into the following four points, and I explain each of them in the Design decisions section later in this guide.
- Receiving events from Slack and replying
- Reading and aggregating costs
- Giving the agent only the tools it needs
- Keeping the agent from offering actions it can't perform
This guide takes about 20–30 minutes to complete, and each question costs a few cents.
Prerequisites
Before you start, check that you have the following.
In your AWS account:
- Cost Explorer is enabled (after you enable it, it takes about 24 hours before you can see this month's costs)
- Claude Sonnet 4.6 is available in Amazon Bedrock in us-east-1 (submit the use case form in the Bedrock console, and invoke the model once)
In the environment where you run commands:
- AWS credentials with the permissions needed for deployment, including access to CloudFormation, IAM, AWS Lambda, Amazon Bedrock AgentCore, CloudWatch Logs, and Secrets Manager, and permission to bootstrap your AWS environment for the CDK
- Git, Node.js (v22 or later), npm, and uv
In Slack:
- A workspace where you can install apps, and a channel where it's OK to share your costs
If you use an AWS account that your organization manages, ask your administrator whether you can deploy to it. In particular, if you deploy to an AWS Organizations management account, be aware that the assistant can also answer questions about member accounts' costs.
Deploy
The deployment has three parts: creating the AWS resources, creating the Slack app, and connecting the two.
Get the sample code
git clone https://github.com/iwamot/welt-sample-aws-cost-assistant.gitcd welt-sample-aws-cost-assistant/agentcore/cdknpm ciDeploy the AWS resources with the CDK
Next, use the CDK to deploy the resources inside the box labeled "CDK stack" in the diagram to us-east-1.
- Bootstrap your AWS environment for the CDK, and then deploy the stack
npm run cdk -- bootstrapnpm run cdk -- deploy- When you're asked whether to create IAM roles and policies, enter
y
- When the deployment finishes, note
FunctionUrlin the output
Create and install the Slack app
Then, follow these steps to create and install the Slack app for AWS Cost Assistant, and note its bot token and signing secret.
- Open the AWS Cost Assistant creation page (if the page doesn't open properly, go to the Slack app settings , choose Create New App → Paste a manifest → Continue, switch to the YAML tab, and replace the existing content with the sample manifest )
- Select your workspace, and choose Next
- Choose Create and Install
- In the window that opens, choose Allow
- Back in the original window, open Your app credentials, and note the Bot token
- Choose Go to App Settings, and on the Basic Information page that opens, note the Signing Secret
Store the Slack credentials in Secrets Manager
After that, store the bot token and signing secret of the Slack app in the Secrets Manager secret that the stack created.
- In the Secrets Manager console in us-east-1, open the secret whose name starts with
CostAssistantSlackSecret
- Choose Retrieve secret value, and then Edit
- Enter the bot token and signing secret you noted in
SLACK_BOT_TOKENandSLACK_SIGNING_SECRET, and choose Save (if you didn't note them, you can find the Bot User OAuth Token on the Install App page and the Signing Secret on the Basic Information page of the Slack app settings)
Set the request URL of the Slack app
Finally, set the URL of the deployed Lambda function as the request URL of the Slack app.
- In the Slack app settings, open App Manifest, and on the YAML tab, add the following lines right after
settings:(for bothrequest_urlfields, use theFunctionUrlyou noted when you deployed with the CDK)
event_subscriptions: request_url: <FunctionUrl> bot_events: - message.channels - message.groups interactivity: is_enabled: true request_url: <FunctionUrl>- Choose Save Changes
- In the warning at the top of the page, choose Click here to verify, and check that the warning goes away
If the warning doesn't go away, a Lambda cold start may be the cause, so choose it again. If it still doesn't go away, review your settings so far, especially
request_url and the signing secret. If you changed the value in Secrets Manager, the old value can still be used for up to 5 minutes, so wait a little before you choose it again.Try the assistant
When the deployment is done, ask the assistant a question in your channel, and check that it answers.
- Run
/invite @AWS Cost Assistantto invite the assistant to the channel
- Mention the assistant and ask a question, such as
@AWS Cost Assistant What are my AWS costs this month?
To ask a follow-up question, reply in the same thread; you don't need to mention the assistant again. When a thread starts with a mention of the assistant, the assistant keeps the conversation going in that thread.
Besides this month's costs, you can ask the assistant questions like these:
-
How did my costs change from last month?
-
Where can I save money?
Design decisions
This section explains, one by one, what I decided for each of the four points I listed at the beginning, and why.
Receiving events from Slack and replying
Making an AI agent available in Slack generally requires handling the following tasks:
- Verifying request signatures
- Responding within 3 seconds, and invoking the agent asynchronously
- Handling events that Slack resends
- Converting thread messages into conversation history
- Turning Slack formatting, such as tables, back into text that the model can read
- Streaming replies
Implementing all of this every time you build an AI agent takes a lot of work.
I developed Welt to reduce this work, and it handles all communication with Slack.
Reading and aggregating costs
I used the AWS MCP Server to retrieve and aggregate cost data, with the agent accessing it through AgentCore Gateway.
Actually, I first tried the AWS Billing and Cost Management MCP Server , but the model did the math on its own and sometimes got the amounts wrong. For example, when I asked about this month's costs, it forgot to subtract the credits in the breakdown.
So I looked for a way for the model to aggregate costs using Python scripts. With scripts, the model can calculate totals from the breakdown in code, which can reduce errors from doing the arithmetic itself.
To run these scripts, I considered the AWS MCP Server and AgentCore Code Interpreter . I chose the AWS MCP Server because it doesn't require creating an additional resource. To call AWS APIs from Code Interpreter, you need to create a custom Code Interpreter with an execution role.
After I switched to the AWS MCP Server, the agent answered with the correct totals, at least in my tests. When I asked about this month's costs, it no longer forgot to subtract the credits.
I connected the agent to the AWS MCP Server through the Gateway so that I don't have to change the agent even if I add or replace MCP servers later. The agent only needs to know the Gateway URL, and it isn't affected by what the Gateway connects to.
Going through the Gateway also lets me give the permissions to call AWS APIs to the Gateway's IAM role instead of the agent. This way, I can control access to AWS on the Gateway side, without giving the agent extra permissions.
Giving the agent only the tools it needs
To narrow down the tools, I used a Cedar policy , which you can attach to the Gateway. Cedar is an open source policy language developed by AWS that lets you declare who is allowed to do what. It's also used in Amazon Verified Permissions and AWS Verified Access.
I wanted to narrow down the tools because this assistant doesn't need one of the eight tools the AWS MCP Server currently provides:
get_presigned_url , which is used for file transfers. get_presigned_url is a tool that creates presigned URLs for uploading files to and downloading files from S3. This assistant only looks up costs and answers questions, so it doesn't need access to S3.Besides a Cedar policy, you can also exclude unneeded tools in the agent's code. For example, with Strands Agents, which this sample uses, you specify the allowed tools with
tool_filters .With that approach, however, you have to redeploy the agent whenever you want to change which tools are allowed.
A Cedar policy, on the other hand, is attached to the Gateway outside the agent, so you only need to change the policy. When you change the policy and deploy it, the agent on AgentCore Runtime isn't overwritten; only the tools it can use change.
Tools that the Cedar policy doesn't allow aren't included in the list that the Gateway returns to the agent, and calls to them are denied. Note that this is the behavior when the policy engine is attached to the Gateway in ENFORCE mode; in LOG_ONLY mode, the Gateway only logs the decisions. This sample uses ENFORCE mode.
The following Cedar policy allows the seven tools the agent needs and denies the unneeded
get_presigned_url . principal identifies the agent's IAM role, action specifies the permitted tools, and resource identifies the Gateway.permit( principal == AgentCore::IamEntity::"arn:aws:sts::<account-id>:assumed-role/<agent-role-name>", action in [ AgentCore::Action::"awsmcp___aws___retrieve_skill", AgentCore::Action::"awsmcp___aws___search_documentation", AgentCore::Action::"awsmcp___aws___read_documentation", AgentCore::Action::"awsmcp___aws___list_regions", AgentCore::Action::"awsmcp___aws___get_regional_availability", AgentCore::Action::"awsmcp___aws___run_script", // AgentCore::Action::"awsmcp___aws___get_presigned_url", AgentCore::Action::"awsmcp___aws___get_tasks" ], resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-east-1:<account-id>:gateway/<gateway-id>");With an allowlist like this, tools that are added to the AWS MCP Server later aren't passed to the agent automatically, which gives you peace of mind. When you want the agent to use a new tool, check what it does, and then add one line to
action in the policy.Keeping the agent from offering actions it can't perform
An agent that offers to do things it can't do is hard to use, so I decided to tell the agent its role in the system prompt.
My first system prompt only listed the actions that the Gateway's IAM role allows, like this:
Your AWS access is read-only and limited to these actions: autoscaling:DescribeAutoScalingGroups, autoscaling:DescribeAutoScalingInstances, billing:GetBillingViewData, ...
The allowed actions are only read actions, such as those for Cost Explorer and Compute Optimizer. For example, the agent can view budgets, but it can't create budgets or set up alerts.
With this prompt, however, the agent sometimes offered operations that go beyond reading, such as "Shall I set up a budget alert?"
To align the agent's behavior with its intended purpose, I followed Anthropic's guidance to " Give Claude a role " and added the following two sentences to the beginning of the system prompt.
You answer questions about this AWS account's costs in a Slack thread, for the people in the channel. You run only when someone messages you: you can read cost and usage data, but you can't monitor, notify, or change anything in the account.
After I changed the prompt, the agent stopped offering to do things it couldn't do, at least in my tests. Even when I asked it several times to help me manage budgets, it declined every time, saying that it couldn't create budgets.
Code highlights
The first thing to notice is that
gateway is the only item passed to tools .def new_agent(model: BedrockModel, gateway: MCPClient, prompt: str) -> Agent: return Agent( model=model, system_prompt=prompt, tools=[gateway], plugins=[ContextInjector(today)], callback_handler=None, )gateway only holds an MCP client that connects to the Gateway, and the code doesn't specify the actual tools to use. That's because Strands Agents gets the list of tools from the Gateway. Which tools are included in the list is decided by the Cedar policy attached to the Gateway.By the way, I passed
ContextInjector(today) to plugins to add today's date to the end of the latest message, so that the model can properly answer questions about periods like "this month." It's nice that Strands Agents provides a mechanism like this out of the box.The second thing to notice is that, on the Slack side, the agent only needs
start_reply and renderable_events from welt-io-strands , an adapter between Strands Agents and Welt. start_reply reads the conversation sent by Welt and starts the agent's reply. renderable_events filters the events emitted as the agent replies, returning only those that Welt displays in Slack.async with new_agent(model, gateway, prompt) as agent: async for event in renderable_events( start_reply(agent, dict(payload)), agent=agent ): yield eventBeing able to handle communication with Slack in just a few lines is what makes Welt handy.
Recommendations for production
If you use the sample in production, I recommend the following.
- Run Welt continuously on Amazon ECS or a similar service, and connect it with Socket Mode (this guide uses AWS Lambda to run Welt because it's easy to set up, but running it continuously removes cold starts and the public function URL)
- Set up alerts with AWS Budgets so that you notice when costs exceed what you expect (the main costs are the model, at about USD 0.05 per question, and the Cost Explorer API, at USD 0.01 per request)
- Check important amounts in Cost Explorer (the model can make calculation mistakes or base its answers on nonexistent data)
- Check that the Cedar policy is working with the
AllowDecisionsandDenyDecisionspolicy metrics in CloudWatch
Clean up
To delete the resources you created in this guide, follow these steps.
- In the Slack app settings, open Basic Information for AWS Cost Assistant, choose Delete App, and then choose Delete App again on the confirmation screen
- In the same
agentcore/cdkdirectory that you deployed from, run the following command, and enterywhen you're asked whether to delete the stack
npm run cdk -- destroy- In the CloudWatch Logs console in us-east-1, delete the log group whose name starts with
/aws/bedrock-agentcore/runtimes/WeltCostAssistant_default_
- If you bootstrapped this AWS environment for the first time for this guide, delete the
CDKToolkitstack in the CloudFormation console in us-east-1 after the stack deletion in step 2 finishes (a creation time close to when you deployed is a hint that it's yours)
- If you deleted the stack in step 4, in the S3 console, empty the us-east-1 bucket whose name starts with
cdk-hnb659fds-assets-, and then delete it
Conclusion
In this guide, you used Amazon Bedrock AgentCore and MCP servers to build an assistant that answers questions about your AWS costs in Slack.
Because Welt handles communication with Slack, you can focus on developing the agent. In fact, the agent in this sample doesn't contain a single line of code that calls the Slack API.
The architecture of this sample is also easy to adapt to assistants for other purposes. It's loosely coupled, so you can reuse it for something else just by replacing the MCP server, the Cedar policy, the IAM role, and the system prompt.
AgentCore makes it easy to build agents, so I hope you play around with it and enjoy building things.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article