Open
Description
I'd like to be able to retrieve the client_id
from inside a tool call on an MCP server. However, it is always None
. What do I need to set in the MCP client in order to be able to retrieve it on the server?
The client code is basically taken from the documentation:
from mcp import ClientSession, StdioServerParameters, types
from mcp.client.stdio import stdio_client
server_params = StdioServerParameters(
command="python", # Executable
args=["example_server.py"], # Optional command line arguments
env=None, # Optional environment variables
)
async def run():
async with stdio_client(server_params) as (read, write):
async with ClientSession(
read, write
) as session:
# Initialize the connection
await session.initialize()
# Call a tool
result = await session.call_tool("my_tool")
The tool function is as follows:
mcp = FastMCP("docint")
@mcp.tool()
async def my_tool(ctx: Context) -> Optional[str]:
"""
Extracts client ID and request ID from the context.
Returns:
client ID and request ID from the context.
"""
return f"{ctx.client_id} {ctx.request_id}" # <----- THIS
This prints None 2
(client_id
as None
).
What am I missing?
Thank you.
Metadata
Metadata
Assignees
Labels
No labels