freddyaboulton HF Staff commited on
Commit
80a2cf1
·
verified ·
1 Parent(s): b93b962

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -22
app.py CHANGED
@@ -1,17 +1,11 @@
1
- """
2
- Run from the repository root:
3
- uv run examples/snippets/servers/oauth_server.py
4
- """
5
-
6
  from pydantic import AnyHttpUrl
7
 
8
  from mcp.server.auth.provider import AccessToken, TokenVerifier
9
  from mcp.server.auth.settings import AuthSettings
10
  from mcp.server.fastmcp import FastMCP
11
- import contextlib
12
- from starlette.applications import Starlette
13
- from starlette.routing import Mount, Route
14
  from starlette.responses import JSONResponse
 
15
 
16
 
17
  class SimpleTokenVerifier(TokenVerifier):
@@ -46,27 +40,21 @@ async def get_weather(city: str = "London") -> dict[str, str]:
46
  "humidity": "65%",
47
  }
48
 
49
-
50
- # Create a combined lifespan to manage both session managers
51
  @contextlib.asynccontextmanager
52
- async def lifespan(app: Starlette):
53
  async with contextlib.AsyncExitStack() as stack:
54
  await stack.enter_async_context(mcp.session_manager.run())
55
  yield
56
 
57
 
58
- async def homepage(request):
59
- return JSONResponse({'hello': 'world'})
 
 
 
 
60
 
61
- # Create the Starlette app and mount the MCP servers
62
- app = Starlette(
63
- routes=[
64
- Mount("/sub", mcp.streamable_http_app()),
65
- Route("/", homepage),
66
- ],
67
- lifespan=lifespan,
68
- )
69
 
70
  if __name__ == "__main__":
71
  import uvicorn
72
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
1
  from pydantic import AnyHttpUrl
2
 
3
  from mcp.server.auth.provider import AccessToken, TokenVerifier
4
  from mcp.server.auth.settings import AuthSettings
5
  from mcp.server.fastmcp import FastMCP
6
+ from fastapi import FastAPI
 
 
7
  from starlette.responses import JSONResponse
8
+ import contextlib
9
 
10
 
11
  class SimpleTokenVerifier(TokenVerifier):
 
40
  "humidity": "65%",
41
  }
42
 
 
 
43
  @contextlib.asynccontextmanager
44
+ async def lifespan(app: FastAPI):
45
  async with contextlib.AsyncExitStack() as stack:
46
  await stack.enter_async_context(mcp.session_manager.run())
47
  yield
48
 
49
 
50
+ app = FastAPI(lifespan=lifespan)
51
+ app.mount("/weather_mcp", mcp.streamable_http_app())
52
+
53
+ @app.get("/")
54
+ async def _():
55
+ return JSONResponse({"message": "Welcome to the Weather Service!"})
56
 
 
 
 
 
 
 
 
 
57
 
58
  if __name__ == "__main__":
59
  import uvicorn
60
+ uvicorn.run(app, host="0.0.0.0", port=7860)