Skip to content

fix: restore context vars in resolve_tool endpoint#781

Merged
kichanyurd merged 1 commit intoemcie-co:developfrom
flowjzh:fix/resolve-context-vars
Apr 21, 2026
Merged

fix: restore context vars in resolve_tool endpoint#781
kichanyurd merged 1 commit intoemcie-co:developfrom
flowjzh:fix/resolve-context-vars

Conversation

@flowjzh
Copy link
Copy Markdown
Contributor

@flowjzh flowjzh commented Apr 21, 2026

Summary

Fix context var propagation in resolve_tool endpoint for uvicorn 0.39.0+.

Problem

Uvicorn PR #2742 fixed contextvar loading
in version 0.39.0. Previously, p.Server.current was accessible in the resolve_tool
endpoint, but after uvicorn 0.39.0 it became unavailable.

This breaks choice providers that depend on p.Server.current, causing:

RuntimeError: No server available in current context

Solution

Restore context vars in the resolve_tool endpoint, following the same pattern
used in the call_tool endpoint:

# Restore context vars for same-process hosted mode
for var, value in self.context_vars.items():
    var.set(value)

This ensures p.Server.current is accessible regardless of uvicorn version.

Testing

  • Tested with uvicorn 0.38.0 (before fix)
  • Tested with uvicorn 0.39.0+ (after fix)
  • Choice providers now work correctly in both versions

References

Uvicorn 0.39.0+ fixed contextvar propagation, causing p.Server.current
to become unavailable in the resolve_tool endpoint. This breaks
choice providers that depend on p.Server.current.

This fix follows the same pattern used in call_tool, restoring
context vars before processing the request.

Signed-off-by: Flow Jiang <flowjzh@gmail.com>
@kichanyurd kichanyurd merged commit 9bac1ea into emcie-co:develop Apr 21, 2026
1 check passed
@kichanyurd
Copy link
Copy Markdown
Contributor

Good catch, I bet this manifested in the usage of choice provider. Merged!

@Kludex
Copy link
Copy Markdown

Kludex commented Apr 21, 2026

I've reverted the changes in Uvicorn. It will be available in the next release (in a few minutes).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants