@mgsoftwarebv/mg-dashboard-mcp 2.2.4 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -373,6 +373,835 @@ ${rawJson.substring(0, 500)}`
373
373
  }
374
374
  }
375
375
 
376
+ // src/script-tools.ts
377
+ var SCRIPTS = [
378
+ {
379
+ name: "commit-feedback",
380
+ filename: "commit-feedback.ps1",
381
+ description: "PowerShell script that provides colored terminal feedback for the Ctrl+Shift+K commit & push keybinding. Polls for a new commit and shows branch, message, and file stats when done.",
382
+ content: `param()
383
+
384
+ $initial = git log --oneline -1 2>$null
385
+
386
+ Write-Host "\`n==========================================" -ForegroundColor Cyan
387
+ Write-Host " Starting Commit & Push..." -ForegroundColor Cyan
388
+ Write-Host "==========================================" -ForegroundColor Cyan
389
+ Write-Host " [1/5] Pulling latest..." -ForegroundColor DarkGray
390
+ Write-Host " [2/5] Staging changes..." -ForegroundColor DarkGray
391
+ Write-Host " [3/5] Generating AI commit message..." -ForegroundColor DarkGray
392
+ Write-Host " [4/5] Committing..." -ForegroundColor DarkGray
393
+ Write-Host " [5/5] Pushing to remote..." -ForegroundColor DarkGray
394
+ Write-Host ""
395
+ Write-Host " Waiting for commit" -ForegroundColor DarkGray -NoNewline
396
+
397
+ $timeout = 60
398
+ $elapsed = 0
399
+ while ($elapsed -lt $timeout) {
400
+ $current = git log --oneline -1 2>$null
401
+ if ($current -ne $initial) { break }
402
+ Start-Sleep -Seconds 1
403
+ $elapsed++
404
+ Write-Host "." -ForegroundColor DarkGray -NoNewline
405
+ }
406
+ Write-Host ""
407
+
408
+ if ($current -eq $initial) {
409
+ Write-Host ""
410
+ Write-Host " [!] No new commit detected (timed out after \${timeout}s)" -ForegroundColor Yellow
411
+ Write-Host ""
412
+ exit
413
+ }
414
+
415
+ Start-Sleep -Seconds 2
416
+
417
+ $branch = git rev-parse --abbrev-ref HEAD 2>$null
418
+ $commit = git log --format='%h %s' -1 2>$null
419
+
420
+ Write-Host ""
421
+ Write-Host "==========================================" -ForegroundColor Cyan
422
+ Write-Host " [OK] Commit & Push Complete" -ForegroundColor Green
423
+ Write-Host "==========================================" -ForegroundColor Cyan
424
+ Write-Host " Branch: $branch" -ForegroundColor Yellow
425
+ Write-Host " Commit: $commit" -ForegroundColor White
426
+ Write-Host ""
427
+ git --no-pager diff HEAD~1 --stat --color=always 2>$null
428
+ Write-Host ""
429
+ Write-Host "==========================================" -ForegroundColor Cyan
430
+ Write-Host ""
431
+ `
432
+ },
433
+ {
434
+ name: "create-pr",
435
+ filename: "create-pr.cmd",
436
+ description: "Windows batch script that auto-detects the target branch based on your release pipeline (v*-changes branches), merges the target into your branch, pushes, and creates a PR via gh CLI. Bound to Ctrl+Shift+J.",
437
+ content: `@echo off
438
+ setlocal enabledelayedexpansion
439
+ title MG Dashboard - Create PR
440
+
441
+ REM --- ANSI color setup ---
442
+ for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
443
+ set "GREEN=%ESC%[32m"
444
+ set "RED=%ESC%[31m"
445
+ set "YELLOW=%ESC%[33m"
446
+ set "CYAN=%ESC%[36m"
447
+ set "DIM=%ESC%[90m"
448
+ set "BOLD=%ESC%[1m"
449
+ set "RESET=%ESC%[0m"
450
+
451
+ echo.
452
+ echo %CYAN%%BOLD%==========================================%RESET%
453
+ echo %CYAN%%BOLD% MG Dashboard - Create Pull Request%RESET%
454
+ echo %CYAN%%BOLD%==========================================%RESET%
455
+ echo.
456
+
457
+ REM --- Merge origin/main first ---
458
+ echo %CYAN%[0/4]%RESET% Merging origin/main into current branch...
459
+ git fetch --prune >nul 2>&1
460
+ git merge origin/main --no-edit >nul 2>&1
461
+ if errorlevel 1 (
462
+ git merge --abort >nul 2>&1
463
+ echo %RED%X Merge conflict with origin/main. Resolve manually.%RESET%
464
+ echo.
465
+ exit /b 1
466
+ )
467
+ echo %GREEN% OK%RESET%
468
+ echo.
469
+
470
+ REM --- Prerequisite: gh CLI ---
471
+ where gh >nul 2>&1
472
+ if errorlevel 1 (
473
+ echo %RED%X GitHub CLI ^(gh^) is not installed.%RESET%
474
+ echo %DIM% Install it from: https://cli.github.com/%RESET%
475
+ echo.
476
+ exit /b 1
477
+ )
478
+
479
+ REM --- Prerequisite: git repo ---
480
+ git rev-parse --is-inside-work-tree >nul 2>&1
481
+ if errorlevel 1 (
482
+ echo %RED%X Not inside a git repository.%RESET%
483
+ echo.
484
+ exit /b 1
485
+ )
486
+
487
+ REM --- Detect repo from git remote ---
488
+ set "REPO="
489
+ for /f "tokens=*" %%u in ('git remote get-url origin 2^>nul') do set "REMOTE_URL=%%u"
490
+ if defined REMOTE_URL (
491
+ echo !REMOTE_URL! | findstr /r "git@github.com:" >nul 2>&1
492
+ if not errorlevel 1 (
493
+ set "REPO=!REMOTE_URL:git@github.com:=!"
494
+ set "REPO=!REPO:.git=!"
495
+ )
496
+ echo !REMOTE_URL! | findstr /r "https://github.com/" >nul 2>&1
497
+ if not errorlevel 1 (
498
+ set "REPO=!REMOTE_URL:https://github.com/=!"
499
+ set "REPO=!REPO:.git=!"
500
+ )
501
+ )
502
+ if not defined REPO (
503
+ echo %RED%X Could not detect GitHub repo from git remote.%RESET%
504
+ echo %DIM% Make sure origin points to a GitHub repository.%RESET%
505
+ echo.
506
+ exit /b 1
507
+ )
508
+
509
+ REM --- Get current branch ---
510
+ for /f "tokens=*" %%b in ('git rev-parse --abbrev-ref HEAD') do set "CURRENT_BRANCH=%%b"
511
+
512
+ echo %DIM% Repository: !REPO!%RESET%
513
+ echo %DIM% Branch: %CURRENT_BRANCH%%RESET%
514
+ echo.
515
+
516
+ REM --- Guard: don't PR from main or a bare version branch ---
517
+ if "%CURRENT_BRANCH%"=="main" (
518
+ echo %RED%X You are on main. Switch to a feature or changes branch first.%RESET%
519
+ echo.
520
+ exit /b 1
521
+ )
522
+ echo %CURRENT_BRANCH% | findstr /r "^v[0-9]*\\.[0-9]*\\.[0-9]*$" >nul 2>&1
523
+ if not errorlevel 1 (
524
+ echo %RED%X You are on a version branch ^(%CURRENT_BRANCH%^). Switch to a feature or changes branch first.%RESET%
525
+ echo.
526
+ exit /b 1
527
+ )
528
+
529
+ REM --- Fetch latest remote refs ---
530
+ echo %CYAN%[1/4]%RESET% Fetching remote branches...
531
+ git fetch --prune >nul 2>&1
532
+
533
+ REM --- Determine target branch ---
534
+ set "TARGET="
535
+
536
+ REM Check if current branch is a -changes branch (last 8 chars)
537
+ if "!CURRENT_BRANCH:~-8!"=="-changes" (
538
+ set "TARGET=!CURRENT_BRANCH:~0,-8!"
539
+ echo %GREEN% Detected -changes branch. Target: %BOLD%!TARGET!%RESET%
540
+ goto :do_merge
541
+ )
542
+
543
+ REM Look for v*-changes branches on remote (highest version first)
544
+ set "TARGET="
545
+ for /f "tokens=*" %%r in ('git branch -r --sort=-version:refname --list "origin/v*-changes" 2^>nul') do (
546
+ if not defined TARGET (
547
+ set "RAW=%%r"
548
+ set "BRANCH=!RAW: origin/=!"
549
+ set "BRANCH=!BRANCH:origin/=!"
550
+ set "TARGET=!BRANCH!"
551
+ )
552
+ )
553
+
554
+ if defined TARGET (
555
+ echo %GREEN% Release pipeline detected. Target: %BOLD%!TARGET!%RESET%
556
+ ) else (
557
+ REM No -changes branch; try highest version branch
558
+ set "TARGET="
559
+ for /f "tokens=*" %%r in ('git branch -r --sort=-version:refname --list "origin/v*" 2^>nul') do (
560
+ if not defined TARGET (
561
+ set "RAW=%%r"
562
+ set "BRANCH=!RAW: origin/=!"
563
+ set "BRANCH=!BRANCH:origin/=!"
564
+ set "TAIL=!BRANCH:~-8!"
565
+ if not "!TAIL!"=="-changes" set "TARGET=!BRANCH!"
566
+ )
567
+ )
568
+ if defined TARGET (
569
+ echo %GREEN% Version branch detected. Target: %BOLD%!TARGET!%RESET%
570
+ ) else (
571
+ set "TARGET=main"
572
+ echo %YELLOW% No release pipeline or version branch found. Target: %BOLD%main%RESET%
573
+ )
574
+ )
575
+
576
+ :do_merge
577
+ echo.
578
+
579
+ REM --- Merge target into current branch ---
580
+ echo %CYAN%[2/4]%RESET% Merging %BOLD%!TARGET!%RESET% into %BOLD%%CURRENT_BRANCH%%RESET%...
581
+ git merge origin/!TARGET! --no-edit
582
+ if errorlevel 1 (
583
+ echo.
584
+ echo %RED%X Merge conflict detected.%RESET%
585
+ git merge --abort >nul 2>&1
586
+ echo %DIM% Resolve conflicts manually, then retry.%RESET%
587
+ echo.
588
+ exit /b 1
589
+ )
590
+ echo %GREEN% OK%RESET%
591
+ echo.
592
+
593
+ REM --- Push current branch to origin ---
594
+ echo %CYAN%[3/4]%RESET% Pushing %CURRENT_BRANCH% to origin...
595
+ git push -u origin HEAD
596
+ if errorlevel 1 (
597
+ echo.
598
+ echo %RED%X Failed to push branch to origin.%RESET%
599
+ echo.
600
+ exit /b 1
601
+ )
602
+ echo %GREEN% OK%RESET%
603
+ echo.
604
+
605
+ REM --- Guard: don't PR into the same branch ---
606
+ if "!TARGET!"=="!CURRENT_BRANCH!" (
607
+ echo %RED%X Target branch is the same as current branch ^(!TARGET!^). Cannot create PR.%RESET%
608
+ echo.
609
+ exit /b 1
610
+ )
611
+
612
+ echo %CYAN%[4/4]%RESET% Creating PR: %BOLD%%CURRENT_BRANCH%%RESET% %DIM%->%RESET% %BOLD%%TARGET%%RESET%
613
+ echo.
614
+
615
+ REM --- Create the PR ---
616
+ gh pr create --repo "!REPO!" --base "%TARGET%" --fill
617
+ if errorlevel 1 (
618
+ echo.
619
+ echo %RED%X Failed to create pull request.%RESET%
620
+ echo %DIM% Check the error above. A PR may already exist for this branch.%RESET%
621
+ echo.
622
+ exit /b 1
623
+ )
624
+
625
+ echo.
626
+ echo %GREEN%%BOLD% PR created successfully!%RESET%
627
+ echo.
628
+ `
629
+ },
630
+ {
631
+ name: "commit-and-pr",
632
+ filename: "commit-and-pr.ps1",
633
+ description: "PowerShell script that combines commit & push with PR creation. Checks for pending changes first: if changes exist, waits for the commit (triggered by Cursor keybinding), shows a summary, then runs create-pr.cmd. If no changes, skips straight to PR. Requires create-pr.cmd in the same directory. Bound to Ctrl+Shift+M.",
634
+ content: `param()
635
+
636
+ $hasChanges = (git status --porcelain 2>$null)
637
+
638
+ if ($hasChanges) {
639
+ $initial = git log --oneline -1 2>$null
640
+
641
+ Write-Host "\`n==========================================" -ForegroundColor Cyan
642
+ Write-Host " Starting Commit, Push & PR..." -ForegroundColor Cyan
643
+ Write-Host "==========================================" -ForegroundColor Cyan
644
+ Write-Host " [1/5] Pulling latest..." -ForegroundColor DarkGray
645
+ Write-Host " [2/5] Staging changes..." -ForegroundColor DarkGray
646
+ Write-Host " [3/5] Generating AI commit message..." -ForegroundColor DarkGray
647
+ Write-Host " [4/5] Committing..." -ForegroundColor DarkGray
648
+ Write-Host " [5/5] Pushing to remote..." -ForegroundColor DarkGray
649
+ Write-Host ""
650
+ Write-Host " Waiting for commit" -ForegroundColor DarkGray -NoNewline
651
+
652
+ $timeout = 60
653
+ $elapsed = 0
654
+ while ($elapsed -lt $timeout) {
655
+ $current = git log --oneline -1 2>$null
656
+ if ($current -ne $initial) { break }
657
+ Start-Sleep -Seconds 1
658
+ $elapsed++
659
+ Write-Host "." -ForegroundColor DarkGray -NoNewline
660
+ }
661
+ Write-Host ""
662
+
663
+ if ($current -eq $initial) {
664
+ Write-Host ""
665
+ Write-Host " [!] No new commit detected (timed out after \${timeout}s)" -ForegroundColor Yellow
666
+ Write-Host " Skipping PR creation." -ForegroundColor Yellow
667
+ Write-Host ""
668
+ exit
669
+ }
670
+
671
+ Start-Sleep -Seconds 2
672
+
673
+ $branch = git rev-parse --abbrev-ref HEAD 2>$null
674
+ $commit = git log --format='%h %s' -1 2>$null
675
+
676
+ Write-Host ""
677
+ Write-Host "==========================================" -ForegroundColor Cyan
678
+ Write-Host " [OK] Commit & Push Complete" -ForegroundColor Green
679
+ Write-Host "==========================================" -ForegroundColor Cyan
680
+ Write-Host " Branch: $branch" -ForegroundColor Yellow
681
+ Write-Host " Commit: $commit" -ForegroundColor White
682
+ Write-Host ""
683
+ git --no-pager diff HEAD~1 --stat --color=always 2>$null
684
+ Write-Host ""
685
+ Write-Host "==========================================" -ForegroundColor Cyan
686
+ Write-Host ""
687
+ } else {
688
+ Write-Host "\`n==========================================" -ForegroundColor Cyan
689
+ Write-Host " No changes to commit" -ForegroundColor DarkGray
690
+ Write-Host " Proceeding to PR creation..." -ForegroundColor Cyan
691
+ Write-Host "==========================================" -ForegroundColor Cyan
692
+ Write-Host ""
693
+ }
694
+
695
+ & .\\create-pr.cmd
696
+ `
697
+ }
698
+ ];
699
+ var SCRIPT_TOOLS = [
700
+ {
701
+ name: "scripts-list",
702
+ description: "List available MG Dashboard workflow scripts that can be downloaded into your project. Returns script names, filenames, and descriptions.",
703
+ inputSchema: { type: "object", properties: {}, required: [] }
704
+ },
705
+ {
706
+ name: "script-get",
707
+ description: "Get the content of an MG Dashboard workflow script by name. Save the returned content to the filename indicated in the response. Place scripts in the repository root.",
708
+ inputSchema: {
709
+ type: "object",
710
+ properties: {
711
+ name: {
712
+ type: "string",
713
+ description: `Script name. Available: ${SCRIPTS.map((s) => s.name).join(", ")}`
714
+ }
715
+ },
716
+ required: ["name"]
717
+ }
718
+ }
719
+ ];
720
+ var SCRIPT_TOOL_NAMES = new Set(SCRIPT_TOOLS.map((t) => t.name));
721
+ function handleScriptTool(toolName, args2) {
722
+ switch (toolName) {
723
+ case "scripts-list": {
724
+ const list = SCRIPTS.map((s) => ({
725
+ name: s.name,
726
+ filename: s.filename,
727
+ description: s.description
728
+ }));
729
+ return {
730
+ content: [
731
+ {
732
+ type: "text",
733
+ text: JSON.stringify(list, null, 2)
734
+ }
735
+ ]
736
+ };
737
+ }
738
+ case "script-get": {
739
+ const scriptName = String(args2.name);
740
+ const script = SCRIPTS.find((s) => s.name === scriptName);
741
+ if (!script) {
742
+ const available = SCRIPTS.map((s) => s.name).join(", ");
743
+ return {
744
+ content: [
745
+ {
746
+ type: "text",
747
+ text: `Unknown script: "${scriptName}". Available scripts: ${available}`
748
+ }
749
+ ]
750
+ };
751
+ }
752
+ return {
753
+ content: [
754
+ {
755
+ type: "text",
756
+ text: [
757
+ `Filename: ${script.filename}`,
758
+ `Description: ${script.description}`,
759
+ `Place this file in the repository root.`,
760
+ "",
761
+ "--- CONTENT START ---",
762
+ script.content,
763
+ "--- CONTENT END ---"
764
+ ].join("\n")
765
+ }
766
+ ]
767
+ };
768
+ }
769
+ default:
770
+ return { content: [{ type: "text", text: `Unknown script tool: ${toolName}` }] };
771
+ }
772
+ }
773
+
774
+ // src/agent-tools.ts
775
+ var VALID_FINDING_TYPES = /* @__PURE__ */ new Set([
776
+ "missing_docs",
777
+ "inaccurate",
778
+ "incomplete",
779
+ "outdated",
780
+ "improvement",
781
+ "n_plus_1_query",
782
+ "bundle_size",
783
+ "unnecessary_rerender",
784
+ "slow_endpoint",
785
+ "memory_leak",
786
+ "missing_memoization",
787
+ "large_dependency"
788
+ ]);
789
+ var VALID_SEVERITIES = /* @__PURE__ */ new Set(["info", "warning", "critical"]);
790
+ var VALID_SCOPES = /* @__PURE__ */ new Set([
791
+ "architecture",
792
+ "module",
793
+ "component",
794
+ "api",
795
+ "package"
796
+ ]);
797
+ var VALID_REVIEW_STATUSES = /* @__PURE__ */ new Set([
798
+ "pending",
799
+ "approved",
800
+ "rejected",
801
+ "outdated"
802
+ ]);
803
+ var AGENT_TOOLS = [
804
+ {
805
+ name: "agent-report-coverage",
806
+ description: "Report documentation coverage data for a repository. Upserts records into doc_coverage. Call once per logical unit (package, module, theme, plugin directory). Provide all entries in a single call for efficiency.",
807
+ inputSchema: {
808
+ type: "object",
809
+ properties: {
810
+ repo_slug: {
811
+ type: "string",
812
+ description: 'Repository slug (e.g. "mg-dashboard", "bna-wordpress")'
813
+ },
814
+ refront_project_id: {
815
+ type: "string",
816
+ description: "Refront project UUID linked to this repository"
817
+ },
818
+ entries: {
819
+ type: "array",
820
+ description: "Array of coverage entries, one per logical unit",
821
+ items: {
822
+ type: "object",
823
+ properties: {
824
+ path: { type: "string", description: 'Logical unit path (e.g. "packages/ui", "wp-content/themes/bna")' },
825
+ total_functions: { type: "number", description: "Total public functions/methods" },
826
+ documented_functions: { type: "number", description: "Functions with doc comments" },
827
+ total_types: { type: "number", description: "Total classes/interfaces/types" },
828
+ documented_types: { type: "number", description: "Types with doc comments" },
829
+ total_endpoints: { type: "number", description: "Total API/REST/hook endpoints" },
830
+ documented_endpoints: { type: "number", description: "Endpoints with documentation" }
831
+ },
832
+ required: ["path", "total_functions", "documented_functions"]
833
+ }
834
+ }
835
+ },
836
+ required: ["repo_slug", "refront_project_id", "entries"]
837
+ }
838
+ },
839
+ {
840
+ name: "agent-report-finding",
841
+ description: "Report documentation or performance findings for a repository. Inserts records into doc_suggestion. Auto-creates a parent documentation record if one does not exist yet. Batch multiple findings in one call.",
842
+ inputSchema: {
843
+ type: "object",
844
+ properties: {
845
+ repo_slug: {
846
+ type: "string",
847
+ description: "Repository slug"
848
+ },
849
+ refront_project_id: {
850
+ type: "string",
851
+ description: "Refront project UUID linked to this repository"
852
+ },
853
+ category: {
854
+ type: "string",
855
+ enum: ["scan_findings", "perf_audit"],
856
+ description: 'Finding category: "scan_findings" for doc issues, "perf_audit" for performance issues'
857
+ },
858
+ findings: {
859
+ type: "array",
860
+ description: "Array of findings to report",
861
+ items: {
862
+ type: "object",
863
+ properties: {
864
+ type: {
865
+ type: "string",
866
+ enum: [...VALID_FINDING_TYPES],
867
+ description: "Finding type"
868
+ },
869
+ severity: {
870
+ type: "string",
871
+ enum: ["info", "warning", "critical"],
872
+ description: "Severity level"
873
+ },
874
+ description: {
875
+ type: "string",
876
+ description: "Clear description of the issue (max 2000 chars)"
877
+ },
878
+ file_path: {
879
+ type: "string",
880
+ description: "Relative file path where the issue was found"
881
+ },
882
+ suggested_fix: {
883
+ type: "string",
884
+ description: "Suggested fix with code example (max 5000 chars)"
885
+ }
886
+ },
887
+ required: ["type", "severity", "description"]
888
+ }
889
+ }
890
+ },
891
+ required: ["repo_slug", "refront_project_id", "category", "findings"]
892
+ }
893
+ },
894
+ {
895
+ name: "agent-save-documentation",
896
+ description: "Save or update a documentation record for a repository. Upserts by repo_slug + scope + path combination.",
897
+ inputSchema: {
898
+ type: "object",
899
+ properties: {
900
+ repo_slug: { type: "string", description: "Repository slug" },
901
+ refront_project_id: { type: "string", description: "Refront project UUID" },
902
+ scope: {
903
+ type: "string",
904
+ enum: [...VALID_SCOPES],
905
+ description: "Documentation scope"
906
+ },
907
+ path: {
908
+ type: "string",
909
+ description: 'Path within the repo (e.g. "packages/ui", "__perf_audit__")'
910
+ },
911
+ title: { type: "string", description: "Document title" },
912
+ content: { type: "string", description: "Documentation content (markdown)" },
913
+ review_status: {
914
+ type: "string",
915
+ enum: [...VALID_REVIEW_STATUSES],
916
+ description: 'Review status (default: "pending")'
917
+ }
918
+ },
919
+ required: ["repo_slug", "refront_project_id", "scope", "path", "title", "content"]
920
+ }
921
+ },
922
+ {
923
+ name: "agent-list-findings",
924
+ description: "List existing findings (doc_suggestion records) for a repository. Use this to check what has already been reported before submitting new findings.",
925
+ inputSchema: {
926
+ type: "object",
927
+ properties: {
928
+ repo_slug: { type: "string", description: "Repository slug" },
929
+ type: {
930
+ type: "string",
931
+ enum: [...VALID_FINDING_TYPES],
932
+ description: "Filter by finding type"
933
+ },
934
+ severity: {
935
+ type: "string",
936
+ enum: ["info", "warning", "critical"],
937
+ description: "Filter by severity"
938
+ },
939
+ status: {
940
+ type: "string",
941
+ enum: ["open", "accepted", "rejected", "fixed"],
942
+ description: "Filter by status (default: all)"
943
+ },
944
+ limit: {
945
+ type: "number",
946
+ description: "Max results to return (default: 50, max: 200)"
947
+ }
948
+ },
949
+ required: ["repo_slug"]
950
+ }
951
+ },
952
+ {
953
+ name: "agent-get-documentation",
954
+ description: "Retrieve existing documentation records for a repository. Use this to read current docs before generating or reviewing.",
955
+ inputSchema: {
956
+ type: "object",
957
+ properties: {
958
+ repo_slug: { type: "string", description: "Repository slug" },
959
+ scope: {
960
+ type: "string",
961
+ enum: [...VALID_SCOPES],
962
+ description: "Filter by scope"
963
+ },
964
+ path: { type: "string", description: "Filter by exact path" },
965
+ limit: {
966
+ type: "number",
967
+ description: "Max results to return (default: 20, max: 100)"
968
+ }
969
+ },
970
+ required: ["repo_slug"]
971
+ }
972
+ }
973
+ ];
974
+ var AGENT_TOOL_NAMES = new Set(AGENT_TOOLS.map((t) => t.name));
975
+ var AGENT_TOOL_MODULE_MAP = {
976
+ "agent-report-coverage": "agent_reporting",
977
+ "agent-report-finding": "agent_reporting",
978
+ "agent-save-documentation": "agent_reporting",
979
+ "agent-list-findings": "agent_reporting",
980
+ "agent-get-documentation": "agent_reporting"
981
+ };
982
+ function clamp(val, min, max) {
983
+ return Math.max(min, Math.min(max, val));
984
+ }
985
+ function sanitizeString(val, maxLen) {
986
+ return String(val ?? "").slice(0, maxLen);
987
+ }
988
+ async function getOrCreateParentDoc(supabase2, repoSlug, refrontProjectId, category) {
989
+ const path = category === "perf_audit" ? "__perf_audit__" : "__scan_findings__";
990
+ const title = category === "perf_audit" ? `Performance Audit: ${repoSlug}` : `Scan Findings: ${repoSlug}`;
991
+ const { data: existing } = await supabase2.from("project_documentation").select("id").eq("repo_slug", repoSlug).eq("path", path).maybeSingle();
992
+ if (existing) return existing.id;
993
+ const { data: inserted, error } = await supabase2.from("project_documentation").insert({
994
+ refront_project_id: refrontProjectId,
995
+ repo_slug: repoSlug,
996
+ scope: "architecture",
997
+ path,
998
+ title,
999
+ content: `Auto-generated parent record for ${category.replace("_", " ")} suggestions.`,
1000
+ generated_by: `${category}-agent-v1`,
1001
+ review_status: "pending"
1002
+ }).select("id").single();
1003
+ if (error || !inserted) {
1004
+ throw new Error(`Failed to create parent doc record: ${error?.message}`);
1005
+ }
1006
+ return inserted.id;
1007
+ }
1008
+ async function handleAgentTool(name, args2, deps) {
1009
+ const { supabase: supabase2 } = deps;
1010
+ switch (name) {
1011
+ // -----------------------------------------------------------------
1012
+ case "agent-report-coverage": {
1013
+ const repoSlug = sanitizeString(args2.repo_slug, 200);
1014
+ const refrontProjectId = sanitizeString(args2.refront_project_id, 100);
1015
+ const entries = Array.isArray(args2.entries) ? args2.entries : [];
1016
+ if (!repoSlug) throw new Error("repo_slug is required");
1017
+ if (!refrontProjectId) throw new Error("refront_project_id is required");
1018
+ if (entries.length === 0) throw new Error("entries array must not be empty");
1019
+ const wsId = deps.workspaceId;
1020
+ const scanCommit = wsId ? `agent-scan-${wsId.slice(0, 8)}` : `agent-scan-${Date.now().toString(36)}`;
1021
+ let upserted = 0;
1022
+ let errors = 0;
1023
+ for (const entry of entries) {
1024
+ const { error } = await supabase2.from("doc_coverage").upsert(
1025
+ {
1026
+ refront_project_id: refrontProjectId,
1027
+ repo_slug: repoSlug,
1028
+ path: sanitizeString(entry.path, 500),
1029
+ total_functions: clamp(Number(entry.total_functions) || 0, 0, 99999),
1030
+ documented_functions: clamp(Number(entry.documented_functions) || 0, 0, 99999),
1031
+ total_types: clamp(Number(entry.total_types) || 0, 0, 99999),
1032
+ documented_types: clamp(Number(entry.documented_types) || 0, 0, 99999),
1033
+ total_endpoints: clamp(Number(entry.total_endpoints) || 0, 0, 99999),
1034
+ documented_endpoints: clamp(Number(entry.documented_endpoints) || 0, 0, 99999),
1035
+ scan_commit: scanCommit,
1036
+ scanned_at: (/* @__PURE__ */ new Date()).toISOString()
1037
+ },
1038
+ { onConflict: "repo_slug,path" }
1039
+ );
1040
+ if (error) errors++;
1041
+ else upserted++;
1042
+ }
1043
+ return {
1044
+ content: [{
1045
+ type: "text",
1046
+ text: `Coverage reported: ${upserted} entries upserted${errors > 0 ? `, ${errors} errors` : ""}`
1047
+ }]
1048
+ };
1049
+ }
1050
+ // -----------------------------------------------------------------
1051
+ case "agent-report-finding": {
1052
+ const repoSlug = sanitizeString(args2.repo_slug, 200);
1053
+ const refrontProjectId = sanitizeString(args2.refront_project_id, 100);
1054
+ const category = args2.category;
1055
+ const findings = Array.isArray(args2.findings) ? args2.findings : [];
1056
+ if (!repoSlug) throw new Error("repo_slug is required");
1057
+ if (!refrontProjectId) throw new Error("refront_project_id is required");
1058
+ if (!category || !["scan_findings", "perf_audit"].includes(category)) {
1059
+ throw new Error('category must be "scan_findings" or "perf_audit"');
1060
+ }
1061
+ if (findings.length === 0) throw new Error("findings array must not be empty");
1062
+ const parentDocId = await getOrCreateParentDoc(supabase2, repoSlug, refrontProjectId, category);
1063
+ let inserted = 0;
1064
+ let errors = 0;
1065
+ for (const f of findings) {
1066
+ const findingType = VALID_FINDING_TYPES.has(f.type) ? f.type : "improvement";
1067
+ const severity = VALID_SEVERITIES.has(f.severity) ? f.severity : "info";
1068
+ const description = sanitizeString(f.description, 2e3);
1069
+ if (!description) continue;
1070
+ const { error } = await supabase2.from("doc_suggestion").insert({
1071
+ documentation_id: parentDocId,
1072
+ type: findingType,
1073
+ severity,
1074
+ description,
1075
+ file_path: f.file_path ? sanitizeString(f.file_path, 500) : null,
1076
+ suggested_fix: f.suggested_fix ? sanitizeString(f.suggested_fix, 5e3) : null,
1077
+ status: "open"
1078
+ });
1079
+ if (error) errors++;
1080
+ else inserted++;
1081
+ }
1082
+ return {
1083
+ content: [{
1084
+ type: "text",
1085
+ text: `Findings reported: ${inserted} inserted under ${category}${errors > 0 ? `, ${errors} errors` : ""}`
1086
+ }]
1087
+ };
1088
+ }
1089
+ // -----------------------------------------------------------------
1090
+ case "agent-save-documentation": {
1091
+ const repoSlug = sanitizeString(args2.repo_slug, 200);
1092
+ const refrontProjectId = sanitizeString(args2.refront_project_id, 100);
1093
+ const scope = VALID_SCOPES.has(args2.scope) ? args2.scope : "module";
1094
+ const path = sanitizeString(args2.path, 500);
1095
+ const title = sanitizeString(args2.title, 500);
1096
+ const content = sanitizeString(args2.content, 1e5);
1097
+ const reviewStatus = VALID_REVIEW_STATUSES.has(args2.review_status) ? args2.review_status : "pending";
1098
+ if (!repoSlug) throw new Error("repo_slug is required");
1099
+ if (!refrontProjectId) throw new Error("refront_project_id is required");
1100
+ if (!path) throw new Error("path is required");
1101
+ if (!title) throw new Error("title is required");
1102
+ if (!content) throw new Error("content is required");
1103
+ const { data: existing } = await supabase2.from("project_documentation").select("id").eq("repo_slug", repoSlug).eq("scope", scope).eq("path", path).maybeSingle();
1104
+ if (existing) {
1105
+ const { error: error2 } = await supabase2.from("project_documentation").update({
1106
+ title,
1107
+ content,
1108
+ review_status: reviewStatus,
1109
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
1110
+ }).eq("id", existing.id);
1111
+ if (error2) throw new Error(`Failed to update documentation: ${error2.message}`);
1112
+ return { content: [{ type: "text", text: `Documentation updated: ${title} (${scope}/${path})` }] };
1113
+ }
1114
+ const generatedBy = deps.workspaceId ? `agent-${deps.workspaceId.slice(0, 8)}` : "agent-mcp";
1115
+ const { error } = await supabase2.from("project_documentation").insert({
1116
+ refront_project_id: refrontProjectId,
1117
+ repo_slug: repoSlug,
1118
+ scope,
1119
+ path,
1120
+ title,
1121
+ content,
1122
+ generated_by: generatedBy,
1123
+ review_status: reviewStatus
1124
+ });
1125
+ if (error) throw new Error(`Failed to save documentation: ${error.message}`);
1126
+ return { content: [{ type: "text", text: `Documentation saved: ${title} (${scope}/${path})` }] };
1127
+ }
1128
+ // -----------------------------------------------------------------
1129
+ case "agent-list-findings": {
1130
+ const repoSlug = sanitizeString(args2.repo_slug, 200);
1131
+ if (!repoSlug) throw new Error("repo_slug is required");
1132
+ const limit = clamp(Number(args2.limit) || 50, 1, 200);
1133
+ let docQuery = supabase2.from("project_documentation").select("id").eq("repo_slug", repoSlug);
1134
+ const docIds = await docQuery;
1135
+ if (!docIds.data || docIds.data.length === 0) {
1136
+ return { content: [{ type: "text", text: `No documentation records found for repo "${repoSlug}"` }] };
1137
+ }
1138
+ let query = supabase2.from("doc_suggestion").select("id, type, severity, description, file_path, status, created_at").in("documentation_id", docIds.data.map((d) => d.id)).order("created_at", { ascending: false }).limit(limit);
1139
+ if (args2.type && VALID_FINDING_TYPES.has(args2.type)) {
1140
+ query = query.eq("type", args2.type);
1141
+ }
1142
+ if (args2.severity && VALID_SEVERITIES.has(args2.severity)) {
1143
+ query = query.eq("severity", args2.severity);
1144
+ }
1145
+ if (args2.status) {
1146
+ query = query.eq("status", args2.status);
1147
+ }
1148
+ const { data: findings, error } = await query;
1149
+ if (error) throw new Error(`Failed to query findings: ${error.message}`);
1150
+ if (!findings || findings.length === 0) {
1151
+ return { content: [{ type: "text", text: `No findings found for repo "${repoSlug}"` }] };
1152
+ }
1153
+ const summary = findings.map(
1154
+ (f) => `[${f.severity}] ${f.type}: ${String(f.description).slice(0, 120)}${f.file_path ? ` (${f.file_path})` : ""} \u2014 ${f.status}`
1155
+ ).join("\n");
1156
+ return {
1157
+ content: [{
1158
+ type: "text",
1159
+ text: `${findings.length} findings for "${repoSlug}":
1160
+
1161
+ ${summary}`
1162
+ }]
1163
+ };
1164
+ }
1165
+ // -----------------------------------------------------------------
1166
+ case "agent-get-documentation": {
1167
+ const repoSlug = sanitizeString(args2.repo_slug, 200);
1168
+ if (!repoSlug) throw new Error("repo_slug is required");
1169
+ const limit = clamp(Number(args2.limit) || 20, 1, 100);
1170
+ let query = supabase2.from("project_documentation").select("id, repo_slug, scope, path, title, content, review_status, generated_by, created_at, updated_at").eq("repo_slug", repoSlug).order("updated_at", { ascending: false }).limit(limit);
1171
+ if (args2.scope && VALID_SCOPES.has(args2.scope)) {
1172
+ query = query.eq("scope", args2.scope);
1173
+ }
1174
+ if (args2.path) {
1175
+ query = query.eq("path", sanitizeString(args2.path, 500));
1176
+ }
1177
+ const { data: docs, error } = await query;
1178
+ if (error) throw new Error(`Failed to query documentation: ${error.message}`);
1179
+ if (!docs || docs.length === 0) {
1180
+ return { content: [{ type: "text", text: `No documentation found for repo "${repoSlug}"` }] };
1181
+ }
1182
+ const output = docs.map((d) => {
1183
+ const contentPreview = String(d.content || "").slice(0, 500);
1184
+ return [
1185
+ `## ${d.title} (${d.scope}/${d.path})`,
1186
+ `Status: ${d.review_status} | By: ${d.generated_by || "unknown"}`,
1187
+ `Updated: ${d.updated_at || d.created_at}`,
1188
+ "",
1189
+ contentPreview + (String(d.content || "").length > 500 ? "\n...(truncated)" : ""),
1190
+ ""
1191
+ ].join("\n");
1192
+ }).join("\n---\n\n");
1193
+ return {
1194
+ content: [{ type: "text", text: `${docs.length} docs for "${repoSlug}":
1195
+
1196
+ ${output}` }]
1197
+ };
1198
+ }
1199
+ // -----------------------------------------------------------------
1200
+ default:
1201
+ return { content: [{ type: "text", text: `Unknown agent tool: ${name}` }] };
1202
+ }
1203
+ }
1204
+
376
1205
  // src/index.ts
377
1206
  var args = process.argv.slice(2);
378
1207
  function getArg(name) {
@@ -383,6 +1212,7 @@ var supabaseUrl = getArg("supabase-url") || process.env.SUPABASE_URL;
383
1212
  var supabaseKey = getArg("supabase-key") || process.env.SUPABASE_SERVICE_ROLE_KEY;
384
1213
  var encryptionKey = getArg("encryption-key") || process.env.ENCRYPTION_KEY;
385
1214
  var mijnhostApiKey = getArg("mijnhost-api-key") || process.env.MIJNHOST_API_KEY;
1215
+ var agentWorkspaceId = getArg("workspace-id") || process.env.AGENT_WORKSPACE_ID || null;
386
1216
  if (!apiKey) {
387
1217
  console.error("API key is required. Use --api-key=dk_xxx or set MG_DASHBOARD_API_KEY");
388
1218
  process.exit(1);
@@ -439,16 +1269,14 @@ setInterval(() => {
439
1269
  }, 5 * 60 * 1e3).unref();
440
1270
  var MODULE_KEYS = [
441
1271
  "users",
442
- "emails",
443
- "logs",
444
1272
  "ssh_servers",
445
1273
  "supabase",
446
1274
  "wiki",
447
1275
  "ci_cd",
448
1276
  "source_control",
449
1277
  "domains",
450
- "google_search_console",
451
- "settings"
1278
+ "settings",
1279
+ "agent_reporting"
452
1280
  ];
453
1281
  var FULL_PERMISSIONS = {
454
1282
  modules: Object.fromEntries(MODULE_KEYS.map((k) => [k, true])),
@@ -487,28 +1315,18 @@ function intersectServerAccess(keyServerIds, permissionServerIds) {
487
1315
  }
488
1316
  var TOOL_MODULE_MAP = {
489
1317
  "list-servers": "ssh_servers",
490
- "server-status": "ssh_servers",
491
1318
  "ssh-execute": "ssh_servers",
492
- "server-reboot": "ssh_servers",
493
- "server-restart-service": "ssh_servers",
494
1319
  "sftp-list": "ssh_servers",
495
1320
  "sftp-read": "ssh_servers",
496
1321
  "sftp-write": "ssh_servers",
497
1322
  "sftp-delete": "ssh_servers",
498
1323
  "docker-list": "ssh_servers",
499
- "docker-action": "ssh_servers",
500
1324
  "docker-logs": "ssh_servers",
501
1325
  "db-discover": "ssh_servers",
502
1326
  "db-tables": "ssh_servers",
503
1327
  "db-describe": "ssh_servers",
504
1328
  "db-query": "ssh_servers",
505
1329
  "cache-purge": "ssh_servers",
506
- "log-list": "ssh_servers",
507
- "log-read": "ssh_servers",
508
- "cron-list": "ssh_servers",
509
- "cron-add": "ssh_servers",
510
- "cron-remove": "ssh_servers",
511
- "cron-toggle": "ssh_servers",
512
1330
  "env-list": "ci_cd",
513
1331
  "env-get": "ci_cd",
514
1332
  "env-store": "ci_cd",
@@ -518,7 +1336,8 @@ var TOOL_MODULE_MAP = {
518
1336
  "dns-create": "domains",
519
1337
  "dns-update": "domains",
520
1338
  "dns-delete": "domains",
521
- ...TRIGGER_TOOL_MODULE_MAP
1339
+ ...TRIGGER_TOOL_MODULE_MAP,
1340
+ ...AGENT_TOOL_MODULE_MAP
522
1341
  };
523
1342
  var authContext = null;
524
1343
  async function validateApiKey(key) {
@@ -660,6 +1479,11 @@ function stageToVercelTargets(stageType, customEnvId) {
660
1479
  if (customEnvId) return { customEnvironmentIds: [customEnvId] };
661
1480
  return { target: ["preview"] };
662
1481
  }
1482
+ function getVercelEnvSyncTargetings(stageType, customEnvId) {
1483
+ const primary = stageToVercelTargets(stageType, customEnvId);
1484
+ if (stageType !== "dev") return [primary];
1485
+ return [primary, { target: ["development"] }];
1486
+ }
663
1487
  async function syncEnvVarsToVercel(token, projectId, envVars) {
664
1488
  if (envVars.length === 0) return { created: 0, error: null };
665
1489
  const res = await fetch(
@@ -735,18 +1559,24 @@ async function attemptVercelSync(appName, environment, knownStageId) {
735
1559
  syncResults.push(`${app.label}: empty config`);
736
1560
  continue;
737
1561
  }
738
- const targeting = stageToVercelTargets(stageType, app.vercelCustomEnvId);
739
- const envVars = keys.map((key) => ({
740
- key,
741
- value: pairs[key],
742
- type: "encrypted",
743
- ...targeting
744
- }));
745
- const { created, error } = await syncEnvVarsToVercel(token, app.vercelProjectId, envVars);
746
- if (error) {
747
- syncResults.push(`${app.label}: FAILED - ${error}`);
1562
+ const targetings = getVercelEnvSyncTargetings(stageType, app.vercelCustomEnvId);
1563
+ let createdTotal = 0;
1564
+ let lastErr = null;
1565
+ for (const targeting of targetings) {
1566
+ const envVars = keys.map((key) => ({
1567
+ key,
1568
+ value: pairs[key],
1569
+ type: "plain",
1570
+ ...targeting
1571
+ }));
1572
+ const { created, error } = await syncEnvVarsToVercel(token, app.vercelProjectId, envVars);
1573
+ if (error) lastErr = error;
1574
+ else createdTotal += created;
1575
+ }
1576
+ if (lastErr) {
1577
+ syncResults.push(`${app.label}: FAILED - ${lastErr}`);
748
1578
  } else {
749
- syncResults.push(`${app.label}: ${created} vars synced`);
1579
+ syncResults.push(`${app.label}: ${createdTotal} var upsert(s) synced`);
750
1580
  }
751
1581
  }
752
1582
  return `Vercel sync: ${syncResults.join("; ")}`;
@@ -1215,16 +2045,6 @@ function assertSafeCommand(command) {
1215
2045
  }
1216
2046
  }
1217
2047
  }
1218
- var ALLOWED_LOG_PREFIXES = ["/var/log/", "/usr/local/lsws/logs/", "/var/www/"];
1219
- function assertAllowedLogPath(filePath) {
1220
- const safe = sanitizePath(filePath);
1221
- if (!safe.endsWith(".log")) {
1222
- throw new Error("Only .log files can be read with log-read");
1223
- }
1224
- if (!ALLOWED_LOG_PREFIXES.some((prefix) => safe.startsWith(prefix))) {
1225
- throw new Error(`Path not allowed. Must be under: ${ALLOWED_LOG_PREFIXES.join(", ")}`);
1226
- }
1227
- }
1228
2048
  async function discoverSiteDatabases(conn, proxy) {
1229
2049
  const script = `
1230
2050
  check_dir() {
@@ -1390,17 +2210,6 @@ var TOOLS = [
1390
2210
  description: "List all SSH servers you have access to. Returns id, name, hostname, and tags for each server.",
1391
2211
  inputSchema: { type: "object", properties: {}, required: [] }
1392
2212
  },
1393
- {
1394
- name: "server-status",
1395
- description: "Get server status including uptime, disk usage, memory, and load average.",
1396
- inputSchema: {
1397
- type: "object",
1398
- properties: {
1399
- serverId: { type: "string", description: "UUID of the SSH server" }
1400
- },
1401
- required: ["serverId"]
1402
- }
1403
- },
1404
2213
  {
1405
2214
  name: "ssh-execute",
1406
2215
  description: "Execute a shell command on a remote server via SSH. Some dangerous commands are blocked for safety.",
@@ -1414,29 +2223,6 @@ var TOOLS = [
1414
2223
  required: ["serverId", "command"]
1415
2224
  }
1416
2225
  },
1417
- {
1418
- name: "server-reboot",
1419
- description: "Reboot a remote server. This will cause downtime. The server will be unavailable until it restarts.",
1420
- inputSchema: {
1421
- type: "object",
1422
- properties: {
1423
- serverId: { type: "string", description: "UUID of the SSH server to reboot" }
1424
- },
1425
- required: ["serverId"]
1426
- }
1427
- },
1428
- {
1429
- name: "server-restart-service",
1430
- description: "Restart a systemd service on a remote server using systemctl restart.",
1431
- inputSchema: {
1432
- type: "object",
1433
- properties: {
1434
- serverId: { type: "string", description: "UUID of the SSH server" },
1435
- serviceName: { type: "string", description: "Name of the systemd service (e.g. nginx, docker, lsws)" }
1436
- },
1437
- required: ["serverId", "serviceName"]
1438
- }
1439
- },
1440
2226
  {
1441
2227
  name: "sftp-list",
1442
2228
  description: "List files and directories at a given path on a remote server via SFTP.",
@@ -1497,19 +2283,6 @@ var TOOLS = [
1497
2283
  required: ["serverId"]
1498
2284
  }
1499
2285
  },
1500
- {
1501
- name: "docker-action",
1502
- description: "Perform an action on a Docker container: start, stop, restart, or remove.",
1503
- inputSchema: {
1504
- type: "object",
1505
- properties: {
1506
- serverId: { type: "string", description: "UUID of the SSH server" },
1507
- containerName: { type: "string", description: "Container name or ID" },
1508
- action: { type: "string", enum: ["start", "stop", "restart", "remove"], description: "Action to perform" }
1509
- },
1510
- required: ["serverId", "containerName", "action"]
1511
- }
1512
- },
1513
2286
  {
1514
2287
  name: "docker-logs",
1515
2288
  description: "Get recent logs from a Docker container.",
@@ -1621,86 +2394,6 @@ var TOOLS = [
1621
2394
  required: ["serverId"]
1622
2395
  }
1623
2396
  },
1624
- {
1625
- name: "log-list",
1626
- description: "Discover available log files on a server. Scans LiteSpeed logs, PHP error logs, syslog, and per-site application logs (WordPress debug.log, PrestaShop var/logs, Laravel storage/logs). Returns paths with file sizes and last modified dates.",
1627
- inputSchema: {
1628
- type: "object",
1629
- properties: {
1630
- serverId: { type: "string", description: "UUID of the SSH server" }
1631
- },
1632
- required: ["serverId"]
1633
- }
1634
- },
1635
- {
1636
- name: "log-read",
1637
- description: "Read the last N lines from a specific log file on a server. Use log-list first to discover available files. Optionally filter lines with a grep pattern.",
1638
- inputSchema: {
1639
- type: "object",
1640
- properties: {
1641
- serverId: { type: "string", description: "UUID of the SSH server" },
1642
- path: { type: "string", description: "Absolute path to the log file (must end in .log)" },
1643
- lines: { type: "number", description: "Number of lines to read (default: 100, max: 500)" },
1644
- filter: { type: "string", description: "Optional grep pattern to filter lines before tailing" }
1645
- },
1646
- required: ["serverId", "path"]
1647
- }
1648
- },
1649
- // ----- Cron Jobs -----
1650
- {
1651
- name: "cron-list",
1652
- description: "List all cron jobs on a server. Shows root crontab, system cron directories (/etc/cron.d/), and www-data crontab. Each entry shows the schedule, command, user, and source. Disabled (commented) entries are marked.",
1653
- inputSchema: {
1654
- type: "object",
1655
- properties: {
1656
- serverId: { type: "string", description: "UUID of the SSH server" },
1657
- user: { type: "string", description: "Specific user crontab to list (default: lists root + www-data + /etc/cron.d/)" }
1658
- },
1659
- required: ["serverId"]
1660
- }
1661
- },
1662
- {
1663
- name: "cron-add",
1664
- description: 'Add a new cron job to a user crontab. Provide a standard cron schedule expression and command. Examples: "0 2 * * * /usr/bin/backup.sh" (daily at 2am), "*/5 * * * * curl http://example.com/cron.php" (every 5 min).',
1665
- inputSchema: {
1666
- type: "object",
1667
- properties: {
1668
- serverId: { type: "string", description: "UUID of the SSH server" },
1669
- schedule: { type: "string", description: 'Cron schedule expression (e.g. "0 2 * * *" or "@daily")' },
1670
- command: { type: "string", description: "Command to execute" },
1671
- user: { type: "string", description: "User whose crontab to edit (default: root)" },
1672
- comment: { type: "string", description: "Optional comment to add above the cron entry (for identification)" }
1673
- },
1674
- required: ["serverId", "schedule", "command"]
1675
- }
1676
- },
1677
- {
1678
- name: "cron-remove",
1679
- description: "Remove a cron job from a user crontab by matching the command string (exact or partial match). Use cron-list first to see existing jobs.",
1680
- inputSchema: {
1681
- type: "object",
1682
- properties: {
1683
- serverId: { type: "string", description: "UUID of the SSH server" },
1684
- commandMatch: { type: "string", description: "Full or partial command string to match for removal" },
1685
- user: { type: "string", description: "User whose crontab to edit (default: root)" }
1686
- },
1687
- required: ["serverId", "commandMatch"]
1688
- }
1689
- },
1690
- {
1691
- name: "cron-toggle",
1692
- description: "Enable or disable a cron job by commenting/uncommenting it. Matches by command string. Use cron-list to find the job first.",
1693
- inputSchema: {
1694
- type: "object",
1695
- properties: {
1696
- serverId: { type: "string", description: "UUID of the SSH server" },
1697
- commandMatch: { type: "string", description: "Full or partial command string to match" },
1698
- enable: { type: "boolean", description: "true to enable (uncomment), false to disable (comment out)" },
1699
- user: { type: "string", description: "User whose crontab to edit (default: root)" }
1700
- },
1701
- required: ["serverId", "commandMatch", "enable"]
1702
- }
1703
- },
1704
2397
  // ----- Domains (mijn.host) -----
1705
2398
  {
1706
2399
  name: "domain-list",
@@ -1775,7 +2468,11 @@ var TOOLS = [
1775
2468
  }
1776
2469
  },
1777
2470
  // ----- Trigger.dev -----
1778
- ...TRIGGER_TOOLS
2471
+ ...TRIGGER_TOOLS,
2472
+ // ----- Scripts -----
2473
+ ...SCRIPT_TOOLS,
2474
+ // ----- Agent Reporting -----
2475
+ ...AGENT_TOOLS
1779
2476
  ];
1780
2477
  var server = new Server(
1781
2478
  { name: "mg-dashboard-mcp", version: "2.2.0" },
@@ -1821,15 +2518,6 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
1821
2518
  });
1822
2519
  return { content: [{ type: "text", text: lines.length ? lines.join("\n") : "No servers found" }] };
1823
2520
  }
1824
- case "server-status": {
1825
- const { conn, proxy } = await getServerConnection(String(a.serverId));
1826
- const cmd = 'echo "=== UPTIME ===" && uptime && echo "=== DISK ===" && df -h --total && echo "=== MEMORY ===" && free -h && echo "=== LOAD ===" && cat /proc/loadavg';
1827
- const result = await sshExec(conn, cmd, proxy);
1828
- const output = result.exitCode === 0 ? result.stdout : `Exit ${result.exitCode}
1829
- ${result.stdout}
1830
- ${result.stderr}`;
1831
- return { content: [{ type: "text", text: output }] };
1832
- }
1833
2521
  // ----- SSH -----
1834
2522
  case "ssh-execute": {
1835
2523
  const command = String(a.command);
@@ -1844,21 +2532,6 @@ ${result.stdout}`);
1844
2532
  ${result.stderr}`);
1845
2533
  return { content: [{ type: "text", text: output.join("\n") }] };
1846
2534
  }
1847
- case "server-reboot": {
1848
- const { conn, proxy } = await getServerConnection(String(a.serverId));
1849
- const result = await sshExec(conn, "sudo reboot", proxy);
1850
- return { content: [{ type: "text", text: result.exitCode === 0 ? "Reboot command sent. Server will be unavailable shortly." : `Reboot failed: ${result.stderr}` }] };
1851
- }
1852
- case "server-restart-service": {
1853
- const service = String(a.serviceName).replace(/[^a-zA-Z0-9._@-]/g, "");
1854
- const { conn, proxy } = await getServerConnection(String(a.serverId));
1855
- const result = await sshExec(conn, `sudo systemctl restart ${service}`, proxy);
1856
- if (result.exitCode === 0) {
1857
- const status = await sshExec(conn, `sudo systemctl is-active ${service}`, proxy);
1858
- return { content: [{ type: "text", text: `Service "${service}" restarted. Status: ${status.stdout.trim()}` }] };
1859
- }
1860
- return { content: [{ type: "text", text: `Failed to restart "${service}": ${result.stderr}` }] };
1861
- }
1862
2535
  // ----- SFTP (no proxy support yet — direct connection only) -----
1863
2536
  case "sftp-list": {
1864
2537
  const { conn } = await getServerConnection(String(a.serverId));
@@ -1886,17 +2559,6 @@ ${result.stderr}`);
1886
2559
  const result = await sshExec(conn, 'docker ps -a --format "table {{.Names}} {{.Image}} {{.Status}} {{.Ports}}"', proxy);
1887
2560
  return { content: [{ type: "text", text: result.exitCode === 0 ? result.stdout : `Error: ${result.stderr}` }] };
1888
2561
  }
1889
- case "docker-action": {
1890
- const container = String(a.containerName).replace(/[^a-zA-Z0-9._-]/g, "");
1891
- const action = String(a.action);
1892
- if (!["start", "stop", "restart", "remove"].includes(action)) {
1893
- throw new Error(`Invalid action: ${action}. Use start, stop, restart, or remove.`);
1894
- }
1895
- const { conn, proxy } = await getServerConnection(String(a.serverId));
1896
- const dockerCmd = action === "remove" ? `docker rm -f ${container}` : `docker ${action} ${container}`;
1897
- const result = await sshExec(conn, dockerCmd, proxy);
1898
- return { content: [{ type: "text", text: result.exitCode === 0 ? `Container "${container}" ${action}ed successfully` : `Error: ${result.stderr}` }] };
1899
- }
1900
2562
  case "docker-logs": {
1901
2563
  const container = String(a.containerName).replace(/[^a-zA-Z0-9._-]/g, "");
1902
2564
  const lines = Number(a.lines) || 100;
@@ -2120,157 +2782,6 @@ echo -e "$R"
2120
2782
  const output = (result.stdout || "").trim();
2121
2783
  return { content: [{ type: "text", text: output || "Cache purge completed (no output)" }] };
2122
2784
  }
2123
- // ----- Log Reading -----
2124
- case "log-list": {
2125
- const { conn, proxy } = await getServerConnection(String(a.serverId));
2126
- const script = `
2127
- {
2128
- [ -d /usr/local/lsws/logs ] && find /usr/local/lsws/logs -maxdepth 2 -name "*.log" -type f 2>/dev/null
2129
- for f in /var/log/syslog /var/log/messages /var/log/auth.log /var/log/kern.log; do [ -f "$f" ] && echo "$f"; done
2130
- find /var/log -maxdepth 2 \\( -name "php*.log" -o -name "lsphp*.log" \\) -type f 2>/dev/null
2131
- [ -d /var/log/mg-monitoring ] && find /var/log/mg-monitoring -maxdepth 1 -name "*.log" -type f 2>/dev/null
2132
- for dir in /var/www/*/; do
2133
- [ -d "$dir" ] || continue
2134
- for root in "$dir" "\${dir}html" "\${dir}public_html" "\${dir}public" "\${dir}httpdocs"; do
2135
- [ -d "$root" ] || continue
2136
- [ -f "$root/wp-content/debug.log" ] && echo "$root/wp-content/debug.log"
2137
- [ -d "$root/var/logs" ] && find "$root/var/logs" -maxdepth 1 -name "*.log" -type f 2>/dev/null
2138
- [ -d "$root/log" ] && find "$root/log" -maxdepth 1 -name "*.log" -type f 2>/dev/null
2139
- [ -d "$root/storage/logs" ] && find "$root/storage/logs" -maxdepth 1 -name "*.log" -type f 2>/dev/null
2140
- done
2141
- done
2142
- } | sort -u | while IFS= read -r f; do
2143
- SZ=$(stat -c%s "$f" 2>/dev/null || echo 0)
2144
- MOD=$(stat -c%y "$f" 2>/dev/null | cut -d. -f1)
2145
- if [ "$SZ" -ge 1048576 ] 2>/dev/null; then HR="$(( SZ / 1048576 ))MB"
2146
- elif [ "$SZ" -ge 1024 ] 2>/dev/null; then HR="$(( SZ / 1024 ))KB"
2147
- else HR="\${SZ}B"; fi
2148
- echo "$f $HR $MOD"
2149
- done
2150
- `.trim();
2151
- const result = await sshExec(conn, script, proxy);
2152
- return { content: [{ type: "text", text: result.stdout || "No log files found" }] };
2153
- }
2154
- case "log-read": {
2155
- const logPath = sanitizePath(String(a.path));
2156
- assertAllowedLogPath(logPath);
2157
- const lineCount = Math.min(Math.max(Number(a.lines) || 100, 1), 500);
2158
- const filter = a.filter ? String(a.filter).replace(/'/g, "'\\''") : "";
2159
- const { conn, proxy } = await getServerConnection(String(a.serverId));
2160
- const cmd = filter ? `grep '${filter}' '${logPath}' 2>/dev/null | tail -n ${lineCount}` : `tail -n ${lineCount} '${logPath}' 2>/dev/null`;
2161
- const result = await sshExec(conn, cmd, proxy);
2162
- if (result.exitCode !== 0 && !result.stdout) {
2163
- throw new Error(result.stderr || `Failed to read log: ${logPath}`);
2164
- }
2165
- return { content: [{ type: "text", text: result.stdout || "(empty log file)" }] };
2166
- }
2167
- // ----- Cron Jobs -----
2168
- case "cron-list": {
2169
- const { conn, proxy } = await getServerConnection(a.serverId);
2170
- const user = a.user ? String(a.user) : null;
2171
- const parts = [];
2172
- if (user) {
2173
- const result = await sshExec(conn, `crontab -l -u ${user} 2>/dev/null || echo '(no crontab for ${user})'`, proxy);
2174
- parts.push(`## Crontab for ${user}
2175
- ${result.stdout}`);
2176
- } else {
2177
- const rootCron = await sshExec(conn, `crontab -l 2>/dev/null || echo '(no crontab for root)'`, proxy);
2178
- parts.push(`## Root crontab
2179
- ${rootCron.stdout}`);
2180
- const wwwCron = await sshExec(conn, `crontab -l -u www-data 2>/dev/null || echo '(no crontab for www-data)'`, proxy);
2181
- parts.push(`## www-data crontab
2182
- ${wwwCron.stdout}`);
2183
- const cronD = await sshExec(conn, `for f in /etc/cron.d/*; do [ -f "$f" ] && echo "--- $f ---" && cat "$f" && echo; done 2>/dev/null || echo '(no files in /etc/cron.d/)'`, proxy);
2184
- parts.push(`## /etc/cron.d/
2185
- ${cronD.stdout}`);
2186
- const summary = await sshExec(conn, [
2187
- `echo "## Scheduled directories"`,
2188
- `echo "cron.hourly: $(ls /etc/cron.hourly/ 2>/dev/null | wc -l) scripts"`,
2189
- `echo "cron.daily: $(ls /etc/cron.daily/ 2>/dev/null | wc -l) scripts"`,
2190
- `echo "cron.weekly: $(ls /etc/cron.weekly/ 2>/dev/null | wc -l) scripts"`,
2191
- `echo "cron.monthly: $(ls /etc/cron.monthly/ 2>/dev/null | wc -l) scripts"`
2192
- ].join(" && "), proxy);
2193
- parts.push(summary.stdout);
2194
- }
2195
- return { content: [{ type: "text", text: parts.join("\n\n") }] };
2196
- }
2197
- case "cron-add": {
2198
- if (!a.schedule || !a.command) {
2199
- throw new Error("Both schedule and command are required");
2200
- }
2201
- const { conn, proxy } = await getServerConnection(a.serverId);
2202
- const user = a.user ? String(a.user) : "root";
2203
- const schedule = String(a.schedule).trim();
2204
- const command = String(a.command).trim();
2205
- const commentText = a.comment ? String(a.comment).trim() : "";
2206
- const entry = commentText ? `# ${commentText}
2207
- ${schedule} ${command}` : `${schedule} ${command}`;
2208
- const result = await sshExec(conn, `(crontab -l -u ${user} 2>/dev/null; echo '${entry.replace(/'/g, "'\\''")}') | crontab -u ${user} -`, proxy);
2209
- if (result.exitCode !== 0) {
2210
- throw new Error(result.stderr || "Failed to add cron job");
2211
- }
2212
- return { content: [{ type: "text", text: `Cron job added for ${user}:
2213
- ${schedule} ${command}` }] };
2214
- }
2215
- case "cron-remove": {
2216
- if (!a.commandMatch) {
2217
- throw new Error("commandMatch is required \u2013 pass a (partial) command string to identify the cron entry");
2218
- }
2219
- const { conn, proxy } = await getServerConnection(a.serverId);
2220
- const user = a.user ? String(a.user) : "root";
2221
- const match = String(a.commandMatch).trim();
2222
- const current = await sshExec(conn, `crontab -l -u ${user} 2>/dev/null`, proxy);
2223
- const lines = current.stdout.split("\n");
2224
- const before = lines.length;
2225
- const filtered = lines.filter((line) => {
2226
- const trimmed = line.replace(/^#\s*/, "");
2227
- return !trimmed.includes(match);
2228
- });
2229
- const removed = before - filtered.length;
2230
- if (removed === 0) {
2231
- return { content: [{ type: "text", text: `No cron entries found matching "${match}"` }] };
2232
- }
2233
- const escapedCrontab = filtered.join("\n").replace(/'/g, "'\\''");
2234
- const result = await sshExec(conn, `echo '${escapedCrontab}' | crontab -u ${user} -`, proxy);
2235
- if (result.exitCode !== 0) {
2236
- throw new Error(result.stderr || "Failed to update crontab");
2237
- }
2238
- return { content: [{ type: "text", text: `Removed ${removed} cron entry/entries matching "${match}" from ${user} crontab` }] };
2239
- }
2240
- case "cron-toggle": {
2241
- if (!a.commandMatch) {
2242
- throw new Error("commandMatch is required \u2013 pass a (partial) command string to identify the cron entry");
2243
- }
2244
- const { conn, proxy } = await getServerConnection(a.serverId);
2245
- const user = a.user ? String(a.user) : "root";
2246
- const match = String(a.commandMatch).trim();
2247
- const enable = Boolean(a.enable);
2248
- const current = await sshExec(conn, `crontab -l -u ${user} 2>/dev/null`, proxy);
2249
- const lines = current.stdout.split("\n");
2250
- let toggled = 0;
2251
- const updated = lines.map((line) => {
2252
- if (enable && line.startsWith("#") && line.replace(/^#\s*/, "").includes(match)) {
2253
- toggled++;
2254
- return line.replace(/^#\s*/, "");
2255
- }
2256
- if (!enable && !line.startsWith("#") && line.includes(match)) {
2257
- toggled++;
2258
- return `# ${line}`;
2259
- }
2260
- return line;
2261
- });
2262
- if (toggled === 0) {
2263
- const state = enable ? "disabled" : "enabled";
2264
- return { content: [{ type: "text", text: `No ${state} cron entries found matching "${match}"` }] };
2265
- }
2266
- const escapedCrontab = updated.join("\n").replace(/'/g, "'\\''");
2267
- const result = await sshExec(conn, `echo '${escapedCrontab}' | crontab -u ${user} -`, proxy);
2268
- if (result.exitCode !== 0) {
2269
- throw new Error(result.stderr || "Failed to update crontab");
2270
- }
2271
- const action = enable ? "Enabled" : "Disabled";
2272
- return { content: [{ type: "text", text: `${action} ${toggled} cron entry/entries matching "${match}" in ${user} crontab` }] };
2273
- }
2274
2785
  // ----- Domains (mijn.host) -----
2275
2786
  case "domain-list": {
2276
2787
  const res = await mijnhostFetch("/domains");
@@ -2412,6 +2923,12 @@ ${lines.join("\n")}` }] };
2412
2923
  if (TRIGGER_TOOL_NAMES.has(name)) {
2413
2924
  return handleTriggerTool(name, a, { sshExec, getServerConnection });
2414
2925
  }
2926
+ if (SCRIPT_TOOL_NAMES.has(name)) {
2927
+ return handleScriptTool(name, a);
2928
+ }
2929
+ if (AGENT_TOOL_NAMES.has(name)) {
2930
+ return handleAgentTool(name, a, { supabase, workspaceId: agentWorkspaceId });
2931
+ }
2415
2932
  return { content: [{ type: "text", text: `Unknown tool: ${name}` }] };
2416
2933
  }
2417
2934
  } catch (err) {