|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + |
| 16 | + "github.com/lima-vm/lima/v2/pkg/mcp/toolset" |
| 17 | + "github.com/lima-vm/lima/v2/pkg/store" |
| 18 | + "github.com/lima-vm/lima/v2/pkg/version" |
| 19 | +) |
| 20 | + |
| 21 | +func main() { |
| 22 | + if err := newApp().Execute(); err != nil { |
| 23 | + logrus.Fatal(err) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func newApp() *cobra.Command { |
| 28 | + cmd := &cobra.Command{ |
| 29 | + Use: "limactl-mcp", |
| 30 | + Short: "Model Context Protocol plugin for Lima (EXPERIMENTAL)", |
| 31 | + Version: strings.TrimPrefix(version.Version, "v"), |
| 32 | + SilenceUsage: true, |
| 33 | + SilenceErrors: true, |
| 34 | + } |
| 35 | + cmd.AddCommand( |
| 36 | + newMcpInfoCommand(), |
| 37 | + newMcpServeCommand(), |
| 38 | + // TODO: `limactl-mcp install-gemini` ? |
| 39 | + ) |
| 40 | + return cmd |
| 41 | +} |
| 42 | + |
| 43 | +func newServer() *mcp.Server { |
| 44 | + impl := &mcp.Implementation{ |
| 45 | + Name: "lima", |
| 46 | + Title: "Lima VM, for sandboxing local command executions and file I/O operations", |
| 47 | + Version: version.Version, |
| 48 | + } |
| 49 | + serverOpts := &mcp.ServerOptions{ |
| 50 | + Instructions: `This MCP server provides tools for sandboxing local command executions and file I/O operations, |
| 51 | +by wrapping them in Lima VM (https://lima-vm.io). |
| 52 | +
|
| 53 | +Use these tools to avoid accidentally executing malicious codes directly on the host. |
| 54 | +`, |
| 55 | + } |
| 56 | + if runtime.GOOS != "linux" { |
| 57 | + serverOpts.Instructions += fmt.Sprintf(` |
| 58 | +
|
| 59 | +NOTE: the guest OS of the VM is Linux, while the host OS is %s. |
| 60 | +`, strings.ToTitle(runtime.GOOS)) |
| 61 | + } |
| 62 | + return mcp.NewServer(impl, serverOpts) |
| 63 | +} |
| 64 | + |
| 65 | +func newMcpInfoCommand() *cobra.Command { |
| 66 | + cmd := &cobra.Command{ |
| 67 | + Use: "info", |
| 68 | + Short: "Show information about the MCP server", |
| 69 | + Args: cobra.NoArgs, |
| 70 | + RunE: mcpInfoAction, |
| 71 | + } |
| 72 | + return cmd |
| 73 | +} |
| 74 | + |
| 75 | +func mcpInfoAction(cmd *cobra.Command, args []string) error { |
| 76 | + ctx := cmd.Context() |
| 77 | + |
| 78 | + ts, err := toolset.New() |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + server := newServer() |
| 83 | + if err = ts.RegisterServer(server); err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + serverTransport, clientTransport := mcp.NewInMemoryTransports() |
| 87 | + serverSession, err := server.Connect(ctx, serverTransport, nil) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + client := mcp.NewClient(&mcp.Implementation{Name: "client"}, nil) |
| 92 | + clientSession, err := client.Connect(ctx, clientTransport, nil) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + toolsResult, err := clientSession.ListTools(ctx, &mcp.ListToolsParams{}) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + if err = clientSession.Close(); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + if err = serverSession.Wait(); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + info := &Info{ |
| 107 | + Tools: toolsResult.Tools, |
| 108 | + } |
| 109 | + j, err := json.MarshalIndent(info, "", " ") |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + _, err = fmt.Fprint(cmd.OutOrStdout(), string(j)) |
| 114 | + return err |
| 115 | +} |
| 116 | + |
| 117 | +type Info struct { |
| 118 | + Tools []*mcp.Tool `json:"tools"` |
| 119 | +} |
| 120 | + |
| 121 | +func newMcpServeCommand() *cobra.Command { |
| 122 | + cmd := &cobra.Command{ |
| 123 | + Use: "serve INSTANCE", |
| 124 | + Short: "Serve MCP over stdio", |
| 125 | + Long: `Serve MCP over stdio. |
| 126 | +
|
| 127 | +Expected to be executed via an AI agent, not by a human`, |
| 128 | + Args: cobra.MaximumNArgs(1), |
| 129 | + RunE: mcpServeAction, |
| 130 | + } |
| 131 | + return cmd |
| 132 | +} |
| 133 | + |
| 134 | +func mcpServeAction(cmd *cobra.Command, args []string) error { |
| 135 | + ctx := cmd.Context() |
| 136 | + instName := "default" |
| 137 | + if len(args) > 0 { |
| 138 | + instName = args[0] |
| 139 | + } |
| 140 | + |
| 141 | + ts, err := toolset.New() |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + server := newServer() |
| 146 | + if err = ts.RegisterServer(server); err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + inst, err := store.Inspect(ctx, instName) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + if err = ts.RegisterInstance(ctx, inst); err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + transport := &mcp.StdioTransport{} |
| 158 | + return server.Run(ctx, transport) |
| 159 | +} |
0 commit comments