davanstrien HF Staff commited on
Commit
784cc00
·
1 Parent(s): f289ebb

Add workaround for MCP initialization race condition

Browse files

- Use GRADIO_MCP_SERVER environment variable instead of parameter
- Add fallback to parameter approach if env var fails
- Addresses "Received request before initialization was complete" error
- Based on solutions from MCP Python SDK issues #423 and #737

Files changed (1) hide show
  1. app.py +20 -1
app.py CHANGED
@@ -518,4 +518,23 @@ with gr.Blocks(title="HuggingFace Search MCP Server") as demo:
518
 
519
  if __name__ == "__main__":
520
  # Launch with MCP server enabled
521
- demo.launch(mcp_server=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
 
519
  if __name__ == "__main__":
520
  # Launch with MCP server enabled
521
+ # Try environment variable approach as workaround for initialization race condition
522
+ import os
523
+ os.environ["GRADIO_MCP_SERVER"] = "true"
524
+
525
+ try:
526
+ # First try with environment variable only
527
+ demo.launch(
528
+ server_name="0.0.0.0",
529
+ server_port=7860,
530
+ show_error=True
531
+ )
532
+ except Exception as e:
533
+ logger.error(f"Environment variable approach failed: {e}")
534
+ # Fallback to parameter approach
535
+ demo.launch(
536
+ mcp_server=True,
537
+ server_name="0.0.0.0",
538
+ server_port=7860,
539
+ show_error=True
540
+ )