Skip to main content

Sample code for using Amazon Bedrock with Chainlit and MCP servers

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 90.8 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 127d7bd08dc8548b8acc20ae0588aa612cfcdcf46def6f2678dca8dcd2164664
MD5 22afea350e5cbb408d94ef81c0763b8a
BLAKE2b-256 57f68fa4c5b69bb125d5692d3861344bf34677afe1f99ed87c824af02677d000

See more details on using hashes here.

File details

Details for the file iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for iflow_mcp_aws_samples_sample_bedrock_chainlit_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d1ad4b39a4b50a6991608bf619f24774384dc8e836cb15d32cead8edbb474b9
MD5 aecc7b72457584be7d6935ef3daf282b
BLAKE2b-256 cf005356e6e20368257d9bc7b7fcecf300943f15e23a3ce56f7c6f673196192f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page