| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | |
| 9 | "reasonix/internal/plugin" |
| 10 | "reasonix/internal/tool" |
| 11 | ) |
| 12 | |
| 13 | // resolveRegistryTool binds a registry tool by name for use_capability call. |
| 14 | // MCP adapters additionally cross the current runtime authorization boundary. |
| 15 | func (t *UseCapabilityTool) resolveRegistryTool(ctx context.Context, name, id string, args json.RawMessage, base tool.ResolvedCall) (tool.ResolvedCall, error) { |
| 16 | name = strings.TrimSpace(name) |
| 17 | if name == "" { |
| 18 | return tool.ResolvedCall{}, fmt.Errorf("capability id %q is missing a tool name", id) |
| 19 | } |
| 20 | if name == "use_capability" { |
| 21 | return tool.ResolvedCall{}, fmt.Errorf("cannot proxy use_capability through itself") |
| 22 | } |
| 23 | if t.registry == nil { |
| 24 | return t.resolveUnavailable(base, id, name, "tool registry is unavailable"), nil |
| 25 | } |
| 26 | target, ok := t.registry.Get(name) |
| 27 | if !ok { |
| 28 | return t.resolveUnavailable(base, id, name, fmt.Sprintf("tool %q is not registered in this session", name)), nil |
| 29 | } |
| 30 | if metadata, isMCP := target.(tool.MCPMetadata); isMCP && t.runtime != nil { |
| 31 | server := strings.TrimSpace(metadata.MCPServerName()) |
| 32 | raw := strings.TrimSpace(metadata.MCPRawToolName()) |
| 33 | if server == "" || raw == "" { |
| 34 | return t.resolveUnavailable(base, id, name, fmt.Sprintf("MCP tool %q has incomplete runtime identity", name)), nil |
| 35 | } |
| 36 | spec, unlock, err := t.lockAuthorizedRuntimeServer(ctx, server) |
| 37 | if err != nil { |
| 38 | return t.resolveUnavailable(base, id, name, err.Error()), nil |
| 39 | } |
| 40 | matches := plugin.MCPToolMatchesSpec(target, spec) |
| 41 | unlock() |
| 42 | if !matches { |
| 43 | return t.resolveUnavailable(base, id, name, fmt.Sprintf("connected MCP server %q identity does not match the current runtime configuration", server)), nil |
| 44 | } |
| 45 | target = t.bindRuntimeMCP(spec, target) |
| 46 | } |
| 47 | base.Target = target |
| 48 | base.TargetName = name |
| 49 | base.ReadOnly = target.ReadOnly() |
| 50 | if len(args) == 0 { |
| 51 | base.Args = json.RawMessage(`{}`) |
| 52 | } else { |
| 53 | base.Args = args |
| 54 | } |
| 55 | return base, nil |
| 56 | } |
| 57 |