Skip to content

Commit 03a2b13

Browse files
committed
Fix timeout handling in inline package
Update inline package to properly handle time.Duration timeouts after the config changes. Remove unused time import and fix test cases to use duration strings instead of numeric values.
1 parent 51e072f commit 03a2b13

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

internal/inline/resolver.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package inline
33
import (
44
"encoding/json"
55
"fmt"
6+
"time"
67

78
"github.com/dgellow/mcp-front/internal/config"
89
)
@@ -57,6 +58,13 @@ func ResolveConfig(rawConfig json.RawMessage) (Config, []ResolvedToolConfig, err
5758
resolved.Env = values
5859
}
5960

61+
// Validate timeout format if specified
62+
if resolved.Timeout != "" {
63+
if _, err := time.ParseDuration(resolved.Timeout); err != nil {
64+
return Config{}, nil, fmt.Errorf("invalid timeout format for tool %s: %w (use Go duration format like '30s', '5m', '1h')", tool.Name, err)
65+
}
66+
}
67+
6068
resolvedTools[i] = resolved
6169
}
6270

internal/inline/resolver_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestResolveConfig(t *testing.T) {
111111
"inputSchema": {"type": "object"},
112112
"command": "echo",
113113
"args": ["two"],
114-
"timeout": 30000000000
114+
"timeout": "30s"
115115
}
116116
]
117117
}`,
@@ -120,7 +120,7 @@ func TestResolveConfig(t *testing.T) {
120120
assert.Len(t, tools, 2)
121121
assert.Equal(t, "tool1", tools[0].Name)
122122
assert.Equal(t, "tool2", tools[1].Name)
123-
assert.Equal(t, 30*1000*1000*1000, int(tools[1].Timeout)) // 30s in nanoseconds
123+
assert.Equal(t, "30s", tools[1].Timeout)
124124
},
125125
},
126126
}

internal/inline/server.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"os/exec"
1010
"strings"
11+
"time"
1112

1213
"github.com/dgellow/mcp-front/internal"
1314
)
@@ -92,11 +93,14 @@ func (s *Server) HandleToolCall(ctx context.Context, toolName string, args map[s
9293
cmd.Env = append(cmd.Env, os.Environ()...)
9394

9495
// Set timeout if specified
95-
if tool.Timeout > 0 {
96-
var cancel context.CancelFunc
97-
ctx, cancel = context.WithTimeout(ctx, tool.Timeout)
98-
defer cancel()
99-
cmd = exec.CommandContext(ctx, tool.Command, tool.Args...)
96+
if tool.Timeout != "" {
97+
timeout, _ := time.ParseDuration(tool.Timeout)
98+
if timeout > 0 {
99+
var cancel context.CancelFunc
100+
ctx, cancel = context.WithTimeout(ctx, timeout)
101+
defer cancel()
102+
cmd = exec.CommandContext(ctx, tool.Command, tool.Args...)
103+
}
100104
}
101105

102106
// Capture output

internal/inline/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func TestServer_HandleToolCall_Timeout(t *testing.T) {
198198
Description: "Slow command",
199199
Command: "sleep",
200200
Args: []string{"5"},
201-
Timeout: 100 * 1000 * 1000, // 100ms in nanoseconds
201+
Timeout: "100ms",
202202
},
203203
}
204204

internal/inline/types.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package inline
22

33
import (
44
"encoding/json"
5-
"time"
65
)
76

87
// Config represents an inline MCP server configuration
@@ -19,7 +18,7 @@ type ToolConfig struct {
1918
Command string `json:"command"` // Command to run (e.g., "docker", "gcloud", etc.)
2019
Args []json.RawMessage `json:"args,omitempty"` // Arguments with {"$env": "..."} support
2120
Env map[string]json.RawMessage `json:"env,omitempty"` // Environment variables with {"$env": "..."} support
22-
Timeout time.Duration `json:"timeout,omitempty"` // Timeout for command execution
21+
Timeout string `json:"timeout,omitempty"` // Timeout for command execution (e.g. "30s")
2322
}
2423

2524
// ResolvedToolConfig represents a tool config with all values resolved
@@ -30,5 +29,5 @@ type ResolvedToolConfig struct {
3029
Command string `json:"command"`
3130
Args []string `json:"args,omitempty"` // Resolved arguments
3231
Env map[string]string `json:"env,omitempty"` // Resolved environment variables
33-
Timeout time.Duration `json:"timeout,omitempty"`
32+
Timeout string `json:"timeout,omitempty"`
3433
}

0 commit comments

Comments
 (0)