Repository navigation
How to get session_id in tool #485
Copy link
Copy link
Closed
Labels
P1Significant bug affecting many users, highly requested featureSignificant bug affecting many users, highly requested featureenhancementRequest for a new feature that's not currently supportedRequest for a new feature that's not currently supportedready for workEnough information for someone to start working onEnough information for someone to start working on
Description
Activity
Im having the same issue also, i can see the session_id is being passed
mark
+1
from mcp.server.fastmcp import Context, FastMCP ... def _get_session_id(ctx: Context) -> int: """Get a unique session from the context.""" return id(ctx.session) ... @mcp.tool() async def your_tool(ctx: Context = None): """Some cool description""" session_id = _get_session_id(ctx) ...
Reacted by Amir Arad and Felix Weinbergerdef get_session_id(ctx: Context) -> str: """ Get a consistent session ID from an MCP context regardless of transport. Args: ctx: The MCP context object Returns: A unique session identifier that persists for the duration of the session """ if not ctx or not hasattr(ctx, 'session') or not ctx.session: raise ValueError("Invalid context - missing session") session = ctx.session # Check if we already assigned a session ID internal_attr_name = '__session_id' if hasattr(session, internal_attr_name): return getattr(session, internal_attr_name) session_id = f"s_{id(session):x}_{random.randint(0, 255):02x}" setattr(session, internal_attr_name, session_id) return session_id
- addedenhancementRequest for a new feature that's not currently supportedRequest for a new feature that's not currently supportedready for workEnough information for someone to start working onEnough information for someone to start working onP1Significant bug affecting many users, highly requested featureSignificant bug affecting many users, highly requested feature
on Oct 7, 2025 - added a commit that references this issue
on Nov 10, 2025 Now tracked in #2098.
Metadata
Metadata
Assignees
Labels
P1Significant bug affecting many users, highly requested featureSignificant bug affecting many users, highly requested featureenhancementRequest for a new feature that's not currently supportedRequest for a new feature that's not currently supportedready for workEnough information for someone to start working onEnough information for someone to start working on
in the function of @mcp.tool , i want to get session_id, how i can get it?