Skip to content

Commit 9e97764

Browse files
authored
feat: adds auto-remove flag for run (#115)
Signed-off-by: ChrisJBurns <29541485+ChrisJBurns@users.noreply.github.com>
1 parent 45da211 commit 9e97764

File tree

10 files changed

+37
-7
lines changed

10 files changed

+37
-7
lines changed

cmd/thv/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ var (
4040
runVolumes []string
4141
runSecrets []string
4242
runAuthzConfig string
43+
autoRemove bool
4344
)
4445

4546
func init() {
47+
runCmd.Flags().BoolVar(&autoRemove, "rm", false, "Auto-remove the server container when the server has been stopped")
4648
runCmd.Flags().StringVar(&runTransport, "transport", "stdio", "Transport mode (sse or stdio)")
4749
runCmd.Flags().StringVar(&runName, "name", "", "Name of the MCP server (auto-generated from image if not provided)")
4850
runCmd.Flags().IntVar(&runPort, "port", 0, "Port for the HTTP proxy to listen on (host port)")
@@ -120,6 +122,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
120122
cmdArgs,
121123
runName,
122124
debugMode,
125+
autoRemove,
123126
runVolumes,
124127
runSecrets,
125128
runAuthzConfig,

cmd/thv/run_common.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ func detachProcess(cmd *cobra.Command, options *runner.RunConfig) error {
6767
detachedArgs = append(detachedArgs, "--transport", string(options.Transport))
6868
}
6969

70+
if options.Debug {
71+
detachedArgs = append(detachedArgs, "--debug")
72+
}
73+
74+
if options.AutoRemove {
75+
detachedArgs = append(detachedArgs, "--rm")
76+
}
77+
7078
// Use Name if available
7179
if options.Name != "" {
7280
detachedArgs = append(detachedArgs, "--name", options.Name)

pkg/container/docker/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func (c *Client) CreateContainer(
238238

239239
// Create host configuration
240240
hostConfig := &container.HostConfig{
241+
AutoRemove: false,
241242
Mounts: convertMounts(permissionConfig.Mounts),
242243
NetworkMode: container.NetworkMode(permissionConfig.NetworkMode),
243244
CapAdd: permissionConfig.CapAdd,

pkg/runner/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ type RunConfig struct {
6262
// Debug indicates whether debug mode is enabled
6363
Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"`
6464

65+
// AutoRemove indicates whether the container should be removed when the server has been stopped
66+
AutoRemove bool `json:"auto_remove,omitempty" yaml:"auto_remove,omitempty"`
67+
6568
// Volumes are the directory mounts to pass to the container
6669
// Format: "host-path:container-path[:ro]"
6770
Volumes []string `json:"volumes,omitempty" yaml:"volumes,omitempty"`
@@ -117,6 +120,7 @@ func NewRunConfigFromFlags(
117120
cmdArgs []string,
118121
name string,
119122
debug bool,
123+
autoRemove bool,
120124
volumes []string,
121125
secretsList []string,
122126
authzConfigPath string,
@@ -132,6 +136,7 @@ func NewRunConfigFromFlags(
132136
CmdArgs: cmdArgs,
133137
Name: name,
134138
Debug: debug,
139+
AutoRemove: autoRemove,
135140
Volumes: volumes,
136141
Secrets: secretsList,
137142
AuthzConfigPath: authzConfigPath,

pkg/runner/config_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ func TestNewRunConfigFromFlags(t *testing.T) {
801801
cmdArgs := []string{"arg1", "arg2"}
802802
name := "test-server"
803803
debug := true
804+
autoRemove := true
804805
volumes := []string{"/host:/container"}
805806
secretsList := []string{"secret1,target=ENV_VAR1"}
806807
authzConfigPath := "/path/to/authz.json"
@@ -816,6 +817,7 @@ func TestNewRunConfigFromFlags(t *testing.T) {
816817
cmdArgs,
817818
name,
818819
debug,
820+
autoRemove,
819821
volumes,
820822
secretsList,
821823
authzConfigPath,

pkg/runner/runner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (r *Runner) Run(ctx context.Context) error {
4343
TargetHost: r.Config.TargetHost,
4444
Runtime: r.Config.Runtime,
4545
Debug: r.Config.Debug,
46+
AutoRemove: r.Config.AutoRemove,
4647
}
4748

4849
// Add OIDC middleware if OIDC validation is enabled

pkg/transport/factory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ func NewFactory() *Factory {
1919
func (*Factory) Create(config types.Config) (types.Transport, error) {
2020
switch config.Type {
2121
case types.TransportTypeStdio:
22-
return NewStdioTransport(config.Port, config.Runtime, config.Debug, config.Middlewares...), nil
22+
return NewStdioTransport(config.Port, config.Runtime, config.Debug, config.AutoRemove, config.Middlewares...), nil
2323
case types.TransportTypeSSE:
2424
return NewSSETransport(
2525
config.Host,
2626
config.Port,
2727
config.TargetPort,
2828
config.Runtime,
2929
config.Debug,
30+
config.AutoRemove,
3031
config.TargetHost,
3132
config.Middlewares...,
3233
), nil

pkg/transport/sse.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type SSETransport struct {
3030
containerName string
3131
runtime rt.Runtime
3232
debug bool
33+
autoRemove bool
3334
middlewares []types.Middleware
3435

3536
// Mutex for protecting shared state
@@ -53,6 +54,7 @@ func NewSSETransport(
5354
targetPort int,
5455
runtime rt.Runtime,
5556
debug bool,
57+
autoRemove bool,
5658
targetHost string,
5759
middlewares ...types.Middleware,
5860
) *SSETransport {
@@ -73,6 +75,7 @@ func NewSSETransport(
7375
targetHost: targetHost,
7476
runtime: runtime,
7577
debug: debug,
78+
autoRemove: autoRemove,
7679
shutdownCh: make(chan struct{}),
7780
}
7881
}
@@ -253,15 +256,15 @@ func (t *SSETransport) Stop(ctx context.Context) error {
253256
return fmt.Errorf("failed to stop container: %w", err)
254257
}
255258

256-
// Remove the container if debug mode is not enabled
257-
if !t.debug {
259+
// Remove the container if auto-remove is enabled
260+
if t.autoRemove {
258261
logger.Log.Info(fmt.Sprintf("Removing container %s...", t.containerName))
259262
if err := t.runtime.RemoveContainer(ctx, t.containerID); err != nil {
260263
logger.Log.Warn(fmt.Sprintf("Warning: Failed to remove container: %v", err))
261264
}
262265
logger.Log.Info(fmt.Sprintf("Container %s removed", t.containerName))
263266
} else {
264-
logger.Log.Info(fmt.Sprintf("Debug mode enabled, container %s not removed", t.containerName))
267+
logger.Log.Info(fmt.Sprintf("Auto-remove disabled, container %s not removed", t.containerName))
265268
}
266269
}
267270

pkg/transport/stdio.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type StdioTransport struct {
2929
containerName string
3030
runtime rt.Runtime
3131
debug bool
32+
autoRemove bool
3233
middlewares []types.Middleware
3334

3435
// Mutex for protecting shared state
@@ -54,12 +55,14 @@ func NewStdioTransport(
5455
port int,
5556
runtime rt.Runtime,
5657
debug bool,
58+
autoRemove bool,
5759
middlewares ...types.Middleware,
5860
) *StdioTransport {
5961
return &StdioTransport{
6062
port: port,
6163
runtime: runtime,
6264
debug: debug,
65+
autoRemove: autoRemove,
6366
middlewares: middlewares,
6467
shutdownCh: make(chan struct{}),
6568
}
@@ -242,16 +245,16 @@ func (t *StdioTransport) Stop(ctx context.Context) error {
242245
}
243246
}
244247

245-
// Remove the container if debug mode is not enabled
246-
if !t.debug {
248+
// Remove the container if auto-remove is enabled
249+
if t.autoRemove {
247250
logger.Log.Info(fmt.Sprintf("Removing container %s...", t.containerName))
248251
if err := t.runtime.RemoveContainer(ctx, t.containerID); err != nil {
249252
logger.Log.Error(fmt.Sprintf("Warning: Failed to remove container: %v", err))
250253
} else {
251254
logger.Log.Info(fmt.Sprintf("Container %s removed", t.containerName))
252255
}
253256
} else {
254-
logger.Log.Info(fmt.Sprintf("Debug mode enabled, container %s not removed", t.containerName))
257+
logger.Log.Info(fmt.Sprintf("Auto-remove disabled, container %s not removed", t.containerName))
255258
}
256259
}
257260

pkg/transport/types/transport.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ type Config struct {
123123
// If debug mode is enabled, containers will not be removed when stopped.
124124
Debug bool
125125

126+
// AutoRemove indicates whether the container should be removed when the server has been stopped.
127+
AutoRemove bool
128+
126129
// Middlewares is a list of middleware functions to apply to the transport.
127130
// These are applied in order, with the first middleware being the outermost wrapper.
128131
Middlewares []Middleware

0 commit comments

Comments
 (0)