Skip to content

Commit 400f541

Browse files
committed
test: narrow websocket_server pragma now that coverage reaches it
The function-level pragma existed because the server ran in a subprocess where coverage couldn't instrument it. The in-thread conversion makes it reachable, so strict-no-cover (#2305) flags it as stale. Narrow the pragma to the three error paths the smoke test doesn't exercise.
1 parent b66b4ea commit 400f541

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/mcp/server/websocket.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from mcp.shared.message import SessionMessage
1111

1212

13-
@asynccontextmanager # pragma: no cover
13+
@asynccontextmanager
1414
async def websocket_server(scope: Scope, receive: Receive, send: Send):
1515
"""WebSocket server transport for MCP. This is an ASGI application, suitable for use
1616
with a framework like Starlette and a server like Hypercorn.
@@ -34,13 +34,13 @@ async def ws_reader():
3434
async for msg in websocket.iter_text():
3535
try:
3636
client_message = types.jsonrpc_message_adapter.validate_json(msg, by_name=False)
37-
except ValidationError as exc:
37+
except ValidationError as exc: # pragma: no cover
3838
await read_stream_writer.send(exc)
3939
continue
4040

4141
session_message = SessionMessage(client_message)
4242
await read_stream_writer.send(session_message)
43-
except anyio.ClosedResourceError:
43+
except anyio.ClosedResourceError: # pragma: no cover
4444
await websocket.close()
4545

4646
async def ws_writer():
@@ -49,7 +49,7 @@ async def ws_writer():
4949
async for session_message in write_stream_reader:
5050
obj = session_message.message.model_dump_json(by_alias=True, exclude_unset=True)
5151
await websocket.send_text(obj)
52-
except anyio.ClosedResourceError:
52+
except anyio.ClosedResourceError: # pragma: no cover
5353
await websocket.close()
5454

5555
async with anyio.create_task_group() as tg:

tests/shared/test_ws.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Smoke test for the WebSocket transport.
22
3-
Runs the full WS stack end-to-end over a real TCP connection to provide
4-
coverage of ``src/mcp/client/websocket.py``. MCP semantics (error
5-
propagation, timeouts, etc.) are transport-agnostic and are covered in
6-
``tests/client/test_client.py`` and ``tests/issues/test_88_random_error.py``.
3+
Runs the full WS stack end-to-end over a real TCP connection, covering both
4+
``src/mcp/client/websocket.py`` and ``src/mcp/server/websocket.py``. MCP
5+
semantics (error propagation, timeouts, etc.) are transport-agnostic and are
6+
covered in ``tests/client/test_client.py`` and ``tests/issues/test_88_random_error.py``.
77
"""
88

99
from collections.abc import Generator

0 commit comments

Comments
 (0)