Skip to content

Commit ba61613

Browse files
authored
Merge pull request #5019 from modelcontextprotocol/v2/fix/360-python-server-versions
fix(fetch,git,time): report the package version in serverInfo
2 parents b556150 + 38166ee commit ba61613

6 files changed

Lines changed: 40 additions & 20 deletions

File tree

‎src/fetch/src/mcp_server_fetch/server.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Annotated, Tuple
2+
from importlib.metadata import version
23
from urllib.parse import urlparse, urlunparse
34

45
import markdownify
@@ -20,6 +21,11 @@
2021
from protego import Protego
2122
from pydantic import BaseModel, Field, AnyUrl
2223

24+
# The version this server reports in serverInfo, read from the installed
25+
# distribution's metadata (pyproject.toml) so it cannot drift from the
26+
# published version (#360). Without it the SDK reports its own `mcp` version.
27+
SERVER_VERSION = version("mcp-server-fetch")
28+
2329
DEFAULT_USER_AGENT_AUTONOMOUS = "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)"
2430
DEFAULT_USER_AGENT_MANUAL = "ModelContextProtocol/1.0 (User-Specified; +https://github.com/modelcontextprotocol/servers)"
2531

@@ -202,7 +208,7 @@ async def serve(
202208
ignore_robots_txt: Whether to ignore robots.txt restrictions
203209
proxy_url: Optional proxy URL to use for requests
204210
"""
205-
server = Server("mcp-fetch")
211+
server = Server("mcp-fetch", version=SERVER_VERSION)
206212
user_agent_autonomous = custom_user_agent or DEFAULT_USER_AGENT_AUTONOMOUS
207213
user_agent_manual = custom_user_agent or DEFAULT_USER_AGENT_MANUAL
208214

‎src/fetch/tests/test_protocol.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,13 @@ async def test_initialize_advertises_tools_and_prompts(web: FakeWeb) -> None:
9292
assert "resources" not in data["capabilities"]
9393

9494

95-
# KNOWN BUG #360: pins current (wrong) behavior; the fix changes this assertion.
96-
async def test_server_version_is_the_sdk_version(web: FakeWeb) -> None:
97-
# Characterizes #360: serverInfo.version is the `mcp` SDK's version, not
98-
# this package's, because Server("mcp-fetch") is given no version.
95+
async def test_server_version_is_the_package_version(web: FakeWeb) -> None:
96+
# #360: serverInfo.version is this package's version (from pyproject.toml
97+
# via the installed metadata), not the `mcp` SDK's.
9998
async with connect() as (_, init):
10099
data = wire(init)
101-
assert data["serverInfo"]["version"] == version("mcp")
102-
assert data["serverInfo"]["version"] != version("mcp-server-fetch")
100+
assert data["serverInfo"]["version"] == version("mcp-server-fetch")
101+
assert data["serverInfo"]["version"] != version("mcp")
103102

104103

105104
# --------------------------------------------------------------------------

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from importlib.metadata import version
23
from pathlib import Path
34
from typing import Any, Optional
45
from mcp.server import Server
@@ -13,6 +14,11 @@
1314
from git.exc import BadName
1415
from pydantic import BaseModel, Field
1516

17+
# The version this server reports in serverInfo, read from the installed
18+
# distribution's metadata (pyproject.toml) so it cannot drift from the
19+
# published version (#360). Without it the SDK reports its own `mcp` version.
20+
SERVER_VERSION = version("mcp-server-git")
21+
1622
# Default number of context lines to show in diff output
1723
DEFAULT_CONTEXT_LINES = 3
1824

@@ -337,7 +343,7 @@ async def serve(repository: Path | None) -> None:
337343
logger.error(f"{repository} is not a valid Git repository")
338344
return
339345

340-
server = Server("mcp-git")
346+
server = Server("mcp-git", version=SERVER_VERSION)
341347

342348
@server.list_tools()
343349
async def list_tools() -> list[Tool]:

‎src/git/tests/test_protocol.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ def staged_paths(repo: git.Repo) -> set[str]:
5858
# --------------------------------------------------------------------------
5959

6060

61-
# KNOWN BUG #360: pins current (wrong) behavior; the fix changes this assertion.
62-
async def test_initialize_reports_sdk_version_as_server_version(repo: git.Repo):
63-
# Pins #360: serverInfo.version is the `mcp` SDK's version, not this
64-
# package's, because serve() builds `Server("mcp-git")` without a version.
61+
async def test_initialize_reports_package_version_as_server_version(repo: git.Repo):
62+
# #360: serverInfo.version is this package's version (from pyproject.toml
63+
# via the installed metadata), not the `mcp` SDK's.
6564
async with connect(root_of(repo), initialize=False) as session:
6665
init = wire(await session.initialize())
67-
assert init["serverInfo"] == {"name": "mcp-git", "version": version("mcp")}
66+
assert init["serverInfo"] == {
67+
"name": "mcp-git",
68+
"version": version("mcp-server-git"),
69+
}
6870
assert version("mcp") != version("mcp-server-git")
6971
assert init["capabilities"] == {
7072
"experimental": {},

‎src/time/src/mcp_server_time/server.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, timedelta
2+
from importlib.metadata import version
23
from enum import Enum
34
import json
45
from typing import Sequence
@@ -21,6 +22,11 @@
2122

2223
from pydantic import BaseModel
2324

25+
# The version this server reports in serverInfo, read from the installed
26+
# distribution's metadata (pyproject.toml) so it cannot drift from the
27+
# published version (#360). Without it the SDK reports its own `mcp` version.
28+
SERVER_VERSION = version("mcp-server-time")
29+
2430

2531
class TimeTools(str, Enum):
2632
GET_CURRENT_TIME = "get_current_time"
@@ -131,7 +137,7 @@ def convert_time(
131137

132138

133139
async def serve(local_timezone: str | None = None) -> None:
134-
server = Server("mcp-time")
140+
server = Server("mcp-time", version=SERVER_VERSION)
135141
time_server = TimeServer()
136142
local_tz = str(get_local_tz(local_timezone))
137143

‎src/time/tests/test_protocol.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,17 @@ def handler_error(message: str) -> dict[str, Any]:
120120
# ---------------------------------------------------------------------------
121121

122122

123-
# KNOWN BUG #360: pins current (wrong) behavior; the fix changes this assertion.
124123
async def test_initialize_reports_server_info_and_capabilities() -> None:
125124
async with serving("UTC") as session:
126125
init = wire(await session.initialize())
127126

128-
# Characterizes #360: `Server("mcp-time")` is constructed without a
129-
# version, so the SDK reports its OWN package version as the server's
130-
# version, not mcp-server-time's. A fix will change this assertion.
131-
assert init["serverInfo"] == {"name": "mcp-time", "version": version("mcp")}
132-
assert init["serverInfo"]["version"] != version("mcp-server-time")
127+
# #360: serverInfo.version is mcp-server-time's own version (from
128+
# pyproject.toml via the installed metadata), not the `mcp` SDK's.
129+
assert init["serverInfo"] == {
130+
"name": "mcp-time",
131+
"version": version("mcp-server-time"),
132+
}
133+
assert init["serverInfo"]["version"] != version("mcp")
133134
# Only tools are advertised; no resources, prompts or logging.
134135
assert init["capabilities"] == {"experimental": {}, "tools": {"listChanged": False}}
135136
assert "instructions" not in init

0 commit comments

Comments
 (0)