Hosting MCP Servers on AWS Lambda with Lambda Web Adapter and FastMCP
Explains how to deploy a FastMCP server on AWS Lambda using the AWS Lambda Web Adapter. It covers two approaches: a Docker container method and a zip-based managed runtime method, both using AWS SAM for deployment.
The Model Context Protocol (MCP) is rapidly becoming the standard way to connect LLMs to tools and data. If you're building MCP servers, you've likely come across FastMCP — the most popular Python framework for building MCP applications. But once your server is built, where do you host it?
In this post, we'll walk through how to deploy a FastMCP server to AWS Lambda using the AWS Lambda Web Adapter , with two complete examples: a Docker container approach and a zip-based managed runtime approach.
What is the AWS Lambda Web Adapter?
The AWS Lambda Web Adapter is an open-source project that lets you run web applications on AWS Lambda without any code changes. It works by sitting between the Lambda runtime API and your HTTP application, translating Lambda invocation events into HTTP requests that your app already understands.
This is significant because it means you can take any standard web framework — Flask, FastAPI, Express, or in our case, a FastMCP server running on Uvicorn — and deploy it to Lambda without writing Lambda-specific handler code. Your application just listens on a port, and the adapter handles the rest.
What is FastMCP?
FastMCP is the standard Python framework for building MCP servers, clients, and applications. You declare tools as Python functions, and FastMCP automatically generates the schema, validation, and documentation.
The Application Code
Both examples share the same core application. Here's the entire MCP server:
from fastmcp import FastMCPfrom starlette.responses import JSONResponse
mcp = FastMCP("My MCP Server")
@mcp.tooldef greet(name: str) -> str: return f"Hello, {name}!"
@mcp.custom_route("/healthz", methods=["GET"])async def health_check(request): return JSONResponse({"status": "ok"})
app = mcp.http_app(transport="http", stateless_http=True, json_response=True)A few things to note:
- The
@mcp.tooldecorator registersgreetas an MCP tool. FastMCP handles all the protocol plumbing — schema generation, input validation, and response formatting.
- The
/healthzcustom route is a health check endpoint. This is important because the Lambda Web Adapter uses it to know when your application is ready to receive traffic.
-
mcp.http_app(transport="http", stateless_http=True, json_response=True)creates a standard ASGI application. Thestateless_http=Trueflag is critical for Lambda — it tells FastMCP not to maintain session state between requests, which aligns with Lambda's stateless execution model. Thejson_response=Trueflag ensures responses are returned as JSON rather than server-sent events, which simplifies the HTTP interaction.
Both examples also share the same startup script (
run.sh ):#!/bin/bashPATH=$PATH:$LAMBDA_TASK_ROOT/bin:/opt/bin PYTHONPATH=/opt/python:$LAMBDA_RUNTIME_DIR exec python -m uvicorn --port=$PORT app:appThis launches Uvicorn on the port specified by the
PORT environment variable. The Lambda Web Adapter will forward requests to this port.Example 1: Docker Container Deployment
The first approach packages the FastMCP server as a Docker container image.
The Dockerfile
FROM --platform=linux/amd64 public.ecr.aws/docker/library/python:3.14-slim AS builderWORKDIR /var/taskCOPY requirements.txt ./RUN pip install --no-cache-dir --target=/var/task/deps -r requirements.txt
FROM --platform=linux/amd64 public.ecr.aws/docker/library/python:3.14-slimCOPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:1.0.0-rc1 /lambda-adapter /opt/extensions/lambda-adapterENV PORT=8000 PYTHONPATH=/var/task/depsENV AWS_LWA_READINESS_CHECK_PATH=/healthzENV AWS_LWA_READINESS_CHECK_HEALTHY_STATUS=100-499WORKDIR /var/taskCOPY --from=builder /var/task/deps ./depsCOPY *.py ./CMD exec python -m uvicorn --port=$PORT app:appThe key line is:
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:1.0.0-rc1 /lambda-adapter /opt/extensions/lambda-adapterThis copies the Lambda Web Adapter binary into
/opt/extensions/ , which is where Lambda looks for extensions. That single line is the only change needed to make this a Lambda-compatible container. The rest is a standard Python Docker image.The environment variables configure the adapter:
-
PORT=8000— the port Uvicorn listens on
-
AWS_LWA_READINESS_CHECK_PATH=/healthz— the adapter polls this endpoint to know when the app is ready
-
AWS_LWA_READINESS_CHECK_HEALTHY_STATUS=100-499— any HTTP status in this range is considered healthy
The SAM Template
AWSTemplateFormatVersion: '2010-09-09'Transform: AWS::Serverless-2016-10-31Description: Sample SAM Template for lambda-fastmcp
Globals: Function: Timeout: 29 LoggingConfig: LogFormat: JSON SystemLogLevel: WARN
Resources: MCPServerFunction: Type: AWS::Serverless::Function Properties: PackageType: Image Architectures: - x86_64 MemorySize: 512 Tracing: Active Events: ApiEvent: Type: HttpApi Properties: ApiId: !Ref McpHttpApi Path: /{proxy+} Method: ANY Metadata: Dockerfile: Dockerfile DockerContext: ./my_mcp_server DockerTag: python3.14-v1
McpHttpApi: Type: AWS::Serverless::HttpApi
Outputs: McpApiUrl: Description: MCP API endpoint URL Value: !Sub https://${McpHttpApi}.execute-api.${AWS::Region}.amazonaws.com/mcpPackageType: Image tells Lambda to use the container image. The HttpApi event routes all requests through API Gateway to the function. The Metadata section tells SAM how to build the Docker image.To Deploy
sam buildsam deploy --guidedExample 2: Zip-Based Managed Runtime Deployment
The second approach uses Lambda's managed Python runtime with the Web Adapter delivered as a Lambda Layer, no Docker required.
The SAM Template
AWSTemplateFormatVersion: '2010-09-09'Transform: AWS::Serverless-2016-10-31Description: Sample SAM Template for lambda-fastmcp
Globals: Function: Timeout: 29 LoggingConfig: LogFormat: JSON SystemLogLevel: WARN
Resources: MCPServerFunction: Type: AWS::Serverless::Function Properties: CodeUri: my_mcp_server/ Handler: run.sh Runtime: python3.14 Architectures: - arm64 MemorySize: 512 Tracing: Active EventInvokeConfig: MaximumRetryAttempts: 0 Environment: Variables: AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap PORT: '8000' AWS_LWA_READINESS_CHECK_PATH: /healthz AWS_LWA_READINESS_CHECK_HEALTHY_STATUS: 100-499 Layers: - !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerArm64:26 Events: ApiEvent: Type: HttpApi Properties: ApiId: !Ref McpHttpApi Path: /{proxy+} Method: ANY
McpHttpApi: Type: AWS::Serverless::HttpApi
Outputs: McpApiUrl: Description: MCP API endpoint URL Value: !Sub https://${McpHttpApi}.execute-api.${AWS::Region}.amazonaws.com/mcpInstead of a Dockerfile, the adapter is attached as a Lambda Layer:
Layers: - !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerArm64:26The layer contains the adapter binary and a wrapper script. The
AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap environment variable tells Lambda to use this wrapper script, which intercepts the invocation flow and boots your application via the Handler: run.sh startup command. The wrapper script mechanism is a standard Lambda feature documented here .The configuration environment variables (
PORT , AWS_LWA_READINESS_CHECK_PATH , AWS_LWA_READINESS_CHECK_HEALTHY_STATUS ) serve the same purpose as in the Docker example.To Deploy
sam build --use-containersam deploy --guidedNote the
--use-container flag on sam build, this ensures dependencies are compiled in a Lambda-compatible Linux environment.Testing Your Deployed Server
Once either example is deployed, SAM outputs the
McpApiUrl . You can test it with the FastMCP Python client:import asynciofrom fastmcp import Client
client = Client("https://<api-id>.execute-api.<region>.amazonaws.com/mcp")
async def call_tool(name: str): async with client: result = await client.call_tool("greet", {"name": name}) print(result)
asyncio.run(call_tool("World"))This connects to your Lambda-hosted MCP server over HTTP and calls the
greet tool. You should see Hello, World! in the output.Which Approach Should You Choose?
For simple MCP servers with minimal dependencies, the zip-based approach is simpler and has faster cold starts. For servers with complex dependencies or custom system libraries, the Docker approach gives you full control.
Wrapping Up
The combination of FastMCP and AWS Lambda Web Adapter makes it straightforward to deploy MCP servers to Lambda. You write a standard FastMCP application, add the adapter (either as a Docker layer or a Lambda Layer), and deploy with SAM. No Lambda-specific handler code, no event translation logic. Just your MCP server running on Uvicorn behind API Gateway.
The key ingredients are:
- A FastMCP app configured with
stateless_http=Truefor Lambda's execution model
- A health check endpoint for the adapter's readiness probe
- The Lambda Web Adapter either copied into a Docker image or attached as a Layer
- An API Gateway
HttpApito route traffic to the function
From there, any MCP client can connect to your server over HTTP using the API Gateway URL.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article