Skip to content

Commit 239813d

Browse files
committed
major update
major update
1 parent ac93bfc commit 239813d

16 files changed

Lines changed: 3525 additions & 353 deletions

README.md

Lines changed: 123 additions & 349 deletions
Large diffs are not rendered by default.

app/main.py

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
import uvicorn
77
from fastapi import FastAPI, HTTPException
88
from fastapi.middleware.cors import CORSMiddleware
9+
from fastapi.responses import FileResponse
10+
from fastapi.staticfiles import StaticFiles
911
from datetime import datetime
10-
from typing import Dict, Any
12+
from typing import Dict, Any, List
13+
from pathlib import Path
1114

1215
from .config.settings import settings
1316
from .utils.logger import get_logger, setup_logger
1417
from .services.commute_service import CommuteService
1518
from .workers.tasks import check_commute_and_notify, health_check
19+
from .mcp.server_factory import server_factory, ServerType
1620

1721
# 初始化日志系统
1822
setup_logger()
@@ -56,12 +60,103 @@ async def shutdown_event():
5660
async def root() -> Dict[str, Any]:
5761
"""根路径"""
5862
return {
59-
"message": "欢迎使用MCP智能通勤助手API",
63+
"message": "欢迎使用MCP工具大全API",
6064
"version": "1.0.0",
6165
"timestamp": datetime.now().isoformat()
6266
}
6367

6468

69+
# MCP服务器管理端点
70+
@app.get("/mcp/servers")
71+
async def list_mcp_servers() -> Dict[str, Any]:
72+
"""列出所有可用的MCP服务器"""
73+
servers = server_factory.list_available_servers()
74+
return {
75+
"servers": [
76+
{
77+
"name": name,
78+
"config": {
79+
"command": config.command,
80+
"args": config.args,
81+
"timeout": config.timeout,
82+
"max_concurrent": config.max_concurrent,
83+
"auto_restart": config.auto_restart
84+
}
85+
}
86+
for name, config in servers.items()
87+
],
88+
"total": len(servers),
89+
"timestamp": datetime.now().isoformat()
90+
}
91+
92+
93+
@app.get("/mcp/servers/{server_name}")
94+
async def get_mcp_server_info(server_name: str) -> Dict[str, Any]:
95+
"""获取指定MCP服务器信息"""
96+
config = server_factory.get_server_config(server_name)
97+
if not config:
98+
raise HTTPException(status_code=404, detail=f"服务器不存在: {server_name}")
99+
100+
return {
101+
"name": config.name,
102+
"command": config.command,
103+
"args": config.args,
104+
"env": config.env,
105+
"working_dir": config.working_dir,
106+
"timeout": config.timeout,
107+
"max_concurrent": config.max_concurrent,
108+
"auto_restart": config.auto_restart,
109+
"health_check_interval": config.health_check_interval,
110+
"timestamp": datetime.now().isoformat()
111+
}
112+
113+
114+
@app.get("/mcp/server-types")
115+
async def list_server_types() -> Dict[str, Any]:
116+
"""列出所有可用的服务器类型"""
117+
return {
118+
"server_types": [
119+
{"value": t.value, "name": t.name}
120+
for t in ServerType
121+
],
122+
"timestamp": datetime.now().isoformat()
123+
}
124+
125+
126+
@app.post("/mcp/execute/{server_name}/{tool_name}")
127+
async def execute_mcp_tool(
128+
server_name: str,
129+
tool_name: str,
130+
params: Dict[str, Any] = {}
131+
) -> Dict[str, Any]:
132+
"""执行MCP服务器工具"""
133+
try:
134+
server_config = server_factory.get_server_config(server_name)
135+
if not server_config:
136+
raise HTTPException(status_code=404, detail=f"服务器不存在: {server_name}")
137+
138+
module_path = f"app.mcp.servers.{server_name}_server"
139+
module = __import__(module_path, fromlist=[""])
140+
server_class_name = f"{server_name.upper()}MCPServer"
141+
server_class = getattr(module, server_class_name, None)
142+
143+
if not server_class:
144+
raise HTTPException(status_code=404, detail=f"未找到服务器类: {server_class_name}")
145+
146+
server = server_class(config={})
147+
result = await server.execute_tool(tool_name, params)
148+
149+
return {
150+
"server": server_name,
151+
"tool": tool_name,
152+
"result": result,
153+
"timestamp": datetime.now().isoformat()
154+
}
155+
except Exception as e:
156+
logger.error(f"MCP工具执行失败: {server_name}/{tool_name}", error=str(e))
157+
raise HTTPException(status_code=500, detail=f"工具执行失败: {str(e)}")
158+
159+
65160
@app.get("/health")
66161
async def health_check_endpoint() -> Dict[str, Any]:
67162
"""健康检查端点"""
@@ -184,6 +279,14 @@ def create_app() -> FastAPI:
184279
return app
185280

186281

282+
@app.get("/ui")
283+
async def get_ui():
284+
"""MCP管理界面"""
285+
import os
286+
web_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "web", "mcp_manager.html")
287+
return FileResponse(web_path)
288+
289+
187290
if __name__ == "__main__":
188291
# 直接运行时的配置
189292
uvicorn.run(

app/mcp/server_factory.py

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ class ServerType(Enum):
2323
NEWS = "news"
2424
STOCK = "stock"
2525
TRANSLATION = "translation"
26+
FILESYSTEM = "filesystem"
27+
GIT = "git"
28+
DATABASE = "database"
29+
HTTP = "http"
30+
SLACK = "slack"
31+
GITHUB = "github"
32+
BRAVE_SEARCH = "brave_search"
33+
NOTION = "notion"
34+
GOOGLE_SHEETS = "google_sheets"
35+
BROWSER = "browser"
36+
MEMORY = "memory"
2637

2738

2839
class ServerFactory:
@@ -79,10 +90,132 @@ def _load_builtin_servers(self):
7990
max_concurrent=15,
8091
auto_restart=True,
8192
health_check_interval=120
82-
)
93+
),
94+
ServerType.FILESYSTEM: ManagedServer(
95+
name="filesystem-mcp-server",
96+
command="python",
97+
args=["-m", "app.mcp.servers.filesystem_server"],
98+
env={"PYTHONPATH": "."},
99+
working_dir=".",
100+
timeout=300,
101+
max_concurrent=20,
102+
auto_restart=True,
103+
health_check_interval=120
104+
),
105+
ServerType.GIT: ManagedServer(
106+
name="git-mcp-server",
107+
command="python",
108+
args=["-m", "app.mcp.servers.git_server"],
109+
env={"PYTHONPATH": "."},
110+
working_dir=".",
111+
timeout=300,
112+
max_concurrent=10,
113+
auto_restart=True,
114+
health_check_interval=60
115+
),
116+
ServerType.DATABASE: ManagedServer(
117+
name="database-mcp-server",
118+
command="python",
119+
args=["-m", "app.mcp.servers.database_server"],
120+
env={"PYTHONPATH": "."},
121+
working_dir=".",
122+
timeout=300,
123+
max_concurrent=15,
124+
auto_restart=True,
125+
health_check_interval=120
126+
),
127+
ServerType.HTTP: ManagedServer(
128+
name="http-mcp-server",
129+
command="python",
130+
args=["-m", "app.mcp.servers.http_client_server"],
131+
env={"PYTHONPATH": "."},
132+
working_dir=".",
133+
timeout=300,
134+
max_concurrent=30,
135+
auto_restart=True,
136+
health_check_interval=120
137+
),
138+
ServerType.SLACK: ManagedServer(
139+
name="slack-mcp-server",
140+
command="python",
141+
args=["-m", "app.mcp.servers.slack_server"],
142+
env={"PYTHONPATH": "."},
143+
working_dir=".",
144+
timeout=300,
145+
max_concurrent=10,
146+
auto_restart=True,
147+
health_check_interval=60
148+
),
149+
ServerType.GITHUB: ManagedServer(
150+
name="github-mcp-server",
151+
command="python",
152+
args=["-m", "app.mcp.servers.github_server"],
153+
env={"PYTHONPATH": "."},
154+
working_dir=".",
155+
timeout=300,
156+
max_concurrent=20,
157+
auto_restart=True,
158+
health_check_interval=60
159+
),
160+
ServerType.BRAVE_SEARCH: ManagedServer(
161+
name="brave-search-mcp-server",
162+
command="python",
163+
args=["-m", "app.mcp.servers.brave_search_server"],
164+
env={"PYTHONPATH": "."},
165+
working_dir=".",
166+
timeout=300,
167+
max_concurrent=20,
168+
auto_restart=True,
169+
health_check_interval=120
170+
),
171+
ServerType.NOTION: ManagedServer(
172+
name="notion-mcp-server",
173+
command="python",
174+
args=["-m", "app.mcp.servers.notion_server"],
175+
env={"PYTHONPATH": "."},
176+
working_dir=".",
177+
timeout=300,
178+
max_concurrent=15,
179+
auto_restart=True,
180+
health_check_interval=60
181+
),
182+
ServerType.GOOGLE_SHEETS: ManagedServer(
183+
name="google-sheets-mcp-server",
184+
command="python",
185+
args=["-m", "app.mcp.servers.google_sheets_server"],
186+
env={"PYTHONPATH": "."},
187+
working_dir=".",
188+
timeout=300,
189+
max_concurrent=15,
190+
auto_restart=True,
191+
health_check_interval=120
192+
),
193+
ServerType.BROWSER: ManagedServer(
194+
name="browser-mcp-server",
195+
command="python",
196+
args=["-m", "app.mcp.servers.browser_server"],
197+
env={"PYTHONPATH": "."},
198+
working_dir=".",
199+
timeout=300,
200+
max_concurrent=5,
201+
auto_restart=True,
202+
health_check_interval=60
203+
),
204+
ServerType.MEMORY: ManagedServer(
205+
name="memory-mcp-server",
206+
command="python",
207+
args=["-m", "app.mcp.servers.memory_server"],
208+
env={"PYTHONPATH": "."},
209+
working_dir=".",
210+
timeout=300,
211+
max_concurrent=20,
212+
auto_restart=True,
213+
health_check_interval=120
214+
),
83215
}
84216

85-
self.registered_configs.update(builtin_configs)
217+
for key, value in builtin_configs.items():
218+
self.registered_configs[key.value] = value
86219
logger.info("内置MCP服务器配置加载完成")
87220

88221
def register_custom_server(

0 commit comments

Comments
 (0)