Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,7 @@ async def lifespan(app: FastAPI):
|
|
| 16 |
await fetch_github_trending()
|
| 17 |
yield
|
| 18 |
|
|
|
|
| 19 |
app = FastAPI(lifespan=lifespan)
|
| 20 |
|
| 21 |
# CORS 配置
|
|
@@ -93,7 +94,7 @@ async def fetch_github_trending() -> List[Dict]:
|
|
| 93 |
detail="Failed to fetch trending repositories"
|
| 94 |
)
|
| 95 |
|
| 96 |
-
# 初始化 MCP 服务器
|
| 97 |
mcp_server = FastMCP(name="GitHubTrendingAPI")
|
| 98 |
|
| 99 |
@mcp_server.tool()
|
|
@@ -103,8 +104,10 @@ async def get_trending_repos(num: int = 10) -> dict:
|
|
| 103 |
await fetch_github_trending()
|
| 104 |
return {"trending": cached_trending[:num]}
|
| 105 |
|
| 106 |
-
# 挂载 MCP 服务
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
|
| 109 |
@app.get("/")
|
| 110 |
async def root():
|
|
@@ -112,8 +115,7 @@ async def root():
|
|
| 112 |
"message": "GitHub Trending API is running",
|
| 113 |
"documentation": "/docs",
|
| 114 |
"mcp_endpoint": "/mcp",
|
| 115 |
-
"trending_endpoint": "/trending"
|
| 116 |
-
"sse_endpoint": "/trending/stream"
|
| 117 |
}
|
| 118 |
|
| 119 |
@app.get("/trending")
|
|
@@ -127,6 +129,4 @@ async def get_trending(num: int = 10):
|
|
| 127 |
"last_updated": last_updated.isoformat() if last_updated else None,
|
| 128 |
"cache_expires_in": CACHE_EXPIRY.total_seconds(),
|
| 129 |
"rate_limit_info": "Consider adding a GitHub token for higher rate limits"
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
print(type(mcp_server.http_app()))
|
|
|
|
| 16 |
await fetch_github_trending()
|
| 17 |
yield
|
| 18 |
|
| 19 |
+
# 创建主 FastAPI 应用
|
| 20 |
app = FastAPI(lifespan=lifespan)
|
| 21 |
|
| 22 |
# CORS 配置
|
|
|
|
| 94 |
detail="Failed to fetch trending repositories"
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# 初始化 MCP 服务器
|
| 98 |
mcp_server = FastMCP(name="GitHubTrendingAPI")
|
| 99 |
|
| 100 |
@mcp_server.tool()
|
|
|
|
| 104 |
await fetch_github_trending()
|
| 105 |
return {"trending": cached_trending[:num]}
|
| 106 |
|
| 107 |
+
# 创建独立的 FastAPI 子应用并挂载 MCP 服务
|
| 108 |
+
mcp_subapp = FastAPI()
|
| 109 |
+
mcp_subapp.router.routes.extend(mcp_server.http_app().routes) # 手动合并路由
|
| 110 |
+
app.mount("/mcp", mcp_subapp) # 挂载到 /mcp 路径
|
| 111 |
|
| 112 |
@app.get("/")
|
| 113 |
async def root():
|
|
|
|
| 115 |
"message": "GitHub Trending API is running",
|
| 116 |
"documentation": "/docs",
|
| 117 |
"mcp_endpoint": "/mcp",
|
| 118 |
+
"trending_endpoint": "/trending"
|
|
|
|
| 119 |
}
|
| 120 |
|
| 121 |
@app.get("/trending")
|
|
|
|
| 129 |
"last_updated": last_updated.isoformat() if last_updated else None,
|
| 130 |
"cache_expires_in": CACHE_EXPIRY.total_seconds(),
|
| 131 |
"rate_limit_info": "Consider adding a GitHub token for higher rate limits"
|
| 132 |
+
}
|
|
|
|
|
|