Skip to content

Conversation

@jlowin
Copy link
Owner

@jlowin jlowin commented Oct 24, 2025

The Context.fastmcp property already handles None checks internally, raising a RuntimeError if the FastMCP instance is no longer available. The property is typed to return FastMCP (not optional), so accessing ctx.fastmcp will either return a valid instance or raise an exception - it can never be None.

This makes the if self.fastmcp is None checks in these methods unreachable:

  • list_resources()
  • list_prompts()
  • get_prompt()
  • read_resource()
@property
def fastmcp(self) -> FastMCP:
    """Get the FastMCP instance."""
    fastmcp = self._fastmcp()
    if fastmcp is None:
        raise RuntimeError("FastMCP instance is no longer available")
    return fastmcp

This cleanup improves code clarity by removing defensive checks that can never trigger.

Fixes the outstanding request from PR #2249.

The fastmcp property already raises RuntimeError if None, making these checks unreachable.
@jlowin jlowin added the enhancement Improvement to existing functionality. For issues and smaller PR improvements. label Oct 24, 2025
@marvin-context-protocol marvin-context-protocol bot added the server Related to FastMCP server implementation or server-side functionality. label Oct 24, 2025
@jlowin jlowin merged commit b57a39c into main Oct 24, 2025
10 checks passed
@jlowin jlowin deleted the remove-redundant-none-checks branch October 24, 2025 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to existing functionality. For issues and smaller PR improvements. server Related to FastMCP server implementation or server-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants