-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
380 lines (327 loc) · 12.4 KB
/
main.cpp
File metadata and controls
380 lines (327 loc) · 12.4 KB
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
// ********************************************************************
// EoF MCP Server
// Copyright © 2025 by EoF Software Labs
// SPDX-License-Identifier: GPLv3
// ********************************************************************
#include "myresourcehandler.h"
#include "mysourcecodehandler.h"
#include <IMCPMiddleware.h>
#include <IMCPPromptService.h>
#include <IMCPResourceService.h>
#include <IMCPServer.h>
#include <IMCPServerConfig.h>
#include <IMCPToolService.h>
#include <IMCPTransport.h>
#include <MCPAutoServer.h>
#include <MCPLog.h>
#include <MCPServer.h>
#include <MCPServer_global.h>
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QLocale>
#include <QStandardPaths>
#include <QTranslator>
static MCPAutoServer autoServer;
static inline void StartAutoMCPServer()
{
autoServer.performStart();
}
static inline void StopAutoMCPServer()
{
autoServer.performStop();
}
static inline bool createDirectory(const QDir &dir)
{
if (!dir.exists()) {
QFile::Permissions permissions;
permissions.setFlag(QFile::Permission::ReadOwner, true);
permissions.setFlag(QFile::Permission::ReadGroup, true);
permissions.setFlag(QFile::Permission::WriteOwner, true);
permissions.setFlag(QFile::Permission::WriteGroup, true);
permissions.setFlag(QFile::Permission::ExeOwner, true);
permissions.setFlag(QFile::Permission::ExeGroup, true);
if (!dir.mkpath(dir.absolutePath(), permissions)) {
qWarning("Unable to create directory: %s", qPrintable(dir.absolutePath()));
return false;
}
}
return true;
}
#ifdef LIBMCPServer
QString preferencePath;
#endif
static inline bool createConfigDirectory(const QString &pathName, QDir &target)
{
#ifdef LIBMCPServer
if (preferencePath.isEmpty()) {
preferencePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
}
QDir dir(preferencePath);
#else
// server tools configuration
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
#endif
// fallback to internal resource
if (!QFileInfo::exists(dir.absoluteFilePath(pathName))) {
if (!createDirectory(dir.absoluteFilePath(pathName))) {
return false;
}
}
target = QDir(dir.absoluteFilePath(pathName));
return true;
}
// Helper function to copy directories recursively
static inline bool copyDirectoryRecursively(const QString &sourceDirPath, const QString &targetDirPath)
{
QDir sourceDir(sourceDirPath);
QDir targetDir(targetDirPath);
if (!targetDir.exists()) {
if (!targetDir.mkpath(".")) {
return false;
}
}
QFileInfoList fileInfoList = sourceDir.entryInfoList( //
QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QFileInfo &fileInfo, fileInfoList) {
QString fileName = fileInfo.fileName();
QString sourceFilePath = fileInfo.absoluteFilePath();
QString targetFilePath = targetDirPath + QDir::separator() + fileName;
if (fileInfo.isDir()) {
// Recursively copy subdirectory
if (!copyDirectoryRecursively(sourceFilePath, targetFilePath)) {
return false;
}
} else {
// Copy file
QFile sourceFile(sourceFilePath);
QFile targetFile(targetFilePath);
if (targetFile.exists()) {
if (!targetFile.remove()) {
return false;
}
}
if (!sourceFile.copy(targetFilePath)) {
return false;
}
}
}
return true;
}
static inline bool deployResourceFiles(const QString &resourcePath, const QDir &targetDir)
{
// Ensure target directory exists
if (!targetDir.exists()) {
if (!createDirectory(targetDir)) {
qCritical() << "Failed to create target directory:" << targetDir.absolutePath();
return false;
}
}
bool success = true;
// Iterate through each subdirectory
QDir sourceDir(resourcePath);
QFileInfo sourceInfo(sourceDir.absolutePath());
// Check if the source subdirectory exists
if (!sourceDir.exists()) {
qCritical() << "Source subdirectory does not exist:" << sourceDir.absolutePath();
return false;
}
// Get all files in the subdirectory
QFileInfoList fileInfoList = sourceDir.entryInfoList( //
QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QFileInfo &fileInfo, fileInfoList) {
QString fileName = fileInfo.fileName();
QString sourceFilePath = fileInfo.absoluteFilePath();
QString targetFilePath = targetDir.absoluteFilePath(fileName);
if (fileInfo.isDir()) {
// Handle directories recursively
QDir targetSubDir(targetFilePath);
if (!targetSubDir.exists()) {
if (!targetSubDir.mkpath(".")) {
qWarning() << "Failed to create directory:" << targetFilePath;
success = false;
continue;
}
}
// Copy directory contents recursively
if (!copyDirectoryRecursively(sourceFilePath, targetFilePath)) {
qWarning() << "Failed to copy directory:" << sourceFilePath;
success = false;
}
} else {
// Handle files
QFile sourceFile(sourceFilePath);
QFileInfo sfi(sourceFile.fileName());
QFile targetFile(targetFilePath);
QFileInfo tfi(targetFile.fileName());
// skip existing newer files
if (sfi.lastModified() < tfi.lastModified()) {
continue;
}
// Remove existing file if it exists
if (targetFile.exists()) {
if (!targetFile.remove()) {
qWarning() << "Failed to remove existing file:" << targetFilePath;
success = false;
continue;
}
}
// Copy file
if (!sourceFile.copy(targetFilePath)) {
qWarning() << "Failed to copy file from" << sourceFilePath << "to" << targetFilePath;
success = false;
} else {
qDebug() << "Copied file:" << fileName;
}
}
}
return success;
}
#ifdef LIBMCPServer
extern "C" {
int MCPCORE_EXPORT MCPServerQuit()
{
if (qApp != nullptr) {
qApp->quit();
}
return 0;
}
int MCPCORE_EXPORT MCPSetPreferencePath(const char *path)
{
preferencePath = path;
return 0;
}
int MCPCORE_EXPORT MCPServerStartup( //
const char *appName, //
const char *displayName,
const char *orgName,
const char *orgDomain,
const char *version)
{
const char *argv[] = {"lib"};
int argc = 0;
// Same as in Info.plist
QApplication::setApplicationName(appName ? appName : QStringLiteral("eofmcp"));
QApplication::setApplicationDisplayName(displayName ? displayName : QStringLiteral("EoF MCP Server"));
QApplication::setOrganizationName(orgName ? orgName : QStringLiteral("EoF Software Labs"));
QApplication::setOrganizationDomain(orgDomain ? orgDomain : QStringLiteral("org.eof.tools.eofmcp"));
QApplication::setApplicationVersion(version ? version : QStringLiteral("4.31.6"));
QCoreApplication a(argc, (char **) argv);
a.connect(&a, &QCoreApplication::aboutToQuit, &a, []() { //
StopAutoMCPServer();
});
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "eofmcp_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
// deploy configuration files from internal resource
QDir target;
if (createConfigDirectory(QStringLiteral("Tools"), target)) {
if (!deployResourceFiles(QStringLiteral(":/cfg/Tools"), target)) {
qCritical("Unable to create configuration directory: Tools");
return -1;
}
}
if (createConfigDirectory(QStringLiteral("Prompts"), target)) {
if (!deployResourceFiles(QStringLiteral(":/cfg/Prompts"), target)) {
qCritical("Unable to create configuration directory: Prompts");
return -1;
}
}
if (createConfigDirectory(QStringLiteral("Resources"), target)) {
if (!deployResourceFiles(QStringLiteral(":/cfg/Resources"), target)) {
qCritical("Unable to create configuration directory: Resources");
return -1;
}
}
QObjectList handlers;
// The source code handler has following invokable tools:
// - displayProjectFiles <project_path> [recursive] [sort_by]
// - listSourceFiles <project_path>
// - readSourceFile <file_path>
// - writeSourceFile <file_path> <content> [create_backup]
handlers.append(new SourceCodeHandler(qApp));
// Create a resource Handler object (used for validating MCPResourceWrapper)
// The resource Handler must be created; MCPHandlerResolver will locate it
// via objectName or the "MCPResourceHandlerName" property
// The "MCPResourceHandlerName" property is already set in the MyResourceHandler constructor
handlers.append(new MyResourceHandler(qApp));
// Use automatic startup to load and start the server from the configuration file
// The configuration file is located in the config folder.
StartAutoMCPServer();
return a.exec();
}
} // extern "C"
#endif
#ifndef LIBMCPServer
int main(int argc, char *argv[])
{
// Same as in Info.plist
QApplication::setApplicationName(QStringLiteral("eofmcp"));
QApplication::setApplicationDisplayName(QStringLiteral("EoF MCP Server"));
QApplication::setOrganizationName(QStringLiteral("EoF Software Labs"));
QApplication::setOrganizationDomain(QStringLiteral("org.eof.tools.eofmcp"));
#ifndef XCODE_BUILD
QApplication::setApplicationVersion("4.31.6");
#else
QApplication::setApplicationVersion( //
QStringLiteral("%1.%2").arg(getBundleVersion(), getBuildNumber()));
#endif
QCoreApplication a(argc, argv);
a.connect(&a, &QCoreApplication::aboutToQuit, &a, []() { //
StopAutoMCPServer();
});
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "eofmcp_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
// deploy configuration files from internal resource
QDir target;
if (createConfigDirectory(QStringLiteral("Tools"), target)) {
if (!deployResourceFiles(QStringLiteral(":/cfg/Tools"), target)) {
qCritical("Unable to create configuration directory: Tools");
return -1;
}
}
if (createConfigDirectory(QStringLiteral("Prompts"), target)) {
if (!deployResourceFiles(QStringLiteral(":/cfg/Prompts"), target)) {
qCritical("Unable to create configuration directory: Prompts");
return -1;
}
}
if (createConfigDirectory(QStringLiteral("Resources"), target)) {
if (!deployResourceFiles(QStringLiteral(":/cfg/Resources"), target)) {
qCritical("Unable to create configuration directory: Resources");
return -1;
}
}
QObjectList handlers;
// The source code handler has following invokable tools:
// - displayProjectFiles <project_path> [recursive] [sort_by]
// - listSourceFiles <project_path>
// - readSourceFile <file_path>
// - writeSourceFile <file_path> <content> [create_backup]
handlers.append(new SourceCodeHandler(qApp));
// Create a resource Handler object (used for validating MCPResourceWrapper)
// The resource Handler must be created; MCPHandlerResolver will locate it
// via objectName or the "MCPResourceHandlerName" property
// The "MCPResourceHandlerName" property is already set in the MyResourceHandler constructor
handlers.append(new MyResourceHandler(qApp));
// Use automatic startup to load and start the server from the configuration file
// The configuration file is located in the config folder.
StartAutoMCPServer();
return a.exec();
}
#endif