Skip to content

cleanup: handleListDevices builds output with string concatenation loop instead of strings.Join #34

Description

@gitcoder89431

Problem

In internal/mcpserver/server.go, handleListDevices builds its output with an intermediate []string slice and then a second loop concatenating strings:

var lines []string
for _, d := range devices {
    lines = append(lines, fmt.Sprintf("[%s] %s — %s", d.Status, d.Name, d.ID))
}
out := ""
for _, l := range lines {
    out += l + "\n"
}

This is two passes, an extra allocation, and less idiomatic Go.

Fix

Use strings.Builder or collapse into a single loop:

var b strings.Builder
for _, d := range devices {
    fmt.Fprintf(&b, "[%s] %s — %s\n", d.Status, d.Name, d.ID)
}
return mcp.NewToolResultText(b.String()), nil

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions