Skip to content

sse-pass-http-req #549

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ async def handle_sse(request: Request) -> None:
streams[0],
streams[1],
self._mcp_server.create_initialization_options(),
extra_metadata={"http_request": request},
)

return Starlette(
Expand All @@ -502,6 +503,15 @@ async def handle_sse(request: Request) -> None:
],
)

def get_http_request(self) -> Request | None:
ctx = self.get_context()
if (ctx.request_context and
ctx.request_context.meta and
hasattr(ctx.request_context.meta, "extra_metadata")):
req: Request = ctx.request_context.meta.extra_metadata.get("http_request") # type: ignore
return req
return None

async def list_prompts(self) -> list[MCPPrompt]:
"""List all available prompts."""
prompts = self._prompt_manager.list_prompts()
Expand Down
6 changes: 5 additions & 1 deletion src/mcp/server/lowlevel/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ async def run(
# but also make tracing exceptions much easier during testing and when using
# in-process servers.
raise_exceptions: bool = False,
extra_metadata: dict[str, Any] | None = None,
):
async with AsyncExitStack() as stack:
lifespan_context = await stack.enter_async_context(self.lifespan(self))
Expand All @@ -489,7 +490,10 @@ async def run(
async with anyio.create_task_group() as tg:
async for message in session.incoming_messages:
logger.debug(f"Received message: {message}")

if (hasattr(message, "request_meta") and
getattr(message, "request_meta")):
message.request_meta = message.request_meta or types.RequestParams.Meta() # type: ignore
message.request_meta.extra_metadata = extra_metadata # type: ignore
tg.start_soon(
self._handle_message,
message,
Expand Down
1 change: 1 addition & 0 deletions src/mcp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
class RequestParams(BaseModel):
class Meta(BaseModel):
progressToken: ProgressToken | None = None
extra_metadata: dict[str, Any] | None = None
"""
If specified, the caller requests out-of-band progress notifications for
this request (as represented by notifications/progress). The value of this
Expand Down
Loading