Skip to content

Commit 57736fb

Browse files
committed
return path translation logs to agent
Signed-off-by: Praful Khanduri <holiodin@gmail.com>
1 parent c44939c commit 57736fb

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

pkg/mcp/msi/filesystem.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type ListDirectoryResultEntry struct {
3535

3636
type ListDirectoryResult struct {
3737
Entries []ListDirectoryResultEntry `json:"entries" jsonschema:"The directory content entries."`
38+
Logs []string `json:"logs,omitempty" jsonschema:"Logs related to path translation and other operations."`
3839
}
3940

4041
var ReadFile = &mcp.Tool{
@@ -43,7 +44,8 @@ var ReadFile = &mcp.Tool{
4344
}
4445

4546
type ReadFileResult struct {
46-
Content string `json:"content" jsonschema:"The content of the file."`
47+
Content string `json:"content" jsonschema:"The content of the file."`
48+
Logs []string `json:"logs,omitempty" jsonschema:"Logs related to path translation and other operations."`
4749
}
4850

4951
type ReadFileParams struct {
@@ -58,7 +60,7 @@ var WriteFile = &mcp.Tool{
5860
}
5961

6062
type WriteFileResult struct {
61-
// Empty for now
63+
Logs []string `json:"logs,omitempty" jsonschema:"Logs related to path translation and other operations."`
6264
}
6365

6466
type WriteFileParams struct {
@@ -79,6 +81,7 @@ type GlobParams struct {
7981

8082
type GlobResult struct {
8183
Matches []string `json:"matches" jsonschema:"A list of absolute file paths that match the provided glob pattern."`
84+
Logs []string `json:"logs,omitempty" jsonschema:"Logs related to path translation and other operations."`
8285
}
8386

8487
var SearchFileContent = &mcp.Tool{
@@ -93,7 +96,8 @@ type SearchFileContentParams struct {
9396
}
9497

9598
type SearchFileContentResult struct {
96-
GitGrepOutput string `json:"git_grep_output" jsonschema:"The raw output from the 'git grep -n --no-index' command, containing matching lines with filenames and line numbers."`
99+
GitGrepOutput string `json:"git_grep_output" jsonschema:"The raw output from the 'git grep -n --no-index' command, containing matching lines with filenames and line numbers."`
100+
Logs []string `json:"logs,omitempty" jsonschema:"Logs related to path translation and other operations."`
97101
}
98102

99103
// TODO: implement Replace

pkg/mcp/msi/shell.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ type RunShellCommandParams struct {
2222
}
2323

2424
type RunShellCommandResult struct {
25-
Stdout string `json:"stdout" jsonschema:"Output from the standard output stream."`
26-
Stderr string `json:"stderr" jsonschema:"Output from the standard error stream."`
27-
Error string `json:"error,omitempty" jsonschema:"Any error message reported by the subprocess."`
28-
ExitCode *int `json:"exit_code,omitempty" jsonschema:"Exit code of the command."`
25+
Stdout string `json:"stdout" jsonschema:"Output from the standard output stream."`
26+
Stderr string `json:"stderr" jsonschema:"Output from the standard error stream."`
27+
Error string `json:"error,omitempty" jsonschema:"Any error message reported by the subprocess."`
28+
ExitCode *int `json:"exit_code,omitempty" jsonschema:"Exit code of the command."`
29+
Logs []string `json:"logs,omitempty" jsonschema:"Logs related to path translation and other operations."`
2930
}

pkg/mcp/toolset/filesystem.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (ts *ToolSet) ListDirectory(ctx context.Context,
2323
if ts.inst == nil {
2424
return nil, nil, errors.New("instance not registered")
2525
}
26-
guestPath, err := ts.TranslateHostPath(args.Path)
26+
guestPath, logs, err := ts.TranslateHostPath(args.Path)
2727
if err != nil {
2828
return nil, nil, err
2929
}
@@ -34,6 +34,9 @@ func (ts *ToolSet) ListDirectory(ctx context.Context,
3434
res := &msi.ListDirectoryResult{
3535
Entries: make([]msi.ListDirectoryResultEntry, len(guestEnts)),
3636
}
37+
if logs != "" {
38+
res.Logs = append(res.Logs, logs)
39+
}
3740
for i, f := range guestEnts {
3841
res.Entries[i].Name = f.Name()
3942
res.Entries[i].Size = ptr.Of(f.Size())
@@ -52,7 +55,7 @@ func (ts *ToolSet) ReadFile(_ context.Context,
5255
if ts.inst == nil {
5356
return nil, nil, errors.New("instance not registered")
5457
}
55-
guestPath, err := ts.TranslateHostPath(args.Path)
58+
guestPath, logs, err := ts.TranslateHostPath(args.Path)
5659
if err != nil {
5760
return nil, nil, err
5861
}
@@ -70,6 +73,9 @@ func (ts *ToolSet) ReadFile(_ context.Context,
7073
res := &msi.ReadFileResult{
7174
Content: string(b),
7275
}
76+
if logs != "" {
77+
res.Logs = append(res.Logs, logs)
78+
}
7379
return &mcp.CallToolResult{
7480
// Gemini:
7581
// For text files: The file content, potentially prefixed with a truncation message
@@ -84,7 +90,7 @@ func (ts *ToolSet) WriteFile(_ context.Context,
8490
if ts.inst == nil {
8591
return nil, nil, errors.New("instance not registered")
8692
}
87-
guestPath, err := ts.TranslateHostPath(args.Path)
93+
guestPath, logs, err := ts.TranslateHostPath(args.Path)
8894
if err != nil {
8995
return nil, nil, err
9096
}
@@ -103,6 +109,9 @@ func (ts *ToolSet) WriteFile(_ context.Context,
103109
return nil, nil, err
104110
}
105111
res := &msi.WriteFileResult{}
112+
if logs != "" {
113+
res.Logs = append(res.Logs, logs)
114+
}
106115
return &mcp.CallToolResult{
107116
// Gemini:
108117
// A success message, e.g., `Successfully overwrote file: /path/to/your/file.txt`
@@ -124,7 +133,7 @@ func (ts *ToolSet) Glob(_ context.Context,
124133
if args.Path != nil && *args.Path != "" {
125134
pathStr = *args.Path
126135
}
127-
guestPath, err := ts.TranslateHostPath(pathStr)
136+
guestPath, logs, err := ts.TranslateHostPath(pathStr)
128137
if err != nil {
129138
return nil, nil, err
130139
}
@@ -139,6 +148,9 @@ func (ts *ToolSet) Glob(_ context.Context,
139148
res := &msi.GlobResult{
140149
Matches: matches,
141150
}
151+
if logs != "" {
152+
res.Logs = append(res.Logs, logs)
153+
}
142154
return &mcp.CallToolResult{
143155
// Gemini:
144156
// A message like: Found 5 file(s) matching "*.ts" within src, sorted by modification time (newest first):\nsrc/file1.ts\nsrc/subdir/file2.ts...
@@ -159,7 +171,7 @@ func (ts *ToolSet) SearchFileContent(ctx context.Context,
159171
if args.Path != nil && *args.Path != "" {
160172
pathStr = *args.Path
161173
}
162-
guestPath, err := ts.TranslateHostPath(pathStr)
174+
guestPath, logs, err := ts.TranslateHostPath(pathStr)
163175
if err != nil {
164176
return nil, nil, err
165177
}
@@ -176,6 +188,9 @@ func (ts *ToolSet) SearchFileContent(ctx context.Context,
176188
res := &msi.SearchFileContentResult{
177189
GitGrepOutput: cmdRes.Stdout,
178190
}
191+
if logs != "" {
192+
res.Logs = append(res.Logs, logs)
193+
}
179194
return &mcp.CallToolResult{
180195
// Gemini:
181196
// 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) {...}...

pkg/mcp/toolset/shell.go

Lines changed: 5 additions & 1 deletion
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, err := ts.TranslateHostPath(args.Directory)
24+
guestPath, logs, err := ts.TranslateHostPath(args.Directory)
2525
if err != nil {
2626
return nil, nil, err
2727
}
@@ -36,6 +36,10 @@ func (ts *ToolSet) RunShellCommand(ctx context.Context,
3636
Stdout: stdout.String(),
3737
Stderr: stderr.String(),
3838
}
39+
40+
if logs != "" {
41+
res.Logs = append(res.Logs, logs)
42+
}
3943
if cmdErr == nil {
4044
res.ExitCode = ptr.Of(0)
4145
} else {

pkg/mcp/toolset/toolset.go

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

106-
func (ts *ToolSet) TranslateHostPath(hostPath string) (string, error) {
106+
func (ts *ToolSet) TranslateHostPath(hostPath string) (guestPath, logs string, err error) {
107107
if hostPath == "" {
108-
return "", errors.New("path is empty")
108+
return "", "", errors.New("path is empty")
109109
}
110110
if !filepath.IsAbs(hostPath) {
111-
return "", fmt.Errorf("expected an absolute path, got a relative path: %q", hostPath)
111+
return "", "", fmt.Errorf("expected an absolute path, got a relative path: %q", hostPath)
112112
}
113113

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

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

pkg/mcp/toolset/toolset_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestTranslateHostPath(t *testing.T) {
2020
hostPath string
2121
toolSet ToolSet
2222
wantGuestPath string
23+
wantLogs bool
2324
wantErr bool
2425
}{
2526
{
@@ -35,6 +36,7 @@ func TestTranslateHostPath(t *testing.T) {
3536
},
3637
},
3738
wantGuestPath: "/mnt/home-user/documents/file.txt",
39+
wantLogs: false,
3840
wantErr: false,
3941
},
4042
{
@@ -50,6 +52,7 @@ func TestTranslateHostPath(t *testing.T) {
5052
},
5153
},
5254
wantGuestPath: "/other/path/file.txt",
55+
wantLogs: true,
5356
wantErr: false,
5457
},
5558
{
@@ -65,6 +68,7 @@ func TestTranslateHostPath(t *testing.T) {
6568
},
6669
},
6770
wantGuestPath: "/home/user2/file.txt",
71+
wantLogs: true,
6872
wantErr: false,
6973
},
7074
{
@@ -81,18 +85,24 @@ func TestTranslateHostPath(t *testing.T) {
8185
},
8286
},
8387
wantGuestPath: "/mnt/tmp/myfile",
88+
wantLogs: false,
8489
wantErr: false,
8590
},
8691
}
8792

8893
for _, test := range tests {
8994
t.Run(test.name, func(t *testing.T) {
90-
got, err := test.toolSet.TranslateHostPath(test.hostPath)
95+
got, logs, err := test.toolSet.TranslateHostPath(test.hostPath)
9196
if test.wantErr {
9297
assert.Assert(t, err != nil)
9398
} else {
9499
assert.NilError(t, err)
95100
assert.Equal(t, test.wantGuestPath, got)
101+
if test.wantLogs {
102+
assert.Assert(t, logs != "")
103+
} else {
104+
assert.Equal(t, "", logs)
105+
}
96106
}
97107
})
98108
}

0 commit comments

Comments
 (0)