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