wxMaxima
Loading...
Searching...
No Matches
McpServer.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 MCPSERVER_H
23#define MCPSERVER_H
24
25#include "precomp.h"
26#include "McpTools.h"
27#include <wx/socket.h>
28#include <functional>
29#include <memory>
30#include <string>
31#include <unordered_map>
32
33class Configuration;
34class Worksheet;
35class Variablespane;
36
81class McpServer : public wxEvtHandler {
82public:
83 McpServer(Worksheet *worksheet, Variablespane *variablesPane);
84 ~McpServer() override;
85
89 void ReconcileWithConfig(const Configuration &config);
90
97 void SetConnectionCheck(std::function<bool()> isConnected)
98 {
99 m_tools.SetConnectionCheck(std::move(isConnected));
100 }
101
106 void SetSidebarAccess(std::function<std::vector<McpSidebarInfo>()> list,
107 std::function<bool(const wxString &, bool)> setVisible)
108 {
109 m_tools.SetSidebarAccess(std::move(list), std::move(setVisible));
110 }
111
112private:
114 struct Connection {
119 wxSocketBase *socket = nullptr;
120 std::string buffer;
121 long contentLength = -1;
122 std::size_t bodyStart = 0;
123 bool badRequest = false;
124 };
125
126 void Start(int port);
127 void Stop();
128 void OnServerEvent(wxSocketEvent &event);
129 void OnClientEvent(wxSocketEvent &event);
132 void PumpConnection(wxSocketBase *socket, Connection &conn);
135 bool TryParseHeaders(Connection &conn, std::string &method,
136 std::string &path,
137 std::unordered_map<std::string, std::string> &headers);
138 void HandleRequest(Connection &conn, const std::string &method,
139 const std::string &path,
140 const std::unordered_map<std::string, std::string> &headers,
141 const std::string &body);
144 std::string HandleJsonRpc(const std::string &requestBody);
145 void SendResponse(Connection &conn, int status, const char *statusText,
146 const std::string &contentType, const std::string &body);
147 void CloseConnection(wxSocketBase *socket);
151 static bool OriginAllowed(const std::string &origin);
152
153 McpTools m_tools;
154 std::unique_ptr<wxSocketServer> m_server;
155 std::unordered_map<wxSocketBase *, std::unique_ptr<Connection>> m_connections;
156 int m_listeningPort = -1;
157};
158
159#endif // MCPSERVER_H
The configuration storage for the current worksheet.
Definition: Configuration.h:100
A minimal, opt-in MCP (Model Context Protocol) server exposing the current worksheet as read-only con...
Definition: McpServer.h:81
void SetConnectionCheck(std::function< bool()> isConnected)
Plugs in a live "is Maxima actually connected right now" query – see McpTools::SetConnectionCheck() f...
Definition: McpServer.h:97
void ReconcileWithConfig(const Configuration &config)
Starts/stops listening to match the current configuration. Call this once after construction and agai...
Definition: McpServer.cpp:61
void SetSidebarAccess(std::function< std::vector< McpSidebarInfo >()> list, std::function< bool(const wxString &, bool)> setVisible)
Plugs in access to the dockable sidebars, for list_sidebars/ show_sidebar/hide_sidebar – see McpTools...
Definition: McpServer.h:106
Implements the read-only "worksheet context" tools the MCP server exposes to an external AI tool (GH ...
Definition: McpTools.h:97
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
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
A "variables" sidepane.
Definition: VariablesPane.h:45
The canvas that contains the spreadsheet the whole program is about.
Definition: Worksheet.h:118