config_modify.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.GetArguments()
  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. Name: filepath.Base(absPath),
  52. })).Where(q.Filepath.Eq(absPath)).FirstOrCreate()
  53. if err != nil {
  54. return nil, err
  55. }
  56. // Update database record
  57. _, err = q.Where(q.Filepath.Eq(absPath)).
  58. Select(q.SyncNodeIds, q.SyncOverwrite).
  59. Updates(&model.Config{
  60. SyncNodeIds: syncNodeIds,
  61. SyncOverwrite: syncOverwrite,
  62. })
  63. if err != nil {
  64. return nil, err
  65. }
  66. cfg.SyncNodeIds = syncNodeIds
  67. cfg.SyncOverwrite = syncOverwrite
  68. err = config.Save(absPath, content, cfg)
  69. if err != nil {
  70. return nil, err
  71. }
  72. result := map[string]interface{}{
  73. "name": filepath.Base(absPath),
  74. "content": content,
  75. "file_path": absPath,
  76. "dir": filepath.Dir(relativePath),
  77. "sync_node_ids": cfg.SyncNodeIds,
  78. "sync_overwrite": cfg.SyncOverwrite,
  79. }
  80. jsonResult, _ := json.Marshal(result)
  81. return mcp.NewToolResultText(string(jsonResult)), nil
  82. }