Skip to content
Closed
23 changes: 20 additions & 3 deletions Server/src/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import argparse
import asyncio
import logging
from contextlib import asynccontextmanager
from contextlib import asynccontextmanager, redirect_stdout
import os
import sys
import threading
import time
from typing import AsyncIterator, Any
from urllib.parse import urlparse
import io
from utils.cr_stripper import CRStripper

if sys.platform == 'win32':
import msvcrt
# Set binary mode on stdin/stdout to prevent automatic translation at the FD level
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)


from fastmcp import FastMCP
from logging.handlers import RotatingFileHandler
Expand Down Expand Up @@ -402,8 +411,16 @@ def main():
mcp.run(transport=transport, host=host, port=port)
else:
# Use stdio transport for traditional MCP
logger.info("Starting FastMCP with stdio transport")
mcp.run(transport='stdio')
removed_crlf = io.TextIOWrapper(
CRStripper(sys.stdout.buffer),
encoding=sys.stdout.encoding or 'utf-8',
newline='\n',
line_buffering=True,
)

with redirect_stdout(removed_crlf):
logger.info("Starting FastMCP with stdio transport")
mcp.run(transport='stdio')


# Run the server
Expand Down
6 changes: 6 additions & 0 deletions Server/src/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
SUMMARY: Utils package initialization.
"""
from .cr_stripper import CRStripper

__all__ = ["CRStripper"]
18 changes: 18 additions & 0 deletions Server/src/utils/cr_stripper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# This utility strips carriage return characters (\r) from bytes or strings
class CRStripper:
def __init__(self, stream):
self._stream = stream

def write(self, data):
if isinstance(data, bytes):
return self._stream.write(data.replace(b'\r', b''))
if isinstance(data, str):
return self._stream.write(data.replace('\r', ''))
return self._stream.write(data)

def flush(self):
return self._stream.flush()

def __getattr__(self, name):
return getattr(self._stream, name)