Skip to content

client.set_roots not update the roots #630

@xxzero12

Description

@xxzero12

Hi, is trying to test the root capability, somewhat the client set the root but the server still return old one

Ex:

Client.py

client = Client(
    "http://localhost:9000/sse", 
    sampling_handler=sampling_handler, 
    log_handler=log_handler,
    roots=[
        "file://home/projects/roots-example/frontend"
    ],
    message_handler=message_handler)

async def main():
    # Connection is established here
    async with client:
        await client.call_tool("get_forecast",{ "latitude": 47.6101, "longitude": -122.2015 })
        client.set_roots([
            "file://home/projects/roots-example/backend"
        ])
        await client.send_roots_list_changed()
        await client.call_tool("get_forecast",{ "latitude": 47.6101, "longitude": -122.2015 })

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

Server.py

logging.basicConfig(level=logging.DEBUG)
mcp = FastMCP(name="Tutorial Server")


@mcp.tool()
async def get_forecast(latitude: float, longitude: float, ctx: Context) -> str:
    """Get weather forecast for a location.

    Args:
        latitude: Latitude of the location
        longitude: Longitude of the location
    """
    roots = await ctx.list_roots()
    await ctx.info(f"{roots}")
    await ctx.info(f"Info Message: Processing coordinates: {latitude}, {longitude}")

if __name__ == "__main__":
    mcp.run(transport="sse", host="127.0.0.1", port=9000)

Somewhat the result is like below

First call
DEBUG:mcp.server.sse:Sending message via SSE: root=JSONRPCRequest(method='roots/list', params=None, jsonrpc='2.0', id=1)
DEBUG:mcp.server.sse:Received JSON: b'{"jsonrpc":"2.0","id":0,"result":{"roots":[{"uri":"file://home/projects/roots-example/frontend"}]}}'
Second call(should return backend since set the root)
DEBUG:mcp.server.sse:Received JSON: b'{"method":"notifications/roots/list_changed","jsonrpc":"2.0"}'
DEBUG:mcp.server.sse:Sending message via SSE: root=JSONRPCRequest(method='roots/list', params=None, jsonrpc='2.0', id=1)
DEBUG:mcp.server.sse:Received JSON: b'{"jsonrpc":"2.0","id":1,"result":{"roots":[{"uri":"file://home/projects/roots-example/frontend"}]}}'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds confirmationNeeds confirmation that the PR is actually required or needed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions