AWS Machine Learning Blog

Harness the power of MCP servers with HAQM Bedrock Agents

AI agents extend large language models (LLMs) by interacting with external systems, executing complex workflows, and maintaining contextual awareness across operations. HAQM Bedrock Agents enables this functionality by orchestrating foundation models (FMs) with data sources, applications, and user inputs to complete goal-oriented tasks through API integration and knowledge base augmentation. However, in the past, connecting these agents to diverse enterprise systems has created development bottlenecks, with each integration requiring custom code and ongoing maintenance—a standardization challenge that slows the delivery of contextual AI assistance across an organization’s digital ecosystem. This is a problem that you can solve by using Model Context Protocol (MCP), which provides a standardized way for LLMs to connect to data sources and tools.

Today, MCP is providing agents standard access to an expanding list of accessible tools that you can use to accomplish a variety of tasks. In time, MCP can promote better discoverability of agents and tools through marketplaces, enabling agents to share context and have common workspaces for better interaction, and scale agent interoperability across the industry.

In this post, we show you how to build an HAQM Bedrock agent that uses MCP to access data sources to quickly build generative AI applications. Using HAQM Bedrock Agents, your agent can be assembled on the fly with MCP-based tools as in this example:

InlineAgent(
    foundation_model="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
    instruction="You are a friendly assistant for resolving user queries",
    agent_name="SampleAgent",
    action_groups=[
        ActionGroup(
            name="SampleActionGroup",
            mcp_clients=[mcp_client_1, mcp_client_2],
        )
    ],
).invoke(input_text=”Convert 11am from NYC time to London time”)

We showcase an example of building an agent to understand your HAQM Web Service (AWS) spend by connecting to AWS Cost Explorer, HAQM CloudWatch, and Perplexity AI through MCP. You can use the code referenced in this post to connect your agents to other MCP servers to address challenges for your business. We envision a world where agents have access to an ever-growing list of MCP servers that they can use for accomplishing a wide variety of tasks.

Model Context Protocol

Developed by Anthropic as an open protocol, MCP provides a standardized way to connect AI models to virtually any data source or tool. Using a client-server architecture, MCP enables developers to expose their data through lightweight MCP servers while building AI applications as MCP clients that connect to these servers. Through this architecture, MCP enables users to build more powerful, context-aware AI agents that can seamlessly access the information and tools they need. Whether you’re connecting to external systems or internal data stores or tools, you can now use MCP to interface with all of them in the same way. The client-server architecture of MCP enables your agent to access new capabilities as the MCP server updates without requiring any changes to the application code.

MCP architecture

MCP uses a client-server architecture that contains the following components and is shown in the following figure:

  • Host: An MCP host is a program or AI tool that requires access to data through the MCP protocol, such as Claude Desktop, an integrated development environment (IDE), or any other AI application.
  • Client: Protocol clients that maintain one-to-one connections with servers.
  • Server: Lightweight programs that expose capabilities through standardized MCP.
  • Local data sources: Your databases, local data sources, and services that MCP servers can securely access.
  • Remote services: External systems available over the internet through APIs that MCP servers can connect to.

Let’s walk through how to set up HAQM Bedrock agents that take advantage of MCP servers.

Using MCP with HAQM Bedrock agents

In this post, we provide a step-by-step guide on how to connect your favorite MCP servers with HAQM Bedrock agents as Action Groups that an agent can use to accomplish tasks provided by the user. We also introduce HAQM Bedrock Inline Agent SDK , which streamlines the process of invoking inline agents by managing the complex workflow orchestration. The SDK is also packaged with built-in MCP client implementation that provides you with direct access to tools delivered by an MCP server. Without this SDK, developers must write and maintain custom code for:

  • Parsing response streams
  • Handling return control flows
  • Managing state between agent interactions
  • Coordinating API calls

As part of creating an agent, the developer creates an MCP client specific to each MCP server that requires agent communication. When invoked, the agent determines which tools are needed for the user’s task; if MCP server tools are required, it uses the corresponding MCP client to request tool execution from that server.

To orchestrate this workflow, you take advantage of the return control capability of HAQM Bedrock Agents. The following diagram illustrates the end-to-end flow of an agent handling a request that uses two tools. In the first flow, a Lambda-based action is taken, and in the second, the agent uses an MCP server.

Use case: transform how you manage your AWS spend across different AWS services including HAQM Bedrock

To show how an HAQM Bedrock agent can use MCP servers, let’s walk through a sample use case. Imagine asking questions like “Help me understand my Bedrock spend over the last few weeks” or “What were my EC2 costs last month across regions and instance types?” and getting a human-readable analysis of the data instead of raw numbers on a dashboard. The system interprets your intent and delivers precisely what you need—whether that’s detailed breakdowns, trend analyses, visualizations, or cost-saving recommendations. This is useful because what you’re interested in is insights rather than data. You can accomplish this using two MCP servers: a custom-built MCP server for retrieving the AWS spend data and an open source MCP server from Perplexity AI to interpret the data. You add these two MCP servers as action groups in an inline HAQM Bedrock agent. This gives you an AI agent that can transform the way you manage your AWS spend. All the code for this post is available in the GitHub repository.

Let’s walk through how this agent is created using inline agents. You can use inline agents to define and configure HAQM Bedrock agents dynamically at runtime. They provide greater flexibility and control over agent capabilities, enabling users to specify FMs, instructions, action groups, guardrails, and knowledge bases as needed without relying on pre-configured control plane settings. It’s worth noting that you can also orchestrate this behavior without inline agents by using RETURN_CONTROL with the InvokeAgent API.

MCP components in HAQM Bedrock Agents

  1. Host: This is the HAQM Bedrock inline agent. This agent adds MCP clients as action groups that can be invoked through RETURN_CONTROL when the user asks an AWS spend-related question.
  2. Client: You create two clients that establish one-to-one connections with their respective servers: a cost explorer client with specific cost server parameters and a Perplexity AI client with Perplexity server parameters.
  3. Servers: You create two MCP servers that each run locally on your machine and communicate to your application over standard input/output (alternatively, you could also configure the client to talk to remote MCP servers).
    1. Cost Explorer and HAQM CloudWatch Logs (for HAQM Bedrock model invocation log data) and an MCP server to retrieve the AWS spend data.
    2. Perplexity AI MCP server to interpret the AWS spend data.
  4. Data sources: The MCP servers talk to remote data sources such as Cost Explorer API, CloudWatch Logs and the Perplexity AI search API.

Prerequisites

You need the following prerequisites to get started implementing the solution in this post:

  1. An AWS account
  2. Familiarity with FMs and HAQM Bedrock
  3. Install AWS Command Line Interface (AWS CLI) and set up credentials
  4. Python 3.11 or later
  5. AWS Cloud Development Kit (AWS CDK) CLI
  6. Enable model access for Anthropic’s Claude 3.5 Sonnet v2
  7. You need to have your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY so that you can set them using environment variables for the server
  8. The two MCP servers are run as Docker daemons, so you need to have Docker installed and running on your computer

The MCP servers run locally on your computer and need to access AWS services and the Perplexity API. You can read more about AWS credentials in Manage access keys for IAM users. Make sure that your credentials include AWS Identity and Access Manager (IAM) read access to Cost Explorer and CloudWatch. You can do this by using AWSBillingReadOnlyAccess and CloudWatchReadOnlyAccess managed IAM permissions. You can get the Perplexity API key from the Perplexity Sonar API page.

Steps to run

With the prerequisites in place, you’re ready to implement the solution.

  1. Navigate to the InlineAgent GitHub repository.
  2. Follow the setup steps.
  3. Navigate to the cost_explorer_agent This folder contains the code for this post.
    cd examples/mcp/cost_explorer_agent
  4. Create a .env file in cost_explorer_agent directory using example.
    AWS_ACCESS_KEY_ID=
    AWS_SECRET_ACCESS_KEY=
    AWS_REGION=
    BEDROCK_LOG_GROUP_NAME=
    PERPLEXITY_API_KEY=

Note: While this post demonstrates the use of AWS access keys for simplicity and to focus on the MCP use case, in production environments we strongly recommend using AWS IAM roles instead of long-term access keys. If you must use access keys, please follow these security best practices. Never share or expose your access keys publicly (including in code repositories). Implement the principle of least privilege by scoping the IAM policies to only the required permissions, and regularly rotate your access keys (recommended every 90 days). It is also important to use AWS CloudTrail to monitor access key usage and consider using temporary credentials through AWS STS when possible.

  1. Build aws-cost-explorer-mcp server
    git clone http://github.com/aws-samples/sample-cloud-spend-mcp-server.git
    cd sample-cloud-spend-mcp-server
    docker build -t aws-cost-explorer-mcp .
  2. You are now ready to invoke the agent to obtain insights into your AWS spend, execute the agent by running python main.py. The query being processed is: What is HAQM Bedrock? What are the AWS services where I spent most in the last 7 days? Be precise and create a bar graph. The agent establishes connections with two MCP servers and accesses their associated tools. Subsequently, the agent executes a sequential plan utilizing specific tools:
    • Use the Code Interpreter to generate a visual representation (bar graph) of cost distribution
    • Use AWS Cost Explorer MCP server to analyze spending patterns
    • Interfaces with the Perplexity MCP server to retrieve comprehensive information about HAQM Bedrock
    Connected to server with tools: ['get_bedrock_daily_usage_stats', 'get_bedrock_hourly_usage_stats', 'get_ec2_spend_last_day', 'get_detailed_breakdown_by_day']
    Connected to server with tools: ['perplexity_ask']
    
    Thought: Let me break this down into parts:
    1. First, I'll ask about HAQM Bedrock using Perplexity
    2. Then I'll get the cost breakdown for the last 7 days
    3. Finally, I'll create a visualization of the spending data
    
    Tool use: perplexity_ask with these inputs: messages[[{role=user, content=What is HAQM Bedrock? Provide a concise explanation.}] (array)]
    
    Tool use: get_detailed_breakdown_by_day with these inputs: days[7 (integer)]
    
    Thought: Let me analyze the data and create a visualization of the top services by cost. I'll use Python to process the data and create a bar chart.
    
    Code interpreter:
    Generated code
    ```
    import pandas as pd
    import matplotlib.pyplot as plt
    ...
    ```
    
    Code interpreter output:
    aws_costs.png (image/png)
    Directory output created successfully
    
    Thought: Now I can provide a comprehensive answer about HAQM Bedrock and the cost analysis. HAQM Bedrock is a fully managed service that provides access to foundation models (FMs) from leading AI companies through a single API. Based on the cost analysis of the last 7 days, your top spending services were:
    
    1. HAQM Neptune: $24
    2. HAQM SageMaker: $567
    3. EC2 (Compute + Other): $454
    4. HAQM OpenSearch Service: $34
    5. HAQM Bedrock: $1235
    6. HAQM Q: $178

Implementation details

Now that you understand the output produced by an agent, let’s lift the curtain and review some of the important pieces of code that produce the output.

  1. Creating MCP clients: config.py, defines the two MCP clients that talk to your two MCP servers.
    1. Server parameters are defined for the cost explorer and Perplexity clients. The solution uses StdioServerParameters, which configures how the client should communicate over standard input/output (stdio) streams. This contains the parameters required by the server to access the required data through APIs.
      # Cost server parameters
      cost_server_params = StdioServerParameters(
          command="/usr/local/bin/docker",
          args=[
              "run",
              "-i",
              "--rm",
              "-e",
              "AWS_ACCESS_KEY_ID",
              "-e",
              "AWS_SECRET_ACCESS_KEY",
              "-e",
              "AWS_REGION",
              "-e",
              "BEDROCK_LOG_GROUP_NAME",
              "-e",
              "stdio",
              "aws-cost-explorer-mcp:latest",
          ],
          env={
              "AWS_ACCESS_KEY_ID": AWS_ACCESS_KEY_ID,
              "AWS_SECRET_ACCESS_KEY": AWS_SECRET_ACCESS_KEY,
              "AWS_REGION": AWS_REGION,
              "BEDROCK_LOG_GROUP_NAME": BEDROCK_LOG_GROUP_NAME,
          },
      )
      
      # Perplexity server parameters
      perplexity_server_params = StdioServerParameters(
          command="/usr/local/bin/docker",
          args=["run", "-i", "--rm", "-e", "PERPLEXITY_API_KEY", "mcp/perplexity-ask"],
          env={"PERPLEXITY_API_KEY": PERPLEXITY_API_KEY},
      )
    2. In main.py, the MCP server parameters are imported and used to create your two MCP clients.
      cost_explorer_mcp_client = await MCPStdio.create(server_params=cost_server_params)
      perplexity_mcp_client = await MCPStdio.create(server_params=perplexity_server_params)
  1. Configure agent action group: main.py creates the action group that combines the MCP clients into a single interface that the agent can access. This enables the agent to ask your application to invoke either of these MCP servers as needed through return of control.
    # Create action group with both MCP clients
    cost_action_group = ActionGroup(
        name="CostActionGroup",
        mcp_clients=[cost_explorer_mcp_client, perplexity_mcp_client]
    )
  2. Inline agent creation: The inline agent can be created with the following specifications:
    1. Foundation model: Configure your choice of FM to power your agent. This can be any model provided on HAQM Bedrock. This example uses Anthropic’s Claude 3.5 Sonnet model.
    2. Agent instruction: Provide instructions to your agent that contain the guidance and steps for orchestrating responses to user queries. These instructions anchor the agent’s approach to handling various types of queries
    3. Agent name: Name of your agent.
    4. Action groups: Define the action groups that your agent can access. These can include single or multiple action groups, with each group having access to multiple MCP clients or AWS Lambda As an option, you can configure your agent to use Code Interpreter to generate, run, and test code for your application.
# Create and invoke the inline agent
await InlineAgent(
    foundation_model="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
    instruction="""You are a friendly assistant that is responsible for resolving user queries.
    
    You have access to search, cost tool and code interpreter. 
    
    """,
    agent_name="cost_agent",
    action_groups=[
        cost_action_group,
        {
            "name": "CodeInterpreter",
            "builtin_tools": {
                "parentActionGroupSignature": "AMAZON.CodeInterpreter"
            },
        },
    ],
).invoke(
    input_text="<user-query-here>"
)

You can use this example to build an inline agent on HAQM Bedrock that establishes connections with different MCP servers and groups their clients into a single action group for the agent to access.

Conclusion

The Anthropic MCP protocol offers a standardized way of connecting FMs to data sources, and now you can use this capability with HAQM Bedrock Agents. In this post, you saw an example of combining the power of HAQM Bedrock and MCP to build an application that offers a new perspective on understanding and managing your AWS spend.

Organizations can now offer their teams natural, conversational access to complex financial data while enhancing responses with contextual intelligence from sources like Perplexity. As AI continues to evolve, the ability to securely connect models to your organization’s critical systems will become increasingly valuable. Whether you’re looking to transform customer service, streamline operations, or gain deeper business insights, the HAQM Bedrock and MCP integration provides a flexible foundation for your next AI innovation. You can dive deeper on this MCP integration by exploring our code samples.

Here are some examples of what you can build by connecting your HAQM Bedrock Agents to MCP servers:

  • A multi-data source agent that retrieves data from different data sources such as HAQM Bedrock Knowledge Bases, Sqlite, or even your local filesystem.
  • A developer productivity assistant agent that integrates with Slack and GitHub MCP servers.
  • A machine learning experiment tracking agent that integrates with the Opik MCP server from Comet ML for managing, visualizing, and tracking machine learning experiments directly within development environments.

What business challenges will you tackle with these powerful new capabilities?


About the authors

Mark Roy is a Principal Machine Learning Architect for AWS, helping customers design and build generative AI solutions. His focus since early 2023 has been leading solution architecture efforts for the launch of HAQM Bedrock, the flagship generative AI offering from AWS for builders. Mark’s work covers a wide range of use cases, with a primary interest in generative AI, agents, and scaling ML across the enterprise. He has helped companies in insurance, financial services, media and entertainment, healthcare, utilities, and manufacturing. Prior to joining AWS, Mark was an architect, developer, and technology leader for over 25 years, including 19 years in financial services. Mark holds six AWS certifications, including the ML Specialty Certification.

Eashan Kaushik is a Specialist Solutions Architect AI/ML at HAQM Web Services. He is driven by creating cutting-edge generative AI solutions while prioritizing a customer-centric approach to his work. Before this role, he obtained an MS in Computer Science from NYU Tandon School of Engineering. Outside of work, he enjoys sports, lifting, and running marathons.

Madhur Prashant is an AI and ML Solutions Architect at HAQM Web Services. He is passionate about the intersection of human thinking and generative AI. His interests lie in generative AI, specifically building solutions that are helpful and harmless, and most of all optimal for customers. Outside of work, he loves doing yoga, hiking, spending time with his twin, and playing the guitar.

Amit Arora is an AI and ML Specialist Architect at HAQM Web Services, helping enterprise customers use cloud-based machine learning services to rapidly scale their innovations. He is also an adjunct lecturer in the MS data science and analytics program at Georgetown University in Washington, D.C.

Andy Palmer is a Director of Technology for AWS Strategic Accounts. His teams provide Specialist Solutions Architecture skills across a number of speciality domain areas, including AIML, generative AI, data and analytics, security, network, and open source software. Andy and his team have been at the forefront of guiding our most advanced customers through their generative AI journeys and helping to find ways to apply these new tools to both existing problem spaces and net new innovations and product experiences.