This example demonstrates how to deploy a Serverless MCP server to AWS Lambda with Function URL and AWS Lambda response streaming support (no SSE).
-
AWS CLI configured with credentials
aws configure
-
AWS SAM CLI installed
brew install aws-sam-cli # macOS # or pip install aws-sam-cli # Python
-
Node.js 20.x or later
-
Dependencies installed
cd examples/lambda-server npm install
The Lambda function uses:
- Lambda Function URL: Direct HTTPS endpoint without API Gateway
- Response Streaming: AWS Lambda response streaming for large responses
- CORS Support: Configured for web clients
- MCP Protocol: Full Model Context Protocol implementation over HTTP JSON-RPC
npm run build:handlernpm run deployThis will guide you through the deployment process. Accept the defaults or customize as needed.
For subsequent deployments:
npm run deploy:quicknpm run test:deployednpm run localThis starts SAM local Lambda runtime on http://localhost:3001
npm run test:localnpm run build- Build both handler and test clientnpm run build:handler- Build Lambda handler onlynpm run build:client- Build test client onlynpm run deploy- Deploy with guided configurationnpm run deploy:quick- Deploy using saved configurationnpm run local- Start local Lambda runtimenpm run test:local- Test against local Lambdanpm run test:deployed- Test against deployed Lambdanpm run test:client -- <url>- Test specific Lambda URLnpm run logs- Stream CloudWatch logsnpm run validate- Validate SAM templatenpm run clean- Clean build artifacts
The test client (test-client.ts) demonstrates:
- Health Check: Tests basic connectivity
- MCP Protocol: Tests initialize, tools, resources, and prompts
- Tool Execution: Calls echo, calculate, and aws-info tools
- Resource Reading: Fetches Lambda function information
- Performance Testing: Concurrent request testing with
--perfflag
# Get the function URL from deployment output or AWS Console
FUNCTION_URL="https://xxxxx.lambda-url.us-east-1.on.aws/"
# Test health endpoint
curl -H "Accept: application/json" $FUNCTION_URL
# Test MCP request
curl -X POST $FUNCTION_URL \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'
# Test tool execution
curl -X POST $FUNCTION_URL \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"echo","arguments":{"message":"Hello Lambda!"}},"id":2}'# Stream logs in real-time
npm run logs
# Or use AWS CLI
aws logs tail /aws/lambda/serverless-mcp-dev --followView metrics in AWS Console:
- Lambda > Functions > serverless-mcp-dev > Monitoring
Key metrics:
- Invocations
- Duration
- Errors
- Throttles
- Concurrent executions
- Runtime: Node.js 20.x
- Memory: 512 MB (adjustable)
- Timeout: 30 seconds (max for Function URL streaming)
- CORS: Configured for all origins (customize for production)
Set in template.yaml:
Environment:
Variables:
NODE_ENV: dev
LOG_LEVEL: INFO
CUSTOM_VAR: value-
Authentication: Add authentication to Function URL
FunctionUrlConfig: AuthType: AWS_IAM # or use custom authorizer
-
CORS: Restrict origins for production
Cors: AllowOrigins: - 'https://your-domain.com'
-
Monitoring: Set up CloudWatch alarms
- High error rate
- High latency
- Throttling
-
Cost Optimization:
- Adjust memory size based on usage
- Enable reserved concurrency for predictable traffic
- Use provisioned concurrency for low latency
- Ensure Lambda function has response streaming enabled
- Check that
InvokeMode: RESPONSE_STREAMis set in template - Verify Node.js 20.x runtime (streaming requires Node 18+)
- Lambda Function URL has 30-second timeout
- Check Lambda function logs for errors
- Verify request/response format matches JSON-RPC 2.0
- Verify CORS configuration in template.yaml
- Check browser console for specific CORS errors
- Test with curl to isolate client issues
- Ensure requests follow JSON-RPC 2.0 format
- Check that all required MCP fields are present
- Verify tool/resource/prompt names are correct
Remove the deployed stack:
aws cloudformation delete-stack --stack-name serverless-mcp-stackOr keep the stack and just clean local artifacts:
npm run clean