alan5543 commited on
Commit ·
bcfd645
1
Parent(s): 2505ad5
update
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
import sys
|
| 6 |
import asyncio
|
| 7 |
import logging
|
|
|
|
| 8 |
|
| 9 |
# Configure basic logging to see server activity
|
| 10 |
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
|
@@ -16,11 +17,15 @@ logger = logging.getLogger(__name__)
|
|
| 16 |
HOST_PORT = int(os.getenv("PORT", 7860))
|
| 17 |
HOST_ADDRESS = os.getenv("HOST", "0.0.0.0") # Bind to all interfaces
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
mcp = FastMCP(
|
| 21 |
name="GradioSpacesTools",
|
| 22 |
-
host=HOST_ADDRESS,
|
| 23 |
-
port=HOST_PORT
|
| 24 |
)
|
| 25 |
|
| 26 |
clients = {}
|
|
@@ -94,17 +99,18 @@ async def run_dia_tts(prompt: str, space_id: str = "ysharma/Dia-1.6B") -> str:
|
|
| 94 |
logger.error(f"Error performing TTS: {e}")
|
| 95 |
return f"Error performing TTS: {e}"
|
| 96 |
|
| 97 |
-
async def main():
|
| 98 |
-
logger.info(f"Attempting to start GradioSpacesTools MCP server on http://{HOST_ADDRESS}:{HOST_PORT}")
|
| 99 |
-
try:
|
| 100 |
-
# CORRECT FIX: Use 'streamable-http' transport AND mcp.run_async()
|
| 101 |
-
# This function runs within an already existing asyncio loop (started by asyncio.run(main()))
|
| 102 |
-
await mcp.run_async(transport='streamable-http')
|
| 103 |
-
except Exception as e:
|
| 104 |
-
logger.exception(f"Failed to start MCP server: {e}")
|
| 105 |
-
logger.info("GradioSpacesTools MCP server stopped.")
|
| 106 |
-
|
| 107 |
if __name__ == "__main__":
|
| 108 |
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import sys
|
| 6 |
import asyncio
|
| 7 |
import logging
|
| 8 |
+
import uvicorn # Correctly imported
|
| 9 |
|
| 10 |
# Configure basic logging to see server activity
|
| 11 |
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
|
|
|
| 17 |
HOST_PORT = int(os.getenv("PORT", 7860))
|
| 18 |
HOST_ADDRESS = os.getenv("HOST", "0.0.0.0") # Bind to all interfaces
|
| 19 |
|
| 20 |
+
# Initialize FastMCP. The host/port settings passed here will be used by
|
| 21 |
+
# mcp.streamable_http_app() to configure its internal server if not mounted to FastAPI.
|
| 22 |
+
# However, when passed to uvicorn.run(), uvicorn's host/port will take precedence.
|
| 23 |
+
# It's good practice to keep them consistent or omit from here if uvicorn dictates.
|
| 24 |
+
# For simplicity, we'll keep them here as they are also part of MCP server's self-description.
|
| 25 |
mcp = FastMCP(
|
| 26 |
name="GradioSpacesTools",
|
| 27 |
+
host=HOST_ADDRESS,
|
| 28 |
+
port=HOST_PORT
|
| 29 |
)
|
| 30 |
|
| 31 |
clients = {}
|
|
|
|
| 99 |
logger.error(f"Error performing TTS: {e}")
|
| 100 |
return f"Error performing TTS: {e}"
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if __name__ == "__main__":
|
| 103 |
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)
|
| 104 |
+
logger.info(f"Starting GradioSpacesTools MCP server via Uvicorn on http://{HOST_ADDRESS}:{HOST_PORT}")
|
| 105 |
+
|
| 106 |
+
# The correct way to get the FastMCP ASGI application for streamable-http
|
| 107 |
+
# as indicated by the article and FastMCP's internal structure.
|
| 108 |
+
# Uvicorn will then run this ASGI application.
|
| 109 |
+
uvicorn.run(
|
| 110 |
+
mcp.streamable_http_app(), # <-- Use this method! It returns the ASGI app.
|
| 111 |
+
host=HOST_ADDRESS,
|
| 112 |
+
port=HOST_PORT,
|
| 113 |
+
log_level="info", # Set logging level for Uvicorn itself
|
| 114 |
+
loop="asyncio" # Ensure Uvicorn uses the asyncio event loop
|
| 115 |
+
)
|
| 116 |
+
logger.info("GradioSpacesTools MCP server stopped.")
|