6119
My
mysis / files
mysis / internal / mcp / proxy.go
6.5 KB
1package mcp 2 3import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "fmt" 8 "regexp" 9 "strconv" 10 "strings" 11 "sync" 12 "time" 13 14 "github.com/rs/zerolog/log" 15) 16 17// ToolHandler is a function that handles a tool call. 18type ToolHandler func(ctx context.Context, arguments json.RawMessage) (*ToolResult, error) 19 20// Proxy combines an upstream MCP client with local tool handlers. 21type Proxy struct { 22 mu sync.RWMutex 23 upstream UpstreamClient 24 localTools map[string]Tool 25 localHandlers map[string]ToolHandler 26} 27 28var ( 29 ErrToolRetryExhausted = errors.New("mcp tool call failed after retries") 30) 31 32// Retry delays increased to respect SpaceMolt's "Try again in 5 seconds" rate limit 33var toolRetryDelays = []time.Duration{2 * time.Second, 5 * time.Second, 10 * time.Second} 34 35// retryAfterRegex matches "Retry-After: N" in error messages 36var retryAfterRegex = regexp.MustCompile(`Retry-After:\s*(\d+)`) 37 38// parseRetryAfter extracts retry delay from error message containing "Retry-After: N seconds" 39func parseRetryAfter(err error) (time.Duration, bool) { 40 if err == nil { 41 return 0, false 42 } 43 44 errMsg := err.Error() 45 46 // Try to extract from "Retry-After: N" header format 47 if matches := retryAfterRegex.FindStringSubmatch(errMsg); len(matches) > 1 { 48 if seconds, parseErr := strconv.Atoi(matches[1]); parseErr == nil { 49 return time.Duration(seconds) * time.Second, true 50 } 51 } 52 53 // Try to extract from "Try again in N seconds" message format 54 if strings.Contains(errMsg, "Try again in") { 55 re := regexp.MustCompile(`Try again in (\d+) seconds?`) 56 if matches := re.FindStringSubmatch(errMsg); len(matches) > 1 { 57 if seconds, parseErr := strconv.Atoi(matches[1]); parseErr == nil { 58 return time.Duration(seconds) * time.Second, true 59 } 60 } 61 } 62 63 return 0, false 64} 65 66// NewProxy creates a new MCP proxy. 67func NewProxy(upstream UpstreamClient) *Proxy { 68 return &Proxy{ 69 upstream: upstream, 70 localTools: make(map[string]Tool), 71 localHandlers: make(map[string]ToolHandler), 72 } 73} 74 75// RegisterTool registers a local tool with the proxy. 76func (p *Proxy) RegisterTool(tool Tool, handler ToolHandler) { 77 p.mu.Lock() 78 defer p.mu.Unlock() 79 80 p.localTools[tool.Name] = tool 81 p.localHandlers[tool.Name] = handler 82} 83 84// ListTools returns all available tools (local + upstream). 85func (p *Proxy) ListTools(ctx context.Context) ([]Tool, error) { 86 p.mu.RLock() 87 defer p.mu.RUnlock() 88 89 // Start with local tools 90 tools := make([]Tool, 0, len(p.localTools)) 91 for _, t := range p.localTools { 92 tools = append(tools, t) 93 } 94 95 // Add upstream tools if available 96 if p.upstream != nil { 97 upstreamTools, err := p.upstream.ListTools(ctx) 98 if err != nil { 99 log.Warn(). 100 Err(err). 101 Msg("failed to list upstream tools") 102 } else { 103 tools = append(tools, upstreamTools...) 104 } 105 } 106 107 return tools, nil 108} 109 110// CallTool invokes a tool, checking local handlers first then upstream. 111func (p *Proxy) CallTool(ctx context.Context, name string, arguments json.RawMessage) (*ToolResult, error) { 112 p.mu.RLock() 113 handler, isLocal := p.localHandlers[name] 114 p.mu.RUnlock() 115 116 // Try local handler first 117 if isLocal { 118 return handler(ctx, arguments) 119 } 120 121 // Fall back to upstream 122 if p.upstream != nil { 123 var args interface{} 124 if len(arguments) > 0 { 125 if err := json.Unmarshal(arguments, &args); err != nil { 126 return nil, fmt.Errorf("unmarshal arguments: %w", err) 127 } 128 } 129 130 return p.callUpstreamWithRetry(ctx, name, args) 131 } 132 133 errorMsg := fmt.Sprintf("tool not found: %s", name) 134 return &ToolResult{ 135 Content: []ContentBlock{{Type: "text", Text: errorMsg}}, 136 IsError: true, 137 }, nil 138} 139 140func (p *Proxy) callUpstreamWithRetry(ctx context.Context, name string, args interface{}) (*ToolResult, error) { 141 var lastErr error 142 for attempt := 0; attempt <= len(toolRetryDelays); attempt++ { 143 if attempt > 0 { 144 delay := toolRetryDelays[attempt-1] 145 146 // Check if error is a 429 rate limit 147 is429 := lastErr != nil && (strings.Contains(lastErr.Error(), "429") || strings.Contains(lastErr.Error(), "Rate limited")) 148 149 // Try to parse Retry-After from error message 150 if retryAfter, ok := parseRetryAfter(lastErr); ok { 151 // Use server-specified delay, but cap at 30 seconds for safety 152 if retryAfter > 30*time.Second { 153 retryAfter = 30 * time.Second 154 } 155 delay = retryAfter 156 157 log.Warn(). 158 Str("tool", name). 159 Int("attempt", attempt). 160 Dur("delay", delay). 161 Dur("server_requested", retryAfter). 162 Err(lastErr). 163 Msg("MCP tool rate limited - respecting Retry-After") 164 } else if is429 { 165 log.Warn(). 166 Str("tool", name). 167 Int("attempt", attempt). 168 Dur("delay", delay). 169 Err(lastErr). 170 Msg("MCP tool rate limited - waiting before retry") 171 } else { 172 log.Warn(). 173 Str("tool", name). 174 Int("attempt", attempt). 175 Dur("delay", delay). 176 Msg("Retrying MCP tool call after error") 177 } 178 179 select { 180 case <-time.After(delay): 181 case <-ctx.Done(): 182 return nil, ctx.Err() 183 } 184 } 185 186 result, err := p.upstream.CallTool(ctx, name, args) 187 if err == nil { 188 // Log successful call at Info level for visibility 189 if attempt > 0 { 190 log.Info(). 191 Str("tool", name). 192 Int("attempt", attempt+1). 193 Msg("MCP tool call succeeded after retry") 194 } 195 return result, nil 196 } 197 198 if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { 199 return nil, err 200 } 201 202 lastErr = err 203 } 204 205 // Log final failure with more context 206 log.Error(). 207 Str("tool", name). 208 Int("total_attempts", len(toolRetryDelays)+1). 209 Err(lastErr). 210 Msg("MCP tool call failed after all retries") 211 212 return nil, fmt.Errorf("%w: %v", ErrToolRetryExhausted, lastErr) 213} 214 215// Initialize initializes the upstream connection if available. 216func (p *Proxy) Initialize(ctx context.Context) error { 217 if p.upstream == nil { 218 return nil 219 } 220 221 clientInfo := map[string]interface{}{ 222 "name": "mysis", 223 "version": "0.1.0", 224 } 225 226 resp, err := p.upstream.Initialize(ctx, clientInfo) 227 if err != nil { 228 return fmt.Errorf("initialize upstream: %w", err) 229 } 230 231 if resp.Error != nil { 232 return fmt.Errorf("upstream error: %s", resp.Error.Message) 233 } 234 235 return nil 236} 237 238// HasUpstream returns true if an upstream client is configured. 239func (p *Proxy) HasUpstream() bool { 240 return p.upstream != nil 241} 242 243// LocalToolCount returns the number of registered local tools. 244func (p *Proxy) LocalToolCount() int { 245 p.mu.RLock() 246 defer p.mu.RUnlock() 247 return len(p.localTools) 248} 249 250// Close closes the upstream client connection if available. 251func (p *Proxy) Close() error { 252 p.mu.RLock() 253 upstream := p.upstream 254 p.mu.RUnlock() 255 256 if upstream != nil { 257 if closer, ok := upstream.(interface{ Close() error }); ok { 258 return closer.Close() 259 } 260 } 261 return nil 262}