95 lines
3.1 KiB
Go
95 lines
3.1 KiB
Go
package archiegateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"mime"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec"
|
|
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
)
|
|
|
|
func executeTaskTool(ctx context.Context, entry tools.ToolEntry, input json.RawMessage, limit int) (result *mcp.CallToolResult, err error) {
|
|
defer func() {
|
|
if recover() != nil {
|
|
result = nil
|
|
err = fmt.Errorf("tool execution failed")
|
|
}
|
|
}()
|
|
if !entry.Available() {
|
|
return nil, fmt.Errorf("tool is unavailable")
|
|
}
|
|
schema, err := json.Marshal(taskToolSchema(entry))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("tool schema unavailable")
|
|
}
|
|
if len(input) == 0 {
|
|
input = json.RawMessage(`{}`)
|
|
}
|
|
if reason, ok := agentexec.ValidateCaptureArgs(agentrun.CaptureTool{Name: entry.Name, Parameters: schema}, input); !ok {
|
|
return nil, fmt.Errorf("%s", reason)
|
|
}
|
|
var args map[string]any
|
|
if err := json.Unmarshal(input, &args); err != nil {
|
|
return nil, fmt.Errorf("arguments must be an object")
|
|
}
|
|
out, err := entry.Handler(ctx, args)
|
|
if err != nil {
|
|
return &mcp.CallToolResult{IsError: true, Content: []mcp.Content{&mcp.TextContent{Text: "tool execution failed"}}}, nil
|
|
}
|
|
if entry.MaxResultSizeChars != 0 {
|
|
limit = entry.MaxResultSizeChars
|
|
}
|
|
if media, ok := out.(tools.MultimodalResult); ok {
|
|
return taskToolMedia(entry.Name, media, limit)
|
|
}
|
|
raw, err := json.Marshal(out)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encode tool result")
|
|
}
|
|
return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: tools.CapPayload(entry.Name, string(raw), limit, "")}}}, nil
|
|
}
|
|
|
|
func taskToolMedia(name string, media tools.MultimodalResult, limit int) (*mcp.CallToolResult, error) {
|
|
result := &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: tools.CapPayload(name, media.Summary, limit, "")}}}
|
|
root, err := os.OpenRoot(media.SubdirHint)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("tool media unavailable")
|
|
}
|
|
defer root.Close()
|
|
for _, file := range media.Files {
|
|
relative, err := filepath.Rel(media.SubdirHint, file)
|
|
if err != nil || !filepath.IsLocal(relative) {
|
|
return nil, fmt.Errorf("tool media outside provider directory")
|
|
}
|
|
raw, err := root.ReadFile(relative)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read tool media")
|
|
}
|
|
mimeType := mime.TypeByExtension(filepath.Ext(file))
|
|
switch mimeType {
|
|
case "image/png", "image/jpeg", "image/gif", "image/webp":
|
|
result.Content = append(result.Content, &mcp.ImageContent{Data: raw, MIMEType: mimeType})
|
|
case "audio/wav", "audio/mpeg":
|
|
result.Content = append(result.Content, &mcp.AudioContent{Data: raw, MIMEType: mimeType})
|
|
default:
|
|
result.Content = append(result.Content, &mcp.EmbeddedResource{Resource: &mcp.ResourceContents{URI: "archie-media:///" + filepath.Base(file), MIMEType: mimeType, Blob: raw}})
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func taskToolSchema(entry tools.ToolEntry) tools.JSONSchema {
|
|
schema := entry.ResolvedSchema()
|
|
if schema == nil {
|
|
return tools.JSONSchema{"type": "object", "properties": map[string]any{}}
|
|
}
|
|
return schema
|
|
}
|