@@ -42,10 +42,11 @@ ProtocolServerMCP::ProtocolServerMCP() : ProtocolServer() {
42
42
AddRequestHandler (" resources/read" ,
43
43
std::bind (&ProtocolServerMCP::ResourcesReadHandler, this ,
44
44
std::placeholders::_1));
45
- AddNotificationHandler (
46
- " notifications/initialized" , [](const protocol::Notification &) {
47
- LLDB_LOG (GetLog (LLDBLog::Host), " MCP initialization complete" );
48
- });
45
+ AddNotificationHandler (" notifications/initialized" ,
46
+ [](const lldb_protocol::mcp::Notification &) {
47
+ LLDB_LOG (GetLog (LLDBLog::Host),
48
+ " MCP initialization complete" );
49
+ });
49
50
50
51
AddTool (
51
52
std::make_unique<CommandTool>(" lldb_command" , " Run an lldb command." ));
@@ -72,11 +73,11 @@ llvm::StringRef ProtocolServerMCP::GetPluginDescriptionStatic() {
72
73
return " MCP Server." ;
73
74
}
74
75
75
- llvm::Expected<protocol ::Response>
76
- ProtocolServerMCP::Handle (protocol ::Request request) {
76
+ llvm::Expected<lldb_protocol::mcp ::Response>
77
+ ProtocolServerMCP::Handle (lldb_protocol::mcp ::Request request) {
77
78
auto it = m_request_handlers.find (request.method );
78
79
if (it != m_request_handlers.end ()) {
79
- llvm::Expected<protocol ::Response> response = it->second (request);
80
+ llvm::Expected<lldb_protocol::mcp ::Response> response = it->second (request);
80
81
if (!response)
81
82
return response;
82
83
response->id = request.id ;
@@ -87,7 +88,7 @@ ProtocolServerMCP::Handle(protocol::Request request) {
87
88
llvm::formatv (" no handler for request: {0}" , request.method ).str ());
88
89
}
89
90
90
- void ProtocolServerMCP::Handle (protocol ::Notification notification) {
91
+ void ProtocolServerMCP::Handle (lldb_protocol::mcp ::Notification notification) {
91
92
auto it = m_notification_handlers.find (notification.method );
92
93
if (it != m_notification_handlers.end ()) {
93
94
it->second (notification);
@@ -133,7 +134,7 @@ llvm::Error ProtocolServerMCP::ReadCallback(Client &client) {
133
134
134
135
for (std::string::size_type pos;
135
136
(pos = client.buffer .find (' \n ' )) != std::string::npos;) {
136
- llvm::Expected<std::optional<protocol ::Message>> message =
137
+ llvm::Expected<std::optional<lldb_protocol::mcp ::Message>> message =
137
138
HandleData (StringRef (client.buffer .data (), pos));
138
139
client.buffer = client.buffer .erase (0 , pos + 1 );
139
140
if (!message)
@@ -208,19 +209,19 @@ llvm::Error ProtocolServerMCP::Stop() {
208
209
return llvm::Error::success ();
209
210
}
210
211
211
- llvm::Expected<std::optional<protocol ::Message>>
212
+ llvm::Expected<std::optional<lldb_protocol::mcp ::Message>>
212
213
ProtocolServerMCP::HandleData (llvm::StringRef data) {
213
- auto message = llvm::json::parse<protocol ::Message>(/* JSON=*/ data);
214
+ auto message = llvm::json::parse<lldb_protocol::mcp ::Message>(/* JSON=*/ data);
214
215
if (!message)
215
216
return message.takeError ();
216
217
217
- if (const protocol ::Request *request =
218
- std::get_if<protocol ::Request>(&(*message))) {
219
- llvm::Expected<protocol ::Response> response = Handle (*request);
218
+ if (const lldb_protocol::mcp ::Request *request =
219
+ std::get_if<lldb_protocol::mcp ::Request>(&(*message))) {
220
+ llvm::Expected<lldb_protocol::mcp ::Response> response = Handle (*request);
220
221
221
222
// Handle failures by converting them into an Error message.
222
223
if (!response) {
223
- protocol ::Error protocol_error;
224
+ lldb_protocol::mcp ::Error protocol_error;
224
225
llvm::handleAllErrors (
225
226
response.takeError (),
226
227
[&](const MCPError &err) { protocol_error = err.toProtcolError (); },
@@ -235,23 +236,23 @@ ProtocolServerMCP::HandleData(llvm::StringRef data) {
235
236
return *response;
236
237
}
237
238
238
- if (const protocol ::Notification *notification =
239
- std::get_if<protocol ::Notification>(&(*message))) {
239
+ if (const lldb_protocol::mcp ::Notification *notification =
240
+ std::get_if<lldb_protocol::mcp ::Notification>(&(*message))) {
240
241
Handle (*notification);
241
242
return std::nullopt;
242
243
}
243
244
244
- if (std::get_if<protocol ::Error>(&(*message)))
245
+ if (std::get_if<lldb_protocol::mcp ::Error>(&(*message)))
245
246
return llvm::createStringError (" unexpected MCP message: error" );
246
247
247
- if (std::get_if<protocol ::Response>(&(*message)))
248
+ if (std::get_if<lldb_protocol::mcp ::Response>(&(*message)))
248
249
return llvm::createStringError (" unexpected MCP message: response" );
249
250
250
251
llvm_unreachable (" all message types handled" );
251
252
}
252
253
253
- protocol ::Capabilities ProtocolServerMCP::GetCapabilities () {
254
- protocol ::Capabilities capabilities;
254
+ lldb_protocol::mcp ::Capabilities ProtocolServerMCP::GetCapabilities () {
255
+ lldb_protocol::mcp ::Capabilities capabilities;
255
256
capabilities.tools .listChanged = true ;
256
257
// FIXME: Support sending notifications when a debugger/target are
257
258
// added/removed.
@@ -288,20 +289,22 @@ void ProtocolServerMCP::AddNotificationHandler(llvm::StringRef method,
288
289
m_notification_handlers[method] = std::move (handler);
289
290
}
290
291
291
- llvm::Expected<protocol::Response>
292
- ProtocolServerMCP::InitializeHandler (const protocol::Request &request) {
293
- protocol::Response response;
292
+ llvm::Expected<lldb_protocol::mcp::Response>
293
+ ProtocolServerMCP::InitializeHandler (
294
+ const lldb_protocol::mcp::Request &request) {
295
+ lldb_protocol::mcp::Response response;
294
296
response.result .emplace (llvm::json::Object{
295
- {" protocolVersion" , protocol ::kVersion },
297
+ {" protocolVersion" , lldb_protocol::mcp ::kVersion },
296
298
{" capabilities" , GetCapabilities ()},
297
299
{" serverInfo" ,
298
300
llvm::json::Object{{" name" , kName }, {" version" , kVersion }}}});
299
301
return response;
300
302
}
301
303
302
- llvm::Expected<protocol::Response>
303
- ProtocolServerMCP::ToolsListHandler (const protocol::Request &request) {
304
- protocol::Response response;
304
+ llvm::Expected<lldb_protocol::mcp::Response>
305
+ ProtocolServerMCP::ToolsListHandler (
306
+ const lldb_protocol::mcp::Request &request) {
307
+ lldb_protocol::mcp::Response response;
305
308
306
309
llvm::json::Array tools;
307
310
for (const auto &tool : m_tools)
@@ -312,9 +315,10 @@ ProtocolServerMCP::ToolsListHandler(const protocol::Request &request) {
312
315
return response;
313
316
}
314
317
315
- llvm::Expected<protocol::Response>
316
- ProtocolServerMCP::ToolsCallHandler (const protocol::Request &request) {
317
- protocol::Response response;
318
+ llvm::Expected<lldb_protocol::mcp::Response>
319
+ ProtocolServerMCP::ToolsCallHandler (
320
+ const lldb_protocol::mcp::Request &request) {
321
+ lldb_protocol::mcp::Response response;
318
322
319
323
if (!request.params )
320
324
return llvm::createStringError (" no tool parameters" );
@@ -335,11 +339,11 @@ ProtocolServerMCP::ToolsCallHandler(const protocol::Request &request) {
335
339
if (it == m_tools.end ())
336
340
return llvm::createStringError (llvm::formatv (" no tool \" {0}\" " , tool_name));
337
341
338
- protocol ::ToolArguments tool_args;
342
+ lldb_protocol::mcp ::ToolArguments tool_args;
339
343
if (const json::Value *args = param_obj->get (" arguments" ))
340
344
tool_args = *args;
341
345
342
- llvm::Expected<protocol ::TextResult> text_result =
346
+ llvm::Expected<lldb_protocol::mcp ::TextResult> text_result =
343
347
it->second ->Call (tool_args);
344
348
if (!text_result)
345
349
return text_result.takeError ();
@@ -349,16 +353,17 @@ ProtocolServerMCP::ToolsCallHandler(const protocol::Request &request) {
349
353
return response;
350
354
}
351
355
352
- llvm::Expected<protocol::Response>
353
- ProtocolServerMCP::ResourcesListHandler (const protocol::Request &request) {
354
- protocol::Response response;
356
+ llvm::Expected<lldb_protocol::mcp::Response>
357
+ ProtocolServerMCP::ResourcesListHandler (
358
+ const lldb_protocol::mcp::Request &request) {
359
+ lldb_protocol::mcp::Response response;
355
360
356
361
llvm::json::Array resources;
357
362
358
363
std::lock_guard<std::mutex> guard (m_server_mutex);
359
364
for (std::unique_ptr<ResourceProvider> &resource_provider_up :
360
365
m_resource_providers) {
361
- for (const protocol ::Resource &resource :
366
+ for (const lldb_protocol::mcp ::Resource &resource :
362
367
resource_provider_up->GetResources ())
363
368
resources.push_back (resource);
364
369
}
@@ -368,9 +373,10 @@ ProtocolServerMCP::ResourcesListHandler(const protocol::Request &request) {
368
373
return response;
369
374
}
370
375
371
- llvm::Expected<protocol::Response>
372
- ProtocolServerMCP::ResourcesReadHandler (const protocol::Request &request) {
373
- protocol::Response response;
376
+ llvm::Expected<lldb_protocol::mcp::Response>
377
+ ProtocolServerMCP::ResourcesReadHandler (
378
+ const lldb_protocol::mcp::Request &request) {
379
+ lldb_protocol::mcp::Response response;
374
380
375
381
if (!request.params )
376
382
return llvm::createStringError (" no resource parameters" );
@@ -390,7 +396,7 @@ ProtocolServerMCP::ResourcesReadHandler(const protocol::Request &request) {
390
396
std::lock_guard<std::mutex> guard (m_server_mutex);
391
397
for (std::unique_ptr<ResourceProvider> &resource_provider_up :
392
398
m_resource_providers) {
393
- llvm::Expected<protocol ::ResourceResult> result =
399
+ llvm::Expected<lldb_protocol::mcp ::ResourceResult> result =
394
400
resource_provider_up->ReadResource (uri_str);
395
401
if (result.errorIsA <UnsupportedURI>()) {
396
402
llvm::consumeError (result.takeError ());
@@ -399,7 +405,7 @@ ProtocolServerMCP::ResourcesReadHandler(const protocol::Request &request) {
399
405
if (!result)
400
406
return result.takeError ();
401
407
402
- protocol ::Response response;
408
+ lldb_protocol::mcp ::Response response;
403
409
response.result .emplace (std::move (*result));
404
410
return response;
405
411
}
0 commit comments