-
Notifications
You must be signed in to change notification settings - Fork 2k
Add CLI update notifications #2839
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| """Version checking utilities for FastMCP.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| import httpx | ||
| from packaging.version import Version | ||
|
|
||
| from fastmcp.utilities.logging import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| PYPI_URL = "https://pypi.org/pypi/fastmcp/json" | ||
| CACHE_TTL_SECONDS = 60 * 60 * 12 # 12 hours | ||
| REQUEST_TIMEOUT_SECONDS = 2.0 | ||
|
|
||
|
|
||
| def _get_cache_path(include_prereleases: bool = False) -> Path: | ||
| """Get the path to the version cache file.""" | ||
| import fastmcp | ||
|
|
||
| suffix = "_prerelease" if include_prereleases else "" | ||
| return fastmcp.settings.home / f"version_cache{suffix}.json" | ||
|
|
||
|
|
||
| def _read_cache(include_prereleases: bool = False) -> tuple[str | None, float]: | ||
| """Read cached version info. | ||
|
|
||
| Returns: | ||
| Tuple of (cached_version, cache_timestamp) or (None, 0) if no cache. | ||
| """ | ||
| cache_path = _get_cache_path(include_prereleases) | ||
| if not cache_path.exists(): | ||
| return None, 0 | ||
|
|
||
| try: | ||
| data = json.loads(cache_path.read_text()) | ||
| return data.get("latest_version"), data.get("timestamp", 0) | ||
| except (json.JSONDecodeError, OSError): | ||
| return None, 0 | ||
|
|
||
|
|
||
| def _write_cache(latest_version: str, include_prereleases: bool = False) -> None: | ||
| """Write version info to cache.""" | ||
| cache_path = _get_cache_path(include_prereleases) | ||
| try: | ||
| cache_path.parent.mkdir(parents=True, exist_ok=True) | ||
| cache_path.write_text( | ||
| json.dumps({"latest_version": latest_version, "timestamp": time.time()}) | ||
| ) | ||
| except OSError: | ||
| # Silently ignore cache write failures | ||
| pass | ||
|
|
||
|
|
||
| def _fetch_latest_version(include_prereleases: bool = False) -> str | None: | ||
| """Fetch the latest version from PyPI. | ||
|
|
||
| Args: | ||
| include_prereleases: If True, include pre-release versions (alpha, beta, rc). | ||
|
|
||
| Returns: | ||
| The latest version string, or None if the fetch failed. | ||
| """ | ||
| try: | ||
| response = httpx.get(PYPI_URL, timeout=REQUEST_TIMEOUT_SECONDS) | ||
| response.raise_for_status() | ||
| data = response.json() | ||
|
|
||
| releases = data.get("releases", {}) | ||
| if not releases: | ||
| return None | ||
|
|
||
| versions = [] | ||
| for version_str in releases: | ||
| try: | ||
| v = Version(version_str) | ||
| # Skip prereleases if not requested | ||
| if not include_prereleases and v.is_prerelease: | ||
| continue | ||
| versions.append(v) | ||
| except ValueError: | ||
| logger.debug(f"Skipping invalid version string: {version_str}") | ||
| continue | ||
|
|
||
| if not versions: | ||
| return None | ||
|
|
||
| return str(max(versions)) | ||
|
|
||
| except (httpx.HTTPError, json.JSONDecodeError, KeyError): | ||
| return None | ||
|
|
||
|
|
||
| def get_latest_version(include_prereleases: bool = False) -> str | None: | ||
| """Get the latest version of FastMCP from PyPI, using cache when available. | ||
|
|
||
| Args: | ||
| include_prereleases: If True, include pre-release versions. | ||
|
|
||
| Returns: | ||
| The latest version string, or None if unavailable. | ||
| """ | ||
| # Check cache first | ||
| cached_version, cache_timestamp = _read_cache(include_prereleases) | ||
| if cached_version and (time.time() - cache_timestamp) < CACHE_TTL_SECONDS: | ||
| return cached_version | ||
|
|
||
| # Fetch from PyPI | ||
| latest_version = _fetch_latest_version(include_prereleases) | ||
|
|
||
| # Update cache if we got a valid version | ||
| if latest_version: | ||
| _write_cache(latest_version, include_prereleases) | ||
| return latest_version | ||
|
|
||
| # Return stale cache if available | ||
| return cached_version | ||
|
|
||
|
|
||
| def check_for_newer_version() -> str | None: | ||
| """Check if a newer version of FastMCP is available. | ||
|
|
||
| Returns: | ||
| The latest version string if newer than current, None otherwise. | ||
| """ | ||
| import fastmcp | ||
|
|
||
| setting = fastmcp.settings.check_for_updates | ||
| if setting == "off": | ||
| return None | ||
|
|
||
| include_prereleases = setting == "prerelease" | ||
| latest_version = get_latest_version(include_prereleases) | ||
| if not latest_version: | ||
| return None | ||
|
|
||
| try: | ||
| current = Version(fastmcp.__version__) | ||
| latest = Version(latest_version) | ||
|
|
||
| if latest > current: | ||
| return latest_version | ||
| except ValueError: | ||
| logger.debug( | ||
| f"Could not compare versions: current={fastmcp.__version__!r}, " | ||
| f"latest={latest_version!r}" | ||
| ) | ||
|
|
||
| return None | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importing
fastmcp.utilities.version_checkdirectly now executesimport fastmcpat module load time. That triggersfastmcp.__init__→fastmcp.server.serverfastmcp.utilities.cli, which doesfrom fastmcp.utilities.version_check import check_for_newer_versionwhileversion_checkis still initializing. In that scenario, the attribute is not defined yet and Python raisesImportErrorfrom a partially initialized module. This breaks direct imports (including the new tests) and any downstream usage that wants version-check utilities without importingfastmcpfirst. Move theimport fastmcpinto the functions that need it or otherwise break the module-level dependency to avoid the cycle.Useful? React with 👍 / 👎.