- errorlint: fmt.Errorf %v→%w across all handlers (errors now wrap the underlying SDK/HTTP error for errors.Is/As); errors.Is for context.Canceled in cmd - staticcheck quickfixes: Fprintf over WriteString(Sprintf), tagged switches, omit inferred 'any'; QF1001 resolved by hoisting hex checks into isHex predicates (normalizeColor, validateSHA) - govet: reflect.Ptr → reflect.Pointer in pkg/ptr - errcheck: explicit '_ =' for fs.Parse (ExitOnError) and deferred zap Sync - repair four test assertions the autofix broke (it dropped the 're.Code != X' half of combined type-assert checks, inverting the test); all errors.As sites use 'var re *resource.ResourceError' golangci-lint: 0 issues. staticcheck, vet, tests: clean.
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package to
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"codeberg.org/goern/forgejo-mcp/v2/pkg/log"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
)
|
|
|
|
type textResult struct {
|
|
Result any
|
|
}
|
|
|
|
func SafeJSONMarshal(v any) string {
|
|
data, err := json.Marshal(v)
|
|
if err != nil {
|
|
log.Errorf("JSON marshal error: %v", err)
|
|
return `"Data couldn't be serialized properly"`
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
func TextResult(v any) (*mcp.CallToolResult, error) {
|
|
result := textResult{v}
|
|
resultBytes, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("marshal result err: %w", err)
|
|
}
|
|
log.Debugf("Text Result: %s", string(resultBytes))
|
|
return mcp.NewToolResultText(string(resultBytes)), nil
|
|
}
|
|
|
|
// SafeTextResult creates a text result with additional safety checks
|
|
func SafeTextResult(v any) (*mcp.CallToolResult, error) {
|
|
// If v is a struct or complex type, try to convert it to a simple map
|
|
// This provides an extra layer of safety against SDK-specific types
|
|
var safeResult = v
|
|
|
|
jsonStr := SafeJSONMarshal(safeResult)
|
|
return mcp.NewToolResultText(fmt.Sprintf(`{"Result":%s}`, jsonStr)), nil
|
|
}
|
|
|
|
func ErrorResult(err error) (*mcp.CallToolResult, error) {
|
|
log.Errorf(err.Error())
|
|
return nil, err
|
|
}
|