config_mkdir.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package config
  2. import (
  3. "context"
  4. "encoding/json"
  5. "os"
  6. "github.com/0xJacky/Nginx-UI/internal/config"
  7. "github.com/0xJacky/Nginx-UI/internal/helper"
  8. "github.com/0xJacky/Nginx-UI/internal/nginx"
  9. "github.com/mark3labs/mcp-go/mcp"
  10. )
  11. const nginxConfigMkdirToolName = "nginx_config_mkdir"
  12. var nginxConfigMkdirTool = mcp.NewTool(
  13. nginxConfigMkdirToolName,
  14. mcp.WithDescription("Create a new directory in the Nginx configuration path"),
  15. mcp.WithString("base_path", mcp.Description("The base path where to create the directory")),
  16. mcp.WithString("folder_name", mcp.Description("The name of the folder to create")),
  17. )
  18. func handleNginxConfigMkdir(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
  19. args := request.Params.Arguments
  20. basePath := args["base_path"].(string)
  21. folderName := args["folder_name"].(string)
  22. fullPath := nginx.GetConfPath(basePath, folderName)
  23. if !helper.IsUnderDirectory(fullPath, nginx.GetConfPath()) {
  24. return nil, config.ErrPathIsNotUnderTheNginxConfDir
  25. }
  26. err := os.Mkdir(fullPath, 0755)
  27. if err != nil {
  28. return nil, err
  29. }
  30. result := map[string]interface{}{
  31. "message": "Directory created successfully",
  32. "path": fullPath,
  33. }
  34. jsonResult, _ := json.Marshal(result)
  35. return mcp.NewToolResultText(string(jsonResult)), nil
  36. }