Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,10 @@ from pydantic import AnyHttpUrl
|
|
| 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 |
|
| 12 |
|
| 13 |
class SimpleTokenVerifier(TokenVerifier):
|
|
@@ -25,7 +29,7 @@ mcp = FastMCP(
|
|
| 25 |
# Auth settings for RFC 9728 Protected Resource Metadata
|
| 26 |
auth=AuthSettings(
|
| 27 |
issuer_url=AnyHttpUrl("https://huggingface.co/.well-known/oauth-authorization-server"), # Authorization Server URL
|
| 28 |
-
resource_server_url=AnyHttpUrl("https://freddyaboulton-fastmcp-oauth.hf.space"), # This server's URL
|
| 29 |
required_scopes=["user"],
|
| 30 |
),
|
| 31 |
port=7860
|
|
@@ -43,5 +47,26 @@ async def get_weather(city: str = "London") -> dict[str, str]:
|
|
| 43 |
}
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
|
|
|
|
|
|
| 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):
|
|
|
|
| 29 |
# Auth settings for RFC 9728 Protected Resource Metadata
|
| 30 |
auth=AuthSettings(
|
| 31 |
issuer_url=AnyHttpUrl("https://huggingface.co/.well-known/oauth-authorization-server"), # Authorization Server URL
|
| 32 |
+
resource_server_url=AnyHttpUrl("https://freddyaboulton-fastmcp-oauth.hf.space/"), # This server's URL
|
| 33 |
required_scopes=["user"],
|
| 34 |
),
|
| 35 |
port=7860
|
|
|
|
| 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)
|