7
7
import anyio .lowlevel
8
8
from anyio .streams .memory import MemoryObjectReceiveStream , MemoryObjectSendStream
9
9
from anyio .streams .text import TextReceiveStream
10
- from pydantic import BaseModel , Field
11
10
12
11
import mcp .types as types
13
12
@@ -52,41 +51,19 @@ def get_default_environment() -> dict[str, str]:
52
51
return env
53
52
54
53
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
-
85
54
@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
+ ):
87
62
"""
88
63
Client transport for stdio: this will connect to a server by spawning a
89
64
process and communicating with it over stdin/stdout.
65
+
66
+
90
67
"""
91
68
read_stream : MemoryObjectReceiveStream [types .JSONRPCMessage | Exception ]
92
69
read_stream_writer : MemoryObjectSendStream [types .JSONRPCMessage | Exception ]
@@ -98,8 +75,8 @@ async def stdio_client(server: StdioServerParameters):
98
75
write_stream , write_stream_reader = anyio .create_memory_object_stream (0 )
99
76
100
77
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 (),
103
80
stderr = sys .stderr ,
104
81
)
105
82
@@ -111,8 +88,8 @@ async def stdout_reader():
111
88
buffer = ""
112
89
async for chunk in TextReceiveStream (
113
90
process .stdout ,
114
- encoding = server . encoding ,
115
- errors = server . encoding_error_handler ,
91
+ encoding = encoding ,
92
+ errors = encoding_error_handler ,
116
93
):
117
94
lines = (buffer + chunk ).split ("\n " )
118
95
buffer = lines .pop ()
@@ -137,8 +114,8 @@ async def stdin_writer():
137
114
json = message .model_dump_json (by_alias = True , exclude_none = True )
138
115
await process .stdin .send (
139
116
(json + "\n " ).encode (
140
- encoding = server . encoding ,
141
- errors = server . encoding_error_handler ,
117
+ encoding = encoding ,
118
+ errors = encoding_error_handler ,
142
119
)
143
120
)
144
121
except anyio .ClosedResourceError :
0 commit comments