aws-bedrock-mcp-lambda

aws-bedrock-mcp-lambda

3.2

If you are the rightful owner of aws-bedrock-mcp-lambda and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

This project demonstrates how to create an MCP client and server hosted in an AWS Lambda accessed via API Gateway that interacts with AWS Bedrock as the model.

AWS Lambda Handling MCP Client and Server using Bedrock

This project demonstrates how to create an MCP client and server hosted in an AWS lambda accessed via API Gateway that interacts with AWS Bedrock as the model.

Prerequisites

Project Structure

hello-lambda/
├── README.md
├── cdk/
│   ├── cdk.json           # CDK configuration
│   └── mcp_stack.py       # CDK stack deployment script
├── lambda/
│   ├── mcp_handler.py      # Lambda handler entry point
│   ├── mcp_client.py       # MCP client that interacts with MCP server & Bedrock
│   ├── mcp_server.py       # MCP server contains all the exposed tools
│   ├── test_mcp_handler.py # Local test script for the handler
│   └── requirements.txt    # Lambda dependencies
└── tests/                  # Tests directory

Setup

  1. Create and activate a virtual environment:
pyenv install 3.12
pyenv virtualenv 3.12 venv
pyenv activate venv
  1. Install dependencies:

There are separate requirements.txt for the lambda and CDK to support a separate bundling process. To test locally you should install the requirements.txt file in the lambda directory.

pip install -r cdk/requirements.txt
pip install -r lambda/requirements.txt

Local Testing

Before deploying to AWS you can test the Lambda function locally. this test should test the weather tool:

cd lambda
python test_mcp_handler.py"

Deployment

  1. Bootstrap your AWS environment (if not already done):
cd cdk
cdk bootstrap
  1. Deploy the MCP stack which includes an API Gateway and Lambda function:
cdk deploy
  1. Note the configuration output parameters:
BedrockMcpStack.ApiUrl = https://<APP_ID>.execute-api.us-west-2.amazonaws.com/prod/
BedrockMcpStack.FunctionName = BedrockMcpStack-BedrockMcpFunction<FUNCTION_ID>

Testing the Lambda

After deployment, you can test your Lambda function via the API gateway using curl:

curl -G "https://<BedrockMcpStack.ApiUrl>" --data-urlencode "query=What's the temperature in New York City"

Note that the API gateway has a strict 1-minute timeout so for queries that take longer try running the following command directly against the AWS Lambda function:

aws lambda invoke  --function-name <BedrockMcpStack.FunctionName>  --payload '{"queryStringParameters": {"query": "Summaries key points from the website - https://en.wikipedia.org/wiki/Black_hole"}}'  --cli-binary-format raw-in-base64-out output.json
cat output.json

Cleanup

To avoid incurring charges, delete the resources when you're done:

cdk destroy