Skip to content

Conversation

@jlowin
Copy link
Owner

@jlowin jlowin commented May 2, 2025

Easily access the current starlette request from context, as described in modelcontextprotocol/python-sdk#380

NOTE: renamed get_http_request in #303

Server:

from fastmcp import Context, FastMCP

mcp = FastMCP()


@mcp.tool()
async def add_and_headers(a: int, b: int, ctx: Context) -> dict[str, Any]:
    request = ctx.get_starlette_request()
    return dict(result=a + b, headers=request.headers)


if __name__ == "__main__":
    mcp.run(transport="sse")

Client:

import asyncio
from fastmcp import Client


async def main():
    async with Client("http://localhost:8000/sse") as client:
        result = await client.call_tool("add_and_headers", dict(a=1, b=2))
        print(result)


if __name__ == "__main__":
    asyncio.run(main())

@jlowin jlowin added the enhancement Improvement to existing functionality. For issues and smaller PR improvements. label May 2, 2025
Copilot AI review requested due to automatic review settings May 2, 2025 21:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

@jlowin jlowin merged commit b01cef6 into main May 2, 2025
5 checks passed
@jlowin jlowin deleted the context branch May 2, 2025 21:29
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to existing functionality. For issues and smaller PR improvements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants