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