Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions cmd/mcpproxy/code_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -167,27 +168,51 @@ func runCodeExec(_ *cobra.Command, _ []string) error {
return runCodeExecStandalone(globalConfig, code, inputData, logger)
}

// codeExecClientSlack is the head-room added to the server-side execution
// budget when deriving the client-side deadline: transport, queueing and
// (de)serialization all happen outside the daemon's own timeout accounting.
const codeExecClientSlack = 30 * time.Second

// codeExecClientTimeout returns how long the CLI waits for the daemon to
// answer a code execution request. The daemon enforces the --timeout budget
// itself, so the client deadline only has to outlive it.
func codeExecClientTimeout(timeoutMS int) time.Duration {
return time.Duration(timeoutMS)*time.Millisecond + codeExecClientSlack
}

// runCodeExecClientMode executes code via the daemon HTTP API.
func runCodeExecClientMode(client *cliclient.Client, code string, input map[string]interface{}, logger *zap.Logger) error {
// Ping daemon to verify connectivity
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := client.Ping(ctx); err != nil {
pingCtx, pingCancel := context.WithTimeout(context.Background(), 2*time.Second)
defer pingCancel()
if err := client.Ping(pingCtx); err != nil {
logger.Warn("Failed to ping daemon, falling back to standalone mode",
zap.Error(err),
zap.Duration("ping_timeout", 2*time.Second),
zap.String("fallback_mode", "standalone"))
// Fall back to standalone mode
cfg, _ := loadCodeConfig()
// Fall back to standalone mode, which needs a usable configuration of
// its own (data dir, limits) - without one there is nothing to run.
cfg, cfgErr := loadCodeConfig()
if cfgErr == nil && cfg == nil {
cfgErr = errors.New("no configuration available")
}
if cfgErr != nil {
fmt.Fprintf(os.Stderr, "Error loading configuration for standalone fallback: %v\n", cfgErr)
return exitError(2)
}
return runCodeExecStandalone(cfg, code, input, logger)
}

// ADD CLI mode indicator
fmt.Fprintf(os.Stderr, "ℹ️ Using daemon mode - fast execution\n")

// Execute code via daemon
clientTimeout := codeExecClientTimeout(codeTimeout)
execCtx, execCancel := context.WithTimeout(context.Background(), clientTimeout)
defer execCancel()

result, err := client.CodeExec(
ctx,
execCtx,
code,
input,
codeTimeout,
Expand All @@ -196,6 +221,12 @@ func runCodeExecClientMode(client *cliclient.Client, code string, input map[stri
cliclient.CodeExecOptions{Language: codeLanguage},
)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
fmt.Fprintf(os.Stderr,
"Error: client-side timeout after %s waiting for the daemon to return results (execution budget --timeout=%dms); the daemon may still be running the code\n",
clientTimeout, codeTimeout)
return exitError(1)
}
// T029: Use formatErrorWithRequestID to include request_id in error output
fmt.Fprintf(os.Stderr, "Error calling daemon: %s\n", formatErrorWithRequestID(err))
return exitError(1)
Expand Down
157 changes: 157 additions & 0 deletions cmd/mcpproxy/code_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package main

import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

"github.com/smart-mcp-proxy/mcpproxy-go/internal/cliclient"

"go.uber.org/zap"
)

// TestCodeExecClientTimeout_TracksServerBudget pins that the client-side
// deadline for a daemon code execution always outlives the server-side
// execution budget requested with --timeout.
func TestCodeExecClientTimeout_TracksServerBudget(t *testing.T) {
tests := []struct {
name string
timeoutMS int
want time.Duration
}{
{"default budget", 120000, 150 * time.Second},
{"smallest budget still gets slack", 1, 30*time.Second + time.Millisecond},
{"largest allowed budget", 600000, 630 * time.Second},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := codeExecClientTimeout(tc.timeoutMS)
if got != tc.want {
t.Fatalf("codeExecClientTimeout(%d) = %s, want %s", tc.timeoutMS, got, tc.want)
}
if got <= time.Duration(tc.timeoutMS)*time.Millisecond {
t.Fatalf("codeExecClientTimeout(%d) = %s, must exceed the server budget", tc.timeoutMS, got)
}
})
}
}

const (
codeExecChildEnv = "MCPPROXY_TEST_CODE_EXEC_CHILD"
codeExecFallbackChildEnv = "MCPPROXY_TEST_CODE_EXEC_FALLBACK_CHILD"
)

// useMissingCodeConfig points the code command at a config file that does not
// exist, so a child process can never read the developer's real
// ~/.mcpproxy/mcp_config.json (and never open the real DataDir BBolt store)
// when a flaked daemon ping sends it down the standalone fallback.
func useMissingCodeConfig(t *testing.T) string {
t.Helper()

missing := filepath.Join(t.TempDir(), "absent", "mcp_config.json")
previous := codeConfigPath
codeConfigPath = missing
t.Cleanup(func() { codeConfigPath = previous })
return missing
}

// TestRunCodeExecClientMode_ExecOutlivesPingDeadline pins that the short
// daemon-connectivity ping deadline does not bound the execution request:
// daemon mode must keep waiting for a result that arrives long after the ping
// budget has elapsed. The command path calls os.Exit on failure, so the real
// path runs in a child process and this test asserts on its exit code.
func TestRunCodeExecClientMode_ExecOutlivesPingDeadline(t *testing.T) {
const execDelay = 3 * time.Second

if os.Getenv(codeExecChildEnv) == "1" {
codeTimeout = 60000
codeMaxToolCalls = 0
codeAllowedSrvs = nil
codeLanguage = "javascript"
useMissingCodeConfig(t)

client := cliclient.NewClientWithAPIKey(os.Getenv(codeExecChildEnv+"_ENDPOINT"), "", nil)
if err := runCodeExecClientMode(client, "({ value: 42 })", map[string]interface{}{}, zap.NewNop()); err != nil {
t.Fatalf("runCodeExecClientMode returned error: %v", err)
}
return
}

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/status":
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]interface{}{"success": true})
case "/api/v1/code/exec":
select {
case <-time.After(execDelay):
case <-r.Context().Done():
return
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"ok": true,
"result": map[string]interface{}{"value": 42},
})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer srv.Close()

cmd := exec.Command(os.Args[0], "-test.run=^TestRunCodeExecClientMode_ExecOutlivesPingDeadline$", "-test.timeout=60s")
cmd.Env = append(os.Environ(),
codeExecChildEnv+"=1",
codeExecChildEnv+"_ENDPOINT="+srv.URL,
)

out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("daemon-mode code exec aborted before the daemon replied (%v)\nchild output:\n%s", err, out)
}
// A flaked ping would silently fall back to standalone mode, where the
// trivial script also succeeds - the child would exit 0 without ever
// exercising the daemon path this test pins. Require the evidence.
if !strings.Contains(string(out), "Using daemon mode") {
t.Fatalf("child never took the daemon path (ping likely fell back to standalone)\nchild output:\n%s", out)
}
}

// TestRunCodeExecClientMode_PingFailureFallbackWithoutConfig pins that when the
// daemon ping fails and no configuration can be loaded, the standalone fallback
// reports a configuration error instead of dereferencing a nil config.
func TestRunCodeExecClientMode_PingFailureFallbackWithoutConfig(t *testing.T) {
if os.Getenv(codeExecFallbackChildEnv) == "1" {
codeTimeout = 1000
codeMaxToolCalls = 0
codeAllowedSrvs = nil
codeLanguage = "javascript"
useMissingCodeConfig(t)

// Port 1 refuses connections immediately, so the ping fails fast and
// the standalone fallback runs with no loadable configuration.
client := cliclient.NewClientWithAPIKey("http://127.0.0.1:1", "", nil)
_ = runCodeExecClientMode(client, "({ value: 42 })", map[string]interface{}{}, zap.NewNop())
return
}

cmd := exec.Command(os.Args[0], "-test.run=^TestRunCodeExecClientMode_PingFailureFallbackWithoutConfig$", "-test.timeout=60s")
cmd.Env = append(os.Environ(), codeExecFallbackChildEnv+"=1")

out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected the fallback to exit non-zero without a loadable config\nchild output:\n%s", out)
}
if strings.Contains(string(out), "panic:") {
t.Fatalf("standalone fallback panicked instead of reporting a config error\nchild output:\n%s", out)
}
if !strings.Contains(string(out), "Error loading configuration") {
t.Fatalf("expected a configuration error from the standalone fallback\nchild output:\n%s", out)
}
}
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ A running MCPProxy core watches `mcp_config.json` and hot-reloads external edits
| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `tools_limit` | integer | `15` | Maximum number of tools to return per request (1-1000) |
| `tool_response_limit` | integer | `20000` | Maximum characters in tool responses (0 = unlimited) |
| `tool_response_limit` | integer | `20000` | Maximum characters in tool responses (0 = unlimited). Applies to built-in tools too: an oversize `retrieve_tools` result is truncated and the full payload is retrievable via `read_cache` (except on the code-execution surface, which does not expose `read_cache`) |
| `call_tool_timeout` | string | `"2m"` | Timeout for tool calls (e.g., `"30s"`, `"2m"`, `"5m"`). **Note**: When using agents like Codex or Claude as MCP servers, you may need to increase this timeout significantly, even up to 10 minutes (`"10m"`), as these agents may require longer processing times for complex operations |
| `init_timeout` | duration | `"30s"` | Deadline for an upstream's MCP `initialize` handshake (e.g. `"30s"`, `"120s"`, `"3m"`). Raise this for servers that do legitimate first-run warmup — building a cache/index or prefetching — before they answer `initialize`, so they are not killed mid-startup. Global default; can be overridden per server (see [Server Fields](#server-fields)). Range: `1s`–`30m`; `"0s"`/unset uses the 30s default. |

Expand Down
17 changes: 16 additions & 1 deletion internal/cliclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -211,6 +212,17 @@ func parseAPIError(errorMsg, requestID string) error {
return &APIError{Message: errorMsg, RequestID: requestID}
}

// execHTTPClient returns a shallow copy of the shared HTTP client with no
// blanket timeout, so a long-running request is bounded by its context alone.
// The shared client keeps a generous timeout as a safety net for ordinary
// commands, but that net is shorter than the largest execution budget the
// daemon accepts.
func (c *Client) execHTTPClient() *http.Client {
clone := *c.httpClient
clone.Timeout = 0
return &clone
}

// CodeExecOptions contains optional parameters for code execution via the daemon API.
type CodeExecOptions struct {
Language string // Source language: "javascript" (default) or "typescript"
Expand Down Expand Up @@ -256,8 +268,11 @@ func (c *Client) CodeExec(
req.Header.Set("Content-Type", "application/json")

// Send request
resp, err := c.httpClient.Do(req)
resp, err := c.execHTTPClient().Do(req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return nil, fmt.Errorf("client-side timeout waiting for the daemon to return code execution results: %w", err)
}
return nil, fmt.Errorf("failed to call code execution API: %w", err)
}
defer resp.Body.Close()
Expand Down
104 changes: 104 additions & 0 deletions internal/cliclient/code_exec_timeout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cliclient

import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)

// TestClient_CodeExec_ClientSideTimeoutIsExplicit pins that a deadline that
// expires before the daemon answers is reported as a client-side timeout
// rather than as an opaque transport failure, and that the sentinel stays
// inspectable with errors.Is.
func TestClient_CodeExec_ClientSideTimeoutIsExplicit(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-time.After(time.Second):
case <-r.Context().Done():
return
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true})
}))
defer server.Close()

client := NewClient(server.URL, nil)

ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()

_, err := client.CodeExec(ctx, "code", map[string]interface{}{}, 60000, 0, nil)
if err == nil {
t.Fatal("expected an error when the client deadline expires first")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("error %v does not wrap context.DeadlineExceeded", err)
}
if !strings.Contains(err.Error(), "client-side timeout") {
t.Fatalf("error %q does not name the client-side timeout", err.Error())
}
}

// TestClient_CodeExec_NotCappedBySharedClientTimeout pins that code execution
// is bounded by the caller's context alone: the shared HTTP client's blanket
// timeout would otherwise cut off executions whose --timeout budget is larger.
func TestClient_CodeExec_NotCappedBySharedClientTimeout(t *testing.T) {
client := NewClient("http://127.0.0.1:1", nil)

execClient := client.execHTTPClient()
if execClient.Timeout != 0 {
t.Fatalf("exec HTTP client Timeout = %s, want 0 so the request context governs", execClient.Timeout)
}
if execClient.Transport != client.httpClient.Transport {
t.Fatal("exec HTTP client must reuse the shared transport (headers, socket dialer)")
}
if client.httpClient.Timeout == 0 {
t.Fatal("the shared client should keep its blanket timeout for other commands")
}
}

// TestClient_CodeExec_OutlivesSharedClientTimeout drives the real call site: a
// daemon that answers well after the shared client's blanket timeout must still
// produce a result, because CodeExec is expected to dispatch on the uncapped
// exec client. Reverting the request to c.httpClient.Do turns this red.
func TestClient_CodeExec_OutlivesSharedClientTimeout(t *testing.T) {
const serverDelay = 200 * time.Millisecond

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/code/exec" {
t.Errorf("unexpected request path %q", r.URL.Path)
w.WriteHeader(http.StatusNotFound)
return
}
select {
case <-time.After(serverDelay):
case <-r.Context().Done():
return
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"ok": true,
"result": "done",
})
}))
defer server.Close()

client := NewClient(server.URL, nil)
// Shorter than the daemon's response time: only a request that escapes the
// shared client's blanket timeout can succeed here.
client.httpClient.Timeout = 50 * time.Millisecond

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

result, err := client.CodeExec(ctx, "code", map[string]interface{}{}, 600000, 0, nil)
if err != nil {
t.Fatalf("CodeExec must be bounded by the context, not the shared client timeout: %v", err)
}
if !result.OK {
t.Fatalf("CodeExec result = %+v, want OK", result)
}
}
Loading
Loading