Building a Serverless MCP Server with AWS Lambda and Bedrock

Listen to this Post

Featured Image
The Model Context Protocol (MCP) is a new specification that enables external systems to pass contextual data to AI models efficiently. A serverless approach using AWS Lambda and Amazon Bedrock provides a scalable and cost-effective way to deploy MCP servers for AI model interactions.

Setting Up an MCP Server with AWS SAM

Using the Serverless Application Model (SAM), you can quickly deploy an MCP server. Below is a step-by-step guide based on Davide De Sio’s minimal serverless MCP server setup.

Prerequisites

  • AWS Account
  • AWS SAM CLI installed (pip install aws-sam-cli)
  • Basic knowledge of YAML for IaC

SAM Template for MCP Server

AWSTemplateFormatVersion: '2010-09-09' 
Transform: AWS::Serverless-2016-10-31 
Resources: 
MCPLambdaFunction: 
Type: AWS::Serverless::Function 
Properties: 
CodeUri: mcp_lambda/ 
Handler: app.lambda_handler 
Runtime: python3.9 
Events: 
ApiEvent: 
Type: Api 
Properties: 
Path: /mcp 
Method: POST 

Lambda Function Code (Python)

import json

def lambda_handler(event, context): 
 Parse MCP request 
mcp_data = json.loads(event['body'])

Process context and interact with Amazon Bedrock 
response = { 
"statusCode": 200, 
"body": json.dumps({"message": "MCP request processed", "data": mcp_data}) 
} 
return response 

Deploying the MCP Server

1. Initialize SAM project:

sam init --name mcp-server 

2. Build and deploy:

sam build 
sam deploy --guided 

3. Test the endpoint using `curl`:

curl -X POST https://YOUR_API_ENDPOINT/mcp -d '{"context": "sample_data"}' 

You Should Know: