Skip to content

Commit 4f36581

Browse files
add text encoding params to STDIO client
1 parent e691c51 commit 4f36581

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Diff for: src/mcp/client/stdio.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
from contextlib import asynccontextmanager
4+
from typing import Literal
45

56
import anyio
67
import anyio.lowlevel
@@ -65,6 +66,20 @@ class StdioServerParameters(BaseModel):
6566
If not specified, the result of get_default_environment() will be used.
6667
"""
6768

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 explanations of possible values
81+
"""
82+
6883

6984
@asynccontextmanager
7085
async def stdio_client(server: StdioServerParameters):
@@ -93,7 +108,7 @@ async def stdout_reader():
93108
try:
94109
async with read_stream_writer:
95110
buffer = ""
96-
async for chunk in TextReceiveStream(process.stdout):
111+
async for chunk in TextReceiveStream(process.stdout, encoding=server.encoding, errors=server.encoding_error_handler):
97112
lines = (buffer + chunk).split("\n")
98113
buffer = lines.pop()
99114

@@ -115,7 +130,7 @@ async def stdin_writer():
115130
async with write_stream_reader:
116131
async for message in write_stream_reader:
117132
json = message.model_dump_json(by_alias=True, exclude_none=True)
118-
await process.stdin.send((json + "\n").encode())
133+
await process.stdin.send((json + "\n").encode(encoding=server.encoding, errors=server.encoding_error_handler))
119134
except anyio.ClosedResourceError:
120135
await anyio.lowlevel.checkpoint()
121136

0 commit comments

Comments
 (0)