Skip to content

Commit 0454f11

Browse files
committed
refactor: remove StdioServerParameters class
1 parent c897868 commit 0454f11

File tree

4 files changed

+25
-53
lines changed

4 files changed

+25
-53
lines changed

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -521,16 +521,9 @@ if __name__ == "__main__":
521521
The SDK provides a high-level client interface for connecting to MCP servers:
522522

523523
```python
524-
from mcp import ClientSession, StdioServerParameters, types
524+
from mcp import ClientSession, types
525525
from mcp.client.stdio import stdio_client
526526

527-
# Create server parameters for stdio connection
528-
server_params = StdioServerParameters(
529-
command="python", # Executable
530-
args=["example_server.py"], # Optional command line arguments
531-
env=None, # Optional environment variables
532-
)
533-
534527

535528
# Optional: create a sampling callback
536529
async def handle_sampling_message(
@@ -548,7 +541,12 @@ async def handle_sampling_message(
548541

549542

550543
async def run():
551-
async with stdio_client(server_params) as (read, write):
544+
# Connect to server using stdio transport
545+
async with stdio_client(
546+
command="python", # Executable
547+
args=["example_server.py"], # Optional command line arguments
548+
env=None, # Optional environment variables
549+
) as (read, write):
552550
async with ClientSession(
553551
read, write, sampling_callback=handle_sampling_message
554552
) as session:

src/mcp/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .client.session import ClientSession
2-
from .client.stdio import StdioServerParameters, stdio_client
2+
from .client.stdio import stdio_client
33
from .server.session import ServerSession
44
from .server.stdio import stdio_server
55
from .shared.exceptions import McpError
@@ -101,7 +101,6 @@
101101
"ServerResult",
102102
"ServerSession",
103103
"SetLevelRequest",
104-
"StdioServerParameters",
105104
"StopReason",
106105
"SubscribeRequest",
107106
"Tool",

src/mcp/client/stdio.py

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import anyio.lowlevel
88
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
99
from anyio.streams.text import TextReceiveStream
10-
from pydantic import BaseModel, Field
1110

1211
import 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:

tests/client/test_stdio.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from mcp.client.stdio import StdioServerParameters, stdio_client
5+
from mcp.client.stdio import stdio_client
66
from mcp.types import JSONRPCMessage, JSONRPCRequest, JSONRPCResponse
77

88
tee: str = shutil.which("tee") # type: ignore
@@ -11,9 +11,7 @@
1111
@pytest.mark.anyio
1212
@pytest.mark.skipif(tee is None, reason="could not find tee command")
1313
async def test_stdio_client():
14-
server_parameters = StdioServerParameters(command=tee)
15-
16-
async with stdio_client(server_parameters) as (read_stream, write_stream):
14+
async with stdio_client(command=tee) as (read_stream, write_stream):
1715
# Test sending and receiving messages
1816
messages = [
1917
JSONRPCMessage(root=JSONRPCRequest(jsonrpc="2.0", id=1, method="ping")),

0 commit comments

Comments
 (0)