A comprehensive implementation of the Model Context Protocol (MCP) using AWS Lambda functions, featuring both server and client components for building AI-powered serverless applications.
This repository demonstrates a complete MCP implementation with:
- MCP Server: An AWS Lambda function that exposes tools through the Model Context Protocol
- MCP Client: An AWS Lambda function that consumes MCP tools using an AI agent
- Infrastructure: CloudFormation templates and deployment scripts for AWS
The implementation showcases how to build scalable AI applications using serverless architecture with MCP tools that can be consumed by AI agents.
mcp/
├── server/ # MCP Server Lambda function
│ ├── lambda_function.py # Server implementation with MCP tools
│ ├── template.yaml # SAM template for deployment
│ ├── install-packages.ps1 # Install dependencies
│ ├── package.ps1 # Package for deployment
│ ├── cleanup.ps1 # Clean up build artifacts
│ └── .gitignore # Git ignore rules
├── client/ # MCP Client Lambda function
│ ├── lambda_function.py # Client implementation with AI agent
│ ├── install-packages.ps1 # Install dependencies
│ ├── package.ps1 # Package for deployment
│ ├── cleanup.ps1 # Clean up build artifacts
│ └── .gitignore # Git ignore rules
├── README.md # This file
└── LICENSE # MIT License
The MCP server is an AWS Lambda function that implements the Model Context Protocol, exposing various tools that can be called by MCP clients:
Available Tools:
hello_mcp(name: str)
- Greets users with their nameadd_numbers(a: int, b: int)
- Adds two numbersroll_dice()
- Returns a random number between 1-6echo_message(message: str)
- Echoes back the provided message
Key Features:
- Built using
aws-lambda-powertools
for observability - Uses
awslabs.mcp-lambda-handler
for MCP protocol implementation - Deployed with AWS SAM (Serverless Application Model)
- RESTful API endpoint for MCP communication
The MCP client is an AWS Lambda function that uses an AI agent to interact with MCP tools:
Key Features:
- Uses
strands-agents
for AI agent functionality - Connects to MCP servers via HTTP streaming
- Processes natural language requests and maps them to tool calls
- Returns AI-generated responses based on tool outputs
Example Usage:
{
"message": "add the numbers 5 and 10"
}
- AWS CLI configured with appropriate permissions
- AWS SAM CLI installed
- PowerShell 5.0 or later
- Python 3.13
- pip package manager
-
Install Dependencies:
cd server .\install-packages.ps1
-
Package the Application:
.\package.ps1
-
Deploy with SAM:
sam deploy --guided
-
Install Dependencies:
cd client .\install-packages.ps1
-
Update the MCP Server URL: Edit
lambda_function.py
and update the server URL in thestreamablehttp_client
call. -
Package the Application:
.\package.ps1
-
Deploy to AWS Lambda: Upload the
deployment-package.zip
to your AWS Lambda function.
The repository includes several PowerShell scripts for managing dependencies, packaging, and deployment:
-
install-packages.ps1
- Installs Python dependencies for the MCP server- Downloads
aws-lambda-powertools
andawslabs.mcp-lambda-handler
- Targets Linux x86_64 platform for Lambda compatibility
- Creates dependencies in the
package/
directory
- Downloads
-
package.ps1
- Creates deployment package for AWS Lambda- Combines package dependencies with
lambda_function.py
- Creates
deployment-package.zip
ready for Lambda deployment - Includes compression optimization and size reporting
- Combines package dependencies with
-
cleanup.ps1
- Removes build artifacts and temporary files- Deletes the
package/
directory - Removes
deployment-package.zip
- Useful for clean rebuilds
- Deletes the
-
install-packages.ps1
- Installs Python dependencies for the MCP client- Downloads
aws-lambda-powertools
andstrands-agents
- Targets Linux x86_64 platform for Lambda compatibility
- Creates dependencies in the
package/
directory
- Downloads
-
package.ps1
- Creates deployment package for AWS Lambda- Combines package dependencies with
lambda_function.py
- Creates
deployment-package.zip
ready for Lambda deployment - Includes compression optimization and size reporting
- Combines package dependencies with
-
cleanup.ps1
- Removes build artifacts and temporary files- Deletes the
package/
directory - Removes
deployment-package.zip
- Useful for clean rebuilds
- Deletes the
All scripts include proper copyright headers and MIT license references.
Both Lambda function files include comprehensive documentation and licensing:
- Purpose: Implements MCP server with tool definitions
- Tools Available:
hello_mcp()
- Greeting functionadd_numbers()
- Mathematical operationsroll_dice()
- Random number generationecho_message()
- Message echoing
- Dependencies:
aws-lambda-powertools
,awslabs.mcp-lambda-handler
- Handler:
lambda_handler(event, context)
- Main entry point
- Purpose: MCP client with AI agent for natural language processing
- Features:
- Natural language request processing
- MCP tool discovery and execution
- Intelligent response generation
- Error handling and validation
- Dependencies:
strands-agents
,mcp
, HTTP streaming client - Handler:
lambda_handler(event, context)
- Processes user messages
Both Lambda functions include proper copyright headers and MIT license references.
Both components can be tested locally:
Server Testing:
# The server exposes tools that can be called via MCP protocol
# Test individual tools in the lambda_function.py
Client Testing:
# Run the client locally
if __name__ == "__main__":
test_event = {"message": "add the numbers 5 and 10"}
result = lambda_handler(test_event, None)
print(json.dumps(result, indent=2))
To clean up build artifacts:
# In either server/ or client/ directory
.\cleanup.ps1
aws-lambda-powertools
- AWS Lambda utilities and observabilityawslabs.mcp-lambda-handler
- MCP protocol implementation for Lambda
aws-lambda-powertools
- AWS Lambda utilities and observabilitystrands-agents
- AI agent frameworkmcp
- Model Context Protocol client library
POST /mcp
- Content-Type:
application/json
- Handles MCP protocol requests
POST (Lambda function URL or API Gateway)
{
"message": "Your natural language request"
}
Response:
{
"statusCode": 200,
"body": {
"response": "AI agent response based on tool execution"
}
}
Both Lambda functions can be configured with environment variables for:
- Logging levels
- MCP server endpoints
- AWS service configurations
- Server: 128MB memory, 10-second timeout
- Client: Recommended 512MB+ memory for AI processing
The implementation includes comprehensive error handling:
- Input validation
- MCP protocol error handling
- AWS Lambda error responses
- Graceful degradation for network issues
- Use IAM roles with least privilege principles
- Implement input validation and sanitization
- Consider API Gateway authentication for production use
- Monitor CloudWatch logs for security events
Both components integrate with AWS observability services:
- CloudWatch Logs for application logging
- CloudWatch Metrics for performance monitoring
- AWS X-Ray for distributed tracing (optional)
Ali Almahdi — https://www.ali.ac
This repository is used as part of the training provided by Ali Almahdi. The code and examples are included for teaching and demonstration purposes to students.
No contributions will be accepted for this repository. The content is maintained for training and reference only.
This project is licensed under the MIT License - see the LICENSE file for details.