|
10 | 10 |
|
11 | 11 | import json |
12 | 12 | import re |
| 13 | +from collections.abc import AsyncIterator |
| 14 | +from contextlib import asynccontextmanager |
13 | 15 | from importlib.metadata import version |
14 | 16 | from pathlib import Path |
15 | 17 | from typing import Any |
16 | 18 | from unittest import mock |
| 19 | +from unittest.mock import patch |
17 | 20 |
|
| 21 | +import anyio |
18 | 22 | import git |
19 | 23 | import pytest |
20 | 24 | from mcp.client.session import ClientRequestContext |
21 | | -from mcp.types import ListRootsResult, Root |
| 25 | +from mcp.shared.memory import create_client_server_memory_streams |
| 26 | +from mcp.shared.message import SessionMessage |
| 27 | +from mcp.types import METHOD_NOT_FOUND, JSONRPCRequest, ListRootsResult, Root |
22 | 28 | from pydantic import FileUrl |
23 | 29 |
|
| 30 | +from mcp_server_git.server import serve |
| 31 | + |
24 | 32 | from conftest import connect, make_repo, text_result, wire |
25 | 33 |
|
26 | 34 | SNAPSHOTS = Path(__file__).parent / "snapshots" |
@@ -1146,3 +1154,55 @@ async def test_server_without_client_roots_behaves_the_same(repo: git.Repo): |
1146 | 1154 | await session.call_tool("git_status", {"repo_path": str(root_of(repo))}) |
1147 | 1155 | ) |
1148 | 1156 | assert result["isError"] is False |
| 1157 | + |
| 1158 | + |
| 1159 | +# --------------------------------------------------------------------------- |
| 1160 | +# Protocol era |
| 1161 | +# --------------------------------------------------------------------------- |
| 1162 | + |
| 1163 | + |
| 1164 | +async def test_modern_era_is_not_served() -> None: |
| 1165 | + # #4851: the SDK v2 port serves the 2025-11-25 handshake era only. |
| 1166 | + # Server.run() would also open a 2026-07-28 connection for a request that |
| 1167 | + # carries the per-request `_meta` envelope; adopting that era is #4853. |
| 1168 | + # Until then such a request is refused as it was on SDK v1. |
| 1169 | + discover = JSONRPCRequest( |
| 1170 | + jsonrpc="2.0", |
| 1171 | + id=1, |
| 1172 | + method="server/discover", |
| 1173 | + params={ |
| 1174 | + "_meta": { |
| 1175 | + "io.modelcontextprotocol/protocolVersion": "2026-07-28", |
| 1176 | + "io.modelcontextprotocol/clientInfo": {"name": "test", "version": "0"}, |
| 1177 | + "io.modelcontextprotocol/clientCapabilities": {}, |
| 1178 | + } |
| 1179 | + }, |
| 1180 | + ) |
| 1181 | + response: SessionMessage | Exception | None = None |
| 1182 | + async with create_client_server_memory_streams() as ( |
| 1183 | + (client_read, client_write), |
| 1184 | + server_streams, |
| 1185 | + ): |
| 1186 | + |
| 1187 | + @asynccontextmanager |
| 1188 | + async def fake_stdio_server() -> AsyncIterator[Any]: |
| 1189 | + yield server_streams |
| 1190 | + |
| 1191 | + with patch("mcp_server_git.server.stdio_server", fake_stdio_server): |
| 1192 | + async with anyio.create_task_group() as tg: |
| 1193 | + tg.start_soon(serve, None) |
| 1194 | + await client_write.send(SessionMessage(discover)) |
| 1195 | + with anyio.fail_after(5): |
| 1196 | + response = await client_read.receive() |
| 1197 | + tg.cancel_scope.cancel() |
| 1198 | + |
| 1199 | + assert isinstance(response, SessionMessage) |
| 1200 | + assert response.message.model_dump(by_alias=True, exclude_none=True) == { |
| 1201 | + "jsonrpc": "2.0", |
| 1202 | + "id": 1, |
| 1203 | + "error": { |
| 1204 | + "code": METHOD_NOT_FOUND, |
| 1205 | + "message": "Method not found", |
| 1206 | + "data": "server/discover", |
| 1207 | + }, |
| 1208 | + } |
0 commit comments