Skip to content

Enhance session start/end hook #1894

@rcxwhiz

Description

@rcxwhiz

Enhancement

At the moment the FastMCP lifespan parameter does not contain any info about the client connection that is beginning/ending (specifically the MCP session ID). This would be useful for remote MCP servers where you want to take some action when a client connects/disconnects. As far as I understand these are the best workarounds right now:

  • Use access_token() in the lifespan to get the identity of an authenticated user (does not differentiate between sessions)
  • Attach some HTTP middleware to the application listening for connection attempts, harvesting MCP session ID from the response
  • Attach some MCP middleware with the on_message() hook to track the first MCP message sent for a session

I believe it would be helpful to add access to the MCP session ID in the lifespan handler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImprovement to existing functionality. For issues and smaller PR improvements.serverRelated to FastMCP server implementation or server-side functionality.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions