16 Commits

Author SHA1 Message Date
John Harrison
af65dc6cd6
[lldb-mcp] Auto connect to the first running lldb mcp instance. (#157503)
This improves the flow by automatically connecting to an exisitng lldb
instance, if one is found.

Future improvements include:

* Launching a binary if an instance isn't detected.
* Multiplexing if multiple instances are detected.
2025-09-09 10:09:03 -07:00
John Harrison
65f60fd465
[lldb] Moving MCPTransport into its own file. (#156712)
Moving `lldb_protocol::mcp::MCPTransport` into its own file and renaming
to `lldb_protocol::mcp::Transport`.
2025-09-04 08:52:33 -07:00
John Harrison
a9ea7cf60c
[lldb] Adjust ProtocolServer connection defaults. (#155714)
This adjusts the ProtocolServer command to default to create a new
connection listening on `localhost:0` and adds a new `ServerMetadata`
details to `~/.lldb/mcp/lldb-<pid>.json` to record information about the
current MCP server.

This can be consumed by the lldb-mcp binary to establish a connection
from an LLM client.

---------

Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com>
2025-08-29 16:17:45 -07:00
Jonas Devlieghere
df4c367585
[lldb] Stop the protocol servers when terminating the plugin (#156101)
Currently, the server keeps running until we call Stop from its dtor in
the static destruction chain. This is too late: the server should stop
when the plugin gets terminated.
2025-08-29 15:41:44 -07:00
Sylvestre Ledru
1bb8db5d5e Revert "[lldb] NFC Moving mcp::Transport into its own file. (#155711)"
This reverts commit 71a065ed07cda66f628e1cfa7b875eeef7e9a141.
2025-08-28 14:01:55 +02:00
John Harrison
71a065ed07
[lldb] NFC Moving mcp::Transport into its own file. (#155711)
Moving `lldb_protocol::mcp::MCPTransport` out of Server.h and into its
own file and simplifying the name to `Transport`.
2025-08-27 16:21:04 -07:00
Jonas Devlieghere
1ba8b36fef
[lldb] Adopt JSONTransport in the MCP Server (Reland) (#155322)
This PR adopts JSONTransport in the MCP server implementation. It
required a slight change in design in the relationship between the two
server classes. Previously, these two had an "is-a" connection, while
now they have a "has-a" connection.

The "generic" protocol server in Protocol/MCP now operates using a
single connection (Transport). This matches the design in DAP where each
DAP instance has its own connection. The protocol server in Plugins
still supports multiple clients and creates a new server instance for
each connection.

I believe the new design makes sense in the long term (as proved by DAP)
and allows us to make the server stateful if we choose to do so. There's
no reason that multiple client support can't live in the generic
protocol library, but for now I kept it in ProtocolServerMCP to avoid
creating unnecessary abstractions.

This is a reland of #155034 but with significant changes to the tests.
The unit tests now test the generic server implementation, which matches
the original intent. This also means the test are now single threaded
and therefore fully deterministic using the MainLoop.
2025-08-25 16:18:21 -07:00
Jonas Devlieghere
aa1dd4b0d6
Revert "[lldb] Adopt JSONTransport in the MCP Server" (#155280)
Reverts llvm/llvm-project#155034 because the unit tests are flakey on
the Debian bot: https://lab.llvm.org/buildbot/#/builders/162.
2025-08-25 12:34:36 -07:00
Jonas Devlieghere
a49df8ec7d
[lldb] Adopt JSONTransport in the MCP Server (#155034)
This PR adopts JSONTransport in the MCP server implementation. It
required a slight change in design in the relationship between the two
server classes. Previously, these two had an "is-a" connection, while
now they have a "has-a" connection.

The "generic" protocol server in Protocol/MCP now operates using a
single connection (Transport). This matches the design in DAP where each
DAP instance has its own connection. The protocol server in Plugins
still supports multiple clients and creates a new server instance for
each connection.

I believe the new design makes sense in the long term (as proved by DAP)
and allows us to make the server stateful if we choose to do so. There's
no reason that multiple client support can't live in the generic
protocol library, but for now I kept it in ProtocolServerMCP to avoid
creating unnecessary abstractions.
2025-08-25 10:55:30 -07:00
Jonas Devlieghere
44aedacb1b
[lldb] Move the generic MCP server code into Protocol/MCP (NFC) (#152396)
This is a continuation of #152188, which started splitting up the MCP
implementation into a generic implementation in Protocol/MCP that will
be shared between LLDB and lldb-mcp.

For now I kept all the networking code in the MCP server plugin. Once
the changes to JSONTransport land, we might be able to move more of it
into the Protocol library.
2025-08-07 09:01:28 -07:00
Jonas Devlieghere
dbaa82b384
[lldb] Move the generic MCP code into Protocol/MCP (NFC) (#152188)
This moves all the generic MCP code into the new ProtocolMCP library so
it can be shared between the MCP implementation in LLDB (built on the
private API) and lldb-mcp (built on the public API).

This PR doesn't include the actual MCP server. The reason is that the
transport mechanism will be different between the LLDB implementation
(using sockets) and the lldb-mcp implementation (using stdio). The goal
s to abstract away that difference by making the server use
JSONTransport (again) but I'm saving that for a separate PR.
2025-08-05 14:10:31 -07:00
Jonas Devlieghere
ed294c28ac
[lldb] Move MCP protocol into its own library (NFC) (#152059)
This PR moves the MCP protocol code into its own library
(`lldbProtocolMCP`) so the code can be shared between the
`ProtocolServerMCP` plugin in LLDB as well as `lldb-mcp`. The goal is to
do the same thing for DAP (which, for now, would be used exclusively
from `lldb-dap`).

To make it clear that it's neither part of the `lldb` nor the
`lldb_private` namespace, I created a new `lldb_protocol` namespace.

Depending on how much code would be reused by lldb-dap, we may move more
code into the protocol library.
2025-08-05 09:48:26 -07:00
Jonas Devlieghere
3c4c2fada2
[lldb] Expose debuggers and target as resources through MCP (#148075)
Expose debuggers and target as resources through MCP. This has two
advantages:

1. Enables returning data in a structured way. Although tools can return
structured data with the latest revision of the protocol, we might not
be able to update before the majority of clients has adopted it.
2. Enables the user to specify a resource themselves, rather than
letting the model guess which debugger instance it should use.

This PR exposes a resource for debuggers and targets.

The following URI returns information about a given debugger instance:

```
lldb://debugger/<debugger id>
```

For example:

```
{
  uri: "lldb://debugger/0"
  mimeType: "application/json"
  text: "{"debugger_id":0,"num_targets":2}"
}
```

The following URI returns information about a given target:

```
lldb://debugger/<debugger id>/target/<target id>
```

For example:

```
{
  uri: "lldb://debugger/0/target/0"
  mimeType: "application/json"
  text: "{"arch":"arm64-apple-macosx26.0.0","debugger_id":0,"path":"/Users/jonas/llvm/build-ra/bin/count","target_id":0}"
}
```
2025-07-11 15:49:27 -07:00
Jonas Devlieghere
e8abdfc88f
[lldb] Make MCP server instance global (#145616)
Rather than having one MCP server per debugger, make the MCP server
global and pass a debugger id along with tool invocations that require
one. This PR also adds a second tool to list the available debuggers
with their targets so the model can decide which debugger instance to
use.
2025-06-25 13:46:33 -07:00
Kazu Hirata
975d4df147
[lldb] Remove an unused local variable (NFC) (#145212) 2025-06-23 18:04:26 -07:00
Jonas Devlieghere
9524bfb270
[lldb] Add Model Context Protocol (MCP) support to LLDB (#143628)
This PR adds an MCP (Model Context Protocol ) server to LLDB. For
motivation and background, please refer to the corresponding RFC:
https://discourse.llvm.org/t/rfc-adding-mcp-support-to-lldb/86798

I implemented this as a new kind of plugin. The idea is that we could
support multiple protocol servers (e.g. if we want to support DAP from
within LLDB). This also introduces a corresponding top-level command
(`protocol-server`) with two subcommands to `start` and `stop` the
server.

```
(lldb) protocol-server start MCP tcp://localhost:1234
MCP server started with connection listeners: connection://[::1]:1234, connection://[127.0.0.1]:1234
```

The MCP sever supports one tool (`lldb_command`) which executes a
command, but can easily be extended with more commands.
2025-06-20 10:48:04 -05:00