Skip to content

Commit 90ee4d7

Browse files
authored
Refactor stdout redirection logic in MCP server to capture output from all tools (#974)
1 parent d56198b commit 90ee4d7

4 files changed

Lines changed: 448 additions & 429 deletions

File tree

pkg/cli/mcp_server.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,36 @@ import (
1515
func redirectStdoutToString(fn func() error) (string, error) {
1616
// Save original stdout
1717
originalStdout := os.Stdout
18-
18+
1919
// Create a pipe to capture stdout
2020
r, w, err := os.Pipe()
2121
if err != nil {
2222
return "", fmt.Errorf("failed to create pipe: %w", err)
2323
}
24-
24+
2525
// Redirect stdout to the pipe
2626
os.Stdout = w
27-
27+
2828
// Execute the function
2929
fnErr := fn()
30-
30+
3131
// Close the write end and restore stdout
3232
w.Close()
3333
os.Stdout = originalStdout
34-
34+
3535
// Read the captured output
3636
output, readErr := io.ReadAll(r)
3737
r.Close()
38-
38+
3939
if readErr != nil {
4040
return "", fmt.Errorf("failed to read captured output: %w", readErr)
4141
}
42-
42+
4343
// Return the captured output and any error from the function
4444
if fnErr != nil {
4545
return string(output), fnErr
4646
}
47-
47+
4848
return string(output), nil
4949
}
5050

@@ -132,16 +132,25 @@ func createMCPServer(verbose bool, allowedTools []string) *mcp.Server {
132132
count = 30
133133
}
134134

135-
err := DownloadWorkflowLogs(args.Workflow, count, "", "", "", args.Engine, "", 0, 0, args.Verbose || verbose, false, false)
135+
// Capture the logs output using the helper function
136+
output, err := redirectStdoutToString(func() error {
137+
return DownloadWorkflowLogs(args.Workflow, count, "", "", "", args.Engine, "", 0, 0, args.Verbose || verbose, false, false)
138+
})
139+
136140
if err != nil {
137141
return &mcp.CallToolResult{
138142
IsError: true,
139143
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Error downloading logs: %v", err)}},
140144
}, nil, nil
141145
}
142146

147+
// Provide the actual output or a default message
148+
if output == "" {
149+
output = "Successfully downloaded and analyzed workflow logs"
150+
}
151+
143152
return &mcp.CallToolResult{
144-
Content: []mcp.Content{&mcp.TextContent{Text: "Successfully downloaded and analyzed workflow logs"}},
153+
Content: []mcp.Content{&mcp.TextContent{Text: output}},
145154
}, nil, nil
146155
})
147156
}
@@ -247,16 +256,25 @@ func createMCPServer(verbose bool, allowedTools []string) *mcp.Server {
247256
}, nil, nil
248257
}
249258

250-
err := AddMCPTool(args.Workflow, args.Server, args.Registry, args.Transport, args.ToolID, args.Verbose || verbose)
259+
// Capture the mcp_add output using the helper function
260+
output, err := redirectStdoutToString(func() error {
261+
return AddMCPTool(args.Workflow, args.Server, args.Registry, args.Transport, args.ToolID, args.Verbose || verbose)
262+
})
263+
251264
if err != nil {
252265
return &mcp.CallToolResult{
253266
IsError: true,
254267
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Error adding MCP tool: %v", err)}},
255268
}, nil, nil
256269
}
257270

271+
// Provide the actual output or a default message
272+
if output == "" {
273+
output = fmt.Sprintf("Successfully added MCP tool '%s' to workflow '%s'", args.Server, args.Workflow)
274+
}
275+
258276
return &mcp.CallToolResult{
259-
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Successfully added MCP tool '%s' to workflow '%s'", args.Server, args.Workflow)}},
277+
Content: []mcp.Content{&mcp.TextContent{Text: output}},
260278
}, nil, nil
261279
})
262280
}
@@ -284,21 +302,29 @@ func createMCPServer(verbose bool, allowedTools []string) *mcp.Server {
284302
}, nil, nil
285303
}
286304

287-
err := RunWorkflowsOnGitHub(args.Workflows, args.Repeat, args.Enable, args.Verbose || verbose)
305+
// Capture the run output using the helper function
306+
output, err := redirectStdoutToString(func() error {
307+
return RunWorkflowsOnGitHub(args.Workflows, args.Repeat, args.Enable, args.Verbose || verbose)
308+
})
309+
288310
if err != nil {
289311
return &mcp.CallToolResult{
290312
IsError: true,
291313
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Error running workflows: %v", err)}},
292314
}, nil, nil
293315
}
294316

295-
message := fmt.Sprintf("Successfully ran %d workflow(s)", len(args.Workflows))
296-
if len(args.Workflows) == 1 {
297-
message = fmt.Sprintf("Successfully ran workflow: %s", args.Workflows[0])
317+
// Provide the actual output or generate a default message
318+
if output == "" {
319+
message := fmt.Sprintf("Successfully ran %d workflow(s)", len(args.Workflows))
320+
if len(args.Workflows) == 1 {
321+
message = fmt.Sprintf("Successfully ran workflow: %s", args.Workflows[0])
322+
}
323+
output = message
298324
}
299325

300326
return &mcp.CallToolResult{
301-
Content: []mcp.Content{&mcp.TextContent{Text: message}},
327+
Content: []mcp.Content{&mcp.TextContent{Text: output}},
302328
}, nil, nil
303329
})
304330
}

0 commit comments

Comments
 (0)