1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
|
// Copyright (C) 2025 David M. Cotter
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "mcpcommands.h"
#include "issuesmanager.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/documentmodel.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <coreplugin/session.h>
#include <debugger/debuggerruncontrol.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/runcontrol.h>
#include <projectexplorer/target.h>
// #include <utils/fileutils.h>
#include <utils/id.h>
#include <QApplication>
#include <QFile>
#include <QLoggingCategory>
#include <QProcess>
#include <QThread>
#include <QTimer>
Q_LOGGING_CATEGORY(mcpCommands, "qtc.mcpserver.commands", QtWarningMsg)
namespace Mcp::Internal {
McpCommands::McpCommands(QObject *parent)
: QObject(parent)
, m_sessionLoadResult(false)
{
// Connect signal-slot for session loading
connect(
this,
&McpCommands::sessionLoadRequested,
this,
&McpCommands::handleSessionLoadRequest,
Qt::QueuedConnection);
// Initialize default method timeouts (in seconds)
m_methodTimeouts["debug"] = 60;
m_methodTimeouts["build"] = 1200; // 20 minutes
m_methodTimeouts["run_project"] = 60;
m_methodTimeouts["load_session"] = 120;
m_methodTimeouts["clean_project"] = 300; // 5 minutes
// Initialize issues manager
m_issuesManager = new IssuesManager(this);
}
bool McpCommands::build()
{
if (!hasValidProject()) {
qCDebug(mcpCommands) << "No valid project available for building";
return false;
}
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
qCDebug(mcpCommands) << "No current project";
return false;
}
ProjectExplorer::Target *target = project->activeTarget();
if (!target) {
qCDebug(mcpCommands) << "No active target";
return false;
}
ProjectExplorer::BuildConfiguration *buildConfig = target->activeBuildConfiguration();
if (!buildConfig) {
qCDebug(mcpCommands) << "No active build configuration";
return false;
}
qCDebug(mcpCommands) << "Starting build for project:" << project->displayName();
// Trigger build
ProjectExplorer::BuildManager::buildProjectWithoutDependencies(project);
return true;
}
QString McpCommands::debug()
{
QStringList results;
results.append("=== DEBUG ATTEMPT ===");
if (!hasValidProject()) {
results.append("ERROR: No valid project available for debugging");
return results.join("\n");
}
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
results.append("ERROR: No current project");
return results.join("\n");
}
ProjectExplorer::Target *target = project->activeTarget();
if (!target) {
results.append("ERROR: No active target");
return results.join("\n");
}
ProjectExplorer::RunConfiguration *runConfig = target->activeRunConfiguration();
if (!runConfig) {
results.append("ERROR: No active run configuration available for debugging");
return results.join("\n");
}
results.append("Project: " + project->displayName());
results.append("Run configuration: " + runConfig->displayName());
results.append("");
// Trigger debug action on main thread
results.append("=== STARTING DEBUG SESSION ===");
Core::ActionManager *actionManager = Core::ActionManager::instance();
if (actionManager) {
// Try multiple common debug action IDs
QStringList debugActionIds
= {"Debugger.StartDebugging",
"ProjectExplorer.StartDebugging",
"Debugger.Debug",
"ProjectExplorer.Debug",
"Debugger.StartDebuggingOfStartupProject",
"ProjectExplorer.StartDebuggingOfStartupProject"};
bool debugTriggered = false;
for (const QString &debugActionId : debugActionIds) {
results.append("Trying debug action: " + debugActionId);
Core::Command *command = actionManager->command(Utils::Id::fromString(debugActionId));
if (command && command->action()) {
results.append("Found debug action, triggering...");
command->action()->trigger();
results.append("Debug action triggered successfully");
debugTriggered = true;
break;
} else {
results.append("Debug action not found: " + debugActionId);
}
}
if (!debugTriggered) {
results.append("ERROR: No debug action found among tried IDs");
return results.join("\n");
}
} else {
results.append("ERROR: ActionManager not available");
return results.join("\n");
}
results.append("Debug session initiated successfully!");
results.append("The debugger is now starting in the background.");
results.append("Check Qt Creator's debugger output for progress updates.");
results.append("NOTE: The debug session will continue running asynchronously.");
results.append("");
results.append("=== DEBUG RESULT ===");
results.append("Debug command completed.");
return results.join("\n");
}
QString McpCommands::stopDebug()
{
QStringList results;
results.append("=== STOP DEBUGGING ===");
// Use ActionManager to trigger the "Stop Debugging" action
Core::ActionManager *actionManager = Core::ActionManager::instance();
if (!actionManager) {
results.append("ERROR: ActionManager not available");
return results.join("\n");
}
// Try different possible action IDs for stopping debugging
QStringList stopActionIds
= {"Debugger.StopDebugger",
"Debugger.Stop",
"ProjectExplorer.StopDebugging",
"ProjectExplorer.Stop",
"Debugger.StopDebugging"};
bool actionTriggered = false;
for (const QString &actionId : stopActionIds) {
results.append("Trying stop debug action: " + actionId);
Core::Command *command = actionManager->command(Utils::Id::fromString(actionId));
if (command && command->action()) {
results.append("Found stop debug action, triggering...");
command->action()->trigger();
results.append("Stop debug action triggered successfully");
actionTriggered = true;
break;
} else {
results.append("Stop debug action not found: " + actionId);
}
}
if (!actionTriggered) {
results.append("WARNING: No stop debug action found among tried IDs");
results.append(
"You may need to stop debugging manually from Qt Creator's debugger interface");
}
results.append("");
results.append("=== STOP DEBUG RESULT ===");
results.append("Stop debug command completed.");
return results.join("\n");
}
QString McpCommands::getVersion()
{
return QCoreApplication::applicationVersion();
}
QString McpCommands::getBuildStatus()
{
QStringList results;
results.append("=== BUILD STATUS ===");
// Check if build is currently running
if (ProjectExplorer::BuildManager::isBuilding()) {
results.append("Building: 50%");
results.append("Status: Build in progress");
results.append("Current step: Compiling");
} else {
results.append("Building: 0%");
results.append("Status: Not building");
}
results.append("");
results.append("=== BUILD STATUS RESULT ===");
results.append("Build status retrieved successfully.");
return results.join("\n");
}
bool McpCommands::openFile(const QString &path)
{
if (path.isEmpty()) {
qCDebug(mcpCommands) << "Empty file path provided";
return false;
}
Utils::FilePath filePath = Utils::FilePath::fromString(path);
if (!filePath.exists()) {
qCDebug(mcpCommands) << "File does not exist:" << path;
return false;
}
qCDebug(mcpCommands) << "Opening file:" << path;
Core::EditorManager::openEditor(filePath);
return true;
}
QStringList McpCommands::listProjects()
{
QStringList projects;
QList<ProjectExplorer::Project *> projectList = ProjectExplorer::ProjectManager::projects();
for (ProjectExplorer::Project *project : projectList) {
projects.append(project->displayName());
}
qCDebug(mcpCommands) << "Found projects:" << projects;
return projects;
}
QStringList McpCommands::listBuildConfigs()
{
QStringList configs;
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
qCDebug(mcpCommands) << "No current project";
return configs;
}
ProjectExplorer::Target *target = project->activeTarget();
if (!target) {
qCDebug(mcpCommands) << "No active target";
return configs;
}
QList<ProjectExplorer::BuildConfiguration *> buildConfigs = target->buildConfigurations();
for (ProjectExplorer::BuildConfiguration *config : buildConfigs) {
configs.append(config->displayName());
}
qCDebug(mcpCommands) << "Found build configurations:" << configs;
return configs;
}
bool McpCommands::switchToBuildConfig(const QString &name)
{
if (name.isEmpty()) {
qCDebug(mcpCommands) << "Empty build configuration name provided";
return false;
}
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
qCDebug(mcpCommands) << "No current project";
return false;
}
ProjectExplorer::Target *target = project->activeTarget();
if (!target) {
qCDebug(mcpCommands) << "No active target";
return false;
}
QList<ProjectExplorer::BuildConfiguration *> buildConfigs = target->buildConfigurations();
for (ProjectExplorer::BuildConfiguration *config : buildConfigs) {
if (config->displayName() == name) {
qCDebug(mcpCommands) << "Switching to build configuration:" << name;
target->setActiveBuildConfiguration(config, ProjectExplorer::SetActive::Cascade);
return true;
}
}
qCDebug(mcpCommands) << "Build configuration not found:" << name;
return false;
}
bool McpCommands::quit()
{
qCDebug(mcpCommands) << "Starting graceful quit process...";
// Check if debugging is currently active
bool debuggingActive = isDebuggingActive();
qCDebug(mcpCommands) << "Debug session check result:" << debuggingActive;
if (debuggingActive) {
qCDebug(mcpCommands) << "Debug session detected, attempting to stop debugging gracefully...";
// Perform debugging cleanup synchronously (but using non-blocking timers)
return performDebuggingCleanupSync();
} else {
qCDebug(mcpCommands) << "No active debug session detected, quitting immediately...";
QApplication::quit();
return true;
}
}
bool McpCommands::performDebuggingCleanupSync()
{
qCDebug(mcpCommands) << "Starting synchronous debugging cleanup process...";
// Step 1: Try to stop debugging gracefully
QString stopResult = stopDebug();
qCDebug(mcpCommands) << "Stop debug result:" << stopResult;
// Step 2: Wait up to 10 seconds for debugging to stop (using event loop)
QEventLoop stopLoop;
QTimer stopTimer;
stopTimer.setSingleShot(true);
QObject::connect(&stopTimer, &QTimer::timeout, &stopLoop, &QEventLoop::quit);
// Check every second if debugging has stopped
QTimer checkTimer;
QObject::connect(&checkTimer, &QTimer::timeout, [this, &stopLoop, &checkTimer]() {
if (!isDebuggingActive()) {
qCDebug(mcpCommands) << "Debug session stopped successfully";
checkTimer.stop();
stopLoop.quit();
}
});
checkTimer.start(1000); // Check every second
stopTimer.start(10000); // Maximum 10 seconds
stopLoop.exec(); // Wait for either success or timeout
checkTimer.stop();
// Step 3: If still debugging, try abort debugging
if (isDebuggingActive()) {
qCDebug(mcpCommands) << "Still debugging after stop, attempting abort debugging...";
QString abortResult = abortDebug();
qCDebug(mcpCommands) << "Abort debug result:" << abortResult;
// Wait up to 5 seconds for abort to take effect
QEventLoop abortLoop;
QTimer abortTimer;
abortTimer.setSingleShot(true);
QObject::connect(&abortTimer, &QTimer::timeout, &abortLoop, &QEventLoop::quit);
QTimer abortCheckTimer;
QObject::connect(&abortCheckTimer, &QTimer::timeout, [this, &abortLoop, &abortCheckTimer]() {
if (!isDebuggingActive()) {
qCDebug(mcpCommands) << "Debug session aborted successfully";
abortCheckTimer.stop();
abortLoop.quit();
}
});
abortCheckTimer.start(1000); // Check every second
abortTimer.start(5000); // Maximum 5 seconds
abortLoop.exec(); // Wait for either success or timeout
abortCheckTimer.stop();
}
// Step 4: If still debugging, try to kill debugged processes
if (isDebuggingActive()) {
qCDebug(mcpCommands) << "Still debugging after abort, attempting to kill debugged processes...";
bool killResult = killDebuggedProcesses();
qCDebug(mcpCommands) << "Kill debugged processes result:" << killResult;
// Wait up to 5 seconds for kill to take effect
QEventLoop killLoop;
QTimer killTimer;
killTimer.setSingleShot(true);
QObject::connect(&killTimer, &QTimer::timeout, &killLoop, &QEventLoop::quit);
QTimer killCheckTimer;
QObject::connect(&killCheckTimer, &QTimer::timeout, [this, &killLoop, &killCheckTimer]() {
if (!isDebuggingActive()) {
qCDebug(mcpCommands) << "Debugged processes killed successfully";
killCheckTimer.stop();
killLoop.quit();
}
});
killCheckTimer.start(1000); // Check every second
killTimer.start(5000); // Maximum 5 seconds
killLoop.exec(); // Wait for either success or timeout
killCheckTimer.stop();
}
// Step 5: Final timeout - wait up to configured timeout
if (isDebuggingActive()) {
int timeoutSeconds = getMethodTimeout("stop_debug");
if (timeoutSeconds < 0)
timeoutSeconds = 30; // Default 30 seconds
qCDebug(mcpCommands) << "Still debugging, waiting up to" << timeoutSeconds
<< "seconds for final timeout...";
QEventLoop finalLoop;
QTimer finalTimer;
finalTimer.setSingleShot(true);
QObject::connect(&finalTimer, &QTimer::timeout, &finalLoop, &QEventLoop::quit);
QTimer finalCheckTimer;
QObject::connect(&finalCheckTimer, &QTimer::timeout, [this, &finalLoop, &finalCheckTimer]() {
if (!isDebuggingActive()) {
qCDebug(mcpCommands) << "Debug session finally stopped";
finalCheckTimer.stop();
finalLoop.quit();
}
});
finalCheckTimer.start(1000); // Check every second
finalTimer.start(timeoutSeconds * 1000); // Maximum configured timeout
finalLoop.exec(); // Wait for either success or timeout
finalCheckTimer.stop();
}
// Step 6: Final check - determine success or failure
bool success = !isDebuggingActive();
if (success) {
qCDebug(mcpCommands) << "Debug session cleanup completed successfully, quitting Qt Creator...";
QApplication::quit();
return true;
} else {
qCDebug(mcpCommands) << "ERROR: Failed to stop debugged application after all attempts - NOT quitting "
"Qt Creator";
return false; // Don't quit Qt Creator
}
}
void McpCommands::performDebuggingCleanup()
{
// This method is kept for backward compatibility but should not be used
qCDebug(mcpCommands) << "performDebuggingCleanup called - this method is deprecated";
}
bool McpCommands::isDebuggingActive()
{
// Check if debugging is currently active by looking at debugger actions
Core::ActionManager *actionManager = Core::ActionManager::instance();
if (!actionManager) {
return false;
}
// Try different possible action IDs for checking if debugging is active
QStringList stopActionIds
= {"Debugger.Stop", "Debugger.StopDebugger", "ProjectExplorer.StopDebugging"};
for (const QString &actionId : stopActionIds) {
Core::Command *command = actionManager->command(Utils::Id::fromString(actionId));
if (command && command->action() && command->action()->isEnabled()) {
qCDebug(mcpCommands) << "Debug session is active (Stop action enabled):" << actionId;
return true;
}
}
// Also check "Abort Debugging" action
QStringList abortActionIds
= {"Debugger.Abort", "Debugger.AbortDebugger", "ProjectExplorer.AbortDebugging"};
for (const QString &actionId : abortActionIds) {
Core::Command *command = actionManager->command(Utils::Id::fromString(actionId));
if (command && command->action() && command->action()->isEnabled()) {
qCDebug(mcpCommands) << "Debug session is active (Abort action enabled):" << actionId;
return true;
}
}
qCDebug(mcpCommands) << "No active debug session detected";
return false;
}
QString McpCommands::abortDebug()
{
qCDebug(mcpCommands) << "Attempting to abort debug session...";
// Use ActionManager to trigger the "Abort Debugging" action
Core::ActionManager *actionManager = Core::ActionManager::instance();
if (!actionManager) {
return "ERROR: ActionManager not available";
}
// Try different possible action IDs for aborting debugging
QStringList abortActionIds
= {"Debugger.Abort",
"Debugger.AbortDebugger",
"ProjectExplorer.AbortDebugging",
"Debugger.AbortDebug"};
for (const QString &actionId : abortActionIds) {
qCDebug(mcpCommands) << "Trying abort debug action:" << actionId;
Core::Command *command = actionManager->command(Utils::Id::fromString(actionId));
if (command && command->action() && command->action()->isEnabled()) {
qCDebug(mcpCommands) << "Found abort debug action, triggering...";
command->action()->trigger();
return "Abort debug action triggered successfully: " + actionId;
}
}
return "Abort debug action not found or not enabled";
}
bool McpCommands::killDebuggedProcesses()
{
qCDebug(mcpCommands) << "Attempting to kill debugged processes...";
// This is a simplified implementation
// In a real scenario, you'd need to:
// 1. Get the list of processes being debugged from the debugger
// 2. Kill each process appropriately
// For now, we'll try to find and kill any processes that might be debugged
// This is platform-specific and would need proper implementation
// TODO: Implement proper process killing for debugged applications
// This could involve:
// - Finding the debugged process PID
// - Using platform-specific kill commands
// - Handling different types of debugged processes (local, remote, etc.)
return true; // Simplified for now - always return true
}
QString McpCommands::getCurrentProject()
{
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (project) {
return project->displayName();
}
return QString();
}
QString McpCommands::getCurrentBuildConfig()
{
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
return QString();
}
ProjectExplorer::Target *target = project->activeTarget();
if (!target) {
return QString();
}
ProjectExplorer::BuildConfiguration *buildConfig = target->activeBuildConfiguration();
if (buildConfig) {
return buildConfig->displayName();
}
return QString();
}
bool McpCommands::runProject()
{
if (!hasValidProject()) {
qCDebug(mcpCommands) << "No valid project available for running";
return false;
}
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
qCDebug(mcpCommands) << "No current project";
return false;
}
ProjectExplorer::Target *target = project->activeTarget();
if (!target) {
qCDebug(mcpCommands) << "No active target";
return false;
}
ProjectExplorer::RunConfiguration *runConfig = target->activeRunConfiguration();
if (!runConfig) {
qCDebug(mcpCommands) << "No active run configuration available for running";
return false;
}
qCDebug(mcpCommands) << "Running project:" << project->displayName();
// Use ActionManager to trigger the "Run" action
Core::ActionManager *actionManager = Core::ActionManager::instance();
if (!actionManager) {
qCDebug(mcpCommands) << "ActionManager not available";
return false;
}
// Try different possible action IDs for running
QStringList runActionIds
= {"ProjectExplorer.Run", "ProjectExplorer.RunProject", "ProjectExplorer.RunStartupProject"};
bool actionTriggered = false;
for (const QString &actionId : runActionIds) {
Core::Command *command = actionManager->command(Utils::Id::fromString(actionId));
if (command && command->action()) {
qCDebug(mcpCommands) << "Triggering run action:" << actionId;
command->action()->trigger();
actionTriggered = true;
break;
}
}
if (!actionTriggered) {
qCDebug(mcpCommands) << "No run action found, falling back to RunControl method";
// Fallback: Create a RunControl and start it
ProjectExplorer::RunControl *runControl = new ProjectExplorer::RunControl(
Utils::Id("Desktop"));
runControl->copyDataFromRunConfiguration(runConfig);
runControl->start();
}
return true;
}
bool McpCommands::cleanProject()
{
if (!hasValidProject()) {
qCDebug(mcpCommands) << "No valid project available for cleaning";
return false;
}
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
ProjectExplorer::Target *target = project->activeTarget();
if (target) {
ProjectExplorer::BuildConfiguration *buildConfig = target->activeBuildConfiguration();
if (buildConfig) {
qCDebug(mcpCommands) << "Cleaning project:" << project->displayName();
ProjectExplorer::BuildManager::cleanProjectWithoutDependencies(project);
return true;
}
}
qCDebug(mcpCommands) << "No build configuration available for cleaning";
return false;
}
QStringList McpCommands::listOpenFiles()
{
QStringList files;
QList<Core::IDocument *> documents = Core::DocumentModel::openedDocuments();
for (Core::IDocument *doc : documents) {
files.append(doc->filePath().toUserOutput());
}
qCDebug(mcpCommands) << "Open files:" << files;
return files;
}
bool McpCommands::hasValidProject() const
{
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
return false;
}
ProjectExplorer::Target *target = project->activeTarget();
if (!target) {
return false;
}
return true;
}
QStringList McpCommands::listSessions()
{
QStringList sessions = Core::SessionManager::sessions();
qCDebug(mcpCommands) << "Available sessions:" << sessions;
return sessions;
}
QString McpCommands::getCurrentSession()
{
QString session = Core::SessionManager::activeSession();
qCDebug(mcpCommands) << "Current session:" << session;
return session;
}
bool McpCommands::loadSession(const QString &sessionName)
{
if (sessionName.isEmpty()) {
qCDebug(mcpCommands) << "Empty session name provided";
return false;
}
// Check if the session exists before trying to load it
QStringList availableSessions = Core::SessionManager::sessions();
if (!availableSessions.contains(sessionName)) {
qCDebug(mcpCommands) << "Session does not exist:" << sessionName;
qCDebug(mcpCommands) << "Available sessions:" << availableSessions;
return false;
}
qCDebug(mcpCommands) << "Loading session:" << sessionName;
// Use a safer approach - check if we're already in the target session
QString currentSession = Core::SessionManager::activeSession();
if (currentSession == sessionName) {
qCDebug(mcpCommands) << "Already in session:" << sessionName;
return true;
}
// Try to load the session using QTimer to avoid blocking
QTimer::singleShot(0, [sessionName]() {
qCDebug(mcpCommands) << "Attempting to load session:" << sessionName;
bool success = Core::SessionManager::loadSession(sessionName);
qCDebug(mcpCommands) << "Session load result:" << success;
});
qCDebug(mcpCommands) << "Session loading initiated asynchronously";
return true; // Return true to indicate the request was accepted
}
void McpCommands::handleSessionLoadRequest(const QString &sessionName)
{
qCDebug(mcpCommands) << "Handling session load request on main thread:" << sessionName;
// Load session on main thread
bool success = Core::SessionManager::loadSession(sessionName);
m_sessionLoadResult = success;
if (success) {
qCDebug(mcpCommands) << "Session loaded successfully on main thread:" << sessionName;
} else {
qCDebug(mcpCommands) << "Failed to load session on main thread:" << sessionName;
}
}
bool McpCommands::saveSession()
{
qCDebug(mcpCommands) << "Saving current session";
bool successB = Core::SessionManager::saveSession();
if (successB) {
qCDebug(mcpCommands) << "Successfully saved session";
} else {
qCDebug(mcpCommands) << "Failed to save session";
}
return successB;
}
QStringList McpCommands::listIssues()
{
qCDebug(mcpCommands) << "Listing issues from Qt Creator's Issues panel";
if (!m_issuesManager) {
qCDebug(mcpCommands) << "IssuesManager not initialized";
return QStringList() << "ERROR:Issues manager not initialized";
}
QStringList issues = m_issuesManager->getCurrentIssues();
// Add project status information for context
if (ProjectExplorer::BuildManager::isBuilding()) {
issues.prepend("INFO:Build in progress - issues may not be current");
}
qCDebug(mcpCommands) << "Found" << issues.size() << "issues total";
return issues;
}
QString McpCommands::getMethodMetadata()
{
QStringList results;
results.append("=== METHOD METADATA ===");
results.append("");
// Get all methods with their current timeout settings
QStringList allMethods
= {"build",
"debug",
"run_project",
"clean_project",
"load_session",
"get_version",
"list_projects",
"list_build_configs",
"get_current_project",
"get_current_build_config",
"quit",
"list_open_files",
"list_sessions",
"get_current_session",
"save_session",
"list_issues",
"get_method_metadata",
"set_method_metadata",
"stop_debug"};
results.append("Available methods and their timeout settings:");
results.append("");
for (const QString &method : allMethods) {
int timeout = getMethodTimeout(method);
QString timeoutStr = timeout >= 0 ? QString::number(timeout) + " seconds"
: QString("default");
results.append(QString(" %1: %2").arg(method, -20).arg(timeoutStr));
}
results.append("");
results.append("=== METHOD DESCRIPTIONS ===");
results.append("");
// Add descriptions for key methods
results.append("build: Compile the current project");
results.append("debug: Start debugging the current project");
results.append("stop_debug: Stop the current debug session");
results.append("run_project: Run the current project");
results.append("clean_project: Clean build artifacts");
results.append("list_issues: List current build issues and warnings");
results.append("get_method_metadata: Get metadata about all methods");
results.append("set_method_metadata: Configure timeout values for methods");
results.append("");
results.append("=== METADATA COMPLETE ===");
return results.join("\n");
}
QString McpCommands::setMethodMetadata(const QString &method, int timeoutSeconds)
{
QStringList results;
results.append("=== SET METHOD METADATA ===");
if (method.isEmpty()) {
results.append("ERROR: Method name cannot be empty");
return results.join("\n");
}
if (timeoutSeconds < 0) {
results.append("ERROR: Timeout cannot be negative");
return results.join("\n");
}
// List of valid methods that support timeout configuration
QStringList validMethods = {"debug", "build", "run_project", "load_session", "clean_project"};
if (!validMethods.contains(method)) {
results.append("ERROR: Method '" + method + "' does not support timeout configuration");
results.append("Valid methods: " + validMethods.join(", "));
return results.join("\n");
}
// Store the new timeout value
int oldTimeout = m_methodTimeouts.value(method, -1);
m_methodTimeouts[method] = timeoutSeconds;
results.append("Method: " + method);
results.append(
"Previous timeout: "
+ (oldTimeout >= 0 ? QString::number(oldTimeout) + " seconds" : QString("not set")));
results.append("New timeout: " + QString::number(timeoutSeconds) + " seconds");
results.append("");
results.append("Timeout updated successfully!");
results.append("Note: This change affects the timeout hints shown in method responses.");
results.append(
"The actual operation timeouts are still controlled by Qt Creator's internal mechanisms.");
results.append("");
results.append("=== SET METHOD METADATA RESULT ===");
results.append("Method metadata update completed.");
return results.join("\n");
}
int McpCommands::getMethodTimeout(const QString &method) const
{
return m_methodTimeouts.value(method, -1);
}
// handleSessionLoadRequest method removed - using direct session loading instead
} // namespace Mcp::Internal
|