Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,8 @@ import requests
|
|
| 4 |
from fastapi import FastAPI, Response
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
-
from
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
@@ -52,36 +53,21 @@ async def fetch_github_trending():
|
|
| 52 |
print(f"Error fetching GitHub trending: {e}")
|
| 53 |
return cached_trending if cached_trending else []
|
| 54 |
|
| 55 |
-
# 定义MCP工具
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 60 |
fetch_github_trending()
|
| 61 |
return {"trending": cached_trending[:num]}
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
Tool(
|
| 66 |
-
name="get_trending_repos",
|
| 67 |
-
description="Fetch top GitHub trending projects with stars >1000",
|
| 68 |
-
function=get_trending_repos,
|
| 69 |
-
parameters={
|
| 70 |
-
"type": "object",
|
| 71 |
-
"properties": {"num": {"type": "integer", "description": "Number of repos to return"}},
|
| 72 |
-
"required": []
|
| 73 |
-
}
|
| 74 |
-
)
|
| 75 |
-
]
|
| 76 |
-
|
| 77 |
-
# MCP处理器
|
| 78 |
-
mcp_handler = MCPHandler(tools=tools)
|
| 79 |
-
|
| 80 |
-
# 添加MCP路由到FastAPI
|
| 81 |
-
app.include_router(mcp_route(mcp_handler, prefix="/mcp")) # MCP端点:/mcp/tools, /mcp/call 等
|
| 82 |
|
| 83 |
async def trending_generator():
|
| 84 |
-
"""SSE 事件生成器
|
| 85 |
while True:
|
| 86 |
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 87 |
await fetch_github_trending()
|
|
@@ -93,7 +79,7 @@ async def get_trending():
|
|
| 93 |
"""原有端点"""
|
| 94 |
if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 95 |
await fetch_github_trending()
|
| 96 |
-
return {"trending": cached_trending, "last_updated": last_updated.isoformat()}
|
| 97 |
|
| 98 |
@app.get("/trending-sse")
|
| 99 |
async def get_trending_sse():
|
|
|
|
| 4 |
from fastapi import FastAPI, Response
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
+
from starlette.routing import Mount # 用于挂载
|
| 8 |
+
from mcp.server.fastmcp import FastMCP # 导入MCP SDK
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 53 |
print(f"Error fetching GitHub trending: {e}")
|
| 54 |
return cached_trending if cached_trending else []
|
| 55 |
|
| 56 |
+
# 定义MCP服务器和工具
|
| 57 |
+
mcp_server = FastMCP(name="GithubTrending", stateless_http=True)
|
| 58 |
+
|
| 59 |
+
@mcp_server.tool()
|
| 60 |
+
def get_trending_repos(num: int = 10) -> dict:
|
| 61 |
+
"""Tool to get top GitHub trending repositories with stars >1000"""
|
| 62 |
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 63 |
fetch_github_trending()
|
| 64 |
return {"trending": cached_trending[:num]}
|
| 65 |
|
| 66 |
+
# 挂载MCP到FastAPI(使用/mcp路径)
|
| 67 |
+
app.mount("/mcp", app=mcp_server.streamable_http_app())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
async def trending_generator():
|
| 70 |
+
"""SSE 事件生成器"""
|
| 71 |
while True:
|
| 72 |
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 73 |
await fetch_github_trending()
|
|
|
|
| 79 |
"""原有端点"""
|
| 80 |
if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 81 |
await fetch_github_trending()
|
| 82 |
+
return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}
|
| 83 |
|
| 84 |
@app.get("/trending-sse")
|
| 85 |
async def get_trending_sse():
|