Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
from fastapi import FastAPI, Response
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
from datetime import datetime, timedelta
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
@@ -26,7 +27,7 @@ async def fetch_github_trending():
|
|
| 26 |
headers = {}
|
| 27 |
github_token = os.getenv("GITHUB_TOKEN")
|
| 28 |
if github_token:
|
| 29 |
-
headers["Authorization"] = f"token {github_token}"
|
| 30 |
|
| 31 |
try:
|
| 32 |
response = requests.get(url, params=params, headers=headers)
|
|
@@ -51,29 +52,52 @@ async def fetch_github_trending():
|
|
| 51 |
print(f"Error fetching GitHub trending: {e}")
|
| 52 |
return cached_trending if cached_trending else []
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
async def trending_generator():
|
| 55 |
-
"""SSE 事件生成器"""
|
| 56 |
while True:
|
| 57 |
-
# 每5分钟更新一次数据
|
| 58 |
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 59 |
-
await fetch_github_trending()
|
| 60 |
-
|
| 61 |
-
# 发送当前数据
|
| 62 |
yield f"data: {cached_trending}\n\n"
|
| 63 |
-
|
| 64 |
-
# 等待一段时间再检查更新
|
| 65 |
time.sleep(30)
|
| 66 |
|
| 67 |
@app.get("/trending")
|
| 68 |
async def get_trending():
|
| 69 |
-
"""
|
| 70 |
if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 71 |
await fetch_github_trending()
|
| 72 |
return {"trending": cached_trending, "last_updated": last_updated.isoformat()}
|
| 73 |
|
| 74 |
@app.get("/trending-sse")
|
| 75 |
async def get_trending_sse():
|
| 76 |
-
"""SSE
|
| 77 |
return StreamingResponse(
|
| 78 |
trending_generator(),
|
| 79 |
media_type="text/event-stream",
|
|
|
|
| 4 |
from fastapi import FastAPI, Response
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
+
from mcp_sdk import MCPHandler, Tool, mcp_route # 导入MCP SDK
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 27 |
headers = {}
|
| 28 |
github_token = os.getenv("GITHUB_TOKEN")
|
| 29 |
if github_token:
|
| 30 |
+
headers["Authorization"] = f"token {github_token}"
|
| 31 |
|
| 32 |
try:
|
| 33 |
response = requests.get(url, params=params, headers=headers)
|
|
|
|
| 52 |
print(f"Error fetching GitHub trending: {e}")
|
| 53 |
return cached_trending if cached_trending else []
|
| 54 |
|
| 55 |
+
# 定义MCP工具:暴露给AI模型使用
|
| 56 |
+
def get_trending_repos(params: dict) -> dict:
|
| 57 |
+
"""Tool to get top GitHub trending repositories. Params: {'num': int (default 10)}"""
|
| 58 |
+
num = params.get('num', 10)
|
| 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 |
+
# 注册MCP工具
|
| 64 |
+
tools = [
|
| 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 事件生成器(保留原有,MCP可复用)"""
|
| 85 |
while True:
|
|
|
|
| 86 |
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 87 |
+
await fetch_github_trending()
|
|
|
|
|
|
|
| 88 |
yield f"data: {cached_trending}\n\n"
|
|
|
|
|
|
|
| 89 |
time.sleep(30)
|
| 90 |
|
| 91 |
@app.get("/trending")
|
| 92 |
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():
|
| 100 |
+
"""原有SSE端点"""
|
| 101 |
return StreamingResponse(
|
| 102 |
trending_generator(),
|
| 103 |
media_type="text/event-stream",
|