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 <memory>
29#include <string>
30#include <unordered_map>
31
32class Configuration;
33class Worksheet;
34class Variablespane;
35
80class McpServer : public wxEvtHandler {
81public:
82 McpServer(Worksheet *worksheet, Variablespane *variablesPane);
83 ~McpServer() override;
84
88 void ReconcileWithConfig(const Configuration &config);
89
90private:
92 struct Connection {
97 wxSocketBase *socket = nullptr;
98 std::string buffer;
99 long contentLength = -1;
100 std::size_t bodyStart = 0;
101 bool badRequest = false;
102 };
103
104 void Start(int port);
105 void Stop();
106 void OnServerEvent(wxSocketEvent &event);
107 void OnClientEvent(wxSocketEvent &event);
110 void PumpConnection(wxSocketBase *socket, Connection &conn);
113 bool TryParseHeaders(Connection &conn, std::string &method,
114 std::string &path,
115 std::unordered_map<std::string, std::string> &headers);
116 void HandleRequest(Connection &conn, const std::string &method,
117 const std::string &path,
118 const std::unordered_map<std::string, std::string> &headers,
119 const std::string &body);
122 std::string HandleJsonRpc(const std::string &requestBody);
123 void SendResponse(Connection &conn, int status, const char *statusText,
124 const std::string &contentType, const std::string &body);
125 void CloseConnection(wxSocketBase *socket);
129 static bool OriginAllowed(const std::string &origin);
130
131 McpTools m_tools;
132 std::unique_ptr<wxSocketServer> m_server;
133 std::unordered_map<wxSocketBase *, std::unique_ptr<Connection>> m_connections;
134 int m_listeningPort = -1;
135};
136
137#endif // MCPSERVER_H
The configuration storage for the current worksheet.
Definition: Configuration.h:98
A minimal, opt-in MCP (Model Context Protocol) server exposing the current worksheet as read-only con...
Definition: McpServer.h:80
void ReconcileWithConfig(const Configuration &config)
Starts/stops listening to match the current configuration. Call this once after construction and agai...
Definition: McpServer.cpp:61
Implements the read-only "worksheet context" tools the MCP server exposes to an external AI tool (GH ...
Definition: McpTools.h:70
A "variables" sidepane.
Definition: VariablesPane.h:45
The canvas that contains the spreadsheet the whole program is about.
Definition: Worksheet.h:116