Skip to content

How to set / get client_id? #373

Open
@samirbajaj

Description

@samirbajaj

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions