Skip to content

Commit cf500f6

Browse files
committed
refactor : use constant for io.lima-vm/warnings
Signed-off-by: Praful <[email protected]>
1 parent ef0605a commit cf500f6

File tree

4 files changed

+30
-40
lines changed

4 files changed

+30
-40
lines changed

pkg/mcp/toolset/filesystem.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import (
1717
"github.com/lima-vm/lima/v2/pkg/ptr"
1818
)
1919

20+
const MetaWarnings = "io.lima-vm/warnings"
21+
2022
func (ts *ToolSet) ListDirectory(ctx context.Context,
2123
_ *mcp.CallToolRequest, args msi.ListDirectoryParams,
2224
) (*mcp.CallToolResult, *msi.ListDirectoryResult, error) {
2325
if ts.inst == nil {
2426
return nil, nil, errors.New("instance not registered")
2527
}
26-
guestPath, logs, err := ts.TranslateHostPath(args.Path)
28+
guestPath, warnings, err := ts.TranslateHostPath(args.Path)
2729
if err != nil {
2830
return nil, nil, err
2931
}
@@ -44,10 +46,8 @@ func (ts *ToolSet) ListDirectory(ctx context.Context,
4446
callToolRes := &mcp.CallToolResult{
4547
StructuredContent: res,
4648
}
47-
if logs != "" {
48-
callToolRes.Meta = map[string]any{
49-
"io.lima-vm/logs": []string{logs},
50-
}
49+
if warnings != "" {
50+
callToolRes.Meta[MetaWarnings] = warnings
5151
}
5252
return callToolRes, res, nil
5353
}
@@ -58,7 +58,7 @@ func (ts *ToolSet) ReadFile(_ context.Context,
5858
if ts.inst == nil {
5959
return nil, nil, errors.New("instance not registered")
6060
}
61-
guestPath, logs, err := ts.TranslateHostPath(args.Path)
61+
guestPath, warnings, err := ts.TranslateHostPath(args.Path)
6262
if err != nil {
6363
return nil, nil, err
6464
}
@@ -82,10 +82,8 @@ func (ts *ToolSet) ReadFile(_ context.Context,
8282
// (e.g., [File content truncated: showing lines 1-100 of 500 total lines...]\nActual file content...).
8383
StructuredContent: res,
8484
}
85-
if logs != "" {
86-
callToolRes.Meta = map[string]any{
87-
"io.lima-vm/logs": []string{logs},
88-
}
85+
if warnings != "" {
86+
callToolRes.Meta[MetaWarnings] = warnings
8987
}
9088
return callToolRes, res, nil
9189
}
@@ -96,7 +94,7 @@ func (ts *ToolSet) WriteFile(_ context.Context,
9694
if ts.inst == nil {
9795
return nil, nil, errors.New("instance not registered")
9896
}
99-
guestPath, logs, err := ts.TranslateHostPath(args.Path)
97+
guestPath, warnings, err := ts.TranslateHostPath(args.Path)
10098
if err != nil {
10199
return nil, nil, err
102100
}
@@ -121,10 +119,8 @@ func (ts *ToolSet) WriteFile(_ context.Context,
121119
// or `Successfully created and wrote to new file: /path/to/new/file.txt.`
122120
StructuredContent: res,
123121
}
124-
if logs != "" {
125-
callToolRes.Meta = map[string]any{
126-
"io.lima-vm/logs": []string{logs},
127-
}
122+
if warnings != "" {
123+
callToolRes.Meta[MetaWarnings] = warnings
128124
}
129125
return callToolRes, res, nil
130126
}
@@ -142,7 +138,7 @@ func (ts *ToolSet) Glob(_ context.Context,
142138
if args.Path != nil && *args.Path != "" {
143139
pathStr = *args.Path
144140
}
145-
guestPath, logs, err := ts.TranslateHostPath(pathStr)
141+
guestPath, warnings, err := ts.TranslateHostPath(pathStr)
146142
if err != nil {
147143
return nil, nil, err
148144
}
@@ -162,10 +158,8 @@ func (ts *ToolSet) Glob(_ context.Context,
162158
// A message like: Found 5 file(s) matching "*.ts" within src, sorted by modification time (newest first):\nsrc/file1.ts\nsrc/subdir/file2.ts...
163159
StructuredContent: res,
164160
}
165-
if logs != "" {
166-
callToolRes.Meta = map[string]any{
167-
"io.lima-vm/logs": []string{logs},
168-
}
161+
if warnings != "" {
162+
callToolRes.Meta[MetaWarnings] = warnings
169163
}
170164
return callToolRes, res, nil
171165
}
@@ -183,7 +177,7 @@ func (ts *ToolSet) SearchFileContent(ctx context.Context,
183177
if args.Path != nil && *args.Path != "" {
184178
pathStr = *args.Path
185179
}
186-
guestPath, logs, err := ts.TranslateHostPath(pathStr)
180+
guestPath, warnings, err := ts.TranslateHostPath(pathStr)
187181
if err != nil {
188182
return nil, nil, err
189183
}
@@ -205,10 +199,8 @@ func (ts *ToolSet) SearchFileContent(ctx context.Context,
205199
// 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) {...}...
206200
StructuredContent: res,
207201
}
208-
if logs != "" {
209-
callToolRes.Meta = map[string]any{
210-
"io.lima-vm/logs": []string{logs},
211-
}
202+
if warnings != "" {
203+
callToolRes.Meta[MetaWarnings] = warnings
212204
}
213205
return callToolRes, res, nil
214206
}

pkg/mcp/toolset/shell.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (ts *ToolSet) RunShellCommand(ctx context.Context,
2121
if ts.inst == nil {
2222
return nil, nil, errors.New("instance not registered")
2323
}
24-
guestPath, logs, err := ts.TranslateHostPath(args.Directory)
24+
guestPath, warnings, err := ts.TranslateHostPath(args.Directory)
2525
if err != nil {
2626
return nil, nil, err
2727
}
@@ -48,10 +48,8 @@ func (ts *ToolSet) RunShellCommand(ctx context.Context,
4848
callToolRes := &mcp.CallToolResult{
4949
StructuredContent: res,
5050
}
51-
if logs != "" {
52-
callToolRes.Meta = map[string]any{
53-
"io.lima-vm/logs": []string{logs},
54-
}
51+
if warnings != "" {
52+
callToolRes.Meta[MetaWarnings] = warnings
5553
}
5654
return callToolRes, res, nil
5755
}

pkg/mcp/toolset/toolset.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (ts *ToolSet) Close() error {
103103
return err
104104
}
105105

106-
func (ts *ToolSet) TranslateHostPath(hostPath string) (guestPath, logs string, err error) {
106+
func (ts *ToolSet) TranslateHostPath(hostPath string) (guestPath, warnings string, err error) {
107107
if hostPath == "" {
108108
return "", "", errors.New("path is empty")
109109
}
@@ -113,10 +113,10 @@ func (ts *ToolSet) TranslateHostPath(hostPath string) (guestPath, logs string, e
113113

114114
guestPath, isMounted := ts.translateToGuestPath(hostPath)
115115
if !isMounted {
116-
logs = fmt.Sprintf("path %q is not under any mounted directory, using as guest path", hostPath)
117-
logrus.Info(logs)
116+
warnings = fmt.Sprintf("path %q is not under any mounted directory, using as guest path", hostPath)
117+
logrus.Info(warnings)
118118
}
119-
return guestPath, logs, nil
119+
return guestPath, warnings, nil
120120
}
121121

122122
func (ts *ToolSet) translateToGuestPath(hostPath string) (string, bool) {

pkg/mcp/toolset/toolset_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestTranslateHostPath(t *testing.T) {
2020
hostPath string
2121
toolSet ToolSet
2222
wantGuestPath string
23-
wantLogs bool
23+
wantWarnings bool
2424
wantErr bool
2525
}{
2626
{
@@ -36,7 +36,7 @@ func TestTranslateHostPath(t *testing.T) {
3636
},
3737
},
3838
wantGuestPath: "/mnt/home-user/documents/file.txt",
39-
wantLogs: false,
39+
wantWarnings: false,
4040
wantErr: false,
4141
},
4242
{
@@ -52,7 +52,7 @@ func TestTranslateHostPath(t *testing.T) {
5252
},
5353
},
5454
wantGuestPath: "/other/path/file.txt",
55-
wantLogs: true,
55+
wantWarnings: true,
5656
wantErr: false,
5757
},
5858
{
@@ -68,7 +68,7 @@ func TestTranslateHostPath(t *testing.T) {
6868
},
6969
},
7070
wantGuestPath: "/home/user2/file.txt",
71-
wantLogs: true,
71+
wantWarnings: true,
7272
wantErr: false,
7373
},
7474
{
@@ -85,7 +85,7 @@ func TestTranslateHostPath(t *testing.T) {
8585
},
8686
},
8787
wantGuestPath: "/mnt/tmp/myfile",
88-
wantLogs: false,
88+
wantWarnings: false,
8989
wantErr: false,
9090
},
9191
}
@@ -98,7 +98,7 @@ func TestTranslateHostPath(t *testing.T) {
9898
} else {
9999
assert.NilError(t, err)
100100
assert.Equal(t, test.wantGuestPath, got)
101-
if test.wantLogs {
101+
if test.wantWarnings {
102102
assert.Assert(t, logs != "")
103103
} else {
104104
assert.Equal(t, "", logs)

0 commit comments

Comments
 (0)