forgejo-mcp/pkg/to/to.go
Christoph Görn 2e2604dd73
fix: add /v2 suffix to module path for go install compatibility
Go modules with major version v2+ require the module path to include
the version suffix. Without this, `go install` fails with:
"invalid version: module contains a go.mod file, so module path must
match major version"

Updated go.mod and all internal imports to use
codeberg.org/goern/forgejo-mcp/v2
2025-12-29 15:51:03 +01:00

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: %v", 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 any = 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
}