ChrisSacrumCor commited on
Commit
77099e5
·
verified ·
1 Parent(s): 04ce6ae

more debug

Browse files
Files changed (1) hide show
  1. app.py +34 -14
app.py CHANGED
@@ -1064,6 +1064,7 @@ class CleanTerraformMCPServer:
1064
 
1065
  @self.server.list_tools()
1066
  async def handle_list_tools() -> list[types.Tool]:
 
1067
  """List all available Terraform tools"""
1068
  return [
1069
  types.Tool(
@@ -1233,6 +1234,7 @@ class CleanTerraformMCPServer:
1233
 
1234
  @self.server.call_tool()
1235
  async def handle_call_tool(name: str, arguments: dict) -> list[types.TextContent]:
 
1236
  """Handle tool calls"""
1237
  try:
1238
  if name == "generate_terraform_config":
@@ -1768,29 +1770,47 @@ async def main():
1768
  try:
1769
  async with stdio_server() as (read_stream, write_stream):
1770
  logger.debug("stdio_server context entered successfully. Attempting to run MCP server...")
1771
- await terraform_server.server.run(
1772
- read_stream,
1773
- write_stream,
1774
- InitializationOptions(
1775
- server_name="terraform-mcp-server",
1776
- server_version="1.0.0",
1777
- capabilities=terraform_server.server.get_capabilities(
1778
- notification_options=NotificationOptions(),
1779
- experimental_capabilities={}
 
 
1780
  )
1781
  )
1782
  )
1783
- logger.debug("MCP server.run() completed (should not happen in normal operation unless server exits).")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1784
  except Exception as e:
1785
  logger.critical(f"Unhandled exception during stdio_server or server.run: {e}", exc_info=True)
1786
- # Re-raise or exit with a non-zero code if you want to explicitly signal failure
1787
- raise # Re-raise to ensure HF Spaces sees the error
1788
 
1789
  if __name__ == "__main__":
1790
  try:
1791
  logger.debug("Script execution started via __main__.")
1792
  asyncio.run(main())
1793
- logger.debug("Script finished execution (normal exit).")
 
 
1794
  except Exception as e:
1795
  logger.critical(f"Fatal unhandled exception in main execution block: {e}", exc_info=True)
1796
- # It's possible the exception here is caught by HF Spaces environment directly
 
1064
 
1065
  @self.server.list_tools()
1066
  async def handle_list_tools() -> list[types.Tool]:
1067
+ logger.debug("Handling ListToolsRequest") # Add debug
1068
  """List all available Terraform tools"""
1069
  return [
1070
  types.Tool(
 
1234
 
1235
  @self.server.call_tool()
1236
  async def handle_call_tool(name: str, arguments: dict) -> list[types.TextContent]:
1237
+ logger.debug("Handling CallToolRequest for tool: {name} ") # Add debug here
1238
  """Handle tool calls"""
1239
  try:
1240
  if name == "generate_terraform_config":
 
1770
  try:
1771
  async with stdio_server() as (read_stream, write_stream):
1772
  logger.debug("stdio_server context entered successfully. Attempting to run MCP server...")
1773
+ server_task = asyncio.create_task(
1774
+ terraform_server.server.run(
1775
+ read_stream,
1776
+ write_stream,
1777
+ InitializationOptions(
1778
+ server_name="terraform-mcp-server",
1779
+ server_version="1.0.0",
1780
+ capabilities=terraform_server.server.get_capabilities(
1781
+ notification_options=NotificationOptions(),
1782
+ experimental_capabilities={}
1783
+ )
1784
  )
1785
  )
1786
  )
1787
+ # This is the crucial part: keep the main event loop running.
1788
+ # The server_task is now running in the background.
1789
+ # We need to explicitly await it or another task that keeps the event loop alive.
1790
+ # asyncio.Future() creates a Future that never completes, effectively blocking main.
1791
+ # Or you can await the server_task if it's meant to run indefinitely.
1792
+ # Given your log "MCP server.run() completed", it suggests it exits.
1793
+ # So, we need something *after* it that blocks.
1794
+
1795
+ # Option 1: Await the server task, assuming it will block indefinitely (but your logs contradict this)
1796
+ # await server_task
1797
+
1798
+ # Option 2: Keep the event loop running indefinitely if server_task completes
1799
+ logger.debug("MCP server task created. Keeping event loop alive...")
1800
+ await asyncio.Future() # This creates a future that never resolves, keeping the event loop running
1801
+
1802
+ logger.debug("This line should theoretically not be reached if asyncio.Future() keeps the loop alive.")
1803
+
1804
  except Exception as e:
1805
  logger.critical(f"Unhandled exception during stdio_server or server.run: {e}", exc_info=True)
1806
+ raise
 
1807
 
1808
  if __name__ == "__main__":
1809
  try:
1810
  logger.debug("Script execution started via __main__.")
1811
  asyncio.run(main())
1812
+ logger.debug("Script finished execution (normal exit).") # This might not be reached with asyncio.Future()
1813
+ except asyncio.CancelledError:
1814
+ logger.info("Application received cancellation signal. Exiting gracefully.")
1815
  except Exception as e:
1816
  logger.critical(f"Fatal unhandled exception in main execution block: {e}", exc_info=True)