internal/musebridge/mcp_shim.go

package musebridge

import (
	"strings"

	"git.cafe/ezterry/ez-agent/internal/bridgemcp"
	"git.cafe/ezterry/ez-agent/pkg/externalprovider"
)

type MCPCall = bridgemcp.MCPCall
type ToolNames = bridgemcp.ToolNames

func NewToolNames(tools []externalprovider.Tool) (*ToolNames, error) {
	return bridgemcp.NewToolNames(tools)
}

// MCPShim keeps Muse-only description and diagnostic policies around the shared proxy.
type MCPShim struct{ *bridgemcp.MCPShim }

func NewMCPShim(tools []externalprovider.Tool) (*MCPShim, error) {
	_, bodyLimit := safeBackendFailure(backendError("execution", "limit", scannerGuard("mcp_request_bytes", externalprovider.MaxFrameBytes)))
	shared, err := bridgemcp.NewMCPShim(tools, bridgemcp.Options{
		Describe: museToolDescription, DiagnosticLabel: museToolLabel, Trace: traceEvent, BodyLimitMessage: bodyLimit,
		Failure: func(category, reason string, observed, limit int) error {
			if limit > 0 {
				return measuredGuard(category, reason, observed, limit)
			}
			return guard(category, reason)
		},
	})
	if err != nil {
		return nil, err
	}
	return &MCPShim{shared}, nil
}
func museToolDescription(t externalprovider.Tool) string {
	description := t.Description
	switch t.Name {
	case "buffer":
		description += "\nBridge workspace: relative file: paths resolve from the actual project CWD in conversation_metadata, not Muse's scratch workspace."
	case "write_file":
		description += "\nBridge workspace: relative file paths resolve from the actual project CWD in conversation_metadata, not Muse's scratch workspace."
	case "run-cmd":
		description += "\nBridge workspace: commands execute from the actual project CWD in conversation_metadata, not Muse's scratch workspace."
		description += "\n" + bridgeRunCommandTimeoutHint
	}
	return description
}
func (s *MCPShim) fail(err error)            { s.Fail(err) }
func (s *MCPShim) setSettled(idle bool) bool { return s.SetSettled(idle) }

// This provider-specific reminder does not change the host tool's description.
const bridgeRunCommandTimeoutHint = "Bridge run-cmd reminder: a finite timeout terminates the command, not just the wait. Set timeout to \"0s\" to disable it. background: true returns after startup but does not disable the timeout. For Bash, expiry sends SIGKILL to the owned process group. Earlier side effects may already have happened; verify their state before retrying."

// museToolName is the spelling observed in native stored call records. It is
// not an assertion about the names in model-facing function definitions.
func museToolName(proxy string) string {
	return "mcp__ezagent__" + strings.ReplaceAll(proxy, "-", "_")
}

// Keep diagnostic tool identities finite and require an exact offered identity.
// Custom names and arbitrary native names cannot enter the safe trace contract.
func (s *MCPShim) safeOfferedToolLabel(name string) string {
	if s == nil || s.MCPShim == nil {
		return "unattributed"
	}
	return museDiagnosticLabel(name, s.ProxyName)
}

func museToolLabel(name string, names *ToolNames) string {
	return museDiagnosticLabel(name, names.Proxy)
}

func museDiagnosticLabel(name string, lookup func(string) (string, bool)) string {
	for _, host := range []string{"run-cmd", "buffer", "write_file"} {
		proxy, offered := lookup(host)
		if offered && (name == host || name == proxy || name == museToolName(proxy)) {
			return host
		}
	}
	return "unattributed"
}