Files
gitea-mcp/internal/registry/registry.go
2026-05-04 20:59:15 +02:00

46 lines
1.0 KiB
Go

package registry
import (
"context"
"encoding/json"
"errors"
"fmt"
)
var ErrToolNotFound = errors.New("tool not found")
type ToolDescriptor struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema json.RawMessage `json:"inputSchema"`
}
type Tool interface {
Descriptor() ToolDescriptor
Call(ctx context.Context, args json.RawMessage) (json.RawMessage, error)
}
type Registry struct {
tools map[string]Tool
}
func New() *Registry { return &Registry{tools: map[string]Tool{}} }
func (r *Registry) Register(t Tool) { r.tools[t.Descriptor().Name] = t }
func (r *Registry) Tools() []ToolDescriptor {
out := make([]ToolDescriptor, 0, len(r.tools))
for _, t := range r.tools {
out = append(out, t.Descriptor())
}
return out
}
func (r *Registry) Dispatch(ctx context.Context, name string, args json.RawMessage) (json.RawMessage, error) {
t, ok := r.tools[name]
if !ok {
return nil, fmt.Errorf("tool %q: %w", name, ErrToolNotFound)
}
return t.Call(ctx, args)
}