config_modify.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package config
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "path/filepath"
  7. "github.com/0xJacky/Nginx-UI/internal/config"
  8. "github.com/0xJacky/Nginx-UI/internal/helper"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/model"
  11. "github.com/0xJacky/Nginx-UI/query"
  12. "github.com/mark3labs/mcp-go/mcp"
  13. "gorm.io/gen/field"
  14. )
  15. const nginxConfigModifyToolName = "nginx_config_modify"
  16. // ErrFileNotFound is returned when a file is not found
  17. var ErrFileNotFound = errors.New("file not found")
  18. var nginxConfigModifyTool = mcp.NewTool(
  19. nginxConfigModifyToolName,
  20. mcp.WithDescription("Modify an existing Nginx configuration file"),
  21. mcp.WithString("relative_path", mcp.Description("The relative path to the configuration file")),
  22. mcp.WithString("content", mcp.Description("The new content of the configuration file")),
  23. mcp.WithBoolean("sync_overwrite", mcp.Description("Whether to overwrite existing files when syncing")),
  24. mcp.WithArray("sync_node_ids", mcp.Description("IDs of nodes to sync the configuration to")),
  25. )
  26. func handleNginxConfigModify(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
  27. args := request.Params.Arguments
  28. relativePath := args["relative_path"].(string)
  29. content := args["content"].(string)
  30. syncOverwrite := args["sync_overwrite"].(bool)
  31. // Convert sync_node_ids from []interface{} to []uint64
  32. syncNodeIdsInterface, ok := args["sync_node_ids"].([]interface{})
  33. syncNodeIds := make([]uint64, 0)
  34. if ok {
  35. for _, id := range syncNodeIdsInterface {
  36. if idFloat, ok := id.(float64); ok {
  37. syncNodeIds = append(syncNodeIds, uint64(idFloat))
  38. }
  39. }
  40. }
  41. absPath := nginx.GetConfPath(relativePath)
  42. if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) {
  43. return nil, config.ErrPathIsNotUnderTheNginxConfDir
  44. }
  45. if !helper.FileExists(absPath) {
  46. return nil, ErrFileNotFound
  47. }
  48. q := query.Config
  49. cfg, err := q.Assign(field.Attrs(&model.Config{
  50. Filepath: absPath,
  51. })).Where(q.Filepath.Eq(absPath)).FirstOrCreate()
  52. if err != nil {
  53. return nil, err
  54. }
  55. // Update database record
  56. _, err = q.Where(q.Filepath.Eq(absPath)).
  57. Select(q.SyncNodeIds, q.SyncOverwrite).
  58. Updates(&model.Config{
  59. SyncNodeIds: syncNodeIds,
  60. SyncOverwrite: syncOverwrite,
  61. })
  62. if err != nil {
  63. return nil, err
  64. }
  65. cfg.SyncNodeIds = syncNodeIds
  66. cfg.SyncOverwrite = syncOverwrite
  67. err = config.Save(absPath, content, cfg)
  68. if err != nil {
  69. return nil, err
  70. }
  71. result := map[string]interface{}{
  72. "name": filepath.Base(absPath),
  73. "content": content,
  74. "file_path": absPath,
  75. "dir": filepath.Dir(relativePath),
  76. "sync_node_ids": cfg.SyncNodeIds,
  77. "sync_overwrite": cfg.SyncOverwrite,
  78. }
  79. jsonResult, _ := json.Marshal(result)
  80. return mcp.NewToolResultText(string(jsonResult)), nil
  81. }