Skip to content

Commit bee7adc

Browse files
committed
fix: use streamable_http_app() instead of http_app() for FastMCP Lambda
1 parent b1f312d commit bee7adc

5 files changed

Lines changed: 36 additions & 20 deletions

File tree

src/assets/__tests__/__snapshots__/assets.snapshot.test.ts.snap

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ exports[`Assets Directory Snapshots > File listing > should match the expected f
398398
"mcp/python-fastmcp-lambda/README.md",
399399
"mcp/python-fastmcp-lambda/handler.py",
400400
"mcp/python-fastmcp-lambda/pyproject.toml",
401+
"mcp/python-fastmcp-lambda/run.sh",
402+
"mcp/python-fastmcp-lambda/server.py",
401403
"mcp/python-lambda/README.md",
402404
"mcp/python-lambda/handler.py",
403405
"mcp/python-lambda/pyproject.toml",
@@ -670,7 +672,7 @@ exports[`Assets Directory Snapshots > MCP assets > mcp/mcp/python-fastmcp-lambda
670672
FastMCP Server for AWS Lambda with Function URL.
671673
672674
This template shows:
673-
- FastMCP server running on Lambda via Mangum ASGI adapter
675+
- FastMCP server running on Lambda via Lambda Web Adapter + uvicorn
674676
- HTTP tool patterns with proper error handling
675677
- Retry logic and response validation
676678
@@ -681,13 +683,12 @@ import logging
681683
from typing import Any
682684
683685
import httpx
684-
from mangum import Mangum
685686
from mcp.server.fastmcp import FastMCP
686687
687688
logging.basicConfig(level=logging.INFO, format="%(levelname)s - %(message)s")
688689
logger = logging.getLogger(__name__)
689690
690-
mcp = FastMCP("{{ Name }}")
691+
mcp = FastMCP("{{ Name }}", stateless_http=True, host="0.0.0.0")
691692
692693
HTTP_TIMEOUT = 10.0
693694
MAX_RETRIES = 2
@@ -776,10 +777,6 @@ async def fetch_post(post_id: int) -> str:
776777
f"Title: {data['title']}\\n\\n"
777778
f"{data['body']}"
778779
)
779-
780-
781-
# Create ASGI app from FastMCP server and wrap with Mangum for Lambda
782-
lambda_handler = Mangum(mcp.http_app(), lifespan="off")
783780
"
784781
`;
785782
@@ -789,22 +786,38 @@ requires = ["hatchling"]
789786
build-backend = "hatchling.build"
790787
791788
[project]
792-
name = "{{ Name }}"
789+
name = "{{ name }}"
793790
version = "0.1.0"
794791
description = "FastMCP Server on AWS Lambda"
795792
readme = "README.md"
796793
requires-python = ">=3.10"
797794
dependencies = [
798-
"mcp[cli] >= 1.2.0",
795+
"mcp[cli] >= 1.18.0",
799796
"httpx >= 0.27.0",
800-
"mangum >= 0.19.0",
797+
"fastapi >= 0.115.0",
798+
"uvicorn >= 0.34.0",
801799
]
802800
803801
[tool.hatch.build.targets.wheel]
804802
packages = ["."]
805803
"
806804
`;
807805
806+
exports[`Assets Directory Snapshots > MCP assets > mcp/mcp/python-fastmcp-lambda/run.sh should match snapshot 1`] = `
807+
"#!/bin/bash
808+
exec python -m uvicorn --port=$PORT server:app
809+
"
810+
`;
811+
812+
exports[`Assets Directory Snapshots > MCP assets > mcp/mcp/python-fastmcp-lambda/server.py should match snapshot 1`] = `
813+
"from fastapi import FastAPI
814+
from handler import mcp
815+
816+
app = FastAPI(lifespan=lambda app: mcp.session_manager.run())
817+
app.mount("/", mcp.streamable_http_app())
818+
"
819+
`;
820+
808821
exports[`Assets Directory Snapshots > MCP assets > mcp/mcp/python-lambda/README.md should match snapshot 1`] = `
809822
"# {{ Name }}
810823

src/assets/mcp/python-fastmcp-lambda/handler.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
FastMCP Server for AWS Lambda with Function URL.
33
44
This template shows:
5-
- FastMCP server running on Lambda via Mangum ASGI adapter
5+
- FastMCP server running on Lambda via Lambda Web Adapter + uvicorn
66
- HTTP tool patterns with proper error handling
77
- Retry logic and response validation
88
@@ -13,13 +13,12 @@
1313
from typing import Any
1414

1515
import httpx
16-
from mangum import Mangum
1716
from mcp.server.fastmcp import FastMCP
1817

1918
logging.basicConfig(level=logging.INFO, format="%(levelname)s - %(message)s")
2019
logger = logging.getLogger(__name__)
2120

22-
mcp = FastMCP("{{ Name }}")
21+
mcp = FastMCP("{{ Name }}", stateless_http=True, host="0.0.0.0")
2322

2423
HTTP_TIMEOUT = 10.0
2524
MAX_RETRIES = 2
@@ -108,7 +107,3 @@ async def fetch_post(post_id: int) -> str:
108107
f"Title: {data['title']}\n\n"
109108
f"{data['body']}"
110109
)
111-
112-
113-
# Create ASGI app from FastMCP server and wrap with Mangum for Lambda
114-
lambda_handler = Mangum(mcp.http_app(), lifespan="off")

src/assets/mcp/python-fastmcp-lambda/pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ requires = ["hatchling"]
33
build-backend = "hatchling.build"
44

55
[project]
6-
name = "{{ Name }}"
6+
name = "{{ name }}"
77
version = "0.1.0"
88
description = "FastMCP Server on AWS Lambda"
99
readme = "README.md"
1010
requires-python = ">=3.10"
1111
dependencies = [
12-
"mcp[cli] >= 1.2.0",
12+
"mcp[cli] >= 1.18.0",
1313
"httpx >= 0.27.0",
14-
"mangum >= 0.19.0",
14+
"fastapi >= 0.115.0",
15+
"uvicorn >= 0.34.0",
1516
]
1617

1718
[tool.hatch.build.targets.wheel]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
exec python -m uvicorn --port=$PORT server:app
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from fastapi import FastAPI
2+
from handler import mcp
3+
4+
app = FastAPI(lifespan=lambda app: mcp.session_manager.run())
5+
app.mount("/", mcp.streamable_http_app())

0 commit comments

Comments
 (0)