agent/pkg/loop/tools_mcp.go
Snider 5eb26f90fc refactor: replace fmt.Errorf/os.* with go-io/go-log conventions
Replace all fmt.Errorf and errors.New in production code with
coreerr.E("caller.Method", "message", err) from go-log. Replace
all os.ReadFile/os.WriteFile/os.MkdirAll/os.Remove with coreio.Local
equivalents from go-io. Test files are intentionally untouched.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-16 21:48:31 +00:00

47 lines
1.2 KiB
Go

package loop
import (
"context"
"encoding/json"
coreerr "forge.lthn.ai/core/go-log"
aimcp "forge.lthn.ai/core/mcp/pkg/mcp"
)
// LoadMCPTools converts all tools from a go-ai MCP Service into loop.Tool values.
func LoadMCPTools(svc *aimcp.Service) []Tool {
var tools []Tool
for _, record := range svc.Tools() {
tools = append(tools, Tool{
Name: record.Name,
Description: record.Description,
Parameters: record.InputSchema,
Handler: WrapRESTHandler(RESTHandlerFunc(record.RESTHandler)),
})
}
return tools
}
// RESTHandlerFunc matches go-ai's mcp.RESTHandler signature.
type RESTHandlerFunc func(ctx context.Context, body []byte) (any, error)
// WrapRESTHandler converts a go-ai RESTHandler into a loop.Tool handler.
func WrapRESTHandler(handler RESTHandlerFunc) func(context.Context, map[string]any) (string, error) {
return func(ctx context.Context, args map[string]any) (string, error) {
body, err := json.Marshal(args)
if err != nil {
return "", coreerr.E("mcp.handler", "marshal args", err)
}
result, err := handler(ctx, body)
if err != nil {
return "", err
}
out, err := json.Marshal(result)
if err != nil {
return "", coreerr.E("mcp.handler", "marshal result", err)
}
return string(out), nil
}
}