Skip to content

client: fix set_roots updates live session at runtime#3714

Closed
dgenio wants to merge 4 commits intoPrefectHQ:mainfrom
dgenio:fix-client-set-roots-updates-session
Closed

client: fix set_roots updates live session at runtime#3714
dgenio wants to merge 4 commits intoPrefectHQ:mainfrom
dgenio:fix-client-set-roots-updates-session

Conversation

@dgenio
Copy link
Copy Markdown
Contributor

@dgenio dgenio commented Mar 30, 2026

Description

Fixes #326.

Calling Client.set_roots(...) during an active session previously only updated pending session kwargs, so runtime roots changes were not reflected until reconnect. This PR updates the live session callback when connected and adds a regression test that verifies runtime root updates are immediately visible.

Closes #326

Contribution type

  • Bug fix (simple, well-scoped fix for a clearly broken behavior)
  • Documentation improvement
  • Enhancement (maintainers typically implement enhancements — see CONTRIBUTING.md)

Checklist

  • This PR addresses an existing issue (or fixes a self-evident bug)
  • I have read CONTRIBUTING.md
  • I have added tests that cover my changes
  • I have run uv run prek run --all-files and all checks pass
  • I have self-reviewed my changes
  • If I used an LLM, it followed the repo's contributing conventions (not generic output)

Testing

  • uv run pytest tests/client/test_roots.py -q -> 5 passed
  • uv run prek run --all-files was run previously; local environment was missing loq, so full pass should be confirmed in CI

dgenio added 2 commits March 30, 2026 22:29
Fixes PrefectHQ#326. Calling set_roots now updates the live session's roots callback if connected, not just pending session kwargs. Adds regression test to verify runtime update.
@marvin-context-protocol marvin-context-protocol Bot added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. client Related to the FastMCP client SDK or client-side functionality. labels Mar 30, 2026
@marvin-context-protocol
Copy link
Copy Markdown
Contributor

Test Failure Analysis

Summary: The static analysis (prek) failed because ruff format would reformat 2 files in this PR — the code was not run through the formatter before pushing.

Root Cause: The PR modifies src/fastmcp/client/client.py and tests/client/test_roots.py, but neither was formatted with ruff format. The CI log confirms: 2 files reformatted, 716 files left unchanged. The author noted in the checklist that their local environment was missing loq, so prek wasn't fully verified locally.

Suggested Solution: Run ruff format on the two changed files (or all files) and push:

uv run ruff format src/fastmcp/client/client.py tests/client/test_roots.py

Or run the full prek suite:

uv run prek run --all-files

The most likely formatting issues are:

  • tests/client/test_roots.py: missing blank line between test_set_roots_updates_connected_session and the next @pytest.mark.parametrize block
  • src/fastmcp/client/client.py: a long line that needs wrapping (the if hasattr(...) condition)
Detailed Analysis

Relevant log excerpt:

ruff format...............................................................[FAILED]
  2 files reformatted, 716 files left unchanged

The ruff-format hook ran against 718 files and found 2 that needed reformatting. Since the PR only touches src/fastmcp/client/client.py and tests/client/test_roots.py, those are the two files.

Looking at the diff, likely issues:

tests/client/test_roots.py — no blank line between the new test method and the next decorated method:

            assert result2.data == ["file://b/", "file://c/"]
    @pytest.mark.parametrize("roots", [["x"], ["x", "y"]])  # ← needs blank line before this

src/fastmcp/client/client.py — long chained condition may need wrapping:

        if hasattr(self, "_session_state") and getattr(self._session_state, "session", None) is not None:
Related Files
  • src/fastmcp/client/client.py — modified by PR, needs ruff formatting
  • tests/client/test_roots.py — modified by PR, needs ruff formatting

🤖 Triage analysis by marvin

- Remove redundant getattr/hasattr guards in set_roots; access session
  directly and let AttributeError surface if MCP SDK changes
- Add test_set_roots_before_connect regression test
- File PrefectHQ#3715 for set_sampling_callback/set_elicitation_callback parity
- Add TODO(PrefectHQ#3715) comments to sibling methods

🤖 Generated with GitHub Copilot
@jlowin
Copy link
Copy Markdown
Member

jlowin commented Apr 7, 2026

Thanks for investigating this. The bug is real, but I'm not comfortable reaching into session._list_roots_callback (a private SDK attribute) to work around the lack of a public API. That kind of coupling tends to break silently on SDK upgrades, and the MCP SDK v2 is on the horizon, which will likely change session internals significantly.

Going to close this for now. If the SDK adds a public way to update callbacks on a live session, this becomes straightforward to implement on the FastMCP side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. client Related to the FastMCP client SDK or client-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

client.set_roots and send_roots_list_changed not update the roots

2 participants