Skip to content
39 changes: 39 additions & 0 deletions Server/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,50 @@
import logging
from contextlib import asynccontextmanager
import os
import sys
import threading
import time
from typing import AsyncIterator, Any
from urllib.parse import urlparse

if sys.platform == 'win32':
import msvcrt
import io

# Set binary mode on stdin/stdout to prevent automatic translation at the FD level
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

# Define a proxy buffer that actively strips \r bytes from the output stream
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)

# Detach the underlying buffer from the current sys.stdout
# and re-wrap it with our CRStripper and a new TextIOWrapper
_original_buffer = sys.stdout.detach()
sys.stdout = io.TextIOWrapper(
CRStripper(_original_buffer),
encoding=sys.stdout.encoding or 'utf-8',
errors=sys.stdout.errors or 'strict',
newline='\n', # Enforce LF for text writing
line_buffering=True,
write_through=True
)

from fastmcp import FastMCP
from logging.handlers import RotatingFileHandler
from starlette.requests import Request
Expand Down