RAM2118 commited on
Commit
351c5a1
·
verified ·
1 Parent(s): a87020c

Upload Source/Plugin/McpServer.cpp

Browse files
Files changed (1) hide show
  1. Source/Plugin/McpServer.cpp +33 -12
Source/Plugin/McpServer.cpp CHANGED
@@ -1,5 +1,6 @@
1
  #include "McpServer.h"
2
  #include "HelperConnection.h"
 
3
  #include "IPCProtocol.h"
4
 
5
  #define CPPHTTPLIB_THREAD_POOL_COUNT 2
@@ -8,8 +9,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
  }
@@ -48,7 +49,9 @@ void McpServer::setupRoutes()
48
  });
49
 
50
  server->Get("/health", [this](const httplib::Request&, httplib::Response& res) {
51
- json health = {{"status","ok"},{"port",port},{"initialized",initialized},
 
 
52
  {"helper",helper.isHelperRunning() ? "connected" : "disconnected"}};
53
  res.set_content(health.dump(), "application/json");
54
  });
@@ -64,7 +67,12 @@ void McpServer::setupRoutes()
64
  void McpServer::start()
65
  {
66
  if (running.load()) return;
67
- serverThread = std::thread([this]() { running.store(true); server->listen("127.0.0.1", port); running.store(false); });
 
 
 
 
 
68
  server->wait_until_ready();
69
  }
70
 
@@ -83,7 +91,7 @@ std::string McpServer::handleInitialize(const std::string& requestBody)
83
  sessionId = "pb-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
84
  json response = {{"jsonrpc","2.0"},{"id",body.value("id",json(nullptr))},
85
  {"result",{{"protocolVersion","2025-06-18"},{"capabilities",{{"tools",{{"listChanged",false}}}}},
86
- {"serverInfo",{{"name","PluginBridge"},{"version","0.5.2"}}}}}};
87
  return response.dump();
88
  }
89
 
@@ -93,10 +101,11 @@ std::string McpServer::handleToolsList(const std::string& requestBody)
93
  json response = {{"jsonrpc","2.0"},{"id",body.value("id",json(nullptr))},
94
  {"result",{{"tools",json::array({
95
  {{"name","list_plugins"},{"description","Show loaded plugin info"},{"inputSchema",{{"type","object"},{"properties",json::object()}}}},
 
96
  {{"name","search_param"},{"description","Search params by keyword"},{"inputSchema",{{"type","object"},{"properties",{{"keyword",{{"type","string"}}}}},{"required",json::array({"keyword"})}}}},
97
  {{"name","get_params"},{"description","Get param values by index"},{"inputSchema",{{"type","object"},{"properties",{{"ids",{{"type","array"},{"items",{{"type","integer"}}}}}}},{"required",json::array({"ids"})}}}},
98
  {{"name","set_params"},{"description","Set params (batch, 0.0-1.0)"},{"inputSchema",{{"type","object"},{"properties",{{"values",{{"type","object"}}}}},{"required",json::array({"values"})}}}},
99
- {{"name","get_analysis"},{"description","Real-time audio analysis: LUFS, true peak, 7-band balance, stereo width, brightness. Returns compact string."},{"inputSchema",{{"type","object"},{"properties",json::object()}}}}
100
  })}}}};
101
  return response.dump();
102
  }
@@ -111,14 +120,25 @@ std::string McpServer::handleToolsCall(const std::string& requestBody)
111
  std::string resultText;
112
  bool isError = false;
113
 
114
- if (!helper.isHelperRunning())
115
  {
116
- resultText = "helper not connected";
117
- isError = true;
 
 
 
118
  }
119
  else if (toolName == "list_plugins")
120
  {
121
- resultText = "[\"loaded\"]";
 
 
 
 
 
 
 
 
122
  }
123
  else if (toolName == "search_param")
124
  {
@@ -143,10 +163,11 @@ std::string McpServer::handleToolsCall(const std::string& requestBody)
143
  }
144
  else if (toolName == "get_analysis")
145
  {
 
146
  if (getAnalysisCallback)
147
- resultText = getAnalysisCallback();
148
  else
149
- resultText = "analyser not ready";
150
  }
151
  else
152
  {
 
1
  #include "McpServer.h"
2
  #include "HelperConnection.h"
3
+ #include "InstanceRegistry.h"
4
  #include "IPCProtocol.h"
5
 
6
  #define CPPHTTPLIB_THREAD_POOL_COUNT 2
 
9
 
10
  using json = nlohmann::json;
11
 
12
+ McpServer::McpServer(HelperConnection& helper)
13
+ : server(std::make_unique<httplib::Server>()), helper(helper)
14
  {
15
  setupRoutes();
16
  }
 
49
  });
50
 
51
  server->Get("/health", [this](const httplib::Request&, httplib::Response& res) {
52
+ std::string channel = channelNameCallback ? channelNameCallback() : "Unknown";
53
+ json health = {{"status","ok"},{"port",port},{"channel",channel},
54
+ {"initialized",initialized},
55
  {"helper",helper.isHelperRunning() ? "connected" : "disconnected"}};
56
  res.set_content(health.dump(), "application/json");
57
  });
 
67
  void McpServer::start()
68
  {
69
  if (running.load()) return;
70
+ serverThread = std::thread([this]() {
71
+ running.store(true);
72
+ port = server->bind_to_any_port("127.0.0.1");
73
+ server->listen_after_bind();
74
+ running.store(false);
75
+ });
76
  server->wait_until_ready();
77
  }
78
 
 
91
  sessionId = "pb-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
92
  json response = {{"jsonrpc","2.0"},{"id",body.value("id",json(nullptr))},
93
  {"result",{{"protocolVersion","2025-06-18"},{"capabilities",{{"tools",{{"listChanged",false}}}}},
94
+ {"serverInfo",{{"name","PluginBridge"},{"version",PluginBridgeConstants::kVersion}}}}}};
95
  return response.dump();
96
  }
97
 
 
101
  json response = {{"jsonrpc","2.0"},{"id",body.value("id",json(nullptr))},
102
  {"result",{{"tools",json::array({
103
  {{"name","list_plugins"},{"description","Show loaded plugin info"},{"inputSchema",{{"type","object"},{"properties",json::object()}}}},
104
+ {{"name","list_instances"},{"description","List all PluginBridge instances in DAW (channel names + ports)"},{"inputSchema",{{"type","object"},{"properties",json::object()}}}},
105
  {{"name","search_param"},{"description","Search params by keyword"},{"inputSchema",{{"type","object"},{"properties",{{"keyword",{{"type","string"}}}}},{"required",json::array({"keyword"})}}}},
106
  {{"name","get_params"},{"description","Get param values by index"},{"inputSchema",{{"type","object"},{"properties",{{"ids",{{"type","array"},{"items",{{"type","integer"}}}}}}},{"required",json::array({"ids"})}}}},
107
  {{"name","set_params"},{"description","Set params (batch, 0.0-1.0)"},{"inputSchema",{{"type","object"},{"properties",{{"values",{{"type","object"}}}}},{"required",json::array({"values"})}}}},
108
+ {{"name","get_analysis"},{"description","Real-time audio analysis: LUFS, true peak, 7-band balance, stereo width, brightness. Prefixed with channel name."},{"inputSchema",{{"type","object"},{"properties",json::object()}}}}
109
  })}}}};
110
  return response.dump();
111
  }
 
120
  std::string resultText;
121
  bool isError = false;
122
 
123
+ if (toolName == "list_instances")
124
  {
125
+ auto registry = InstanceRegistry::readAll();
126
+ json instances = json::array();
127
+ for (auto& [name, p] : registry)
128
+ instances.push_back({{"channel", name}, {"port", p}});
129
+ resultText = instances.dump();
130
  }
131
  else if (toolName == "list_plugins")
132
  {
133
+ std::string channel = channelNameCallback ? channelNameCallback() : "Unknown";
134
+ json info = {{"channel", channel}, {"port", port},
135
+ {"helper", helper.isHelperRunning() ? "connected" : "disconnected"}};
136
+ resultText = info.dump();
137
+ }
138
+ else if (!helper.isHelperRunning())
139
+ {
140
+ resultText = "helper not connected";
141
+ isError = true;
142
  }
143
  else if (toolName == "search_param")
144
  {
 
163
  }
164
  else if (toolName == "get_analysis")
165
  {
166
+ std::string channel = channelNameCallback ? channelNameCallback() : "Unknown";
167
  if (getAnalysisCallback)
168
+ resultText = "[" + channel + "] " + getAnalysisCallback();
169
  else
170
+ resultText = "[" + channel + "] analyser not ready";
171
  }
172
  else
173
  {