77import anyio .lowlevel
88from anyio .streams .memory import MemoryObjectReceiveStream , MemoryObjectSendStream
99from anyio .streams .text import TextReceiveStream
10- from pydantic import BaseModel , Field
1110
1211import mcp .types as types
1312
@@ -52,41 +51,19 @@ def get_default_environment() -> dict[str, str]:
5251 return env
5352
5453
55- class StdioServerParameters (BaseModel ):
56- command : str
57- """The executable to run to start the server."""
58-
59- args : list [str ] = Field (default_factory = list )
60- """Command line arguments to pass to the executable."""
61-
62- env : dict [str , str ] | None = None
63- """
64- The environment to use when spawning the process.
65-
66- If not specified, the result of get_default_environment() will be used.
67- """
68-
69- encoding : str = "utf-8"
70- """
71- The text encoding used when sending/receiving messages to the server
72-
73- defaults to utf-8
74- """
75-
76- encoding_error_handler : Literal ["strict" , "ignore" , "replace" ] = "strict"
77- """
78- The text encoding error handler.
79-
80- See https://docs.python.org/3/library/codecs.html#codec-base-classes for
81- explanations of possible values
82- """
83-
84-
8554@asynccontextmanager
86- async def stdio_client (server : StdioServerParameters ):
55+ async def stdio_client (
56+ command : str ,
57+ args : list [str ] | None = None ,
58+ env : dict [str , str ] | None = None ,
59+ encoding : str = "utf-8" ,
60+ encoding_error_handler : Literal ["strict" , "ignore" , "replace" ] = "strict" ,
61+ ):
8762 """
8863 Client transport for stdio: this will connect to a server by spawning a
8964 process and communicating with it over stdin/stdout.
65+
66+
9067 """
9168 read_stream : MemoryObjectReceiveStream [types .JSONRPCMessage | Exception ]
9269 read_stream_writer : MemoryObjectSendStream [types .JSONRPCMessage | Exception ]
@@ -98,8 +75,8 @@ async def stdio_client(server: StdioServerParameters):
9875 write_stream , write_stream_reader = anyio .create_memory_object_stream (0 )
9976
10077 process = await anyio .open_process (
101- [server . command , * server . args ] ,
102- env = server . env if server . env is not None else get_default_environment (),
78+ [command ] + ( args or []) ,
79+ env = env if env is not None else get_default_environment (),
10380 stderr = sys .stderr ,
10481 )
10582
@@ -111,8 +88,8 @@ async def stdout_reader():
11188 buffer = ""
11289 async for chunk in TextReceiveStream (
11390 process .stdout ,
114- encoding = server . encoding ,
115- errors = server . encoding_error_handler ,
91+ encoding = encoding ,
92+ errors = encoding_error_handler ,
11693 ):
11794 lines = (buffer + chunk ).split ("\n " )
11895 buffer = lines .pop ()
@@ -137,8 +114,8 @@ async def stdin_writer():
137114 json = message .model_dump_json (by_alias = True , exclude_none = True )
138115 await process .stdin .send (
139116 (json + "\n " ).encode (
140- encoding = server . encoding ,
141- errors = server . encoding_error_handler ,
117+ encoding = encoding ,
118+ errors = encoding_error_handler ,
142119 )
143120 )
144121 except anyio .ClosedResourceError :
0 commit comments