mirror of
https://github.com/NohamR/Reclass.git
synced 2026-05-10 19:59:21 +00:00
Add MCP bridge for external tool integration
Embedded JSON-RPC server over named pipes (rcx-mcp) enabling external tools like Claude Code to inspect and manipulate the node tree, read/write hex data, switch sources, and trigger UI actions. Includes stdio adapter (rcx-mcp-stdio) for stdin/stdout transport. Server is stopped by default; user starts via File > Start MCP Server. Also extracts MainWindow class declaration to mainwindow.h and improves type selector popup Esc button styling.
This commit is contained in:
@@ -110,6 +110,12 @@ public:
|
||||
RcxDocument* document() const { return m_doc; }
|
||||
void setEditorFont(const QString& fontName);
|
||||
|
||||
// MCP bridge accessors
|
||||
void setSuppressRefresh(bool v) { m_suppressRefresh = v; }
|
||||
const QVector<SavedSourceEntry>& savedSources() const { return m_savedSources; }
|
||||
int activeSourceIndex() const { return m_activeSourceIdx; }
|
||||
void switchSource(int idx) { switchToSavedSource(idx); }
|
||||
|
||||
signals:
|
||||
void nodeSelected(int nodeIdx);
|
||||
void selectionChanged(int count);
|
||||
|
||||
125
src/main.cpp
125
src/main.cpp
@@ -1,6 +1,6 @@
|
||||
#include "controller.h"
|
||||
#include "mainwindow.h"
|
||||
#include "generator.h"
|
||||
#include "pluginmanager.h"
|
||||
#include "mcp/mcp_bridge.h"
|
||||
#include <QApplication>
|
||||
#include <QMainWindow>
|
||||
#include <QMdiArea>
|
||||
@@ -170,98 +170,7 @@ static void applyGlobalTheme(const rcx::Theme& theme) {
|
||||
|
||||
namespace rcx {
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MainWindow(QWidget* parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void newFile();
|
||||
void newDocument();
|
||||
void selfTest();
|
||||
void openFile();
|
||||
void saveFile();
|
||||
void saveFileAs();
|
||||
|
||||
|
||||
void addNode();
|
||||
void removeNode();
|
||||
void changeNodeType();
|
||||
void renameNodeAction();
|
||||
void duplicateNodeAction();
|
||||
void splitView();
|
||||
void unsplitView();
|
||||
|
||||
void undo();
|
||||
void redo();
|
||||
void about();
|
||||
void setEditorFont(const QString& fontName);
|
||||
void exportCpp();
|
||||
void showTypeAliasesDialog();
|
||||
void editTheme();
|
||||
|
||||
public:
|
||||
// Project Lifecycle API
|
||||
QMdiSubWindow* project_new();
|
||||
QMdiSubWindow* project_open(const QString& path = {});
|
||||
bool project_save(QMdiSubWindow* sub = nullptr, bool saveAs = false);
|
||||
void project_close(QMdiSubWindow* sub = nullptr);
|
||||
|
||||
private:
|
||||
enum ViewMode { VM_Reclass, VM_Rendered };
|
||||
|
||||
QMdiArea* m_mdiArea;
|
||||
QLabel* m_statusLabel;
|
||||
PluginManager m_pluginManager;
|
||||
|
||||
struct SplitPane {
|
||||
QTabWidget* tabWidget = nullptr;
|
||||
RcxEditor* editor = nullptr;
|
||||
QsciScintilla* rendered = nullptr;
|
||||
ViewMode viewMode = VM_Reclass;
|
||||
uint64_t lastRenderedRootId = 0;
|
||||
};
|
||||
|
||||
struct TabState {
|
||||
RcxDocument* doc;
|
||||
RcxController* ctrl;
|
||||
QSplitter* splitter;
|
||||
QVector<SplitPane> panes;
|
||||
int activePaneIdx = 0;
|
||||
};
|
||||
QMap<QMdiSubWindow*, TabState> m_tabs;
|
||||
|
||||
|
||||
void createMenus();
|
||||
void createStatusBar();
|
||||
void showPluginsDialog();
|
||||
QIcon makeIcon(const QString& svgPath);
|
||||
|
||||
RcxController* activeController() const;
|
||||
TabState* activeTab();
|
||||
QMdiSubWindow* createTab(RcxDocument* doc);
|
||||
void updateWindowTitle();
|
||||
|
||||
void setViewMode(ViewMode mode);
|
||||
void updateRenderedView(TabState& tab, SplitPane& pane);
|
||||
void updateAllRenderedPanes(TabState& tab);
|
||||
uint64_t findRootStructForNode(const NodeTree& tree, uint64_t nodeId) const;
|
||||
void setupRenderedSci(QsciScintilla* sci);
|
||||
|
||||
SplitPane createSplitPane(TabState& tab);
|
||||
void applyTheme(const Theme& theme);
|
||||
void applyTabWidgetStyle(QTabWidget* tw);
|
||||
SplitPane* findPaneByTabWidget(QTabWidget* tw);
|
||||
SplitPane* findActiveSplitPane();
|
||||
RcxEditor* activePaneEditor();
|
||||
|
||||
// Workspace dock
|
||||
QDockWidget* m_workspaceDock = nullptr;
|
||||
QTreeView* m_workspaceTree = nullptr;
|
||||
QStandardItemModel* m_workspaceModel = nullptr;
|
||||
void createWorkspaceDock();
|
||||
void rebuildWorkspaceModel();
|
||||
};
|
||||
// MainWindow class declaration is in mainwindow.h
|
||||
|
||||
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
|
||||
setWindowTitle("Reclass");
|
||||
@@ -300,6 +209,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
|
||||
// Load plugins
|
||||
m_pluginManager.LoadPlugins();
|
||||
|
||||
// MCP bridge (stopped by default — user starts via File → Start MCP)
|
||||
m_mcp = new McpBridge(this, this);
|
||||
|
||||
connect(m_mdiArea, &QMdiArea::subWindowActivated,
|
||||
this, [this](QMdiSubWindow*) {
|
||||
updateWindowTitle();
|
||||
@@ -336,6 +248,8 @@ void MainWindow::createMenus() {
|
||||
file->addSeparator();
|
||||
file->addAction(makeIcon(":/vsicons/export.svg"), "Export &C++ Header...", this, &MainWindow::exportCpp);
|
||||
file->addSeparator();
|
||||
m_mcpAction = file->addAction("Start &MCP Server", this, &MainWindow::toggleMcp);
|
||||
file->addSeparator();
|
||||
file->addAction(makeIcon(":/vsicons/close.svg"), "E&xit", QKeySequence(Qt::Key_Close), this, &QMainWindow::close);
|
||||
|
||||
// Edit
|
||||
@@ -839,6 +753,18 @@ void MainWindow::about() {
|
||||
dlg.exec();
|
||||
}
|
||||
|
||||
void MainWindow::toggleMcp() {
|
||||
if (m_mcp->isRunning()) {
|
||||
m_mcp->stop();
|
||||
m_mcpAction->setText("Start &MCP Server");
|
||||
m_statusLabel->setText("MCP server stopped");
|
||||
} else {
|
||||
m_mcp->start();
|
||||
m_mcpAction->setText("Stop &MCP Server");
|
||||
m_statusLabel->setText("MCP server listening on pipe: rcx-mcp");
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::applyTheme(const Theme& theme) {
|
||||
applyGlobalTheme(theme);
|
||||
|
||||
@@ -922,6 +848,15 @@ MainWindow::TabState* MainWindow::activeTab() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
MainWindow::TabState* MainWindow::tabByIndex(int index) {
|
||||
auto subs = m_mdiArea->subWindowList();
|
||||
if (index < 0 || index >= subs.size()) return nullptr;
|
||||
auto* sub = subs[index];
|
||||
if (m_tabs.contains(sub))
|
||||
return &m_tabs[sub];
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void MainWindow::updateWindowTitle() {
|
||||
auto* sub = m_mdiArea->activeSubWindow();
|
||||
if (sub && m_tabs.contains(sub)) {
|
||||
@@ -1465,4 +1400,4 @@ int main(int argc, char* argv[]) {
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "main.moc"
|
||||
// MainWindow Q_OBJECT is now in mainwindow.h; AUTOMOC handles moc generation.
|
||||
|
||||
119
src/mainwindow.h
Normal file
119
src/mainwindow.h
Normal file
@@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
#include "controller.h"
|
||||
#include "pluginmanager.h"
|
||||
#include <QMainWindow>
|
||||
#include <QMdiArea>
|
||||
#include <QMdiSubWindow>
|
||||
#include <QLabel>
|
||||
#include <QSplitter>
|
||||
#include <QTabWidget>
|
||||
#include <QDockWidget>
|
||||
#include <QTreeView>
|
||||
#include <QStandardItemModel>
|
||||
#include <QMap>
|
||||
#include <Qsci/qsciscintilla.h>
|
||||
|
||||
namespace rcx {
|
||||
|
||||
class McpBridge;
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
friend class McpBridge;
|
||||
public:
|
||||
explicit MainWindow(QWidget* parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void newFile();
|
||||
void newDocument();
|
||||
void selfTest();
|
||||
void openFile();
|
||||
void saveFile();
|
||||
void saveFileAs();
|
||||
|
||||
|
||||
void addNode();
|
||||
void removeNode();
|
||||
void changeNodeType();
|
||||
void renameNodeAction();
|
||||
void duplicateNodeAction();
|
||||
void splitView();
|
||||
void unsplitView();
|
||||
|
||||
void undo();
|
||||
void redo();
|
||||
void about();
|
||||
void toggleMcp();
|
||||
void setEditorFont(const QString& fontName);
|
||||
void exportCpp();
|
||||
void showTypeAliasesDialog();
|
||||
void editTheme();
|
||||
|
||||
public:
|
||||
// Project Lifecycle API
|
||||
QMdiSubWindow* project_new();
|
||||
QMdiSubWindow* project_open(const QString& path = {});
|
||||
bool project_save(QMdiSubWindow* sub = nullptr, bool saveAs = false);
|
||||
void project_close(QMdiSubWindow* sub = nullptr);
|
||||
|
||||
private:
|
||||
enum ViewMode { VM_Reclass, VM_Rendered };
|
||||
|
||||
QMdiArea* m_mdiArea;
|
||||
QLabel* m_statusLabel;
|
||||
PluginManager m_pluginManager;
|
||||
McpBridge* m_mcp = nullptr;
|
||||
QAction* m_mcpAction = nullptr;
|
||||
|
||||
struct SplitPane {
|
||||
QTabWidget* tabWidget = nullptr;
|
||||
RcxEditor* editor = nullptr;
|
||||
QsciScintilla* rendered = nullptr;
|
||||
ViewMode viewMode = VM_Reclass;
|
||||
uint64_t lastRenderedRootId = 0;
|
||||
};
|
||||
|
||||
struct TabState {
|
||||
RcxDocument* doc;
|
||||
RcxController* ctrl;
|
||||
QSplitter* splitter;
|
||||
QVector<SplitPane> panes;
|
||||
int activePaneIdx = 0;
|
||||
};
|
||||
QMap<QMdiSubWindow*, TabState> m_tabs;
|
||||
|
||||
|
||||
void createMenus();
|
||||
void createStatusBar();
|
||||
void showPluginsDialog();
|
||||
QIcon makeIcon(const QString& svgPath);
|
||||
|
||||
RcxController* activeController() const;
|
||||
TabState* activeTab();
|
||||
TabState* tabByIndex(int index);
|
||||
int tabCount() const { return m_tabs.size(); }
|
||||
QMdiSubWindow* createTab(RcxDocument* doc);
|
||||
void updateWindowTitle();
|
||||
|
||||
void setViewMode(ViewMode mode);
|
||||
void updateRenderedView(TabState& tab, SplitPane& pane);
|
||||
void updateAllRenderedPanes(TabState& tab);
|
||||
uint64_t findRootStructForNode(const NodeTree& tree, uint64_t nodeId) const;
|
||||
void setupRenderedSci(QsciScintilla* sci);
|
||||
|
||||
SplitPane createSplitPane(TabState& tab);
|
||||
void applyTheme(const Theme& theme);
|
||||
void applyTabWidgetStyle(QTabWidget* tw);
|
||||
SplitPane* findPaneByTabWidget(QTabWidget* tw);
|
||||
SplitPane* findActiveSplitPane();
|
||||
RcxEditor* activePaneEditor();
|
||||
|
||||
// Workspace dock
|
||||
QDockWidget* m_workspaceDock = nullptr;
|
||||
QTreeView* m_workspaceTree = nullptr;
|
||||
QStandardItemModel* m_workspaceModel = nullptr;
|
||||
void createWorkspaceDock();
|
||||
void rebuildWorkspaceModel();
|
||||
};
|
||||
|
||||
} // namespace rcx
|
||||
1040
src/mcp/mcp_bridge.cpp
Normal file
1040
src/mcp/mcp_bridge.cpp
Normal file
File diff suppressed because it is too large
Load Diff
67
src/mcp/mcp_bridge.h
Normal file
67
src/mcp/mcp_bridge.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
#include "mainwindow.h"
|
||||
#include <QObject>
|
||||
#include <QLocalServer>
|
||||
#include <QLocalSocket>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QByteArray>
|
||||
|
||||
namespace rcx {
|
||||
|
||||
class McpBridge : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit McpBridge(MainWindow* mainWindow, QObject* parent = nullptr);
|
||||
~McpBridge() override;
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
bool isRunning() const { return m_server != nullptr; }
|
||||
|
||||
// Call from controller refresh / data change to notify MCP clients
|
||||
void notifyTreeChanged();
|
||||
void notifyDataChanged();
|
||||
|
||||
private:
|
||||
MainWindow* m_mainWindow;
|
||||
QLocalServer* m_server = nullptr;
|
||||
QLocalSocket* m_client = nullptr; // single client for v1
|
||||
QByteArray m_readBuffer;
|
||||
bool m_initialized = false;
|
||||
|
||||
// JSON-RPC plumbing
|
||||
void onNewConnection();
|
||||
void onReadyRead();
|
||||
void onDisconnected();
|
||||
void processLine(const QByteArray& line);
|
||||
void sendJson(const QJsonObject& obj);
|
||||
QJsonObject okReply(const QJsonValue& id, const QJsonObject& result);
|
||||
QJsonObject errReply(const QJsonValue& id, int code, const QString& msg);
|
||||
void sendNotification(const QString& method, const QJsonObject& params = {});
|
||||
|
||||
// MCP method handlers
|
||||
QJsonObject handleInitialize(const QJsonValue& id, const QJsonObject& params);
|
||||
QJsonObject handleToolsList(const QJsonValue& id);
|
||||
QJsonObject handleToolsCall(const QJsonValue& id, const QJsonObject& params);
|
||||
|
||||
// Tool implementations
|
||||
QJsonObject toolProjectState(const QJsonObject& args);
|
||||
QJsonObject toolTreeApply(const QJsonObject& args);
|
||||
QJsonObject toolSourceSwitch(const QJsonObject& args);
|
||||
QJsonObject toolHexRead(const QJsonObject& args);
|
||||
QJsonObject toolHexWrite(const QJsonObject& args);
|
||||
QJsonObject toolStatusSet(const QJsonObject& args);
|
||||
QJsonObject toolUiAction(const QJsonObject& args);
|
||||
|
||||
// Helpers
|
||||
QJsonObject makeTextResult(const QString& text, bool isError = false);
|
||||
QString resolvePlaceholder(const QString& ref,
|
||||
const QHash<QString, uint64_t>& placeholderMap);
|
||||
|
||||
// Smart tab resolution: tabIndex arg → activeTab → first tab → auto-create
|
||||
MainWindow::TabState* resolveTab(const QJsonObject& args);
|
||||
};
|
||||
|
||||
} // namespace rcx
|
||||
@@ -131,10 +131,17 @@ TypeSelectorPopup::TypeSelectorPopup(QWidget* parent)
|
||||
|
||||
row->addStretch();
|
||||
|
||||
m_escLabel = new QLabel(QStringLiteral("Esc"));
|
||||
QPalette dimPal = pal;
|
||||
dimPal.setColor(QPalette::WindowText, theme.textDim);
|
||||
m_escLabel->setPalette(dimPal);
|
||||
m_escLabel = new QToolButton;
|
||||
m_escLabel->setText(QStringLiteral("\u2715 Esc"));
|
||||
m_escLabel->setAutoRaise(true);
|
||||
m_escLabel->setCursor(Qt::PointingHandCursor);
|
||||
m_escLabel->setStyleSheet(QStringLiteral(
|
||||
"QToolButton { color: %1; border: none; padding: 2px 6px; }"
|
||||
"QToolButton:hover { color: %2; }")
|
||||
.arg(theme.textDim.name(), theme.indHoverSpan.name()));
|
||||
connect(m_escLabel, &QToolButton::clicked, this, [this]() {
|
||||
hide();
|
||||
});
|
||||
row->addWidget(m_escLabel);
|
||||
|
||||
layout->addLayout(row);
|
||||
@@ -144,8 +151,7 @@ TypeSelectorPopup::TypeSelectorPopup(QWidget* parent)
|
||||
{
|
||||
m_createBtn = new QToolButton;
|
||||
m_createBtn->setText(QStringLiteral("+ Create new type\u2026"));
|
||||
m_createBtn->setIcon(QIcon(QStringLiteral(":/vsicons/add.svg")));
|
||||
m_createBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
||||
m_createBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
|
||||
m_createBtn->setAutoRaise(true);
|
||||
m_createBtn->setCursor(Qt::PointingHandCursor);
|
||||
m_createBtn->setPalette(pal);
|
||||
|
||||
@@ -39,7 +39,7 @@ protected:
|
||||
|
||||
private:
|
||||
QLabel* m_titleLabel = nullptr;
|
||||
QLabel* m_escLabel = nullptr;
|
||||
QToolButton* m_escLabel = nullptr;
|
||||
QToolButton* m_createBtn = nullptr;
|
||||
QLineEdit* m_filterEdit = nullptr;
|
||||
QListView* m_listView = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user