Skip to content

Commit 0f89329

Browse files
committed
1 parent 3f88185 commit 0f89329

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

Server/src/main.py

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,21 @@
11
import argparse
22
import asyncio
33
import logging
4-
from contextlib import asynccontextmanager
4+
from contextlib import asynccontextmanager, redirect_stdout
55
import os
66
import sys
77
import threading
88
import time
99
from typing import AsyncIterator, Any
1010
from urllib.parse import urlparse
11+
import io
12+
from utils.cr_stripper import CRStripper
1113

1214
if sys.platform == 'win32':
1315
import msvcrt
14-
import io
15-
1616
# Set binary mode on stdin/stdout to prevent automatic translation at the FD level
17-
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
1817
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
19-
20-
# Define a proxy buffer that actively strips \r bytes from the output stream
21-
class CRStripper:
22-
def __init__(self, stream):
23-
self._stream = stream
24-
25-
def write(self, data):
26-
if isinstance(data, bytes):
27-
return self._stream.write(data.replace(b'\r', b''))
28-
if isinstance(data, str):
29-
return self._stream.write(data.replace('\r', ''))
30-
return self._stream.write(data)
31-
32-
def flush(self):
33-
return self._stream.flush()
34-
35-
def __getattr__(self, name):
36-
return getattr(self._stream, name)
37-
38-
# Detach the underlying buffer from the current sys.stdout
39-
# and re-wrap it with our CRStripper and a new TextIOWrapper
40-
_original_buffer = sys.stdout.detach()
41-
sys.stdout = io.TextIOWrapper(
42-
CRStripper(_original_buffer),
43-
encoding=sys.stdout.encoding or 'utf-8',
44-
errors=sys.stdout.errors or 'strict',
45-
newline='\n', # Enforce LF for text writing
46-
line_buffering=True,
47-
write_through=True
48-
)
18+
4919

5020
from fastmcp import FastMCP
5121
from logging.handlers import RotatingFileHandler
@@ -441,8 +411,16 @@ def main():
441411
mcp.run(transport=transport, host=host, port=port)
442412
else:
443413
# Use stdio transport for traditional MCP
444-
logger.info("Starting FastMCP with stdio transport")
445-
mcp.run(transport='stdio')
414+
removed_crlf = io.TextIOWrapper(
415+
CRStripper(sys.stdout.buffer),
416+
encoding=sys.stdout.encoding or 'utf-8',
417+
newline='\n',
418+
line_buffering=True,
419+
)
420+
421+
with redirect_stdout(removed_crlf):
422+
logger.info("Starting FastMCP with stdio transport")
423+
mcp.run(transport='stdio')
446424

447425

448426
# Run the server

Server/src/utils/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
SUMMARY: Utils package initialization.
3+
"""
4+
from .cr_stripper import CRStripper
5+
6+
__all__ = ["CRStripper"]

Server/src/utils/cr_stripper.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# This utility strips carriage return characters (\r) from bytes or strings
3+
class CRStripper:
4+
def __init__(self, stream):
5+
self._stream = stream
6+
7+
def write(self, data):
8+
if isinstance(data, bytes):
9+
return self._stream.write(data.replace(b'\r', b''))
10+
if isinstance(data, str):
11+
return self._stream.write(data.replace('\r', ''))
12+
return self._stream.write(data)
13+
14+
def flush(self):
15+
return self._stream.flush()
16+
17+
def __getattr__(self, name):
18+
return getattr(self._stream, name)

0 commit comments

Comments
 (0)