Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

10.0 KB
Raw Blame
package tools

import (
	"context"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/Tencent/WeKnora/internal/models/chat"
	"github.com/Tencent/WeKnora/internal/types"
)

const (
	mcpStartupGrace       = time.Second
	mcpCatalogLoadTimeout = 30 * time.Second
)

// MCPRegisteredTool is a model-visible definition bound to one authorized
// catalog and schema. It must not bypass the directory's live permission and
// configuration checks just because its definition was advertised up front.
type MCPRegisteredTool struct {
	*MCPTool
	catalog *MCPCatalog
	ref     string
}

// Description identifies the external service and original tool.
func (t *MCPRegisteredTool) Description() string {
	return fmt.Sprintf(
		"[MCP service %q, server_id=%q, tool=%q (external)] %s",
		t.service.Name,
		t.service.ID,
		t.mcpTool.Name,
		t.mcpTool.Description,
	)
}

// Execute revalidates the catalog before invoking the bound tool.
func (t *MCPRegisteredTool) Execute(ctx context.Context, args json.RawMessage) (*types.ToolResult, error) {
	tools, _, err := t.catalog.snapshot(ctx, t.service.ID, false)
	if err != nil {
		return mcpDiscoveryFailure(err, "unavailable")
	}
	for _, current := range tools {
		if mcpToolRef(current) != t.ref {
			continue
		}
		if err := t.catalog.checkEnabled(ctx, current); err != nil {
			return mcpDiscoveryFailure(err, "unavailable")
		}
		// Use the latest service configuration, retaining the advertised name for
		// approval/audit records. The registry already validated this exact schema.
		target := NewMCPTool(
			current.service,
			current.mcpTool,
			current.mcpManager,
			current.gate,
			current.authWaitTimeoutSeconds,
		)
		target.registeredName = t.Name()
		return target.Execute(ctx, args)
	}
	return mcpDiscoveryFailure(
		fmt.Errorf("MCP tool definition changed or was removed; rediscover before calling"),
		"unavailable",
	)
}

// mcpRegisteredName is independent of enumeration order and schema revisions.
// Hash raw server/tool identity: sanitizing Unicode or truncating names alone
// silently aliases distinct tools. Keep an ASCII hint for model readability.
func mcpRegisteredName(tool *MCPTool) string {
	sum := sha256.Sum256([]byte(tool.service.ID + "\x00" + tool.mcpTool.Name))
	hint := sanitizeName(tool.service.Name) + "_" + sanitizeName(tool.mcpTool.Name)
	const suffixChars = 16
	const maxHint = maxFunctionNameLength - len("mcp_") - 1 - suffixChars
	if len(hint) > maxHint {
		hint = hint[:maxHint]
	}
	return "mcp_" + hint + "_" + hex.EncodeToString(sum[:suffixChars/2])
}

func (r *ToolRegistry) mcpCatalog() *MCPCatalog {
	if tool, ok := r.tools[ToolDiscoverMCPTools].(*MCPDiscoverTool); ok {
		return tool.catalog
	}
	return nil
}

// PrepareMCPTools advertises sources up front, loading full functions only after
// describe. This is application-level deferred loading via ordinary functions.
// Production readers use persisted metadata and perform no upstream discovery.
func (r *ToolRegistry) PrepareMCPTools(ctx context.Context) {
	r.prepareMCPToolsWithMode(ctx, mcpStartupGrace, false)
}

// PrepareMCPToolsDirect is the full-exposure compatibility path.
func (r *ToolRegistry) PrepareMCPToolsDirect(ctx context.Context) {
	r.prepareMCPTools(ctx, mcpStartupGrace)
}

func (r *ToolRegistry) prepareMCPTools(ctx context.Context, grace time.Duration) {
	r.prepareMCPToolsWithMode(ctx, grace, true)
}

func (r *ToolRegistry) prepareMCPToolsWithMode(ctx context.Context, grace time.Duration, direct bool) {
	c := r.mcpCatalog()
	if c == nil || c.authorize(ctx) != nil {
		return
	}
	r.mcpPrepared, r.mcpDirect = true, direct
	discovery := r.tools[ToolDiscoverMCPTools].(*MCPDiscoverTool)
	discovery.directExposure, discovery.advertiseSources = direct, true
	c.preloadOnce.Do(func() {
		c.preloadDone = make(chan struct{})
		go func() {
			defer close(c.preloadDone)
			// prepareMCPToolsWithMode selects on preloadDone, so a panic in the
			// fan-out below must not skip the close; the barrier also records
			// every service this pass left unfinished.
			defer RecoverGoroutine(ctx, "MCP catalog preload", func() { c.markPreloadFailed() })
			loadCtx, cancel := context.WithTimeout(
				context.WithValue(ctx, execCtxKey{}, (*ToolExecContext)(nil)),
				mcpCatalogLoadTimeout,
			)
			defer cancel()
			var workers sync.WaitGroup
			// No ToolExecContext is installed during engine preparation, so this
			// path cannot open an in-conversation OAuth prompt for every service.
			ids := make([]string, 0, len(c.servers))
			for id := range c.servers {
				ids = append(ids, id)
			}
			sort.Strings(ids)
			jobs := make(chan string)
			for i := 0; i < min(8, len(ids)); i++ {
				workers.Add(1)
				go func() {
					// Two barriers: this one keeps a panic in the loop itself
					// (channel bookkeeping, Done) from escaping; the one inside
					// knows which service the MCP client panicked on.
					defer workers.Done()
					defer RecoverGoroutine(loadCtx, "MCP catalog preload worker", nil)
					for id := range jobs {
						func() {
							defer RecoverGoroutine(loadCtx, "MCP catalog preload for service "+id, func() {
								c.markPreloadFailed(id)
							})
							_, _, _ = c.snapshot(loadCtx, id, false)
						}()
					}
				}()
			}
		sendJobs:
			for _, id := range ids {
				select {
				case jobs <- id:
				case <-loadCtx.Done():
					break sendJobs
				}
			}
			close(jobs)
			workers.Wait()
		}()
	})
	timer := time.NewTimer(grace)
	defer timer.Stop()
	select {
	case <-c.preloadDone:
	case <-timer.C:
	case <-ctx.Done():
	}
	r.RefreshMCPTools(ctx)
}

// markPreloadFailed records a preload pass that a panic ended, reusing the
// catalog's existing store/status mechanism and the same "error" status
// snapshot() stores when a load returns an error. A service whose goroutine is
// gone must not stay "loading", which would read as a preload still in flight.
// With no id every service that never reached "ready" is marked; a ready
// service loaded fine and keeps its snapshot.
func (c *MCPCatalog) markPreloadFailed(ids ...string) {
	if len(ids) == 0 {
		ids = make([]string, 0, len(c.servers))
		for id := range c.servers {
			ids = append(ids, id)
		}
	}
	for _, id := range ids {
		entry := c.servers[id]
		if entry == nil {
			continue
		}
		entry.mu.Lock()
		service, status := entry.service, entry.status
		entry.mu.Unlock()
		if status == "ready" {
			continue
		}
		entry.store(service, nil, "error")
	}
}

// RememberMCPHistory republishes tools this session already described or called
// so a new engine does not require another describe round.
func (r *ToolRegistry) RememberMCPHistory(messages []chat.Message) {
	c := r.mcpCatalog()
	if c == nil {
		return
	}
	for _, msg := range messages {
		for _, call := range msg.ToolCalls {
			name := call.Function.Name
			if name == ToolCallMCPTool {
				var args struct {
					ToolRef string `json:"tool_ref"`
				}
				if json.Unmarshal([]byte(call.Function.Arguments), &args) == nil && args.ToolRef != "" {
					c.historyRefs.Store(args.ToolRef, true)
				}
				continue
			}
			if strings.HasPrefix(name, "mcp_") && name != ToolDiscoverMCPTools {
				c.historyNames.Store(name, true)
			}
		}
	}
}

// RefreshMCPTools publishes ready definitions between model requests, including
// catalogs loaded by discovery/OAuth or refreshed after the initial request.
// No network discovery occurs here. Policy checks remain fresh even when the
// metadata snapshot is cached. Call only when parallel tool execution is idle.
func (r *ToolRegistry) RefreshMCPTools(ctx context.Context) {
	if !r.mcpPrepared {
		return
	}
	c := r.mcpCatalog()
	if c == nil {
		return
	}
	// Keep the proxy executable for history, but do not offer an empty call
	// surface to the model. Publish it only alongside a usable full definition.
	r.deferred[ToolCallMCPTool] = true
	for name, tool := range r.tools {
		if _, ok := tool.(*MCPRegisteredTool); ok {
			delete(r.tools, name)
			delete(r.deferred, name)
		}
	}
	if c.authorize(ctx) != nil {
		return
	}
	ids := make([]string, 0, len(c.servers))
	for id := range c.servers {
		ids = append(ids, id)
	}
	sort.Strings(ids)
	for _, id := range ids {
		entry := c.servers[id]
		entry.mu.Lock()
		service, cached, status := entry.service, entry.tools, entry.status
		entry.mu.Unlock()
		if status != "ready" {
			continue
		}
		if c.lookup != nil {
			current, err := c.lookup(ctx, c.tenantID, id)
			if err != nil || current == nil || current.ID != id || !current.Enabled ||
				!current.UpdatedAt.Equal(service.UpdatedAt) {
				continue
			}
		}
		visible, err := c.visibleTools(ctx, id, cached)
		if err != nil {
			continue
		}
		for _, tool := range visible {
			if !r.mcpDirect && !c.advertised(tool) {
				continue
			}
			c.rememberAdvertised(tool)
			bound := NewMCPTool(tool.service, tool.mcpTool, tool.mcpManager, tool.gate, tool.authWaitTimeoutSeconds)
			bound.registeredName = mcpRegisteredName(tool)
			bound.serverInstructions = tool.serverInstructions
			r.RegisterTool(&MCPRegisteredTool{MCPTool: bound, catalog: c, ref: mcpToolRef(tool)})
			r.deferred[ToolCallMCPTool] = false
		}
	}
}

// MCPExposureSnapshot preserves definitions that were described but have not
// been called yet. Call history alone cannot restore this deferred tool state.
// References contain a schema hash; no credentials or remote definitions are saved.
func (r *ToolRegistry) MCPExposureSnapshot() []string {
	if r == nil {
		return nil
	}
	c := r.mcpCatalog()
	if c == nil {
		return nil
	}
	refs := []string{}
	c.described.Range(func(key, _ any) bool {
		if ref, ok := key.(string); ok {
			refs = append(refs, ref)
		}
		return true
	})
	sort.Strings(refs)
	return refs
}

// RestoreMCPExposure restores exposure only. Refresh and execution still check
// the current principal, enabled policy, service configuration and schema hash.
func (r *ToolRegistry) RestoreMCPExposure(refs []string) {
	if r == nil {
		return
	}
	c := r.mcpCatalog()
	if c == nil {
		return
	}
	for _, ref := range refs {
		if len(ref) != len("mcpt_")+sha256.Size*2 || !strings.HasPrefix(ref, "mcpt_") {
			continue
		}
		if _, err := hex.DecodeString(strings.TrimPrefix(ref, "mcpt_")); err != nil {
			continue
		}
		c.historyRefs.Store(ref, true)
	}
}