Skip to main content

Amazon Bedrock and MCP Server integration, the easy way

This project demonstrates how to integrate foundation models on Amazon Bedrock with Chainlit and MCP (Model Context Protocol) servers to create an interactive chat interface with tool-enhanced capabilities.

Project Overview

This sample application showcases:

  • Integration with Amazon Bedrock using Anthropic Claude 3.7 Sonnet
  • A Chainlit web interface for conversational AI interactions
  • A custom Model Context Protocol (MCP) server that performs basic math operations
  • Streaming responses for a smooth user experience
  • LangChain integration with MCP adapters to connect external tools
  • LangGraph for creating a ReAct (Reasoning and Acting) agent that orchestrates tool use

Prerequisites

Before getting started, ensure you have:

  • AWS Account: You'll need an AWS account with Bedrock access
  • Bedrock Access: Ensure your AWS account has access to the Anthropic Claude 3.7 Sonnet model
  • AWS CLI: Configured with appropriate credentials and permissions
  • Python 3.13+: This project requires Python 3.13 or newer
  • UV: For dependency management and building (install with pip install uv)

Installation

  1. Clone the repository:

    git clone https://github.com/aws-samples/sample-bedrock-mcp.git
    cd sample-bedrock-chainlit-mcp
    
  2. Create a virtual environment and install dependencies using UV:

    uv sync --all-groups
    
  3. Configure AWS credentials (if not already done):

    aws configure
    

Building from Source

To build the project from source using UV:

  1. Build the package:

    uv build
    

This will create distribution files in the dist/ directory, including the wheel file dist/sample_bedrock_chainlit_mcp-0.1.0-py3-none-any.whl that can be installed or used directly as the MCP server.

Usage Guide

Starting the Chainlit Application

To start the Chainlit application:

chainlit run src/ui/app.py

This will launch a web server (typically at http://localhost:8000) with the chat interface.

Setting up the MCP Server

The Math MCP server needs to be connected to provide calculation capabilities:

  1. In the Chainlit web interface, look for the small plug icon under the chat text input element
  2. Click on it to open the MCP connection dialog
  3. Enter the following details:
    • Name: math
    • Command: uvx dist/sample_bedrock_chainlit_mcp-0.1.0-py3-none-any.whl
    • Type: default (stdio)
  4. Click "Confirm"

A successful connection will be indicated in the interface, and the math tools will become available to the Claude model.

Example Usage

Once the Chainlit application is running and the MCP server is connected, you can ask mathematical questions like:

  • "What's (3 + 5) x 12?"
  • "Can you calculate 144 divided by 12?"
  • "If I have 7 apples and get 9 more, then give 4 away, how many do I have left?"

The model will use the MCP tools to perform the calculations and return the results.

Technical Implementation

LangGraph Integration

This project uses LangGraph to create a ReAct agent that follows this workflow:

  1. The agent receives user input via the Chainlit interface
  2. It analyzes the input to determine if mathematical operations are needed
  3. When math is required, it uses the MCP tools to perform calculations
  4. Results are returned to the user with a detailed explanation

The ReAct agent is created using langgraph.prebuilt.create_react_agent(), which orchestrates the reasoning and tool-use process.

LangChain MCP Adapters

The langchain-mcp-adapters package serves as a bridge between LangChain and MCP:

  • load_mcp_tools() converts MCP tools into LangChain-compatible tools
  • These tools are then provided to the LangGraph agent for use in the ReAct loop
  • This enables seamless integration between the Claude model on Amazon Bedrock and the custom math tools

This adapter pattern allows the application to easily incorporate additional MCP servers with different capabilities in the future.

MCP Server Details

This project includes a simple MCP server with the following arithmetic operations:

  • add: Add two numbers together
  • subtract: Subtract one number from another
  • multiply: Multiply two numbers together
  • divide: Divide one number by another

These operations are exposed as tools that the Claude model can use when prompted with mathematical questions.

Development

The project structure includes:

  • src/mcp/server.py: The MCP server implementation with math operations
  • src/ui/app.py: The Chainlit application setup with LangGraph and MCP integration
  • src/utils/: Utility modules for Bedrock integration and streaming

To modify the MCP server or add new capabilities, edit the src/mcp/server.py file and rebuild using uv build.

Troubleshooting

  • If you encounter issues connecting to Bedrock, check your AWS credentials and ensure you have the necessary permissions.
  • For MCP connection issues, verify that the wheel file exists in the dist directory and that you're using the correct command in the Chainlit interface.
  • Check the logs in the terminal running the Chainlit application for detailed error messages. You can set the LOG_LEVEL environment variable to DEBUG to get more detailed logs.

Metadata

Release files for iflow-mcp_aws-samples-sample-bedrock-chainlit-mcp 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iflow-mcp_aws-samples-sample-bedrock-chainlit-mcp 0.1.0
File Size Uploaded
iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0.tar.gz 90.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iflow-mcp_aws-samples-sample-bedrock-chainlit-mcp 0.1.0
File Interpreter ABI Platform
iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 100.3 kB

Release files / iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0.tar.gz

Download URL iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0.tar.gz
Size 90.8 kB
Tags Source
SHA-256 checksum
How to use checksums
127d7bd08dc8548b8acc20ae0588aa612cfcdcf46def6f2678dca8dcd2164664
BLAKE2b-256 checksum
How to use checksums
57f68fa4c5b69bb125d5692d3861344bf34677afe1f99ed87c824af02677d000
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0-py3-none-any.whl

Download URL iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d1ad4b39a4b50a6991608bf619f24774384dc8e836cb15d32cead8edbb474b9
BLAKE2b-256 checksum
How to use checksums
cf005356e6e20368257d9bc7b7fcecf300943f15e23a3ce56f7c6f673196192f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page