|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
| 10 | +) |
| 11 | + |
| 12 | +// ExecMeta is timing/cost metadata for an MCP call (§13.2 placeholders). |
| 13 | +type ExecMeta struct { |
| 14 | + DurationMs int64 |
| 15 | + CostUSD float64 |
| 16 | +} |
| 17 | + |
| 18 | +// CallStdio runs one MCP tools/call over a fresh stdio subprocess, with optional retries on transport errors (§13.4). |
| 19 | +func CallStdio(ctx context.Context, cfg *spec.ToolMCP, retry *spec.ToolRetry, toolName string, arguments map[string]any) (map[string]any, ExecMeta, error) { |
| 20 | + if cfg == nil { |
| 21 | + return nil, ExecMeta{}, errors.New("mcp: nil mcp config") |
| 22 | + } |
| 23 | + if strings.ToLower(strings.TrimSpace(cfg.Transport)) != "stdio" { |
| 24 | + return nil, ExecMeta{}, errors.New("mcp: only transport stdio is supported in MVP") |
| 25 | + } |
| 26 | + cmd := strings.TrimSpace(cfg.Command) |
| 27 | + if cmd == "" { |
| 28 | + return nil, ExecMeta{}, errors.New("mcp: empty command") |
| 29 | + } |
| 30 | + |
| 31 | + attempts := 1 |
| 32 | + if retry != nil && retry.MaxAttempts > 0 { |
| 33 | + attempts = retry.MaxAttempts |
| 34 | + } |
| 35 | + backoff := "" |
| 36 | + if retry != nil { |
| 37 | + backoff = retry.Backoff |
| 38 | + } |
| 39 | + |
| 40 | + startAll := time.Now() |
| 41 | + var lastErr error |
| 42 | + for attempt := 0; attempt < attempts; attempt++ { |
| 43 | + if attempt > 0 { |
| 44 | + sleepBackoff(ctx, attempt, backoff) |
| 45 | + } |
| 46 | + out, err := oneStdioAttempt(ctx, cmd, cfg.Args, toolName, arguments) |
| 47 | + if err == nil { |
| 48 | + return out, ExecMeta{DurationMs: time.Since(startAll).Milliseconds(), CostUSD: 0}, nil |
| 49 | + } |
| 50 | + lastErr = err |
| 51 | + if !retryableTransportErr(err) { |
| 52 | + break |
| 53 | + } |
| 54 | + } |
| 55 | + return nil, ExecMeta{DurationMs: time.Since(startAll).Milliseconds(), CostUSD: 0}, lastErr |
| 56 | +} |
| 57 | + |
| 58 | +func oneStdioAttempt(ctx context.Context, command string, args []string, toolName string, arguments map[string]any) (map[string]any, error) { |
| 59 | + tr := NewStdioTransport(command, args) |
| 60 | + if err := tr.Start(ctx); err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + defer tr.Close() |
| 64 | + |
| 65 | + if err := tr.Initialize(ctx); err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + return tr.CallTool(ctx, toolName, arguments) |
| 69 | +} |
| 70 | + |
| 71 | +func retryableTransportErr(err error) bool { |
| 72 | + if err == nil { |
| 73 | + return false |
| 74 | + } |
| 75 | + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { |
| 76 | + return false |
| 77 | + } |
| 78 | + var re *rpcError |
| 79 | + if errors.As(err, &re) { |
| 80 | + return false |
| 81 | + } |
| 82 | + return true |
| 83 | +} |
| 84 | + |
| 85 | +func sleepBackoff(ctx context.Context, attempt int, kind string) { |
| 86 | + if attempt <= 0 { |
| 87 | + return |
| 88 | + } |
| 89 | + var d time.Duration |
| 90 | + switch strings.ToLower(strings.TrimSpace(kind)) { |
| 91 | + case "exponential": |
| 92 | + shift := attempt |
| 93 | + if shift > 8 { |
| 94 | + shift = 8 |
| 95 | + } |
| 96 | + d = time.Millisecond * time.Duration(50*(1<<shift)) |
| 97 | + case "fixed": |
| 98 | + d = 100 * time.Millisecond |
| 99 | + default: |
| 100 | + d = 50 * time.Millisecond |
| 101 | + } |
| 102 | + select { |
| 103 | + case <-ctx.Done(): |
| 104 | + case <-time.After(d): |
| 105 | + } |
| 106 | +} |
0 commit comments