Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/guard/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"fmt"
"log"
"sync"

"github.com/githubnext/gh-aw-mcpg/internal/logger"
)

var debugLog = logger.New("guard:registry")

// Registry manages guard instances for different MCP servers
type Registry struct {
guards map[string]Guard // serverID -> guard
Expand All @@ -14,13 +18,15 @@ type Registry struct {

// NewRegistry creates a new guard registry
func NewRegistry() *Registry {
debugLog.Print("Creating new guard registry")
return &Registry{
guards: make(map[string]Guard),
}
}

// Register registers a guard for a specific server
func (r *Registry) Register(serverID string, guard Guard) {
debugLog.Printf("Registering guard for serverID=%s, guardName=%s", serverID, guard.Name())
r.mu.Lock()
defer r.mu.Unlock()

Expand All @@ -30,14 +36,17 @@ func (r *Registry) Register(serverID string, guard Guard) {

// Get retrieves the guard for a server, or returns a noop guard if not found
func (r *Registry) Get(serverID string) Guard {
debugLog.Printf("Getting guard for serverID=%s", serverID)
r.mu.RLock()
defer r.mu.RUnlock()

if guard, ok := r.guards[serverID]; ok {
debugLog.Printf("Found guard for serverID=%s, guardName=%s", serverID, guard.Name())
return guard
}

// Return noop guard as default
debugLog.Printf("No guard registered for serverID=%s, returning noop guard", serverID)
log.Printf("[Guard] No guard registered for server '%s', using noop guard", serverID)
return NewNoopGuard()
}
Expand All @@ -52,6 +61,7 @@ func (r *Registry) Has(serverID string) bool {

// Remove removes a guard registration
func (r *Registry) Remove(serverID string) {
debugLog.Printf("Removing guard for serverID=%s", serverID)
r.mu.Lock()
defer r.mu.Unlock()
delete(r.guards, serverID)
Expand Down Expand Up @@ -100,19 +110,23 @@ func RegisterGuardType(name string, factory GuardFactory) {

// CreateGuard creates a guard instance by name using registered factories
func CreateGuard(name string) (Guard, error) {
debugLog.Printf("Creating guard with name=%s", name)
registeredGuardsMu.RLock()
defer registeredGuardsMu.RUnlock()

// Handle built-in guards
if name == "noop" || name == "" {
debugLog.Print("Using built-in noop guard")
return NewNoopGuard(), nil
}

// Try to find in registered factories
if factory, ok := registeredGuards[name]; ok {
debugLog.Printf("Found factory for guard type: %s", name)
return factory()
}

debugLog.Printf("Unknown guard type: %s", name)
return nil, fmt.Errorf("unknown guard type: %s", name)
}

Expand Down