Andrew Gibbs commited on
Commit
0e1faf9
·
1 Parent(s): df86e24

Fix route shadowing: insert server-card route at position 0 so it's checked before Gradio's SPA catch-all

Browse files
Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -324,6 +324,7 @@ _SERVER_CARD = {
324
  }
325
 
326
  if __name__ == "__main__":
 
327
  from fastapi.responses import JSONResponse
328
 
329
  # Let Gradio create and bind its own server first (the same mechanism
@@ -334,8 +335,16 @@ if __name__ == "__main__":
334
  # Space's actual runtime error logs, not guessed.
335
  demo.launch(mcp_server=True, prevent_thread_lock=True)
336
 
337
- @demo.app.get("/.well-known/mcp/server-card.json")
338
- async def server_card():
 
 
 
 
 
 
339
  return JSONResponse(_SERVER_CARD)
340
 
 
 
341
  demo.block_thread()
 
324
  }
325
 
326
  if __name__ == "__main__":
327
+ from starlette.routing import Route
328
  from fastapi.responses import JSONResponse
329
 
330
  # Let Gradio create and bind its own server first (the same mechanism
 
335
  # Space's actual runtime error logs, not guessed.
336
  demo.launch(mcp_server=True, prevent_thread_lock=True)
337
 
338
+ # Starlette matches routes in registration order, not by specificity —
339
+ # Gradio's own SPA catch-all route (registered during launch()) was
340
+ # shadowing a route added the normal way (@demo.app.get(...) appends to
341
+ # the END of the routing table), so every request for our path was
342
+ # silently served Gradio's frontend HTML instead. Confirmed by fetching
343
+ # the live response body directly, not assumed. Fix: insert this route
344
+ # at position 0 so it's checked before Gradio's catch-all.
345
+ async def server_card(request):
346
  return JSONResponse(_SERVER_CARD)
347
 
348
+ demo.app.router.routes.insert(0, Route("/.well-known/mcp/server-card.json", server_card, methods=["GET"]))
349
+
350
  demo.block_thread()