Humanlearning commited on
Commit
4f4a9eb
·
1 Parent(s): 06e607f

feat: Implement CredentialWatch agent with Gradio UI and MCP client for expiry sweep and interactive queries.

Browse files
src/credentialwatch_agent/main.py CHANGED
@@ -107,14 +107,7 @@ with gr.Blocks(title="CredentialWatch") as demo:
107
  # or we can wrap the demo launch.
108
 
109
  if __name__ == "__main__":
110
- # Simple async wrapper to run the app
111
- loop = asyncio.new_event_loop()
112
- asyncio.set_event_loop(loop)
113
-
114
- try:
115
- loop.run_until_complete(mcp_client.connect())
116
- demo.launch(server_name="0.0.0.0", server_port=7860, mcp_server=True)
117
- except KeyboardInterrupt:
118
- pass
119
- finally:
120
- loop.run_until_complete(mcp_client.close())
 
107
  # or we can wrap the demo launch.
108
 
109
  if __name__ == "__main__":
110
+ # Launch the demo.
111
+ # Note: We rely on lazy connection in run_chat_turn/run_expiry_sweep to connect mcp_client.
112
+ # This avoids creating a conflicting event loop before Gradio starts.
113
+ demo.launch(server_name="0.0.0.0", server_port=7860, mcp_server=True)
 
 
 
 
 
 
 
src/credentialwatch_agent/mcp_client.py CHANGED
@@ -29,6 +29,19 @@ class MCPClient:
29
  if self._connected:
30
  return
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Connect to NPI MCP
33
  try:
34
  # Note: mcp.client.sse.sse_client is a context manager that yields (read_stream, write_stream)
@@ -42,7 +55,7 @@ class MCPClient:
42
  import traceback
43
  print(f"Warning: Failed to connect to NPI MCP at {self.npi_url}. Using mock data.")
44
  print(f"Error details: {e}")
45
- traceback.print_exc()
46
 
47
  # Connect to Cred DB MCP
48
  try:
@@ -55,7 +68,7 @@ class MCPClient:
55
  import traceback
56
  print(f"Warning: Failed to connect to Cred DB MCP at {self.cred_db_url}. Using mock data.")
57
  print(f"Error details: {e}")
58
- traceback.print_exc()
59
 
60
  # Connect to Alert MCP
61
  try:
@@ -68,7 +81,7 @@ class MCPClient:
68
  import traceback
69
  print(f"Warning: Failed to connect to Alert MCP at {self.alert_url}. Using mock data.")
70
  print(f"Error details: {e}")
71
- traceback.print_exc()
72
 
73
  self._connected = True
74
 
 
29
  if self._connected:
30
  return
31
 
32
+ # Check if running on HF Spaces and using default localhost URLs
33
+ is_hf = os.getenv("SPACE_ID") is not None
34
+
35
+ # Helper to check if URL is localhost
36
+ def is_localhost(url):
37
+ return "localhost" in url or "127.0.0.1" in url
38
+
39
+ if is_hf and (is_localhost(self.npi_url) or is_localhost(self.cred_db_url) or is_localhost(self.alert_url)):
40
+ print("Detected Hugging Face Spaces environment with localhost URLs.")
41
+ print("Skipping actual MCP connections and defaulting to mock data.")
42
+ self._connected = True
43
+ return
44
+
45
  # Connect to NPI MCP
46
  try:
47
  # Note: mcp.client.sse.sse_client is a context manager that yields (read_stream, write_stream)
 
55
  import traceback
56
  print(f"Warning: Failed to connect to NPI MCP at {self.npi_url}. Using mock data.")
57
  print(f"Error details: {e}")
58
+ # traceback.print_exc() # Reduce noise
59
 
60
  # Connect to Cred DB MCP
61
  try:
 
68
  import traceback
69
  print(f"Warning: Failed to connect to Cred DB MCP at {self.cred_db_url}. Using mock data.")
70
  print(f"Error details: {e}")
71
+ # traceback.print_exc()
72
 
73
  # Connect to Alert MCP
74
  try:
 
81
  import traceback
82
  print(f"Warning: Failed to connect to Alert MCP at {self.alert_url}. Using mock data.")
83
  print(f"Error details: {e}")
84
+ # traceback.print_exc()
85
 
86
  self._connected = True
87