From 279299293778afe517b120178c488bdde9dd665d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AE=E0=AE=A9=E0=AF=8B=E0=AE=9C=E0=AF=8D=E0=AE=95?= =?UTF-8?q?=E0=AF=81=E0=AE=AE=E0=AE=BE=E0=AE=B0=E0=AF=8D=20=E0=AE=AA?= =?UTF-8?q?=E0=AE=B4=E0=AE=A9=E0=AE=BF=E0=AE=9A=E0=AF=8D=E0=AE=9A=E0=AE=BE?= =?UTF-8?q?=E0=AE=AE=E0=AE=BF?= Date: Sat, 26 Jul 2025 17:31:06 +0530 Subject: [PATCH 1/2] Add CORS middleware to Starlette app for frontend support Introduces CORS middleware to the Starlette app instances in FastMCP to allow requests from local web frontends. This enables compatibility with web clients running on localhost and related origins, supporting credentials and common HTTP methods and headers. --- src/mcp/server/fastmcp/server.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/mcp/server/fastmcp/server.py b/src/mcp/server/fastmcp/server.py index 924baaa9b..848404e34 100644 --- a/src/mcp/server/fastmcp/server.py +++ b/src/mcp/server/fastmcp/server.py @@ -847,7 +847,19 @@ async def sse_endpoint(request: Request) -> Response: routes.extend(self._custom_starlette_routes) # Create Starlette app with routes and middleware - return Starlette(debug=self.settings.debug, routes=routes, middleware=middleware) + app = Starlette(debug=self.settings.debug, routes=routes, middleware=middleware) + + # Add CORS middleware for web frontend compatibility + from starlette.middleware.cors import CORSMiddleware + app.add_middleware( + CORSMiddleware, + allow_origins=["http://localhost:*", "http://127.0.0.1:*", "https://localhost:*", "https://127.0.0.1:*"], + allow_credentials=True, + allow_methods=["GET", "POST", "OPTIONS"], + allow_headers=["Content-Type", "Authorization", "X-Requested-With", "Accept"], + ) + + return app def streamable_http_app(self) -> Starlette: """Return an instance of the StreamableHTTP server app.""" @@ -949,12 +961,25 @@ def streamable_http_app(self) -> Starlette: routes.extend(self._custom_starlette_routes) - return Starlette( + # Create Starlette app with routes and middleware + app = Starlette( debug=self.settings.debug, routes=routes, middleware=middleware, lifespan=lambda app: self.session_manager.run(), ) + + # Add CORS middleware for web frontend compatibility + from starlette.middleware.cors import CORSMiddleware + app.add_middleware( + CORSMiddleware, + allow_origins=["http://localhost:*", "http://127.0.0.1:*", "https://localhost:*", "https://127.0.0.1:*"], + allow_credentials=True, + allow_methods=["GET", "POST", "OPTIONS"], + allow_headers=["Content-Type", "Authorization", "X-Requested-With", "Accept"], + ) + + return app async def list_prompts(self) -> list[MCPPrompt]: """List all available prompts.""" From a8ad68125ae064c44ccff43075e0afddbe2334fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AE=E0=AE=A9=E0=AF=8B=E0=AE=9C=E0=AF=8D=E0=AE=95?= =?UTF-8?q?=E0=AF=81=E0=AE=AE=E0=AE=BE=E0=AE=B0=E0=AF=8D=20=E0=AE=AA?= =?UTF-8?q?=E0=AE=B4=E0=AE=A9=E0=AE=BF=E0=AE=9A=E0=AF=8D=E0=AE=9A=E0=AE=BE?= =?UTF-8?q?=E0=AE=AE=E0=AE=BF?= Date: Sat, 26 Jul 2025 18:04:20 +0530 Subject: [PATCH 2/2] lint --- src/mcp/server/fastmcp/server.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mcp/server/fastmcp/server.py b/src/mcp/server/fastmcp/server.py index 848404e34..f2b747897 100644 --- a/src/mcp/server/fastmcp/server.py +++ b/src/mcp/server/fastmcp/server.py @@ -848,9 +848,10 @@ async def sse_endpoint(request: Request) -> Response: # Create Starlette app with routes and middleware app = Starlette(debug=self.settings.debug, routes=routes, middleware=middleware) - + # Add CORS middleware for web frontend compatibility from starlette.middleware.cors import CORSMiddleware + app.add_middleware( CORSMiddleware, allow_origins=["http://localhost:*", "http://127.0.0.1:*", "https://localhost:*", "https://127.0.0.1:*"], @@ -858,7 +859,7 @@ async def sse_endpoint(request: Request) -> Response: allow_methods=["GET", "POST", "OPTIONS"], allow_headers=["Content-Type", "Authorization", "X-Requested-With", "Accept"], ) - + return app def streamable_http_app(self) -> Starlette: @@ -968,9 +969,10 @@ def streamable_http_app(self) -> Starlette: middleware=middleware, lifespan=lambda app: self.session_manager.run(), ) - + # Add CORS middleware for web frontend compatibility from starlette.middleware.cors import CORSMiddleware + app.add_middleware( CORSMiddleware, allow_origins=["http://localhost:*", "http://127.0.0.1:*", "https://localhost:*", "https://127.0.0.1:*"], @@ -978,7 +980,7 @@ def streamable_http_app(self) -> Starlette: allow_methods=["GET", "POST", "OPTIONS"], allow_headers=["Content-Type", "Authorization", "X-Requested-With", "Accept"], ) - + return app async def list_prompts(self) -> list[MCPPrompt]: