@@ -30,7 +30,10 @@ async def run_server():
3030
3131
3232@asynccontextmanager
33- async def stdio_server (stdin : anyio .AsyncFile [str ] | None = None , stdout : anyio .AsyncFile [str ] | None = None ):
33+ async def stdio_server (
34+ stdin : anyio .AsyncFile [str ] | None = None ,
35+ stdout : anyio .AsyncFile [str ] | None = None ,
36+ ):
3437 """Server transport for stdio: this communicates with an MCP client by reading
3538 from the current process' stdin and writing to stdout.
3639 """
@@ -44,22 +47,28 @@ async def stdio_server(stdin: anyio.AsyncFile[str] | None = None, stdout: anyio.
4447 if not stdin :
4548 stdin_fd = os .dup (sys .stdin .fileno ())
4649 stdin_bin = os .fdopen (stdin_fd , "rb" , closefd = True )
47- stdin = anyio .wrap_file (TextIOWrapper (stdin_bin , encoding = "utf-8" , errors = "replace" ))
48-
50+ stdin = anyio .wrap_file (
51+ TextIOWrapper (stdin_bin , encoding = "utf-8" , errors = "replace" )
52+ )
53+
4954 if not stdout :
5055 stdout_fd = os .dup (sys .stdout .fileno ())
5156 stdout_bin = os .fdopen (stdout_fd , "wb" , closefd = True )
5257 stdout = anyio .wrap_file (TextIOWrapper (stdout_bin , encoding = "utf-8" ))
5358
54- read_stream_writer , read_stream = create_context_streams [SessionMessage | Exception ](0 )
59+ read_stream_writer , read_stream = create_context_streams [
60+ SessionMessage | Exception
61+ ](0 )
5562 write_stream , write_stream_reader = create_context_streams [SessionMessage ](0 )
5663
5764 async def stdin_reader ():
5865 try :
5966 async with read_stream_writer :
6067 async for line in stdin :
6168 try :
62- message = types .jsonrpc_message_adapter .validate_json (line , by_name = False )
69+ message = types .jsonrpc_message_adapter .validate_json (
70+ line , by_name = False
71+ )
6372 except Exception as exc :
6473 await read_stream_writer .send (exc )
6574 continue
@@ -73,7 +82,9 @@ async def stdout_writer():
7382 try :
7483 async with write_stream_reader :
7584 async for session_message in write_stream_reader :
76- json = session_message .message .model_dump_json (by_alias = True , exclude_unset = True )
85+ json = session_message .message .model_dump_json (
86+ by_alias = True , exclude_unset = True
87+ )
7788 await stdout .write (json + "\n " )
7889 await stdout .flush ()
7990 except anyio .ClosedResourceError : # pragma: no cover
0 commit comments