Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/state-mcp/internal/registry/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ func DownloadLogsTool() Tool {
}

runner := downloadlogs.New(p)
params := downloadlogs.NewParams(url)
params := downloadlogs.NewParams()
params.LogUrl = url
err = runner.Run(params)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("error downloading logs: %s", errs.JoinMessage(err))), nil
Expand Down
71 changes: 59 additions & 12 deletions internal/runners/mcp/downloadlogs/downloadlogs.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package downloadlogs

import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/primer"
Expand All @@ -20,29 +23,73 @@ func New(p *primer.Values) *DownloadLogsRunner {
}

type Params struct {
logUrl string
LogUrl string
}

func NewParams(logUrl string) *Params {
return &Params{
logUrl: logUrl,
}
func NewParams() *Params {
return &Params{}
}

// Example: {"body": {"facility": "INFO", "msg": "..."}, "artifact_id": "...", "timestamp": "2025-08-12T19:23:51.702971", "type": "artifact_progress", "source": "build-wrapper", "pid": 19}
type LogLine struct {
Body struct {
Msg string `json:"msg"`
} `json:"body"`
}

func (runner *DownloadLogsRunner) Run(params *Params) error {
response, err := http.Get(params.logUrl)
response, err := http.Get(params.LogUrl)
if err != nil {
return fmt.Errorf("error while downloading logs: %v", err)
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("error reading response body: %v", err)
}
if response.StatusCode != 200 {
if response.StatusCode != http.StatusOK {
body, _ := io.ReadAll(response.Body)
return fmt.Errorf("error fetching logs: status %d, %s", response.StatusCode, body)
}
runner.output.Print(string(body))

scanner := bufio.NewScanner(response.Body)

// Read all lines, parse and only store the text messages
var lines []string
for scanner.Scan() {
line := scanner.Text()
var logLine LogLine
if err := json.Unmarshal([]byte(line), &logLine); err != nil {
continue
}
lines = append(lines, logLine.Body.Msg)
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading log content: %v", err)
}

// Check what lines contain the keyword "error" and print the previous 10 and next 10 lines
printedLines := make(map[int]bool)
for i, line := range lines {
if strings.Contains(strings.ToLower(line), "error") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should perhaps include some more synonyms here, eg.

fail
critical
panic
exception

start := i - 10
if start < 0 {
start = 0
}
end := i + 10
if end >= len(lines) {
end = len(lines) - 1
}

for j := start; j <= end; j++ {
if !printedLines[j] {
// Print ellipsis if there are skipped lines
if j > 0 && !printedLines[j-1] {
runner.output.Print("[...]")
}
runner.output.Print(lines[j])
printedLines[j] = true
}
}
}
}

return nil
}
Loading