tianruci commited on
Commit
bef6b58
·
verified ·
1 Parent(s): aac1e01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -10
app.py CHANGED
@@ -4,18 +4,17 @@ import requests
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 fastmcp.server import FastMCP
9
  from contextlib import asynccontextmanager
10
 
11
  @asynccontextmanager
12
  async def lifespan(app: FastAPI):
13
- await fetch_github_trending() # 启动时初始化
14
  yield
15
 
16
  app = FastAPI(lifespan=lifespan)
17
 
18
- # 缓存最新获取的热门项目
19
  cached_trending = []
20
  last_updated = None
21
 
@@ -41,7 +40,6 @@ async def fetch_github_trending():
41
  response.raise_for_status()
42
  items = response.json().get("items", [])
43
 
44
- # 简化项目信息
45
  trending_projects = []
46
  for item in items:
47
  trending_projects.append({
@@ -59,8 +57,8 @@ async def fetch_github_trending():
59
  print(f"Error fetching GitHub trending: {e}")
60
  return cached_trending if cached_trending else []
61
 
62
- # 定义MCP服务器和工具
63
- mcp_server = FastMCP(name="GithubTrending", stateless_http=True)
64
 
65
  @mcp_server.tool()
66
  def get_trending_repos(num: int = 10) -> dict:
@@ -69,10 +67,10 @@ def get_trending_repos(num: int = 10) -> dict:
69
  fetch_github_trending()
70
  return {"trending": cached_trending[:num]}
71
 
72
- app.mount("/mcp", mcp_server.streamable_http_app())
 
73
 
74
  async def trending_generator():
75
- """SSE 事件生成器"""
76
  while True:
77
  if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
78
  await fetch_github_trending()
@@ -81,14 +79,12 @@ async def trending_generator():
81
 
82
  @app.get("/trending")
83
  async def get_trending():
84
- """原有端点"""
85
  if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
86
  await fetch_github_trending()
87
  return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}
88
 
89
  @app.get("/trending-sse")
90
  async def get_trending_sse():
91
- """原有SSE端点"""
92
  return StreamingResponse(
93
  trending_generator(),
94
  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 starlette.routing import Mount
8
  from fastmcp.server import FastMCP
9
  from contextlib import asynccontextmanager
10
 
11
  @asynccontextmanager
12
  async def lifespan(app: FastAPI):
13
+ await fetch_github_trending()
14
  yield
15
 
16
  app = FastAPI(lifespan=lifespan)
17
 
 
18
  cached_trending = []
19
  last_updated = None
20
 
 
40
  response.raise_for_status()
41
  items = response.json().get("items", [])
42
 
 
43
  trending_projects = []
44
  for item in items:
45
  trending_projects.append({
 
57
  print(f"Error fetching GitHub trending: {e}")
58
  return cached_trending if cached_trending else []
59
 
60
+ #mcp_server = FastMCP(name="GithubTrending", stateless_http=True)
61
+ mcp_server = FastMCP(name="GithubTrending")
62
 
63
  @mcp_server.tool()
64
  def get_trending_repos(num: int = 10) -> dict:
 
67
  fetch_github_trending()
68
  return {"trending": cached_trending[:num]}
69
 
70
+ #app.mount("/mcp", mcp_server.streamable_http_app())
71
+ app.mount("/mcp", mcp_server.http_app())
72
 
73
  async def trending_generator():
 
74
  while True:
75
  if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
76
  await fetch_github_trending()
 
79
 
80
  @app.get("/trending")
81
  async def get_trending():
 
82
  if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
83
  await fetch_github_trending()
84
  return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}
85
 
86
  @app.get("/trending-sse")
87
  async def get_trending_sse():
 
88
  return StreamingResponse(
89
  trending_generator(),
90
  media_type="text/event-stream",