-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstart_mcp_server.py
More file actions
131 lines (112 loc) · 4.57 KB
/
start_mcp_server.py
File metadata and controls
131 lines (112 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# start_mcp_server.py - MCP Server Launcher
# start_mcp_server.py - MCP 服务器启动器
"""
MCP Server Launcher with Virtual Environment Support
支持虚拟环境的 MCP 服务器启动器
This script ensures that the MCP server is started from the project root directory
with the virtual environment properly activated.
此脚本确保 MCP 服务器从项目根目录启动,并正确激活虚拟环境。
"""
import os
import sys
import io
import subprocess
from pathlib import Path
os.environ['PYTHONUNBUFFERED'] = '1'
def main():
"""Main entry point for the MCP server launcher
MCP 服务器启动器的主入口点"""
if sys.platform == 'win32':
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
# Install dependencies using winget
install_cmd = [
"winget",
"install",
"Kitware.CMake",
"Ninja-build.Ninja",
"oss-winget.gperf",
"Python.Python.3.12",
"Git.Git",
"oss-winget.dtc",
"wget",
"7zip.7zip",
"-e",
"--accept-package-agreements",
"--accept-source-agreements",
]
try:
print("Installing dependencies with winget...")
subprocess.run(install_cmd, check=True)
print("Dependencies installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Dependency installation failed (exit code {e.returncode}): {e}")
except OSError as e:
print(f"Dependency installation failed (OS error): {e}")
# Get project root directory
# 获取项目根目录
project_root = Path(__file__).parent
print("=== MCP Server Launcher ===")
print("=== MCP 服务器启动器 ===")
print(f"Project root: {project_root}")
print(f"项目根目录: {project_root}")
# Change to project root directory
# 切换到项目根目录
os.chdir(project_root)
print(f"Working directory: {os.getcwd()}")
print(f"工作目录: {os.getcwd()}")
# Check if virtual environment exists
# 检查虚拟环境是否存在
venv_path = project_root / ".venv"
if not venv_path.exists():
print("[Error] Virtual environment not found at: .venv")
print("[错误] 在 .venv 处未找到虚拟环境")
print("[Info] Please create virtual environment first: python -m venv .venv")
print("[信息] 请先创建虚拟环境: python -m venv .venv")
return 1
# Get virtual environment Python executable
# 获取虚拟环境 Python 可执行文件
if os.name == "nt": # Windows
python_exe = venv_path / "Scripts" / "python.exe"
else: # Linux/macOS
python_exe = venv_path / "bin" / "python"
if not python_exe.exists():
print(f"[Error] Python executable not found: {python_exe}")
print(f"[错误] 未找到 Python 可执行文件: {python_exe}")
return 1
print(f"Using Python: {python_exe}")
print(f"使用 Python: {python_exe}")
# MCP server script path
# MCP 服务器脚本路径
mcp_server_script = project_root / "src" / "mcp_server.py"
if not mcp_server_script.exists():
print(f"[Error] MCP server script not found: {mcp_server_script}")
print(f"[错误] 未找到 MCP 服务器脚本: {mcp_server_script}")
return 1
# Build command to start MCP server with virtual environment Python
# 构建使用虚拟环境 Python 启动 MCP 服务器的命令
command = [str(python_exe), str(mcp_server_script)] + sys.argv[1:]
print("\n[Starting] Starting MCP server with virtual environment...")
print("\n[启动] 正在使用虚拟环境启动 MCP 服务器...")
print(f"Command: {' '.join(command)}")
print(f"命令: {' '.join(command)}")
try:
# Start the MCP server
# 启动 MCP 服务器
result = subprocess.run(command, check=True)
return result.returncode
except subprocess.CalledProcessError as e:
print(f"[Error] MCP server failed with exit code {e.returncode}")
print(f"[错误] MCP 服务器失败,退出代码 {e.returncode}")
return e.returncode
except KeyboardInterrupt:
print("\n[Info] MCP server stopped by user")
print("\n[信息] MCP 服务器被用户停止")
return 0
except Exception as e:
print(f"[Error] Unexpected error: {e}")
print(f"[错误] 意外错误: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())