Skip to content
Closed
Changes from all 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
6 changes: 6 additions & 0 deletions src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from mcp.server.auth.middleware.auth_context import AuthContextMiddleware
from mcp.server.auth.middleware.bearer_auth import (
AuthenticatedUser,
BearerAuthBackend,
RequireAuthMiddleware,
)
Expand Down Expand Up @@ -1047,6 +1048,11 @@ async def log(
related_request_id=self.request_id,
)

@property
def user(self) -> AuthenticatedUser | None:
"""Get the authenticated user if available."""
return self.request_context.request.user if isinstance(self.request_context.request, Request) else None
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a huge fan of having a helper that depends on introspecting the type of the Request as we can't guarantee .user is available.

Maybe we can use the same pattern as in client_id instead with getattr?

    @property
    def user(self) -> AuthenticatedUser | None:
        """Get the authenticated user if available."""
        return getattr(self.request_context.request, "user", None)

Also would be good to update the README at

- `ctx.request_id` - Unique ID for the current request


@property
def client_id(self) -> str | None:
"""Get the client ID if available."""
Expand Down
Loading