status.go 993 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package nginx
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx"
  6. "github.com/gin-gonic/gin"
  7. "github.com/mark3labs/mcp-go/mcp"
  8. )
  9. const nginxStatusToolName = "nginx_status"
  10. // statusResource is the status of the Nginx server
  11. var statusTool = mcp.NewTool(
  12. nginxStatusToolName,
  13. mcp.WithDescription("This is the status of the Nginx server"),
  14. )
  15. // handleNginxStatus handles the Nginx status request
  16. func handleNginxStatus(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
  17. lastOutput, err := nginx.GetLastOutput()
  18. if err != nil {
  19. return mcp.NewToolResultError(lastOutput + "\n" + err.Error()), err
  20. }
  21. running := nginx.IsNginxRunning()
  22. level := nginx.GetLogLevel(lastOutput)
  23. // build result
  24. result := gin.H{
  25. "running": running,
  26. "message": lastOutput,
  27. "level": level,
  28. }
  29. // marshal to json and return text result
  30. jsonResult, _ := json.Marshal(result)
  31. return mcp.NewToolResultText(string(jsonResult)), nil
  32. }