Upload Source/Plugin/McpServer.cpp
Browse files1234561 unmodified line8910111213141545 unmodified lines6162636465666726 unmodified lines9495969798991001015 unmodified lines1071081091101111121137 unmodified lines12112212312412512612712812913013113213313413513613713813914014114214314411 unmodified lines156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209#include "McpServer.h"#include "HelperConnection.h"#include "../Shared/IPCProtocol.h"#define CPPHTTPLIB_THREAD_POOL_COUNT 2#include "httplib.h"1 unmodified lineusing json = nlohmann::json;McpServer::McpServer(int port, HelperConnection& helper): server(std::make_unique<httplib::Server>()), port(port), helper(helper){setupRoutes();}45 unmodified lines{"status", "ok"},{"port", port},{"initialized", initialized},{"helper", helper.isHelperRunning() ? "connected" : "disconnected"}};res.set_content(health.dump(), "application/json");});26 unmodified linesbool McpServer::isRunning() const { return running.load(); }// --- Protocol Handlers ---std::string McpServer::handleInitialize(const std::string& requestBody){auto body = json::parse(requestBody);5 unmodified lines{"result", {{"protocolVersion","2025-06-18"},{"capabilities",{{"tools",{{"listChanged",false}}}}},{"serverInfo",{{"name","PluginBridge"},{"version","0.2.0"}}}}}};return response.dump();7 unmodified lines{"id", body.value("id", json(nullptr))},{"result",{{"tools", json::array({{{"name","list_plugins"},{"description","List loaded plugin"},{"inputSchema",{{"type","object"},{"properties",json::object()}}}},{{"name","search_param"},{"description","Search params by keyword"},{"inputSchema",{{"type","object"},{"properties",{{"plugin",{{"type","string"}}},{"keyword",{{"type","string"},{"description","e.g. 'gain', 'band 1', 'cutoff'"}}}}},{"required",json::array({"plugin","keyword"})}}}},{{"name","get_params"},{"description","Get param values by ID"},{"inputSchema",{{"type","object"},{"properties",{{"plugin",{{"type","string"}}},{"ids",{{"type","array"},{"items",{{"type","integer"}}}}}}},{"required",json::array({"plugin","ids"})}}}},{{"name","set_params"},{"description","Set params (batch, 0.0-1.0)"},{"inputSchema",{{"type","object"},{"properties",{{"plugin",{{"type","string"}}},{"values",{{"type","object"},{"description","id→value"}}}}},{"required",json::array({"plugin","values"})}}}},{{"name","get_analysis"},{"description","Real-time audio analysis"},{"inputSchema",{{"type","object"},{"properties",json::object()}}}}})}}}11 unmodified linesstd::string resultText;bool isError = false;if (!helper.isHelperRunning()){resultText = "helper not connected";isError = true;}else if (toolName == "list_plugins"){// Query helper for loaded plugin nameauto resp = helper.sendCommand("{\"cmd\":\"search_param\",\"keyword\":\"\"}");// Just return what's loadedresultText = "[\"loaded plugin\"]"; // simplified — actual name comes from processor}else if (toolName == "search_param"){json cmd = {{"cmd","search_param"},{"keyword", args.value("keyword","")}};auto resp = helper.sendCommand(cmd.dump());try {auto j = json::parse(resp);if (j.value("ok", false))resultText = j["results"].dump();elseresultText = j.value("error", "search failed");} catch (...) { resultText = "parse error"; isError = true; }}else if (toolName == "get_params"){json cmd = {{"cmd","get_params"},{"ids", args.value("ids", json::array())}};auto resp = helper.sendCommand(cmd.dump());try {auto j = json::parse(resp);if (j.value("ok", false))resultText = j["values"].dump();elseresultText = "get failed";} catch (...) { resultText = "parse error"; isError = true; }}else if (toolName == "set_params"){json cmd = {{"cmd","set_params"},{"values", args.value("values", json::object())}};auto resp = helper.sendCommand(cmd.dump());try {auto j = json::parse(resp);resultText = j.value("ok", false) ? "ok" : "set failed";} catch (...) { resultText = "parse error"; isError = true; }}else if (toolName == "get_analysis"){resultText = "silent"; // Phase 4}else{123451 unmodified line789101112131445 unmodified lines606162636465666726 unmodified lines9495969798995 unmodified lines1051061071081091101117 unmodified lines11912012112212312412512612712812913013113213313413513613713813911 unmodified lines151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214#include "McpServer.h"#include "PluginBridgeProcessor.h"#define CPPHTTPLIB_THREAD_POOL_COUNT 2#include "httplib.h"1 unmodified lineusing json = nlohmann::json;McpServer::McpServer(int port, PluginBridgeProcessor& processor): server(std::make_unique<httplib::Server>()), port(port), processor(processor){setupRoutes();}45 unmodified lines{"status", "ok"},{"port", port},{"initialized", initialized},{"plugin", processor.isPluginLoaded() ? processor.getLoadedPluginName().toStdString() : "none"},{"params", processor.getLoadedPluginParamCount()}};res.set_content(health.dump(), "application/json");});26 unmodified linesbool McpServer::isRunning() const { return running.load(); }std::string McpServer::handleInitialize(const std::string& requestBody){auto body = json::parse(requestBody);5 unmodified lines{"result", {{"protocolVersion","2025-06-18"},{"capabilities",{{"tools",{{"listChanged",false}}}}},{"serverInfo",{{"name","PluginBridge"},{"version","0.3.0"}}}}}};return response.dump();7 unmodified lines{"id", body.value("id", json(nullptr))},{"result",{{"tools", json::array({{{"name","list_plugins"},{"description","Show loaded plugin info"},{"inputSchema",{{"type","object"},{"properties",json::object()}}}},{{"name","search_param"},{"description","Search params by keyword"},{"inputSchema",{{"type","object"},{"properties",{{"keyword",{{"type","string"},{"description","e.g. 'gain', 'band 1', 'cutoff'"}}}}},{"required",json::array({"keyword"})}}}},{{"name","get_params"},{"description","Get param values by index"},{"inputSchema",{{"type","object"},{"properties",{{"ids",{{"type","array"},{"items",{{"type","integer"}}}}}}},{"required",json::array({"ids"})}}}},{{"name","set_params"},{"description","Set params (batch, 0.0-1.0)"},{"inputSchema",{{"type","object"},{"properties",{{"values",{{"type","object"},{"description","index→value map"}}}}},{"required",json::array({"values"})}}}},{{"name","get_analysis"},{"description","Real-time audio analysis (future)"},{"inputSchema",{{"type","object"},{"properties",json::object()}}}}})}}}11 unmodified linesstd::string resultText;bool isError = false;if (!processor.isPluginLoaded()){resultText = "no plugin loaded";isError = true;}else if (toolName == "list_plugins"){json info = {{"name", processor.getLoadedPluginName().toStdString()},{"params", processor.getLoadedPluginParamCount()},{"path", processor.getLastPluginPath().toStdString()}};resultText = info.dump();}else if (toolName == "search_param"){auto keyword = args.value("keyword", "");auto results = processor.searchParams(juce::String(keyword));json arr = json::array();for (auto& p : results){arr.push_back({{"i", p.index},{"id", p.id.toStdString()},{"n", p.name.toStdString()},{"v", std::round(p.value * 1000.0f) / 1000.0f},{"t", p.displayText.toStdString()}});}resultText = arr.dump();}else if (toolName == "get_params"){std::vector<int> ids;for (auto& id : args["ids"])ids.push_back(id.get<int>());auto results = processor.getParams(ids);json values = json::object();for (auto& [idx, val] : results)values[std::to_string(idx)] = std::round(val * 1000.0f) / 1000.0f;resultText = values.dump();}else if (toolName == "set_params"){std::map<int, float> values;for (auto& [key, val] : args["values"].items())values[std::stoi(key)] = val.get<float>();bool ok = processor.setParams(values);resultText = ok ? "ok" : "set failed";}else if (toolName == "get_analysis"){resultText = "silent"; // Sprint 4}else{
Source/Plugin/McpServer.cpp+54-49