[Python] Session_id is always null in fastmcp (https://github.com/jlowin/fastmcp)

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    am exploring the new fastmcp framework(https://github.com/jlowin/fastmcp). i have write a server and a client, and i want to have access to the session_id in the server side(am thinking of using it as key for my cache), but i always git a null value for the session id

    here is the server code:

    from fastmcp import FastMCP, Context


    # Initialize FastMCP server
    mcp = FastMCP("test mcp server")


    @mcp.tool()
    async def request_info(ctx: Context) -> dict:
    """Return information about the current request."""
    return {
    "request_id": ctx.request_id,
    "client_id": ctx.client_id or "Unknown client",
    "session_id": ctx.session_id
    }


    # Basic dynamic resource returning a string
    @mcp.resource("resource://greeting")
    def get_greeting() -> str:
    """Provides a simple greeting message."""
    return "Hello from FastMCP Resources!"



    if __name__ == "__main__":
    # Initialize and run the server
    transport = "streamable"
    if transport == "streamable":
    print("Running server with streamable-http transport ...")
    mcp.run(transport="http")
    elif transport == "sse":
    print("Running server with sse transport ...")
    mcp.run(transport="sse", port=8001, path="/sse")


    and here is the client code:

    from fastmcp import Client
    from contextlib import AsyncExitStack
    import asyncio


    class MCP_ChatBot:
    def __init__(self):
    # Initialize client and Azure OpenAI objects
    self.client = Client("http://127.0.0.1:8000/mcp")
    self.available_tools = []
    self.available_prompts = []
    self.exit_stack=AsyncExitStack()
    self.message_history=[]

    async def connect_to_server_and_run(self):
    async with self.client:
    print(f"Client connected: {self.client.is_connected()}")

    # List available tools
    tools = await self.client.list_tools()
    print("\nConnected to server with tools:", len(tools))
    print('--- tool names ---')
    for tool in tools:
    print(tool.name)
    print('---------------')
    try:
    print('--- request info ---')
    result = await self.client.call_tool('request_info')
    print(result)
    except Exception as e:
    print(e)

    async def cleanup(self):
    await self.exit_stack.aclose()
    async def main():
    chatbot = MCP_ChatBot()
    try:
    await chatbot.connect_to_server_and_run()
    finally:
    await chatbot.cleanup()

    if __name__ == "__main__":
    asyncio.run(main())


    and here the output of the client:

    Client connected: True

    Connected to server with tools: 1

    --- Tool Names ---
    - request_info

    --- Request Info ---
    [TextContent(
    type='text',
    text='{
    "request_id": "2",
    "client_id": "Unknown client",
    "session_id": null
    }',
    annotations=None,
    meta=None
    )]


    I'm wondering why the session_id is null. Did I miss something in my implementation, or is it that FastMCP does not handle session_id properly?

    Continue reading...

Compartilhe esta Página