diff --git a/pkgs/mcpx/internal/adapter/adapter.go b/pkgs/mcpx/internal/adapter/adapter.go index fbd12f858a..3c0c295df7 100644 --- a/pkgs/mcpx/internal/adapter/adapter.go +++ b/pkgs/mcpx/internal/adapter/adapter.go @@ -45,7 +45,7 @@ type Spec struct { // Instructions explain conventions the schemas cannot, exactly as a real // server's initialize does. Instructions string `json:"instructions,omitempty"` - // Timeout bounds every invocation. Empty means 60s. + // Timeout bounds every invocation. Empty or zero means 60s. Timeout string `json:"timeout,omitempty"` // Tools are the subcommands worth exposing. Tools []ToolSpec `json:"tools"` @@ -66,7 +66,7 @@ type ToolSpec struct { // JSON parses the output as JSON before returning it, so a caller gets // structure rather than a string containing structure. JSON bool `json:"json,omitempty"` - // Timeout overrides the server's for this tool. + // Timeout overrides the server's for this tool. Empty or zero inherits it. Timeout string `json:"timeout,omitempty"` } @@ -145,6 +145,14 @@ func (s Spec) Validate() error { return nil } +// parseDuration resolves a timeout string against a fallback. +// +// Zero is unset: an empty string or an explicit zero ("0s") yields the +// fallback, so at Call a zero tool timeout inherits the server timeout and +// a zero server timeout yields the 60s default. A negative duration is +// never a timeout - it would hand context.WithTimeout an already-expired +// context and fail every call instantly - so it is an error naming the +// offending value, which Validate surfaces at both levels. func parseDuration(v string, fallback time.Duration) (time.Duration, error) { if strings.TrimSpace(v) == "" { return fallback, nil @@ -153,6 +161,12 @@ func parseDuration(v string, fallback time.Duration) (time.Duration, error) { if err != nil { return 0, fmt.Errorf("is not a duration: %q", v) } + if d < 0 { + return 0, fmt.Errorf("is a negative duration: %q", v) + } + if d == 0 { + return fallback, nil + } return d, nil } @@ -227,6 +241,8 @@ func (s Spec) Call(ctx context.Context, tool string, args map[string]any) (*Resu } full := append(append([]string{}, s.Args...), argv...) + // Zero is unset (see parseDuration): a tool timeout of zero inherits + // the server timeout, and a server timeout of zero gets the default. timeout, _ := parseDuration(spec.Timeout, 0) if timeout == 0 { timeout, _ = parseDuration(s.Timeout, time.Minute) diff --git a/pkgs/mcpx/internal/adapter/adapter_test.go b/pkgs/mcpx/internal/adapter/adapter_test.go index 138287f7af..85e747945c 100644 --- a/pkgs/mcpx/internal/adapter/adapter_test.go +++ b/pkgs/mcpx/internal/adapter/adapter_test.go @@ -194,3 +194,50 @@ func TestEnumIsEnforced(t *testing.T) { t.Fatal("a value outside the enum should be refused") } } + +func TestValidateRefusesNegativeTimeouts(t *testing.T) { + // A negative timeout passes time.ParseDuration, so without an explicit + // refusal it validates and then every call is born expired (#1758). + server := echoSpec() + server.Timeout = "-5s" + err := server.Validate() + if err == nil { + t.Fatal("a negative server timeout should not validate") + } + for _, want := range []string{"negative", `"-5s"`} { + if !strings.Contains(err.Error(), want) { + t.Errorf("server error should name the offending value, missing %q from:\n%v", want, err) + } + } + + tool := echoSpec() + tool.Tools[0].Timeout = "-1ms" + err = tool.Validate() + if err == nil { + t.Fatal("a negative tool timeout should not validate") + } + for _, want := range []string{"negative", `"-1ms"`} { + if !strings.Contains(err.Error(), want) { + t.Errorf("tool error should name the offending value, missing %q from:\n%v", want, err) + } + } +} + +func TestZeroTimeoutIsUnset(t *testing.T) { + // Zero is unset at both levels: a "0s" tool timeout inherits the server + // timeout, and a "0s" server timeout means the 60s default - never an + // already-expired context (#1758). + spec := echoSpec() + spec.Timeout = "0s" + spec.Tools[0].Timeout = "0s" + if err := spec.Validate(); err != nil { + t.Fatalf("zero timeouts are unset and should validate: %v", err) + } + res, err := spec.Call(context.Background(), "say", map[string]any{"text": "hello"}) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(res.Stdout, "said: hello") { + t.Errorf("got %q", res.Stdout) + } +}