Repository navigation
Pass entire request object to handlers; add raw request to MCP base Request #195
Description
Activity
I have developed web servers that integrate MCP SSE functionality:
- Using FastAPI: fastapi_mcp_sse
- Using Starlette: starlette_mcp_sse
These servers can be extended with custom routes while retaining full MCP SSE capabilities. Since these example servers are fully developed platforms, it is possible to add the auth in FastAPI or Starlette. In /sse route and handle_sse function, add the auth part there: https://github.com/panz2018/fastapi_mcp_sse/blob/main/src/app.py#L35
Reacted by Yassine LassouedThank you @panz2018 and @mconflitti-pbc for sharing your code.
The question below is now irrelevant :-). You may now skip this comment.
I am struggling to get the authentication headers in the MCP endpoints. If I understood it correctly, you are suggesting to use functionhandle_sseto somehow inject the authentication headers. It is not clear to me though how to do so. is there a way to inject the headers into the request context or as request parameters?I ended up implementing a solution similar to that suggested by @mconflitti-pbc. But rather than injecting the "raw" request into
Request, I injected the request scope (which contains the headers) intoRequestContext, which is available from the application context from within any MCP endpoint. I did not fork the repo to modify the code. Instead I extended the classes, with the hope that request headers get supported sometime soon in the mcp Python SDK.Anyway, for those who are interested in the solution, attached is the source code.
Now, in the mcp router, you can access the request scope from any type of endpoint as follows:
from .mcp.server import FastMCP mcp = FastMCP("MCP") @mcp.tool(name="echo_tool") def echo_tool(message: str) -> str: ctx = mcp.get_context() request_context = ctx.request_context scope= request_context.scope headers = {k: v for k, v in scope.get("headers", {})} if scope is not None else {} """Echo a message as a tool""" return f"Tool echo: {message}. These are your headers: {headers}"
To include the MCP router in your main, assuming that
appis aFastAPIinstance:app.mount("/", mcp.sse_app())
I hope this will be helpful.
Reacted by Matt Conflitti, Devon Fulcher, Francesco Cabras, 了空, Krb, Qi Tong, trend-wayne-ye and Min@dsp-ant, any interest in implementing the approach described above directly in the MCP SDK classes? If so, I would be happy to contribute this "feature", unless it violates the specifications of course or somebody else is already working on a solution to support request headers.
Reacted by Devon Fulcher and KrbThat seems like a reasonable way of going about it! One reason to pass the entire request would be to allow access to other things than just the headers, but this would be sufficient for my use case. Would definitely encourage you to open a PR!
Reacted by Yassine LassouedThanks @mconflitti-pbc ! Yes, I can pass the whole request along instead of the scope. It's the exact same flow :-). It's actually preferable as it allows you to get anything you need from the request. I'll open a PR then.
Reacted by Ryan EgglestonWill be happy to take a look when it is up!
Reacted by Yassine Lassoued@ylassoued this seems feasible to me, nice work!
Update:
Was able to get this working, created a commit with my working code for handling APIToken auth over here
mifunedev/mcp-sse@5951778nice solution @ylassoued Close?
Reacted by JeongHoon Lee and ShpeedleReacted by Francesco CabrasThank you @ryaneggz! Actually, I have just implemented this (differently though) in my fork: https://github.com/ylassoued/python-sdk/tree/ylassoued/feat-request. Following @mconflitti-pbc's suggestion, I went for injecting the whole request (instead of the request scope) into
RequestContext. Besides, sinceRequestContextis generic (not Scarlette- or HTTP-bound), I had to make the request type generic, and had to propagate this in all the code. Please refer to my fork above. I will create a PR.Reacted by Ryan Eggleston, Trung Nguyen, Shpeedle, MarcusKoolaard and Zhi Min- addedenhancementRequest for a new feature that's not currently supportedRequest for a new feature that's not currently supported
on Sep 29, 2025 This looks addressed via #816



Is your feature request related to a problem? Please describe.
I have an MCP server that sits in front of my API to allow LLMs to interact with it. My API requires an authorization header.
I have hacked a way to do this in my fork, but essentially the MCP client is able to pass through headers. Only need to use this for the
/sserequest. Currently, the handlers extract the arguments they need in the decorator. We could instead add a field to the base Request class calledraw_requestorheadersif we just need that and then ensure this is added to the request object before passing it to the handler.Describe the solution you'd like
and then use this like:
I know auth is a part of the 2025 H1 roadmap so this may be usurped already in terms of how things will be supported. This goes beyond auth headers though since it could be useful to have access to the raw request in total instead within the tool execution context.