Skip to content

Commit f448ea0

Browse files
committed
refactor(server): enhance python server architecture and stability
- Centralize utility functions and logging configuration - Improve argument parsing and main entry point structure - Enhance stdio transport reliability with node wrapper improvements - Remove legacy scripts and cleanup project root - Translate comments to English and fix minor bugs --- refactor(server): 파이썬 서버 아키텍처 및 안정성 개선 - 유틸리티 함수 및 로깅 설정 중앙화 - 인자 파싱 및 메인 엔트리 포인트 구조 개선 - Node 래퍼 개선을 통한 stdio 전송 신뢰성 향상 - 레거시 스크립트 제거 및 프로젝트 루트 정리 - 주석 영문화 및 사소한 버그 수정
1 parent 3d9ff65 commit f448ea0

File tree

11 files changed

+56
-803
lines changed

11 files changed

+56
-803
lines changed

.github/workflows/python-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
version: "latest"
2323

2424
- name: Set up Python
25-
run: uv python install 3.10
25+
run: uv python install 3.11
2626

2727
- name: Install dependencies
2828
run: |

MCPForUnity/Server~/mcp_wrapper.py

Lines changed: 0 additions & 121 deletions
This file was deleted.

MCPForUnity/Server~/pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ dev = [
2020
]
2121

2222
[project.scripts]
23-
mcp-for-unity = "main:main"
23+
mcp-for-unity = "src.main:main"
24+
25+
[tool.setuptools.packages.find]
26+
where = ["src"]
2427

2528
[build-system]
2629
requires = ["setuptools>=80.9.0", "wheel>=0.45.1"]

MCPForUnity/Server~/src/main.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@
6565
"httpx",
6666
"urllib3",
6767
"mcp.server.lowlevel.server",
68-
"uvicorn",
69-
"uvicorn.access",
70-
"uvicorn.error",
71-
"docket",
72-
"docket.worker",
73-
"fastmcp", # <--- 🚨 범인 검거
74-
"starlette" # <--- 혹시 모를 공범
7568
):
7669
try:
7770
logging.getLogger(noisy).setLevel(
@@ -412,9 +405,26 @@ def main():
412405
logger.info(f"Starting FastMCP with HTTP transport on {host}:{port}")
413406
mcp.run(transport=transport, host=host, port=port)
414407
else:
408+
415409
# Use stdio transport for traditional MCP
416410
logger.info("Starting FastMCP with stdio transport")
417-
mcp.run(transport='stdio', show_banner=False)
411+
# 🚨 [CRITICAL] In STDIO mode only, suppress related loggers.
412+
# Silence Uvicorn and related loggers to prevent stdout pollution.
413+
# 🚨 [CRITICAL] Prevent stdout pollution in STDIO mode
414+
for name in (
415+
"uvicorn", "uvicorn.error", "uvicorn.access",
416+
"starlette",
417+
"docket", "docket.worker",
418+
"fastmcp",
419+
):
420+
lg = logging.getLogger(name)
421+
lg.setLevel(logging.WARNING) # ERROR if still too chatty
422+
lg.propagate = False # prevent duplicate root logs
423+
# If the rotating file handler was successfully created, attach it
424+
if '_fh' in globals() and _fh not in lg.handlers:
425+
lg.addHandler(_fh)
426+
427+
mcp.run(transport='stdio')
418428

419429

420430
# Run the server

MCPForUnity/Server~/wrapper.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,45 @@ log("Wrapper started");
1717

1818
const serverDir = __dirname;
1919

20+
// Construct updated PATH with common uv locations
21+
const localAppData = process.env.LOCALAPPDATA || "";
22+
const userProfile = process.env.USERPROFILE || "";
23+
const homeDir = process.env.HOME || process.env.USERPROFILE || "";
24+
25+
const extraPaths =
26+
process.platform === "win32"
27+
? [
28+
path.join(localAppData, "Programs", "uv"), // uv standalone
29+
path.join(localAppData, "uv"), // Alternative
30+
path.join(userProfile, ".cargo", "bin"), // Cargo install
31+
path.join(localAppData, "bin"),
32+
]
33+
: [
34+
path.join(homeDir, ".cargo", "bin"),
35+
path.join(homeDir, ".local", "bin"),
36+
"/usr/local/bin",
37+
"/opt/homebrew/bin", // macOS Homebrew
38+
];
39+
40+
const newPath =
41+
extraPaths.join(path.delimiter) + path.delimiter + (process.env.PATH || "");
42+
2043
// Use uv run with --quiet to minimize noise
21-
// Add --quiet to uv run commands to suppress "resolved ..." messages
2244
const pythonProcess = spawn(
2345
"uv",
2446
["run", "--quiet", "src/main.py", "--transport", "stdio"],
2547
{
2648
cwd: serverDir,
2749
stdio: ["pipe", "pipe", "pipe"],
28-
shell: true, // Needed for windows command resolution sometimes
50+
shell: true,
2951
env: {
3052
...process.env,
53+
PATH: newPath, // Inject updated PATH
3154
PYTHONUNBUFFERED: "1",
3255
PYTHONIOENCODING: "utf-8",
33-
HOME: process.env.USERPROFILE, // Ensure uv finds home on Windows
56+
...(process.platform === "win32" && userProfile
57+
? { HOME: userProfile }
58+
: {}),
3459
},
3560
}
3661
);
@@ -92,9 +117,11 @@ function cleanup() {
92117
try {
93118
pythonProcess.kill();
94119
if (process.platform === "win32") {
95-
require("child_process").execSync(
96-
`taskkill /pid ${pythonProcess.pid} /T /F`
97-
);
120+
if (pythonProcess.pid) {
121+
require("child_process").execSync(
122+
`taskkill /pid ${pythonProcess.pid} /T /F`
123+
);
124+
}
98125
}
99126
} catch (e) {
100127
/* ignore */

claude_skill_unity.zip

-73.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)