Repository navigation
Answer HEAD on the SSE endpoint with GET's headers instead of opening a stream #3759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kludex
wants to merge
5
commits into
main
Choose a base branch
from
sse-head-requests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b82769
Answer HEAD on the SSE endpoint with 405 instead of opening a stream
Kludex 0ebf49d
Answer HEAD on the SSE endpoint with GET's headers and no body
Kludex 43f85b3
Check Host and Origin before answering HEAD, and send nothing after t…
Kludex 58bff01
Serve both SSE routes through one request/response endpoint
Kludex 1e77a6d
Add `SseServerTransport.head_response` so the transport answers HEAD
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,8 +51,7 @@ | |
| from starlette.middleware.authentication import AuthenticationMiddleware | ||
| from starlette.requests import Request | ||
| from starlette.responses import Response | ||
| from starlette.routing import Mount, Route | ||
| from starlette.types import Receive, Scope, Send | ||
| from starlette.routing import Mount, Route, request_response | ||
|
|
||
| from mcp.server.auth.middleware.auth_context import AuthContextMiddleware | ||
| from mcp.server.auth.middleware.bearer_auth import BearerAuthBackend, RequireAuthMiddleware | ||
|
|
@@ -1172,14 +1171,23 @@ def sse_app( | |
| message_path, security_settings=transport_security, max_request_body_size=max_request_body_size | ||
| ) | ||
|
|
||
| async def handle_sse(scope: Scope, receive: Receive, send: Send): # pragma: no cover | ||
| # Add client ID from auth context into request context if available | ||
|
|
||
| async with sse.connect_sse(scope, receive, send) as streams: | ||
| async def sse_endpoint(request: Request) -> Response: | ||
| if request.method == "HEAD": | ||
| # GET's headers without its never-ending stream; a Content-Length would claim an empty one. | ||
| response = Response( | ||
|
Kludex marked this conversation as resolved.
Resolved
|
||
| media_type="text/event-stream", headers={"Cache-Control": "no-store", "X-Accel-Buffering": "no"} | ||
|
Kludex marked this conversation as resolved.
Resolved
|
||
| ) | ||
| del response.headers["content-length"] | ||
| return response | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
| async with sse.connect_sse( # pragma: no cover | ||
| request.scope, | ||
| request.receive, | ||
| request._send, # type: ignore[reportPrivateUsage] | ||
| ) as streams: | ||
| await self._lowlevel_server.run( | ||
| streams[0], streams[1], self._lowlevel_server.create_initialization_options() | ||
| ) | ||
| return Response() | ||
| return Response() # pragma: no cover | ||
|
Kludex marked this conversation as resolved.
Resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents |
||
|
|
||
| # Create routes | ||
| routes: list[Route | Mount] = [] | ||
|
|
@@ -1237,7 +1245,9 @@ async def handle_sse(scope: Scope, receive: Receive, send: Send): # pragma: no | |
| routes.append( | ||
| Route( | ||
| sse_path, | ||
| endpoint=RequireAuthMiddleware(handle_sse, required_scopes, resource_metadata_url), | ||
| endpoint=RequireAuthMiddleware( | ||
| request_response(sse_endpoint), required_scopes, resource_metadata_url | ||
| ), | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
| methods=["GET"], | ||
| ) | ||
| ) | ||
|
|
@@ -1249,11 +1259,6 @@ async def handle_sse(scope: Scope, receive: Receive, send: Send): # pragma: no | |
| ) | ||
| else: | ||
| # Auth is disabled, no need for RequireAuthMiddleware | ||
| # Since handle_sse is an ASGI app, we need to create a compatible endpoint | ||
| async def sse_endpoint(request: Request) -> Response: # pragma: no cover | ||
| # Convert the Starlette request to ASGI parameters | ||
| return await handle_sse(request.scope, request.receive, request._send) # type: ignore[reportPrivateUsage] | ||
|
|
||
| routes.append( | ||
| Route( | ||
| sse_path, | ||
|
|
||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.