sverze/aws-bedrock-mcp-lambda
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
- AWS CLI configured with appropriate credentials
- Node.js (for AWS CDK)
- AWS CDK installed (
npm install -g aws-cdk) - Python 3.11
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
- Create and activate a virtual environment:
pyenv install 3.12
pyenv virtualenv 3.12 venv
pyenv activate venv
- 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
- Bootstrap your AWS environment (if not already done):
cd cdk
cdk bootstrap
- Deploy the MCP stack which includes an API Gateway and Lambda function:
cdk deploy
- 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