Skip to content

Write a plugin

A plugin adds an engine, a secret store, a proxy, a resource kind, commands or MCP tools to lo. You do not change lo itself. A plugin is an ordinary executable. lo starts it when a command uses it, and talks to it over gRPC.

This page shows how to write a plugin in Go with the sdk module. Two examples are in the repository:

For the place of plugins in the code, read architecture.

A plugin has no exported symbol and no special function. main calls sdk.Serve with the items that the plugin gives, with a name for each item. Serve returns when lo is done with the plugin.

package main
import "github.com/sanlilab/localspace/sdk"
func main() {
sdk.Serve(sdk.Plugin{
Engines: map[string]sdk.Engine{"podman": podman{}}, // engine.podman
Secrets: map[string]sdk.Secret{"env": envSecret{}}, // secret.env
Proxies: map[string]sdk.Proxy{"traefik": traefik{}}, // proxy.traefik
Commands: map[string]sdk.Command{"hello": {Short: "Say hello", Run: hello}}, // lo hello
Tools: map[string]sdk.Tool{"podman_ps": {Description: "…", Run: ps}}, // an MCP tool of lo mcp
})
}

Fill in only the maps that the plugin uses. main is a normal program, so it can do other work before Serve. For example, the pm2 plugin runs as a stop shim when you call it as lo-plugin-pm2 shim <command>. In all other cases it serves.

The module of the plugin imports only github.com/sanlilab/localspace/sdk. Do not import the internal/ packages of lo.

start the binary with LOCALSPACE_PLUGIN=v1go-plugin handshake: protocol version, gRPC addressDescribethe providers and the commands of the pluginInvoke, Stream or Exec, as the command needsthe process stops when the lo command endslolo-plugin-namelolo-plugin-name
  • Handshake. Serve uses the handshake of HashiCorp go-plugin. If the binary starts without LOCALSPACE_PLUGIN=v1, it says that it is a plugin and exits. lo refuses a plugin that was built for a different protocol version (sdk.ProtocolVersion).
  • Describe. lo calls it one time for each binary. It caches the answer by the size and the modification time of the file. A start of lo does not run a plugin that did not change.
  • Calls. Invoke calls a method of a provider by name. The arguments are JSON, and Serve sends the call to your Go method. A tool uses the point tool, the method Call and the arguments in input. The logs of an engine and the output of a command return through Stream. The exec of an engine uses the two-way Exec stream. The wire format is in plugin.proto.
  • Lifetime. The process lives for one lo command. Things that must live longer, such as containers, a pm2 daemon or a server, run in a separate process. The plugin finds them again from the handle or the state that lo gives.
  • Output. localspace does not show the stdout and the stderr of the plugin. Return errors to report problems.

Each extension point is a Go interface in the sdk package. It matches the contract of lo that has the same name. The types of the sdk have the same JSON as the types of lo. Read the comments in internal/contract to learn the task of each method.

Map in sdk.Plugin Interface Selected by Notes
Engines sdk.Engine, and sdk.Stater as an option engines.container or engines.process in ~/.localspace/config.yaml Runs tells the run kinds that the engine takes. Implement Stater to show CPU and memory in lo stats and in the dashboard. Exec gets no terminal. A plugin cannot run one-off containers (RunOnce), so seeds and setup commands must run on a built-in container engine.
Secrets sdk.Secret secrets: in config.yaml Resolves ${secret:path}. localspace masks the values as it masks all secrets.
Proxies sdk.Proxy proxy: {provider: …} in config.yaml lo selects the ports and gives them to the plugin. Do not assume the configured ports.
Resources sdk.Resource kind: in a template Attributes lists the values that references can read, such as ${db.url}. After you install the plugin, the template checker accepts the kind and checks the references against this list. A resource provider gets no engine. It gives containers as sdk.Unit and talks to servers.
Commands sdk.Command The name of the command Run(ctx, args, stdout) gets the arguments after the name. lo does not send stdin.
Tools sdk.Tool The name of the tool, [a-zA-Z0-9_-]{1,64} lo mcp shows the tool to coding agents with its built-in tools. See MCP tools.

lo refuses to install a plugin that has a provider, a command or a tool with a name that a built-in or a different plugin has. If you put the file in place by hand, lo does not start.

An engine must not stop, remove or report on an item by its name only. Start returns an sdk.Handle, a map[string]string that lo stores. Status, Stop and Remove get the handle back. Before they act, they must make sure that the live entry is the one that the plugin started for that unit. They use a marker that the plugin set at the start, such as a label or an environment variable.

  • If the entry is gone, Status returns sdk.UnitMissing. Stop and Remove do nothing.
  • If the entry belongs to a different owner, return sdk.Errorf(sdk.Conflict, …) and leave the entry alone.
  • Start can replace a stopped entry that the plugin made with the name of the unit. It must not replace an entry that it did not make.

Give the entries the name ls-<owner>-<unit>, as the built-in engines do. Then two workspaces do not share a name. For a complete engine with integration tests, read cmd/lo-plugin-pm2/engine.go. The rules are also in safety.

When the context of Exec ends, Exec must end the command and all the processes that it started, and then return. The context ends, for example, when an agent gives up on a call through lo mcp. A test runner with workers, or a server that the command started, must not stay after the call.

lo sends the cancel over the Exec stream. It waits up to 15 seconds until the plugin says that it is done. Your Exec sees the cancel as the end of its ctx.

  • An engine that runs the command on this machine calls sdk.EndGroupOnCancel(cmd) on its exec.CommandContext. The command then runs in a separate process group. The group gets a kill signal when ctx ends. On Windows, taskkill ends the process tree of the command. The exec.CommandContext alone kills only the command, and its children continue to run.
  • An engine with units that run in a different place, such as a container, a VM or a remote machine, must end the processes there. A kill of a local client is rarely sufficient. A killed docker exec leaves its command in the container (moby/moby#9098). The Docker engine of lo gives each exec a marker variable that its processes inherit, and it kills the processes that carry the variable.

lo mcp shows each tool to the MCP client of an agent. These rules apply to a tool:

  • InputSchema is the JSON Schema (draft 2020-12) of the arguments object. Its "type" must be "object". lo refuses a plugin with a tool of a different type, when you install it and when lo starts. A nil schema accepts any object.
  • Run gets the arguments as JSON. lo does not check them, so Run must check them.
  • The result goes to the agent as JSON. An *sdk.Error reaches the agent as the error of the tool, with its code, as lo --json shows it.
  • The description is the only text that the model reads. Write the action of the tool and the time to call it.
  • A tool runs in the process of the plugin, as a command does. The process lives as long as the session of lo mcp.
  • Set Effect to sdk.ToolReads, sdk.ToolChanges or sdk.ToolDestroys. The clients of agents run a reading tool without a prompt, and they show a prompt before a tool that destroys. If a tool has no effect, lo treats it as a tool that can destroy anything.
Tools: map[string]sdk.Tool{
"env_secret": {
Description: "Say which environment variable holds a ${secret:path} and whether it is set, never its value.",
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{"path": map[string]any{"type": "string"}},
"required": []string{"path"},
},
Effect: sdk.ToolReads,
Run: func(ctx context.Context, input json.RawMessage) (any, error) { … },
},
},

Return sdk.Errorf(code, format, args…), an *sdk.Error, to give lo a code. lo reports the code as its code. lo reports it in the CLI, in --json and in the API of the dashboard.

Code Meaning
sdk.Invalid The request is not valid, for example an incorrect argument or an operation that the plugin does not support.
sdk.NotFound The requested item is not there.
sdk.Conflict The item is there, but it does not belong to the plugin or its state refuses the operation.
sdk.Unavailable A tool or a service that the plugin uses does not answer or is not installed.

All other errors reach the user as plugin <name>: <message>.

  1. Build the plugin:

    Terminal window
    go build -o lo-plugin-hello .
  2. Install it. The path must have a slash. The name of the plugin comes from lo-plugin-<name>.

    Terminal window
    lo plugin install ./lo-plugin-hello
  3. Show the items that the plugin gives and the commands that it adds:

    Terminal window
    lo plugin ls
  4. Run a command of the plugin:

    Terminal window
    lo hello

Install the plugin again to update it. Run lo plugin rm hello to remove it.

To ship a binary to other users, publish it with its SHA-256 sum:

Terminal window
lo plugin install https://example.com/lo-plugin-hello_darwin_arm64 --sha256 <sum>

See lo plugin install for all the options.

In a test, run the kernel of lo with pluginhost.Module{Dir: …}. The directory holds the built plugin. The file internal/modules/pluginhost/pluginhost_test.go shows how.

A plugin that drives a real tool, such as pm2 or a container CLI, must have tests against that tool. Put them behind the integration build tag. Skip the test when the tool is not installed.

An official plugin lives in this repository as cmd/lo-plugin-<name>. It ships with each lo release.

  1. Add the name of the plugin to PLUGINS in the Makefile.
  2. Push a release tag. The release workflow runs make release. It builds the plugin for all platforms and puts it, with its checksum, in the assets of the release.
  3. Users install the plugin with lo plugin install <name>.