RAM2118 commited on
Commit
bbd0e6f
·
verified ·
1 Parent(s): ab55e95

Upload Source/Plugin/McpServer.cpp

Browse files
Files changed (1) hide show
  1. Source/Plugin/McpServer.cpp +54 -49
Source/Plugin/McpServer.cpp CHANGED
@@ -1,6 +1,5 @@
1
  #include "McpServer.h"
2
- #include "HelperConnection.h"
3
- #include "../Shared/IPCProtocol.h"
4
 
5
  #define CPPHTTPLIB_THREAD_POOL_COUNT 2
6
  #include "httplib.h"
@@ -8,8 +7,8 @@
8
 
9
  using json = nlohmann::json;
10
 
11
- McpServer::McpServer(int port, HelperConnection& helper)
12
- : server(std::make_unique<httplib::Server>()), port(port), helper(helper)
13
  {
14
  setupRoutes();
15
  }
@@ -61,7 +60,8 @@ void McpServer::setupRoutes()
61
  {"status", "ok"},
62
  {"port", port},
63
  {"initialized", initialized},
64
- {"helper", helper.isHelperRunning() ? "connected" : "disconnected"}
 
65
  };
66
  res.set_content(health.dump(), "application/json");
67
  });
@@ -94,8 +94,6 @@ void McpServer::stop()
94
 
95
  bool McpServer::isRunning() const { return running.load(); }
96
 
97
- // --- Protocol Handlers ---
98
-
99
  std::string McpServer::handleInitialize(const std::string& requestBody)
100
  {
101
  auto body = json::parse(requestBody);
@@ -107,7 +105,7 @@ std::string McpServer::handleInitialize(const std::string& requestBody)
107
  {"result", {
108
  {"protocolVersion","2025-06-18"},
109
  {"capabilities",{{"tools",{{"listChanged",false}}}}},
110
- {"serverInfo",{{"name","PluginBridge"},{"version","0.2.0"}}}
111
  }}
112
  };
113
  return response.dump();
@@ -121,24 +119,21 @@ std::string McpServer::handleToolsList(const std::string& requestBody)
121
  {"id", body.value("id", json(nullptr))},
122
  {"result",{
123
  {"tools", json::array({
124
- {{"name","list_plugins"},{"description","List loaded plugin"},
125
  {"inputSchema",{{"type","object"},{"properties",json::object()}}}},
126
  {{"name","search_param"},{"description","Search params by keyword"},
127
  {"inputSchema",{{"type","object"},{"properties",{
128
- {"plugin",{{"type","string"}}},
129
  {"keyword",{{"type","string"},{"description","e.g. 'gain', 'band 1', 'cutoff'"}}}
130
- }},{"required",json::array({"plugin","keyword"})}}}},
131
- {{"name","get_params"},{"description","Get param values by ID"},
132
  {"inputSchema",{{"type","object"},{"properties",{
133
- {"plugin",{{"type","string"}}},
134
  {"ids",{{"type","array"},{"items",{{"type","integer"}}}}}
135
- }},{"required",json::array({"plugin","ids"})}}}},
136
  {{"name","set_params"},{"description","Set params (batch, 0.0-1.0)"},
137
  {"inputSchema",{{"type","object"},{"properties",{
138
- {"plugin",{{"type","string"}}},
139
- {"values",{{"type","object"},{"description","id→value"}}}
140
- }},{"required",json::array({"plugin","values"})}}}},
141
- {{"name","get_analysis"},{"description","Real-time audio analysis"},
142
  {"inputSchema",{{"type","object"},{"properties",json::object()}}}}
143
  })}
144
  }}
@@ -156,54 +151,64 @@ std::string McpServer::handleToolsCall(const std::string& requestBody)
156
  std::string resultText;
157
  bool isError = false;
158
 
159
- if (!helper.isHelperRunning())
160
  {
161
- resultText = "helper not connected";
162
  isError = true;
163
  }
164
  else if (toolName == "list_plugins")
165
  {
166
- // Query helper for loaded plugin name
167
- auto resp = helper.sendCommand("{\"cmd\":\"search_param\",\"keyword\":\"\"}");
168
- // Just return what's loaded
169
- resultText = "[\"loaded plugin\"]"; // simplified — actual name comes from processor
 
 
170
  }
171
  else if (toolName == "search_param")
172
  {
173
- json cmd = {{"cmd","search_param"},{"keyword", args.value("keyword","")}};
174
- auto resp = helper.sendCommand(cmd.dump());
175
- try {
176
- auto j = json::parse(resp);
177
- if (j.value("ok", false))
178
- resultText = j["results"].dump();
179
- else
180
- resultText = j.value("error", "search failed");
181
- } catch (...) { resultText = "parse error"; isError = true; }
 
 
 
 
 
 
182
  }
183
  else if (toolName == "get_params")
184
  {
185
- json cmd = {{"cmd","get_params"},{"ids", args.value("ids", json::array())}};
186
- auto resp = helper.sendCommand(cmd.dump());
187
- try {
188
- auto j = json::parse(resp);
189
- if (j.value("ok", false))
190
- resultText = j["values"].dump();
191
- else
192
- resultText = "get failed";
193
- } catch (...) { resultText = "parse error"; isError = true; }
 
 
194
  }
195
  else if (toolName == "set_params")
196
  {
197
- json cmd = {{"cmd","set_params"},{"values", args.value("values", json::object())}};
198
- auto resp = helper.sendCommand(cmd.dump());
199
- try {
200
- auto j = json::parse(resp);
201
- resultText = j.value("ok", false) ? "ok" : "set failed";
202
- } catch (...) { resultText = "parse error"; isError = true; }
203
  }
204
  else if (toolName == "get_analysis")
205
  {
206
- resultText = "silent"; // Phase 4
207
  }
208
  else
209
  {
 
1
  #include "McpServer.h"
2
+ #include "PluginBridgeProcessor.h"
 
3
 
4
  #define CPPHTTPLIB_THREAD_POOL_COUNT 2
5
  #include "httplib.h"
 
7
 
8
  using json = nlohmann::json;
9
 
10
+ McpServer::McpServer(int port, PluginBridgeProcessor& processor)
11
+ : server(std::make_unique<httplib::Server>()), port(port), processor(processor)
12
  {
13
  setupRoutes();
14
  }
 
60
  {"status", "ok"},
61
  {"port", port},
62
  {"initialized", initialized},
63
+ {"plugin", processor.isPluginLoaded() ? processor.getLoadedPluginName().toStdString() : "none"},
64
+ {"params", processor.getLoadedPluginParamCount()}
65
  };
66
  res.set_content(health.dump(), "application/json");
67
  });
 
94
 
95
  bool McpServer::isRunning() const { return running.load(); }
96
 
 
 
97
  std::string McpServer::handleInitialize(const std::string& requestBody)
98
  {
99
  auto body = json::parse(requestBody);
 
105
  {"result", {
106
  {"protocolVersion","2025-06-18"},
107
  {"capabilities",{{"tools",{{"listChanged",false}}}}},
108
+ {"serverInfo",{{"name","PluginBridge"},{"version","0.3.0"}}}
109
  }}
110
  };
111
  return response.dump();
 
119
  {"id", body.value("id", json(nullptr))},
120
  {"result",{
121
  {"tools", json::array({
122
+ {{"name","list_plugins"},{"description","Show loaded plugin info"},
123
  {"inputSchema",{{"type","object"},{"properties",json::object()}}}},
124
  {{"name","search_param"},{"description","Search params by keyword"},
125
  {"inputSchema",{{"type","object"},{"properties",{
 
126
  {"keyword",{{"type","string"},{"description","e.g. 'gain', 'band 1', 'cutoff'"}}}
127
+ }},{"required",json::array({"keyword"})}}}},
128
+ {{"name","get_params"},{"description","Get param values by index"},
129
  {"inputSchema",{{"type","object"},{"properties",{
 
130
  {"ids",{{"type","array"},{"items",{{"type","integer"}}}}}
131
+ }},{"required",json::array({"ids"})}}}},
132
  {{"name","set_params"},{"description","Set params (batch, 0.0-1.0)"},
133
  {"inputSchema",{{"type","object"},{"properties",{
134
+ {"values",{{"type","object"},{"description","index→value map"}}}
135
+ }},{"required",json::array({"values"})}}}},
136
+ {{"name","get_analysis"},{"description","Real-time audio analysis (future)"},
 
137
  {"inputSchema",{{"type","object"},{"properties",json::object()}}}}
138
  })}
139
  }}
 
151
  std::string resultText;
152
  bool isError = false;
153
 
154
+ if (!processor.isPluginLoaded())
155
  {
156
+ resultText = "no plugin loaded";
157
  isError = true;
158
  }
159
  else if (toolName == "list_plugins")
160
  {
161
+ json info = {
162
+ {"name", processor.getLoadedPluginName().toStdString()},
163
+ {"params", processor.getLoadedPluginParamCount()},
164
+ {"path", processor.getLastPluginPath().toStdString()}
165
+ };
166
+ resultText = info.dump();
167
  }
168
  else if (toolName == "search_param")
169
  {
170
+ auto keyword = args.value("keyword", "");
171
+ auto results = processor.searchParams(juce::String(keyword));
172
+
173
+ json arr = json::array();
174
+ for (auto& p : results)
175
+ {
176
+ arr.push_back({
177
+ {"i", p.index},
178
+ {"id", p.id.toStdString()},
179
+ {"n", p.name.toStdString()},
180
+ {"v", std::round(p.value * 1000.0f) / 1000.0f},
181
+ {"t", p.displayText.toStdString()}
182
+ });
183
+ }
184
+ resultText = arr.dump();
185
  }
186
  else if (toolName == "get_params")
187
  {
188
+ std::vector<int> ids;
189
+ for (auto& id : args["ids"])
190
+ ids.push_back(id.get<int>());
191
+
192
+ auto results = processor.getParams(ids);
193
+
194
+ json values = json::object();
195
+ for (auto& [idx, val] : results)
196
+ values[std::to_string(idx)] = std::round(val * 1000.0f) / 1000.0f;
197
+
198
+ resultText = values.dump();
199
  }
200
  else if (toolName == "set_params")
201
  {
202
+ std::map<int, float> values;
203
+ for (auto& [key, val] : args["values"].items())
204
+ values[std::stoi(key)] = val.get<float>();
205
+
206
+ bool ok = processor.setParams(values);
207
+ resultText = ok ? "ok" : "set failed";
208
  }
209
  else if (toolName == "get_analysis")
210
  {
211
+ resultText = "silent"; // Sprint 4
212
  }
213
  else
214
  {