Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds functionality to retrieve the current Starlette request from the FastMCP context. The key changes include:
- Introducing a context manager and a ContextVar to store the Starlette request.
- Wrapping the SSE Starlette app with a new RequestMiddleware.
- Adding the get_starlette_request method in the Context class for retrieving the active Starlette request.
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/fastmcp/utilities/http.py | New context manager and middleware to track the active Starlette request. |
| src/fastmcp/server/server.py | Wraps the SSE app with RequestMiddleware to support request tracking. |
| src/fastmcp/server/context.py | Adds get_starlette_request to the Context class and updates Pydantic model configuration. |
Files not reviewed (1)
- docs/servers/context.mdx: Language not supported
Comments suppressed due to low confidence (2)
src/fastmcp/server/context.py:230
- [nitpick] The Context method 'get_starlette_request' delegates to a global function 'get_current_starlette_request'; consider renaming the global function to '_get_current_starlette_request' to clarify its internal use.
def get_starlette_request(self) -> Request:
src/fastmcp/utilities/http.py:42
- [nitpick] Consider verifying that the 'scope' contains the expected HTTP keys before instantiating a Request in RequestMiddleware to prevent potential runtime errors when handling non-HTTP requests.
async def __call__(self, scope, receive, send):
This was referenced May 2, 2025
Closed
jordicore
pushed a commit
to jordicore/fastmcp
that referenced
this pull request
Jul 2, 2025
Add method for retrieving current starlette request to FastMCP context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Easily access the current starlette request from context, as described in modelcontextprotocol/python-sdk#380
NOTE: renamed
get_http_requestin #303Server:
Client: