Spaces:
Sleeping
Sleeping
Commit Β·
b5e0c5c
1
Parent(s): b5fe8c8
Fix: Add proxy headers and host binding for FastMCP
Browse files- Dockerfile +1 -1
- app.py +26 -3
- preflight.py +45 -0
Dockerfile
CHANGED
|
@@ -11,4 +11,4 @@ COPY --chown=user ./requirements.txt requirements.txt
|
|
| 11 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 12 |
|
| 13 |
COPY --chown=user . /app
|
| 14 |
-
CMD ["
|
|
|
|
| 11 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 12 |
|
| 13 |
COPY --chown=user . /app
|
| 14 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -34,6 +34,8 @@ BRIDGE_NAME = os.getenv("FEDERATION_BRIDGE_NAME", "federation-bridge")
|
|
| 34 |
|
| 35 |
mcp = FastMCP(
|
| 36 |
name=BRIDGE_NAME,
|
|
|
|
|
|
|
| 37 |
instructions=(
|
| 38 |
"You are connected to the Pantheon Federation Writing Desk. "
|
| 39 |
"This bridge gives you tools to communicate with the Federation Node β "
|
|
@@ -242,10 +244,31 @@ async def federation_verify_seal(input_data: VerifySealInput) -> str:
|
|
| 242 |
# ENTRYPOINT
|
| 243 |
# βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 244 |
|
| 245 |
-
# Make the ASGI app available globally so Docker's CMD ["uvicorn", "app:app"] works directly.
|
| 246 |
app = mcp.sse_app()
|
| 247 |
|
| 248 |
if __name__ == "__main__":
|
| 249 |
import uvicorn
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
mcp = FastMCP(
|
| 36 |
name=BRIDGE_NAME,
|
| 37 |
+
host="0.0.0.0",
|
| 38 |
+
port=7860,
|
| 39 |
instructions=(
|
| 40 |
"You are connected to the Pantheon Federation Writing Desk. "
|
| 41 |
"This bridge gives you tools to communicate with the Federation Node β "
|
|
|
|
| 244 |
# ENTRYPOINT
|
| 245 |
# βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 246 |
|
|
|
|
| 247 |
app = mcp.sse_app()
|
| 248 |
|
| 249 |
if __name__ == "__main__":
|
| 250 |
import uvicorn
|
| 251 |
+
from starlette.requests import Request as StarletteRequest
|
| 252 |
+
from starlette.responses import JSONResponse
|
| 253 |
+
|
| 254 |
+
# Add root health endpoint β FastMCP only mounts /sse + /messages by default
|
| 255 |
+
async def health(request: StarletteRequest):
|
| 256 |
+
return JSONResponse({
|
| 257 |
+
"service": "Federation MCP Bridge (The Writing Desk)",
|
| 258 |
+
"status": "ONLINE",
|
| 259 |
+
"mcp_version": "1.0",
|
| 260 |
+
"bridge_target_node": FEDERATION_NODE_URL,
|
| 261 |
+
"sse_endpoint": "/sse"
|
| 262 |
+
})
|
| 263 |
+
|
| 264 |
+
app.add_route("/", health)
|
| 265 |
+
app.add_route("/health", health)
|
| 266 |
+
|
| 267 |
+
# Standard uvicorn run with proxy headers enabled for Hugging Face Spaces
|
| 268 |
+
uvicorn.run(
|
| 269 |
+
app,
|
| 270 |
+
host="0.0.0.0",
|
| 271 |
+
port=7860,
|
| 272 |
+
proxy_headers=True, # Trust X-Forwarded-* from HF proxy
|
| 273 |
+
forwarded_allow_ips="*" # HF proxy IP is not fixed β allow all
|
| 274 |
+
)
|
preflight.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import httpx
|
| 3 |
+
|
| 4 |
+
async def check():
|
| 5 |
+
print("="*50)
|
| 6 |
+
print("LIVE ENVIRONMENT PREFLIGHT CHECK")
|
| 7 |
+
print("="*50)
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
print("\nChecking Node (kode-animator-federation-node.hf.space)...")
|
| 11 |
+
async with httpx.AsyncClient(timeout=10.0) as c:
|
| 12 |
+
r = await c.get("https://kode-animator-federation-node.hf.space/")
|
| 13 |
+
r.raise_for_status()
|
| 14 |
+
data = r.json()
|
| 15 |
+
print("π’ Node is UP")
|
| 16 |
+
print(f" Identity: {data.get('identity')}")
|
| 17 |
+
print(f" Status: {data.get('status')}")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"π΄ Node check failed: {e}")
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
print("\nChecking Bridge (kode-animator-federation-bridge.hf.space)...")
|
| 23 |
+
async with httpx.AsyncClient(timeout=10.0) as c:
|
| 24 |
+
r = await c.get("https://kode-animator-federation-bridge.hf.space/")
|
| 25 |
+
r.raise_for_status()
|
| 26 |
+
data = r.json()
|
| 27 |
+
print("π’ Bridge is UP")
|
| 28 |
+
print(f" Service: {data.get('service')}")
|
| 29 |
+
print(f" Target Node config: {data.get('bridge_target_node')}")
|
| 30 |
+
|
| 31 |
+
print("\nChecking Bridge /sse endpoint...")
|
| 32 |
+
async with httpx.AsyncClient(timeout=10.0) as c:
|
| 33 |
+
# We just do a HEAD or GET expecting an SSE stream or 422/Method Not Allowed/etc
|
| 34 |
+
r = await c.get("https://kode-animator-federation-bridge.hf.space/sse")
|
| 35 |
+
# Usually SSE endpoint might wait forever, but we can check status
|
| 36 |
+
print(f"π’ Bridge /sse endpoint returned status: {r.status_code}")
|
| 37 |
+
except httpx.HTTPStatusError as e:
|
| 38 |
+
print(f"π΄ Bridge check HTTP error: {e.response.status_code}")
|
| 39 |
+
print(f" Response: {e.response.text[:200]}")
|
| 40 |
+
except httpx.ReadTimeout:
|
| 41 |
+
print("π’ Bridge /sse is waiting for connection (Timeout as expected for SSE stream)")
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"π΄ Bridge check failed: {e}")
|
| 44 |
+
|
| 45 |
+
asyncio.run(check())
|