| 1 | |
| 2 | |
| 3 | |
| 4 | package toolset |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "os" |
| 11 | "path" |
| 12 | "path/filepath" |
| 13 | |
| 14 | "github.com/modelcontextprotocol/go-sdk/mcp" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/mcp/msi" |
| 17 | ) |
| 18 | |
| 19 | func (ts *ToolSet) ListDirectory(ctx context.Context, |
| 20 | _ *mcp.CallToolRequest, args msi.ListDirectoryParams, |
| 21 | ) (*mcp.CallToolResult, *msi.ListDirectoryResult, error) { |
| 22 | if ts.inst == nil { |
| 23 | return nil, nil, errors.New("instance not registered") |
| 24 | } |
| 25 | guestPath, err := ts.TranslateHostPath(args.Path) |
| 26 | if err != nil { |
| 27 | return nil, nil, err |
| 28 | } |
| 29 | guestEnts, err := ts.sftp.ReadDirContext(ctx, guestPath) |
| 30 | if err != nil { |
| 31 | return nil, nil, err |
| 32 | } |
| 33 | res := &msi.ListDirectoryResult{ |
| 34 | Entries: make([]msi.ListDirectoryResultEntry, len(guestEnts)), |
| 35 | } |
| 36 | for i, f := range guestEnts { |
| 37 | res.Entries[i].Name = f.Name() |
| 38 | res.Entries[i].Size = new(f.Size()) |
| 39 | res.Entries[i].Mode = new(f.Mode()) |
| 40 | res.Entries[i].ModTime = new(f.ModTime()) |
| 41 | res.Entries[i].IsDir = new(f.IsDir()) |
| 42 | } |
| 43 | return &mcp.CallToolResult{ |
| 44 | StructuredContent: res, |
| 45 | }, res, nil |
| 46 | } |
| 47 | |
| 48 | func (ts *ToolSet) ReadFile(_ context.Context, |
| 49 | _ *mcp.CallToolRequest, args msi.ReadFileParams, |
| 50 | ) (*mcp.CallToolResult, *msi.ReadFileResult, error) { |
| 51 | if ts.inst == nil { |
| 52 | return nil, nil, errors.New("instance not registered") |
| 53 | } |
| 54 | guestPath, err := ts.TranslateHostPath(args.Path) |
| 55 | if err != nil { |
| 56 | return nil, nil, err |
| 57 | } |
| 58 | f, err := ts.sftp.Open(guestPath) |
| 59 | if err != nil { |
| 60 | return nil, nil, err |
| 61 | } |
| 62 | defer f.Close() |
| 63 | const limitBytes = 32 * 1024 * 1024 |
| 64 | lr := io.LimitReader(f, limitBytes) |
| 65 | b, err := io.ReadAll(lr) |
| 66 | if err != nil { |
| 67 | return nil, nil, err |
| 68 | } |
| 69 | res := &msi.ReadFileResult{ |
| 70 | Content: string(b), |
| 71 | } |
| 72 | return &mcp.CallToolResult{ |
| 73 | |
| 74 | |
| 75 | |
| 76 | StructuredContent: res, |
| 77 | }, res, nil |
| 78 | } |
| 79 | |
| 80 | func (ts *ToolSet) WriteFile(_ context.Context, |
| 81 | _ *mcp.CallToolRequest, args msi.WriteFileParams, |
| 82 | ) (*mcp.CallToolResult, *msi.WriteFileResult, error) { |
| 83 | if ts.inst == nil { |
| 84 | return nil, nil, errors.New("instance not registered") |
| 85 | } |
| 86 | guestPath, err := ts.TranslateHostPath(args.Path) |
| 87 | if err != nil { |
| 88 | return nil, nil, err |
| 89 | } |
| 90 | dir := filepath.Dir(guestPath) |
| 91 | err = ts.sftp.MkdirAll(dir) |
| 92 | if err != nil { |
| 93 | return nil, nil, err |
| 94 | } |
| 95 | f, err := ts.sftp.Create(guestPath) |
| 96 | if err != nil { |
| 97 | return nil, nil, err |
| 98 | } |
| 99 | defer f.Close() |
| 100 | _, err = f.Write([]byte(args.Content)) |
| 101 | if err != nil { |
| 102 | return nil, nil, err |
| 103 | } |
| 104 | res := &msi.WriteFileResult{} |
| 105 | return &mcp.CallToolResult{ |
| 106 | |
| 107 | |
| 108 | |
| 109 | StructuredContent: res, |
| 110 | }, res, nil |
| 111 | } |
| 112 | |
| 113 | func (ts *ToolSet) Glob(_ context.Context, |
| 114 | _ *mcp.CallToolRequest, args msi.GlobParams, |
| 115 | ) (*mcp.CallToolResult, *msi.GlobResult, error) { |
| 116 | if ts.inst == nil { |
| 117 | return nil, nil, errors.New("instance not registered") |
| 118 | } |
| 119 | pathStr, err := os.Getwd() |
| 120 | if err != nil { |
| 121 | return nil, nil, err |
| 122 | } |
| 123 | if args.Path != nil && *args.Path != "" { |
| 124 | pathStr = *args.Path |
| 125 | } |
| 126 | guestPath, err := ts.TranslateHostPath(pathStr) |
| 127 | if err != nil { |
| 128 | return nil, nil, err |
| 129 | } |
| 130 | pattern := path.Join(guestPath, args.Pattern) |
| 131 | matches, err := ts.sftp.Glob(pattern) |
| 132 | if matches == nil { |
| 133 | matches = []string{} |
| 134 | } |
| 135 | if err != nil { |
| 136 | return nil, nil, err |
| 137 | } |
| 138 | res := &msi.GlobResult{ |
| 139 | Matches: matches, |
| 140 | } |
| 141 | return &mcp.CallToolResult{ |
| 142 | |
| 143 | |
| 144 | StructuredContent: res, |
| 145 | }, res, nil |
| 146 | } |
| 147 | |
| 148 | func (ts *ToolSet) SearchFileContent(ctx context.Context, |
| 149 | req *mcp.CallToolRequest, args msi.SearchFileContentParams, |
| 150 | ) (*mcp.CallToolResult, *msi.SearchFileContentResult, error) { |
| 151 | if ts.inst == nil { |
| 152 | return nil, nil, errors.New("instance not registered") |
| 153 | } |
| 154 | pathStr, err := os.Getwd() |
| 155 | if err != nil { |
| 156 | return nil, nil, err |
| 157 | } |
| 158 | if args.Path != nil && *args.Path != "" { |
| 159 | pathStr = *args.Path |
| 160 | } |
| 161 | guestPath, err := ts.TranslateHostPath(pathStr) |
| 162 | if err != nil { |
| 163 | return nil, nil, err |
| 164 | } |
| 165 | if args.Include != nil && *args.Include != "" { |
| 166 | guestPath = path.Join(guestPath, *args.Include) |
| 167 | } |
| 168 | cmdToolRes, cmdRes, err := ts.RunShellCommand(ctx, req, msi.RunShellCommandParams{ |
| 169 | Command: []string{"git", "grep", "-n", "--no-index", args.Pattern, guestPath}, |
| 170 | Directory: pathStr, |
| 171 | }) |
| 172 | if err != nil { |
| 173 | return cmdToolRes, nil, err |
| 174 | } |
| 175 | res := &msi.SearchFileContentResult{ |
| 176 | GitGrepOutput: cmdRes.Stdout, |
| 177 | } |
| 178 | return &mcp.CallToolResult{ |
| 179 | |
| 180 | |
| 181 | StructuredContent: res, |
| 182 | }, res, nil |
| 183 | } |