π The fast, Pythonic way to build MCP servers and clients.
fix: VersionSpec eq matching normalizes versions and selects deterministically (#4058)
Bill Easton24b594b1e3e7
diff --git a/fastmcp_slim/fastmcp/utilities/versions.py b/fastmcp_slim/fastmcp/utilities/versions.pyindex a16389fe3..028f687b5 100644--- a/fastmcp_slim/fastmcp/utilities/versions.py+++ b/fastmcp_slim/fastmcp/utilities/versions.py@@ -39,6 +39,13 @@ class VersionSpec:gte: If set, only versions >= this value match.lt: If set, only versions < this value match.eq: If set, only this exact version matches (gte/lt ignored).+ Matching is PEP 440-normalized and `v`-prefix insensitive, so+ `eq="v1.0"` matches a component versioned `"1.0"`, and `eq="1.0"`+ matches `"1"` (PEP 440 treats `1` and `1.0` as the same version).+ If a server registers two PEP 440-equivalent spellings of the+ same component (e.g. both `"1"` and `"1.0"`), they are the same+ version under this spec; selection among them is deterministic+ (see `version_sort_key`), not registration-order dependent."""gte: str | None = None@@ -62,7 +69,7 @@ class VersionSpec:return match_noneif self.eq is not None:- return version == self.eq+ return parse_version_key(version) == parse_version_key(self.eq)key = parse_version_key(version)@@ -199,16 +206,24 @@ def parse_version_key(version: str | None) -> VersionKey:return VersionKey(version)-def version_sort_key(component: FastMCPComponent) -> VersionKey:+def version_sort_key(component: FastMCPComponent) -> tuple[VersionKey, str]:"""Get a sort key for a component based on its version.Use with sorted() or max() to order components by version.+ The key is a `(VersionKey, raw)` tuple. The `VersionKey` orders by PEP 440+ semantics (or lexicographically for non-PEP 440 strings); the raw version+ string is a deterministic tie-breaker so that two components whose versions+ are PEP 440-equivalent but spelled differently (e.g. `"1"` and `"1.0"`) are+ ordered reproducibly instead of by registration order. The raw tie-breaker+ only affects equivalent-version ties and never the primary version order,+ so range/equality matching (which uses `VersionKey` directly) is unchanged.+Args:component: The component to get a sort key for.Returns:- A sortable VersionKey.+ A deterministic, sortable `(VersionKey, raw)` tuple.Example:```python@@ -216,7 +231,7 @@ def version_sort_key(component: FastMCPComponent) -> VersionKey:highest = max(tools, key=version_sort_key) # Returns tool_v2```"""- return parse_version_key(component.version)+ return (parse_version_key(component.version), component.version or "")def compare_versions(a: str | None, b: str | None) -> int:diff --git a/tests/server/versioning/test_versioning.py b/tests/server/versioning/test_versioning.pyindex f2866df06..689b2ce7b 100644--- a/tests/server/versioning/test_versioning.py+++ b/tests/server/versioning/test_versioning.py@@ -12,8 +12,10 @@ from fastmcp import FastMCPfrom fastmcp.tools import Toolfrom fastmcp.utilities.versions import (VersionKey,+ VersionSpec,compare_versions,is_version_greater,+ version_sort_key,)@@ -84,6 +86,107 @@ class TestVersionFunctions:assert not is_version_greater(None, "1.0")+def _tool(version: str | None) -> Tool:+ """Build a real Tool component (a FastMCPComponent) with a version."""+ return Tool.from_function(lambda: None, name="t", version=version)+++class TestVersionSpecEq:+ """Tests for VersionSpec(eq=...) matching semantics."""++ def test_exact_string_match(self):+ assert VersionSpec(eq="1.0").matches("1.0")+ assert not VersionSpec(eq="1.0").matches("2.0")++ def test_v_prefix_insensitive_both_directions(self):+ # Spec has v-prefix, component does not+ assert VersionSpec(eq="v1.0").matches("1.0")+ # Component has v-prefix, spec does not+ assert VersionSpec(eq="1.0").matches("v1.0")+ assert VersionSpec(eq="v2.3").matches("v2.3")++ def test_pep440_equivalent_spellings_match(self):+ """PEP 440 treats 1, 1.0, 1.0.0 as the same version."""+ assert VersionSpec(eq="1.0").matches("1")+ assert VersionSpec(eq="1").matches("1.0")+ assert VersionSpec(eq="1.0").matches("1.0.0")+ assert not VersionSpec(eq="1.0").matches("1.0.1")++ def test_malformed_versions_use_exact_string_equality(self):+ """Non-PEP 440 strings compare exactly (no normalization, no crash)."""+ assert VersionSpec(eq="latest").matches("latest")+ assert not VersionSpec(eq="latest").matches("LATEST")+ assert VersionSpec(eq="2024-01-01").matches("2024-01-01")+ assert not VersionSpec(eq="2024-01-01").matches("2024-01-02")++ def test_eq_does_not_match_none_when_match_none_false(self):+ assert VersionSpec(eq="1.0").matches(None, match_none=False) is False+ assert VersionSpec(eq="1.0").matches(None, match_none=True) is True+++class TestVersionSelectionDeterminism:+ """Selection among PEP 440-equivalent spellings must be deterministic.++ Regression: previously `max(matching, key=version_sort_key)` returned the+ first element on a tie, so registering both "1" and "1.0" made selection+ depend on registration order. version_sort_key now has a raw tie-breaker.+ """++ def test_version_sort_key_is_total_ordering_tuple(self):+ # PEP 440-equivalent but distinct spellings tie on VersionKey;+ # the raw string breaks the tie deterministically.+ k1 = version_sort_key(_tool("1"))+ k10 = version_sort_key(_tool("1.0"))+ assert k1[0] == k10[0] # same VersionKey (PEP 440 equal)+ assert k1 != k10 # but distinct sort keys+ assert k10 > k1 # deterministic order ("1.0" > "1")++ def test_unversioned_components_do_not_crash_tiebreak(self):+ # Two unversioned components: raw tie-breaker is "" for both,+ # must compare equal without raising (None < None would).+ a = version_sort_key(_tool(None))+ b = version_sort_key(_tool(None))+ assert a == b++ def test_distinct_versions_unaffected_by_tiebreak(self):+ # Primary ordering still wins; raw tie-breaker never overrides it.+ # "1.9" < "1.10" semantically even though "1.9" > "1.10" as strings.+ k19 = version_sort_key(_tool("1.9"))+ k110 = version_sort_key(_tool("1.10"))+ assert k110 > k19++ async def test_selection_is_registration_order_independent(self):+ """End-to-end: registering two PEP 440-equivalent spellings of the+ same tool must select the same one regardless of registration order+ (exercises the real `max(..., key=version_sort_key)` selection path)."""++ forward = FastMCP()++ @forward.tool(name="add", version="1")+ def add_a(x: int) -> int:+ return x++ @forward.tool(name="add", version="1.0")+ def add_b(x: int) -> int:+ return x++ reverse = FastMCP()++ @reverse.tool(name="add", version="1.0")+ def add_c(x: int) -> int:+ return x++ @reverse.tool(name="add", version="1")+ def add_d(x: int) -> int:+ return x++ fwd = await forward.get_tool("add")+ rev = await reverse.get_tool("add")+ assert fwd is not None and rev is not None+ # Deterministic regardless of order (previously order-dependent).+ assert fwd.version == rev.version == "1.0"++class TestComponentVersioning:"""Tests for versioning in FastMCP components."""