跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

filesystem.go

183 行 · 4.9 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package toolset
5
6import (
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
19func (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
48func (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 // Gemini:
74 // For text files: The file content, potentially prefixed with a truncation message
75 // (e.g., [File content truncated: showing lines 1-100 of 500 total lines...]\nActual file content...).
76 StructuredContent: res,
77 }, res, nil
78}
79
80func (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 // Gemini:
107 // A success message, e.g., `Successfully overwrote file: /path/to/your/file.txt`
108 // or `Successfully created and wrote to new file: /path/to/new/file.txt.`
109 StructuredContent: res,
110 }, res, nil
111}
112
113func (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 // Gemini:
143 // A message like: Found 5 file(s) matching "*.ts" within src, sorted by modification time (newest first):\nsrc/file1.ts\nsrc/subdir/file2.ts...
144 StructuredContent: res,
145 }, res, nil
146}
147
148func (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, // Directory must be always set
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 // Gemini:
180 // A message like: Found 10 matching lines for regex "function\\s+myFunction" in directory src:\nsrc/file1.js:10:function myFunction() {...}\nsrc/subdir/file2.ts:45: function myFunction(param) {...}...
181 StructuredContent: res,
182 }, res, nil
183}