xonecas.io
62159
Sy

symb / files

symb / internal / mcp / proxy.go

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