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:
sdk/examples/envsecretis a secret store, a command and an MCP tool.cmd/lo-plugin-pm2is an engine.
For the place of plugins in the code, read architecture.
The entry point is main
Section titled “The entry point is main”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.
The lifecycle
Section titled “The lifecycle”- Handshake.
Serveuses the handshake of HashiCorp go-plugin. If the binary starts withoutLOCALSPACE_PLUGIN=v1, it says that it is a plugin and exits.lorefuses a plugin that was built for a different protocol version (sdk.ProtocolVersion). Describe.localls it one time for each binary. It caches the answer by the size and the modification time of the file. A start oflodoes not run a plugin that did not change.- Calls.
Invokecalls a method of a provider by name. The arguments are JSON, andServesends the call to your Go method. A tool uses the pointtool, the methodCalland the arguments ininput. The logs of an engine and the output of a command return throughStream. Theexecof an engine uses the two-wayExecstream. The wire format is inplugin.proto. - Lifetime. The process lives for one
locommand. 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 thatlogives. - Output. localspace does not show the stdout and the stderr of the plugin. Return errors to report problems.
Extension points
Section titled “Extension points”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.
Engines must check ownership
Section titled “Engines must check ownership”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,
Statusreturnssdk.UnitMissing.StopandRemovedo nothing. - If the entry belongs to a different owner, return
sdk.Errorf(sdk.Conflict, …)and leave the entry alone. Startcan 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.
Engines must end a cancelled exec
Section titled “Engines must end a cancelled exec”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 itsexec.CommandContext. The command then runs in a separate process group. The group gets a kill signal whenctxends. On Windows,taskkillends the process tree of the command. Theexec.CommandContextalone 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 execleaves its command in the container (moby/moby#9098). The Docker engine oflogives each exec a marker variable that its processes inherit, and it kills the processes that carry the variable.
MCP tools
Section titled “MCP tools”lo mcp shows each tool to the MCP client of an agent. These rules apply to a tool:
InputSchemais the JSON Schema (draft 2020-12) of the arguments object. Its"type"must be"object".lorefuses a plugin with a tool of a different type, when you install it and whenlostarts. A nil schema accepts any object.Rungets the arguments as JSON.lodoes not check them, soRunmust check them.- The result goes to the agent as JSON. An
*sdk.Errorreaches the agent as the error of the tool, with its code, aslo --jsonshows 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
Effecttosdk.ToolReads,sdk.ToolChangesorsdk.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,lotreats 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) { … }, },},Errors
Section titled “Errors”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>.
Build, install and try
Section titled “Build, install and try”-
Build the plugin:
Terminal window go build -o lo-plugin-hello . -
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 -
Show the items that the plugin gives and the commands that it adds:
Terminal window lo plugin ls -
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:
lo plugin install https://example.com/lo-plugin-hello_darwin_arm64 --sha256 <sum>See lo plugin install for all the options.
Test a plugin
Section titled “Test a plugin”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.
Official plugins
Section titled “Official plugins”An official plugin lives in this repository as cmd/lo-plugin-<name>. It ships with each lo release.
- Add the name of the plugin to
PLUGINSin theMakefile. - 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. - Users install the plugin with
lo plugin install <name>.