Spaces:
Sleeping
Sleeping
Commit ·
ad25b37
1
Parent(s): 769cc9d
feat: Introduce `MCPClient` for multi-server communication and integrate with FastAPI lifespan events.
Browse files- app.py +14 -11
- src/credentialwatch_agent/mcp_client.py +3 -3
app.py
CHANGED
|
@@ -10,22 +10,25 @@ sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
|
|
| 10 |
|
| 11 |
from credentialwatch_agent.main import demo, mcp_client
|
| 12 |
|
| 13 |
-
|
| 14 |
-
app = FastAPI()
|
| 15 |
|
| 16 |
-
@
|
| 17 |
-
async def
|
| 18 |
-
"""
|
| 19 |
print("Connecting to MCP servers...")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
print("Closing MCP connections...")
|
| 27 |
await mcp_client.close()
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
# Mount Gradio app
|
| 30 |
# path="/" mounts it at the root
|
| 31 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 10 |
|
| 11 |
from credentialwatch_agent.main import demo, mcp_client
|
| 12 |
|
| 13 |
+
from contextlib import asynccontextmanager
|
|
|
|
| 14 |
|
| 15 |
+
@asynccontextmanager
|
| 16 |
+
async def lifespan(app: FastAPI):
|
| 17 |
+
"""Manage application lifespan."""
|
| 18 |
print("Connecting to MCP servers...")
|
| 19 |
+
try:
|
| 20 |
+
await mcp_client.connect()
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error connecting to MCP servers: {e}")
|
| 23 |
+
|
| 24 |
+
yield
|
| 25 |
+
|
| 26 |
print("Closing MCP connections...")
|
| 27 |
await mcp_client.close()
|
| 28 |
|
| 29 |
+
# Create FastAPI app with lifespan
|
| 30 |
+
app = FastAPI(lifespan=lifespan)
|
| 31 |
+
|
| 32 |
# Mount Gradio app
|
| 33 |
# path="/" mounts it at the root
|
| 34 |
app = gr.mount_gradio_app(app, demo, path="/")
|
src/credentialwatch_agent/mcp_client.py
CHANGED
|
@@ -36,7 +36,7 @@ class MCPClient:
|
|
| 36 |
)
|
| 37 |
await self._sessions["npi"].initialize()
|
| 38 |
except Exception as e:
|
| 39 |
-
print(f"Failed to connect to NPI MCP at {self.npi_url}: {e}")
|
| 40 |
|
| 41 |
# Connect to Cred DB MCP
|
| 42 |
try:
|
|
@@ -46,7 +46,7 @@ class MCPClient:
|
|
| 46 |
)
|
| 47 |
await self._sessions["cred_db"].initialize()
|
| 48 |
except Exception as e:
|
| 49 |
-
print(f"Failed to connect to Cred DB MCP at {self.cred_db_url}: {e}")
|
| 50 |
|
| 51 |
# Connect to Alert MCP
|
| 52 |
try:
|
|
@@ -56,7 +56,7 @@ class MCPClient:
|
|
| 56 |
)
|
| 57 |
await self._sessions["alert"].initialize()
|
| 58 |
except Exception as e:
|
| 59 |
-
print(f"Failed to connect to Alert MCP at {self.alert_url}: {e}")
|
| 60 |
|
| 61 |
async def close(self):
|
| 62 |
"""Closes all connections."""
|
|
|
|
| 36 |
)
|
| 37 |
await self._sessions["npi"].initialize()
|
| 38 |
except Exception as e:
|
| 39 |
+
print(f"Warning: Failed to connect to NPI MCP at {self.npi_url}. Using mock data. Error: {e}")
|
| 40 |
|
| 41 |
# Connect to Cred DB MCP
|
| 42 |
try:
|
|
|
|
| 46 |
)
|
| 47 |
await self._sessions["cred_db"].initialize()
|
| 48 |
except Exception as e:
|
| 49 |
+
print(f"Warning: Failed to connect to Cred DB MCP at {self.cred_db_url}. Using mock data. Error: {e}")
|
| 50 |
|
| 51 |
# Connect to Alert MCP
|
| 52 |
try:
|
|
|
|
| 56 |
)
|
| 57 |
await self._sessions["alert"].initialize()
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f"Warning: Failed to connect to Alert MCP at {self.alert_url}. Using mock data. Error: {e}")
|
| 60 |
|
| 61 |
async def close(self):
|
| 62 |
"""Closes all connections."""
|