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 <stdexcept>
28#include <string>
29
30class Worksheet;
31class Variablespane;
32class GroupCell;
33
38class McpToolError : public std::runtime_error {
39public:
40 explicit McpToolError(const std::string &msg) : std::runtime_error(msg) {}
41};
42
70class McpTools {
71public:
72 McpTools(Worksheet *worksheet, Variablespane *variablesPane);
73
75 nlohmann::json ListTools() const;
76
78 nlohmann::json CallTool(const wxString &name,
79 const nlohmann::json &arguments) const;
80
81 nlohmann::json ListCells() const;
82 nlohmann::json ReadCell(const nlohmann::json &arguments) const;
83 nlohmann::json ReadWorksheet() const;
84 nlohmann::json ReadToc() const;
85 nlohmann::json ReadSection(const nlohmann::json &arguments) const;
86 nlohmann::json ReadVariables() const;
87 nlohmann::json WatchVariable(const nlohmann::json &arguments) const;
88 nlohmann::json UnwatchVariable(const nlohmann::json &arguments) const;
94 nlohmann::json SearchCells(const nlohmann::json &arguments) const;
95
98 static constexpr std::size_t MAX_TEXT_LENGTH = 200000;
102 static constexpr std::size_t MAX_SEARCH_MATCHES = 50;
109 static constexpr std::size_t OUTPUT_PREVIEW_LENGTH = 2000;
110
111private:
112 Worksheet *m_worksheet;
113 Variablespane *m_variablesPane;
114
116 GroupCell *FindGroupByUUID(const wxString &uuid) const;
118 static wxString GroupTypeName(int groupType);
120 static wxString InputText(GroupCell &cell);
122 static wxString OutputText(GroupCell &cell);
124 nlohmann::json CellSummary(GroupCell &cell, int index) const;
129 wxString CellText(GroupCell &cell) const;
135 GroupCell *CurrentCell() const;
139 bool HasError(GroupCell &cell) const;
146 bool MaximaIsBusy() const;
148 static wxString CapLength(wxString text);
153 static wxString TruncateText(const wxString &text, std::size_t maxLen,
154 bool fromEnd, bool &wasTruncated);
156 static wxString RequireString(const nlohmann::json &arguments,
157 const char *name);
158};
159
160#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:38
Implements the read-only "worksheet context" tools the MCP server exposes to an external AI tool (GH ...
Definition: McpTools.h:70
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:544
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:98
nlohmann::json CallTool(const wxString &name, const nlohmann::json &arguments) const
Dispatches one tools/call request by tool name. Throws McpToolError.
Definition: McpTools.cpp:331
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:109
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:102
nlohmann::json ListTools() const
The tools/list result: name/description/inputSchema for every tool below.
Definition: McpTools.cpp:170
A "variables" sidepane.
Definition: VariablesPane.h:45
The canvas that contains the spreadsheet the whole program is about.
Definition: Worksheet.h:116