fix: attachment route handler signature (Request vs path params) - #521
fix: attachment route handler signature (Request vs path params)#521dsh wants to merge 1 commit into
Conversation
FastMCP's custom_route decorator creates Starlette routes that pass a Request object as the first argument. The serve_attachment handler used a FastAPI-style signature (file_id: str), so file_id received the Request object instead of the path parameter, causing metadata lookup to fail with a 404. Fix: Accept Request and extract file_id from request.path_params, matching the pattern used by the health_check handler.
📝 WalkthroughWalkthroughThe serve_attachment route handler method signature was refactored to accept a full request object instead of directly receiving a file_id parameter. The file_id is now extracted from request.path_params. All existing logic, response behavior, and error handling remain unchanged. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
This patch and comments were AI-generated but human-reviewed and tested. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
core/server.py (1)
440-445: Good fix! Consider adding type annotation for consistency.The fix correctly aligns with FastMCP's
custom_routebehavior, which passes a StarletteRequestobject as the first argument. This matches the pattern used byhealth_checkat line 425.For consistency with other handlers in this file (
health_check,legacy_oauth2_callback), consider adding the type annotation:🔧 Suggested improvement
`@server.custom_route`("/attachments/{file_id}", methods=["GET"]) -async def serve_attachment(request): +async def serve_attachment(request: Request): """Serve a stored attachment file.""" from core.attachment_storage import get_attachment_storage file_id = request.path_params["file_id"]🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@core/server.py` around lines 440 - 445, Add the Starlette Request type annotation to the serve_attachment handler signature to match other handlers like health_check: update the serve_attachment parameter to accept request: Request and ensure Request is imported (e.g., from starlette.requests import Request) at the top of the file if not already present; keep the function name serve_attachment and its internal logic unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@core/server.py`:
- Around line 440-445: Add the Starlette Request type annotation to the
serve_attachment handler signature to match other handlers like health_check:
update the serve_attachment parameter to accept request: Request and ensure
Request is imported (e.g., from starlette.requests import Request) at the top of
the file if not already present; keep the function name serve_attachment and its
internal logic unchanged.
|
Hm, we actually removed request from that very function a few months back because it was erroring for lack of a request object. Personally, I struggle to see the value of rehosting attachments in general - I run stateless mode and when I pull attachments it just links to the attachment directly in gmail, which is more practical and secure especially from an ediscovery/dlp perspective. Clearly though people have a need so appreciate the PR. Seems like you're right and get_attachment_metadata currently receives a Request object, so we need to parse as such. Thanks! |
|
Merged with a test and request object typing |
Summary
The
/attachments/{file_id}endpoint returns 404 for all requests becauseserve_attachmentuses a FastAPI-style function signature instead of the StarletteRequestpattern required by FastMCP'scustom_route.Problem
FastMCP's
custom_routedecorator creates Starlette routes that pass aRequestobject as the first argument to the handler. The current handler signature:receives the
Requestobject asfile_id, soget_attachment_metadata()is called with a Request object instead of a UUID string, and always fails.The
health_checkhandler on line 432 correctly uses(request: Request)— this fix applies the same pattern toserve_attachment.Fix
Testing
Verified end-to-end in a Docker deployment with
--transport streamable-http --tool-tier complete:get_gmail_attachment_content→ file saved to disk, download URL returnedcurlon the download URL → valid PDF returned (55 KB, PDF v1.3)Summary by CodeRabbit