From 38ef8a78b6f117a485895389378a6a432a3a042e Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Fri, 12 Sep 2025 15:47:05 +0200 Subject: [PATCH 1/2] fix(fastapi): do not read request body for fastapi_mcp --- sentry_sdk/integrations/fastapi.py | 8 +++++++- sentry_sdk/integrations/starlette.py | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index 1473cbcab7..db8beb46cc 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -109,7 +109,13 @@ async def _sentry_app(*args, **kwargs): ) sentry_scope = sentry_sdk.get_isolation_scope() extractor = StarletteRequestExtractor(request) - info = await extractor.extract_request_info() + + request_scope = request.scope + is_fastapi_mcp = ( + request_scope.get("endpoint") + and "FastApiMCP" in request_scope["endpoint"].__qualname__ + ) + info = await extractor.extract_request_info(read_body=not is_fastapi_mcp) def _make_request_event_processor(req, integration): # type: (Any, Any) -> Callable[[Event, Dict[str, Any]], Event] diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index c7ce40618b..3f8d373838 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -600,8 +600,8 @@ def extract_cookies_from_request(self): return cookies - async def extract_request_info(self): - # type: (StarletteRequestExtractor) -> Optional[Dict[str, Any]] + async def extract_request_info(self, read_body=True): + # type: (StarletteRequestExtractor, bool) -> Optional[Dict[str, Any]] client = sentry_sdk.get_client() request_info = {} # type: Dict[str, Any] @@ -623,6 +623,9 @@ async def extract_request_info(self): request_info["data"] = AnnotatedValue.removed_because_over_size_limit() return request_info + if not read_body: + return request_info + # Add JSON body, if it is a JSON request json = await self.json() if json: From 56ec01ca86e262afcf4de411fed98ee16d0da2a0 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Fri, 12 Sep 2025 17:56:09 +0200 Subject: [PATCH 2/2] More defensive check --- sentry_sdk/integrations/fastapi.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index db8beb46cc..cc0aef90e0 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -111,11 +111,12 @@ async def _sentry_app(*args, **kwargs): extractor = StarletteRequestExtractor(request) request_scope = request.scope - is_fastapi_mcp = ( - request_scope.get("endpoint") - and "FastApiMCP" in request_scope["endpoint"].__qualname__ - ) - info = await extractor.extract_request_info(read_body=not is_fastapi_mcp) + if "endpoint" in request_scope: + qualname = getattr(request_scope["endpoint"], "__qualname__", None) + read_body = qualname is None or "FastApiMCP" not in qualname + info = await extractor.extract_request_info(read_body) + else: + info = await extractor.extract_request_info(True) def _make_request_event_processor(req, integration): # type: (Any, Any) -> Callable[[Event, Dict[str, Any]], Event]