Skip to content

Commit

Permalink
Merge pull request #120 from SecretiveShell/fix-encoding-errors
Browse files Browse the repository at this point in the history
add text encoding params to STDIO client
  • Loading branch information
dsp-ant authored Jan 2, 2025
2 parents b89cabc + 99727a9 commit bd5170e
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/mcp/client/stdio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
from contextlib import asynccontextmanager
from typing import Literal

import anyio
import anyio.lowlevel
Expand Down Expand Up @@ -65,6 +66,21 @@ class StdioServerParameters(BaseModel):
If not specified, the result of get_default_environment() will be used.
"""

encoding: str = "utf-8"
"""
The text encoding used when sending/receiving messages to the server
defaults to utf-8
"""

encoding_error_handler: Literal["strict", "ignore", "replace"] = "strict"
"""
The text encoding error handler.
See https://docs.python.org/3/library/codecs.html#codec-base-classes for
explanations of possible values
"""


@asynccontextmanager
async def stdio_client(server: StdioServerParameters):
Expand Down Expand Up @@ -93,7 +109,11 @@ async def stdout_reader():
try:
async with read_stream_writer:
buffer = ""
async for chunk in TextReceiveStream(process.stdout):
async for chunk in TextReceiveStream(
process.stdout,
encoding=server.encoding,
errors=server.encoding_error_handler,
):
lines = (buffer + chunk).split("\n")
buffer = lines.pop()

Expand All @@ -115,7 +135,12 @@ async def stdin_writer():
async with write_stream_reader:
async for message in write_stream_reader:
json = message.model_dump_json(by_alias=True, exclude_none=True)
await process.stdin.send((json + "\n").encode())
await process.stdin.send(
(json + "\n").encode(
encoding=server.encoding,
errors=server.encoding_error_handler,
)
)
except anyio.ClosedResourceError:
await anyio.lowlevel.checkpoint()

Expand Down

0 comments on commit bd5170e

Please sign in to comment.