| 1 | package extension |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "sync" |
| 7 | ) |
| 8 | |
| 9 | // MCPBackend is one replaceable MCP server connection behind MCPProxy. |
| 10 | // The plugin.Host client satisfies this via a thin adapter at the call site. |
| 11 | type MCPBackend interface { |
| 12 | Backend |
| 13 | // Call invokes one tool on this backend. |
| 14 | Call(ctx context.Context, toolName string, args []byte) ([]byte, error) |
| 15 | } |
| 16 | |
| 17 | // MCPProxy is a stable consumer-facing handle for one MCP server name. Tool |
| 18 | // schemas and the provider-visible tool prefix stay owned by RuntimeSnapshot; |
| 19 | // this proxy only swaps the live process/connection underneath. |
| 20 | type MCPProxy struct { |
| 21 | name string |
| 22 | inner *StableProxy |
| 23 | } |
| 24 | |
| 25 | // NewMCPProxy returns an empty proxy for the given server name. |
| 26 | func NewMCPProxy(name string) *MCPProxy { |
| 27 | return &MCPProxy{name: name, inner: NewStableProxy()} |
| 28 | } |
| 29 | |
| 30 | // Name returns the stable MCP server identity. |
| 31 | func (p *MCPProxy) Name() string { |
| 32 | if p == nil { |
| 33 | return "" |
| 34 | } |
| 35 | return p.name |
| 36 | } |
| 37 | |
| 38 | // Replace swaps the active MCP backend and drains the previous one. |
| 39 | func (p *MCPProxy) Replace(ctx context.Context, next MCPBackend, generation uint64) error { |
| 40 | if p == nil || p.inner == nil { |
| 41 | return fmt.Errorf("extension: nil MCPProxy") |
| 42 | } |
| 43 | return p.inner.Replace(ctx, next, generation) |
| 44 | } |
| 45 | |
| 46 | // Call routes a tool invocation to the active backend. Fail-fast when no |
| 47 | // backend is registered (crash/replace window). In-flight calls cancel when |
| 48 | // the backend is replaced or the proxy is closed (drain). |
| 49 | func (p *MCPProxy) Call(ctx context.Context, toolName string, args []byte) ([]byte, error) { |
| 50 | if p == nil || p.inner == nil { |
| 51 | return nil, fmt.Errorf("extension: MCP proxy unavailable") |
| 52 | } |
| 53 | var out []byte |
| 54 | err := p.inner.CallCtx(ctx, func(callCtx context.Context, b Backend) error { |
| 55 | mcp, ok := b.(MCPBackend) |
| 56 | if !ok { |
| 57 | return fmt.Errorf("extension: MCP backend type mismatch") |
| 58 | } |
| 59 | var callErr error |
| 60 | out, callErr = mcp.Call(callCtx, toolName, args) |
| 61 | return callErr |
| 62 | }) |
| 63 | return out, err |
| 64 | } |
| 65 | |
| 66 | // CancelInFlight aborts outstanding MCP calls (generation drain). |
| 67 | func (p *MCPProxy) CancelInFlight() { |
| 68 | if p == nil || p.inner == nil { |
| 69 | return |
| 70 | } |
| 71 | p.inner.CancelInFlight() |
| 72 | } |
| 73 | |
| 74 | // Close drains the active backend. |
| 75 | func (p *MCPProxy) Close(ctx context.Context) error { |
| 76 | if p == nil || p.inner == nil { |
| 77 | return nil |
| 78 | } |
| 79 | return p.inner.Close(ctx) |
| 80 | } |
| 81 | |
| 82 | // Generation returns the active backend generation. |
| 83 | func (p *MCPProxy) Generation() uint64 { |
| 84 | if p == nil || p.inner == nil { |
| 85 | return 0 |
| 86 | } |
| 87 | return p.inner.Generation() |
| 88 | } |
| 89 | |
| 90 | // MCPProxySet is a named registry of stable MCP proxies for one generation. |
| 91 | type MCPProxySet struct { |
| 92 | mu sync.Mutex |
| 93 | byName map[string]*MCPProxy |
| 94 | } |
| 95 | |
| 96 | // NewMCPProxySet returns an empty set. |
| 97 | func NewMCPProxySet() *MCPProxySet { |
| 98 | return &MCPProxySet{byName: make(map[string]*MCPProxy)} |
| 99 | } |
| 100 | |
| 101 | // Get returns the stable proxy for name, creating it if needed. |
| 102 | func (s *MCPProxySet) Get(name string) *MCPProxy { |
| 103 | if s == nil { |
| 104 | return NewMCPProxy(name) |
| 105 | } |
| 106 | s.mu.Lock() |
| 107 | defer s.mu.Unlock() |
| 108 | if s.byName == nil { |
| 109 | s.byName = make(map[string]*MCPProxy) |
| 110 | } |
| 111 | if p, ok := s.byName[name]; ok { |
| 112 | return p |
| 113 | } |
| 114 | p := NewMCPProxy(name) |
| 115 | s.byName[name] = p |
| 116 | return p |
| 117 | } |
| 118 | |
| 119 | // Close drains every proxy. |
| 120 | func (s *MCPProxySet) Close(ctx context.Context) error { |
| 121 | if s == nil { |
| 122 | return nil |
| 123 | } |
| 124 | s.mu.Lock() |
| 125 | proxies := make([]*MCPProxy, 0, len(s.byName)) |
| 126 | for _, p := range s.byName { |
| 127 | proxies = append(proxies, p) |
| 128 | } |
| 129 | s.byName = nil |
| 130 | s.mu.Unlock() |
| 131 | var first error |
| 132 | for _, p := range proxies { |
| 133 | if err := p.Close(ctx); err != nil && first == nil { |
| 134 | first = err |
| 135 | } |
| 136 | } |
| 137 | return first |
| 138 | } |
| 139 |