wxMaxima
Loading...
Searching...
No Matches
McpTools.h
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2026 Gunter Königsmann <wxMaxima@physikbuch.de>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
22#ifndef MCPTOOLS_H
23#define MCPTOOLS_H
24
25#include <nlohmann/json.hpp>
26#include <wx/string.h>
27#include <functional>
28#include <stdexcept>
29#include <string>
30#include <vector>
31
32class Worksheet;
33class Variablespane;
34class GroupCell;
35
47 wxString name;
49 wxString caption;
51 bool visible = false;
58 bool canHide = true;
59};
60
65class McpToolError : public std::runtime_error {
66public:
67 explicit McpToolError(const std::string &msg) : std::runtime_error(msg) {}
68};
69
97class McpTools {
98public:
99 McpTools(Worksheet *worksheet, Variablespane *variablesPane);
100
111 void SetConnectionCheck(std::function<bool()> isConnected)
112 {
113 m_isMaximaConnected = std::move(isConnected);
114 }
115
129 void SetSidebarAccess(std::function<std::vector<McpSidebarInfo>()> list,
130 std::function<bool(const wxString &, bool)> setVisible)
131 {
132 m_listSidebars = std::move(list);
133 m_setSidebarVisible = std::move(setVisible);
134 }
135
137 nlohmann::json ListTools() const;
138
140 nlohmann::json CallTool(const wxString &name,
141 const nlohmann::json &arguments) const;
142
143 nlohmann::json ListCells() const;
144 nlohmann::json ReadCell(const nlohmann::json &arguments) const;
145 nlohmann::json ReadWorksheet() const;
146 nlohmann::json ReadToc() const;
147 nlohmann::json ReadSection(const nlohmann::json &arguments) const;
148 nlohmann::json ReadVariables() const;
155 nlohmann::json EvaluationStatus() const;
156 nlohmann::json WatchVariable(const nlohmann::json &arguments) const;
157 nlohmann::json UnwatchVariable(const nlohmann::json &arguments) const;
163 nlohmann::json SearchCells(const nlohmann::json &arguments) const;
170 nlohmann::json ListSidebars() const;
182 nlohmann::json ShowSidebar(const nlohmann::json &arguments) const;
185 nlohmann::json HideSidebar(const nlohmann::json &arguments) const;
186
189 static constexpr std::size_t MAX_TEXT_LENGTH = 200000;
193 static constexpr std::size_t MAX_SEARCH_MATCHES = 50;
200 static constexpr std::size_t OUTPUT_PREVIEW_LENGTH = 2000;
201
202private:
203 Worksheet *m_worksheet;
204 Variablespane *m_variablesPane;
206 std::function<bool()> m_isMaximaConnected;
208 std::function<std::vector<McpSidebarInfo>()> m_listSidebars;
209 std::function<bool(const wxString &, bool)> m_setSidebarVisible;
210
218 nlohmann::json SetSidebarVisible(const nlohmann::json &arguments,
219 bool show) const;
221 void RequireSidebarAccess() const;
222
224 GroupCell *FindGroupByUUID(const wxString &uuid) const;
226 static wxString GroupTypeName(int groupType);
228 static wxString InputText(GroupCell &cell);
230 static wxString OutputText(GroupCell &cell);
232 nlohmann::json CellSummary(GroupCell &cell, int index) const;
237 wxString CellText(GroupCell &cell) const;
243 GroupCell *CurrentCell() const;
247 bool HasError(GroupCell &cell) const;
254 bool MaximaIsBusy() const;
264 bool IsMaximaConnected() const
265 {
266 return !m_isMaximaConnected || m_isMaximaConnected();
267 }
269 static wxString CapLength(wxString text);
274 static wxString TruncateText(const wxString &text, std::size_t maxLen,
275 bool fromEnd, bool &wasTruncated);
277 static wxString RequireString(const nlohmann::json &arguments,
278 const char *name);
279};
280
281#endif // MCPTOOLS_H
A cell grouping input (and, if there is one, also the output) cell to a foldable item.
Definition: GroupCell.h:88
Thrown by McpTools::CallTool() for an unknown tool name or invalid/missing arguments.
Definition: McpTools.h:65
Implements the read-only "worksheet context" tools the MCP server exposes to an external AI tool (GH ...
Definition: McpTools.h:97
nlohmann::json SearchCells(const nlohmann::json &arguments) const
Finds every cell whose input and/or output contains pattern (a plain substring by default,...
Definition: McpTools.cpp:666
void SetConnectionCheck(std::function< bool()> isConnected)
Lets the owner (McpServer, wired up from wxMaxima – see McpServer::SetConnectionCheck()) plug in a li...
Definition: McpTools.h:111
static constexpr std::size_t MAX_TEXT_LENGTH
A cap on how much text a single response ever carries (read_worksheet/ read_section),...
Definition: McpTools.h:189
nlohmann::json CallTool(const wxString &name, const nlohmann::json &arguments) const
Dispatches one tools/call request by tool name. Throws McpToolError.
Definition: McpTools.cpp:390
nlohmann::json EvaluationStatus() const
Is Maxima actively evaluating a command right now, and if so: which cell, which exact statement withi...
Definition: McpTools.cpp:593
static constexpr std::size_t OUTPUT_PREVIEW_LENGTH
The cap applied to each individual cell's own output when it's one of many being concatenated (ReadWo...
Definition: McpTools.h:200
nlohmann::json HideSidebar(const nlohmann::json &arguments) const
Hide a sidebar. See ShowSidebar() for why this counts as a safe write. Refuses to hide the worksheet ...
Definition: McpTools.cpp:775
nlohmann::json ShowSidebar(const nlohmann::json &arguments) const
Show a sidebar, the same as ticking its View -> Sidebars menu entry.
Definition: McpTools.cpp:771
static constexpr std::size_t MAX_SEARCH_MATCHES
search_cells stops collecting further matches once it hits this many, reporting "truncated" instead –...
Definition: McpTools.h:193
nlohmann::json ListSidebars() const
Which dockable sidebars exist and which are visible right now.
Definition: McpTools.cpp:753
void SetSidebarAccess(std::function< std::vector< McpSidebarInfo >()> list, std::function< bool(const wxString &, bool)> setVisible)
Lets the owner plug in access to wxMaximaFrame's dockable sidebars, for the same reason SetConnection...
Definition: McpTools.h:129
nlohmann::json ListTools() const
The tools/list result: name/description/inputSchema for every tool below.
Definition: McpTools.cpp:167
A "variables" sidepane.
Definition: VariablesPane.h:45
The canvas that contains the spreadsheet the whole program is about.
Definition: Worksheet.h:118
One dockable sidebar, as list_sidebars reports it.
Definition: McpTools.h:45
wxString caption
The translated, human-readable caption the user sees on the pane.
Definition: McpTools.h:49
bool canHide
Can this pane be hidden at all? False for the worksheet itself, which is wxAUI's centre pane: wxMaxim...
Definition: McpTools.h:58
wxString name
The stable, untranslated identifier show_sidebar/hide_sidebar take.
Definition: McpTools.h:47
bool visible
Is this sidebar visible right now?
Definition: McpTools.h:51