Skip to content

Context refactor #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2025
Merged

Context refactor #347

merged 5 commits into from
May 7, 2025

Conversation

jlowin
Copy link
Owner

@jlowin jlowin commented May 7, 2025

Significant refactor of how Context works to avoid passing it via signatures. This generally shouldn't affect users although it does remove an undocumented feature of supplying a context_kwarg

@Copilot Copilot AI review requested due to automatic review settings May 7, 2025 01:27
@jlowin jlowin changed the title Context Context refactor May 7, 2025
@jlowin jlowin added enhancement New feature or request breaking change labels May 7, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the Context handling throughout FastMCP, removing the undocumented context_kwarg feature and eliminating the need to pass Context objects via function signatures. Key changes include:

  • Removal of context_kwarg assertions and parameters in tools, prompts, resources, and templates.
  • Migration of HTTP request context middleware from utilities to the server module.
  • Updating the Context class to use a token‐based context manager pattern for improved consistency.

Reviewed Changes

Copilot reviewed 21 out of 22 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/prompts/test_prompt_manager.py Removed assertions on context_kwarg; tests now rely on the new Context API.
src/fastmcp/utilities/http.py Removed completely in favor of the new RequestContextMiddleware in src/fastmcp/server/http.py.
src/fastmcp/utilities/cache.py Introduced a TimedCache implementation used by FastMCP.
src/fastmcp/tools/tool.py Removed context_kwarg handling and now injects context internally via get_context().
src/fastmcp/server/context.py Refactored Context to a dataclass with explicit enter/exit; token stack management added.
(Other files) Similar removals of context parameters in resources, prompts, and server methods.
Files not reviewed (1)
  • docs/servers/context.mdx: Language not supported
Comments suppressed due to low confidence (5)

tests/prompts/test_prompt_manager.py:309

  • [nitpick] Consider adding tests to verify that the new Context injection via the Context manager works as expected now that the context_kwarg property has been removed.
Prompt.from_function(prompt_with_context)

src/fastmcp/utilities/http.py:1

  • Since this file has been removed in favor of RequestContextMiddleware in the server module, ensure that no other parts of the code still reference the old RequestMiddleware and update import paths accordingly.
from __future__ import annotations

src/fastmcp/resources/resource_manager.py:244

  • [nitpick] Review the resource template creation to confirm that the context is properly injected via get_context() where required, ensuring consistency with the refactored Context handling.
return await template.create_resource(uri_str, params=params)

src/fastmcp/tools/tool.py:140

  • [nitpick] Verify that the change to inject context via get_context() (using find_kwarg_by_type) appropriately covers all cases previously handled by the context_kwarg parameter.
result = type_adapter.validate_python(arguments)

src/fastmcp/server/context.py:89

  • [nitpick] Ensure that the token stack management in the exit method is safe for concurrent use and that any potential issues with token resetting are properly handled.
            token = self._tokens.pop()

@jlowin jlowin merged commit 1c1ded0 into main May 7, 2025
4 checks passed
@jlowin jlowin deleted the context branch May 7, 2025 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant