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
17 changes: 17 additions & 0 deletions go/harness/claude/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ Claude runs in print mode with `permissions.ask` rules for MCP servers
that require approval. Its native `--permission-prompt-tool` calls a private,
authenticated loopback MCP tool before executing a protected call.

The adapter sets `timeout: 100000000` milliseconds (about 27 hours 47 minutes)
on the reserved `kagent_hitl` server only. In the pinned Claude Code 2.1.260,
this bounds the permission call and raises its idle and HTTP request limits
to cover human approval waits, including wall time while the Actor is suspended.
Ordinary MCP servers retain their configured timeouts or native defaults.
Explicit timeouts must be integers from 1,000 to 2,147,483,647 milliseconds;
smaller values fall back to native defaults, and larger values can overflow
JavaScript timers, so the configuration rejects both.
See [Claude Code MCP timeouts](https://code.claude.com/docs/en/mcp) and
[JavaScript timer bounds](https://nodejs.org/api/timers.html#settimeoutcallback-delay-args).

This is a finite wait bound, not indefinite approval durability. A permission
call that waits beyond it still fails; accepting a later decision does not prove
that the protected tool executed. A new call, even with identical arguments,
requires its own approval. The timeout applies to newly configured processes;
it does not repair a parked process whose permission call already expired.

```mermaid
sequenceDiagram
participant Client as A2A client
Expand Down
18 changes: 14 additions & 4 deletions go/harness/claude/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"math"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -93,6 +94,9 @@ type MCPServer struct {
URL string `json:"url"`
Headers map[string]string `json:"headers,omitempty"`
RequireApproval bool `json:"require_approval,omitempty"`
// TimeoutMillis overrides Claude's per-call wall-clock limit and raises
// its idle/request limits. Nil leaves native defaults unchanged.
TimeoutMillis *int64 `json:"timeout,omitempty"`
}

// Agent is one compiler-owned local Claude subagent passed through --agents.
Expand Down Expand Up @@ -172,6 +176,11 @@ func (c Config) Validate() error {
if strings.TrimSpace(server.URL) == "" {
return fmt.Errorf("claude MCP server %q URL is required", name)
}
// Claude ignores values below 1000, including zero. Keep larger values
// within the JavaScript timer range to avoid overflow into a short wait.
if server.TimeoutMillis != nil && (*server.TimeoutMillis < 1000 || *server.TimeoutMillis > math.MaxInt32) {
return fmt.Errorf("claude MCP server %q timeout must be between 1000 and %d milliseconds", name, math.MaxInt32)
}
for header, value := range server.Headers {
if strings.TrimSpace(header) == "" || strings.TrimSpace(value) == "" {
return fmt.Errorf("claude MCP server %q headers require non-empty names and values", name)
Expand All @@ -197,13 +206,14 @@ func (c Config) MCPConfigJSON() ([]byte, error) {
return nil, nil
}
type nativeMCPServer struct {
Type string `json:"type"`
URL string `json:"url"`
Headers map[string]string `json:"headers,omitempty"`
Type string `json:"type"`
URL string `json:"url"`
Headers map[string]string `json:"headers,omitempty"`
TimeoutMillis *int64 `json:"timeout,omitempty"`
}
servers := make(map[string]nativeMCPServer, len(c.MCPServers))
for name, server := range c.MCPServers {
servers[name] = nativeMCPServer{Type: server.Type, URL: server.URL, Headers: server.Headers}
servers[name] = nativeMCPServer{Type: server.Type, URL: server.URL, Headers: server.Headers, TimeoutMillis: server.TimeoutMillis}
}
raw, err := json.Marshal(struct {
Servers map[string]nativeMCPServer `json:"mcpServers"`
Expand Down
67 changes: 67 additions & 0 deletions go/harness/claude/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"encoding/json"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -91,6 +92,72 @@ func TestAgentsJSON(t *testing.T) {
}
}

func TestMCPTimeoutRoundTrip(t *testing.T) {
for _, test := range []struct {
name string
timeout *int64
wantError bool
}{
{name: "absent"},
{name: "minimum", timeout: new(int64(1000))},
{name: "human wait", timeout: new(int64(100_000_000))},
{name: "maximum timer delay", timeout: new(int64(2_147_483_647))},
{name: "zero is not disabled", timeout: new(int64(0)), wantError: true},
{name: "negative", timeout: new(int64(-1)), wantError: true},
{name: "below native minimum", timeout: new(int64(999)), wantError: true},
{name: "timer overflow", timeout: new(int64(2_147_483_648)), wantError: true},
} {
t.Run(test.name, func(t *testing.T) {
cfg := Production("claude-test", "help")
cfg.MCPServers = map[string]MCPServer{"tools": {
Type: "http", URL: "https://mcp.example.com/mcp", RequireApproval: true, TimeoutMillis: test.timeout,
}}
raw, err := json.Marshal(cfg)
if err != nil {
t.Fatal(err)
}
parsed, err := Parse(raw)
if test.wantError {
if err == nil || !strings.Contains(err.Error(), "timeout must be between 1000") {
t.Fatalf("Parse() error = %v, want invalid timeout", err)
}
return
}
if err != nil {
t.Fatal(err)
}
native, err := parsed.MCPConfigJSON()
if err != nil {
t.Fatal(err)
}
var projection struct {
Servers map[string]json.RawMessage `json:"mcpServers"`
}
if err := json.Unmarshal(native, &projection); err != nil {
t.Fatal(err)
}
var fields map[string]json.RawMessage
if err := json.Unmarshal(projection.Servers["tools"], &fields); err != nil {
t.Fatal(err)
}
if _, ok := fields["require_approval"]; ok {
t.Fatal("native JSON contains internal approval policy")
}
value, present := fields["timeout"]
if test.timeout == nil {
if present {
t.Fatalf("absent timeout serialized as %s", value)
}
} else {
var got int64
if !present || json.Unmarshal(value, &got) != nil || got != *test.timeout {
t.Fatalf("native timeout = %s, want %d", value, *test.timeout)
}
}
})
}
}

func TestConfigRejectsInvalidAgents(t *testing.T) {
tests := []map[string]Agent{
{"": {Description: "description", Prompt: "prompt"}},
Expand Down
5 changes: 5 additions & 0 deletions go/harness/claude/internal/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (

const approvalMCPServerName = "kagent_hitl"

// Match Claude's default overall tool limit while lifting the shorter HTTP
// idle/request limits for human decisions, including suspended wall time.
const approvalTimeoutMillis int64 = 100_000_000

// Input contains compiler output and Actor-owned locations used to construct
// the Claude driver.
type Input struct {
Expand Down Expand Up @@ -139,6 +143,7 @@ func New(ctx context.Context, input Input) (*driver.ProcessDriver, error) {
maps.Copy(mcpServers, cfg.MCPServers)
mcpServers[approvalMCPServerName] = config.MCPServer{
Type: "http", URL: approvalBroker.URL(), Headers: approvalBroker.Headers(),
TimeoutMillis: new(approvalTimeoutMillis),
}
cfg.MCPServers = mcpServers
}
Expand Down
23 changes: 19 additions & 4 deletions go/harness/claude/internal/adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestNewMaterializesApprovalSettings(t *testing.T) {
cfg := config.Production("claude-test", "help")
cfg.StrictVersion = false
cfg.MCPServers = map[string]config.MCPServer{
"protected": {Type: "http", URL: "https://mcp.example.com/write", RequireApproval: true},
"protected": {Type: "http", URL: "https://mcp.example.com/write", RequireApproval: true, TimeoutMillis: new(int64(600_000))},
"readonly": {Type: "http", URL: "https://mcp.example.com/read"},
}
raw, err := json.Marshal(cfg)
Expand Down Expand Up @@ -119,9 +119,10 @@ func TestNewMaterializesApprovalSettings(t *testing.T) {
}
var materialized struct {
Servers map[string]struct {
Type string `json:"type"`
URL string `json:"url"`
Headers map[string]string `json:"headers"`
Type string `json:"type"`
URL string `json:"url"`
Headers map[string]string `json:"headers"`
TimeoutMillis *int64 `json:"timeout"`
} `json:"mcpServers"`
}
if err := json.Unmarshal(mcpConfig, &materialized); err != nil {
Expand All @@ -135,11 +136,25 @@ func TestNewMaterializesApprovalSettings(t *testing.T) {
if len(materialized.Servers) != 3 {
t.Fatalf("materialized MCP servers = %#v", materialized.Servers)
}
if approvalServer.TimeoutMillis == nil || *approvalServer.TimeoutMillis != 100_000_000 {
t.Fatalf("private approval timeout = %v, want 100000000 ms", approvalServer.TimeoutMillis)
}
protected := materialized.Servers["protected"]
if protected.TimeoutMillis == nil || *protected.TimeoutMillis != 600_000 {
t.Fatalf("protected server timeout = %v, want its configured 600000 ms", protected.TimeoutMillis)
}
if materialized.Servers["readonly"].TimeoutMillis != nil {
t.Fatal("ordinary server without a timeout acquired an override")
}
if strings.Contains(string(mcpConfig), "require_approval") {
t.Fatal("native MCP configuration contains internal approval policy")
}
args := strings.Join(runner.Args(runtime.Turn{Prompt: "test"}), "\n")
for _, required := range []string{
"--setting-sources\n\n",
"--settings\n" + filepath.Join(ephemeralDir, "settings.json"),
"--dangerously-skip-permissions",
"--strict-mcp-config",
"--permission-prompt-tool\nmcp__" + approvalMCPServerName + "__approve",
} {
if !strings.Contains(args, required) {
Expand Down
Loading
Loading