Skip to content

Commit df31fec

Browse files
author
Henry Lee
committed
test: satisfy stdio pyright checks
1 parent 9dbf4d8 commit df31fec

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/mcp/server/stdio.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ async def run_server():
1919

2020
import os
2121
import sys
22+
from collections.abc import Callable
2223
from contextlib import asynccontextmanager
2324
from io import TextIOWrapper, UnsupportedOperation
25+
from typing import BinaryIO, Literal, Protocol
2426

2527
import anyio
2628
import anyio.lowlevel
@@ -30,7 +32,19 @@ async def run_server():
3032
from mcp.shared.message import SessionMessage
3133

3234

33-
def _wrap_standard_stream(stream, mode: str, *, errors: str | None = None) -> tuple[anyio.AsyncFile[str], bool]:
35+
class _TextStreamWithBuffer(Protocol):
36+
@property
37+
def buffer(self) -> BinaryIO: ...
38+
39+
fileno: Callable[[], int]
40+
41+
42+
def _wrap_standard_stream(
43+
stream: _TextStreamWithBuffer,
44+
mode: Literal["rb", "wb"],
45+
*,
46+
errors: str | None = None,
47+
) -> tuple[anyio.AsyncFile[str], bool]:
3448
"""Wrap a standard stream without taking ownership of the original handle."""
3549
try:
3650
fd = os.dup(stream.fileno())

tests/server/test_stdio.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import io
33
import sys
44
from io import TextIOWrapper
5+
from pathlib import Path
56

67
import anyio
78
import pytest
@@ -96,7 +97,7 @@ async def test_stdio_server_invalid_utf8(monkeypatch: pytest.MonkeyPatch):
9697

9798

9899
@pytest.mark.anyio
99-
async def test_stdio_server_does_not_close_standard_streams(monkeypatch: pytest.MonkeyPatch, tmp_path):
100+
async def test_stdio_server_does_not_close_standard_streams(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
100101
"""Default stdio wrapping must not close the process stdin/stdout handles."""
101102
message = JSONRPCRequest(jsonrpc="2.0", id=1, method="ping")
102103
stdin_path = tmp_path / "stdin.jsonl"

0 commit comments

Comments
 (0)