Skip to content

Commit fd76c28

Browse files
committed
Cleanup after MCP Server runs
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent 573cddc commit fd76c28

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

pkg/teamloader/teamloader.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ func getToolsForAgent(a *latest.AgentConfig, parentDir string, sharedTools map[s
256256
serverName := strings.TrimPrefix(toolset.Ref, "docker:")
257257
args := []string{"mcp", "gateway", "run", "--servers=" + serverName}
258258

259+
var cleanUp func() error
259260
if toolset.Config != nil {
260-
serverConfig := map[string]any{
261-
serverName: toolset.Config,
262-
}
263-
264261
file, err := os.CreateTemp("", "mcp-config-*.yaml")
265262
if err != nil {
266263
return nil, fmt.Errorf("failed to create temp file: %w", err)
267264
}
268-
// TODO(dga): Delete the temp file after use.
265+
cleanUp = func() error { return os.Remove(file.Name()) }
269266

267+
serverConfig := map[string]any{
268+
serverName: toolset.Config,
269+
}
270270
if err := yaml.NewEncoder(file).Encode(serverConfig); err != nil {
271271
return nil, fmt.Errorf("failed to write config to temp file: %w", err)
272272
}
@@ -279,10 +279,10 @@ func getToolsForAgent(a *latest.AgentConfig, parentDir string, sharedTools map[s
279279
args = append(args, "--catalog=https://desktop.docker.com/mcp/catalog/v2/catalog.yaml")
280280

281281
// TODO(dga): If the server's docker image had the right annotations, we could run it directly with `docker run` or with the MCP gateway as a go library.
282-
t = append(t, mcp.NewToolsetCommand("docker", args, os.Environ(), toolset.Tools))
282+
t = append(t, mcp.NewToolsetCommand("docker", args, os.Environ(), toolset.Tools, cleanUp))
283283

284284
case toolset.Type == "mcp" && toolset.Command != "":
285-
t = append(t, mcp.NewToolsetCommand(toolset.Command, toolset.Args, os.Environ(), toolset.Tools))
285+
t = append(t, mcp.NewToolsetCommand(toolset.Command, toolset.Args, os.Environ(), toolset.Tools, nil))
286286

287287
case toolset.Type == "mcp" && toolset.Remote.URL != "":
288288
// Expand headers.

pkg/tools/mcp/toolset.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ import (
1313
type Toolset struct {
1414
c *Client
1515
toolFilter []string
16+
cleanup func() error
1617
}
1718

1819
// Make sure the MCP Toolset always implements _our_ ToolSet interface
1920
var _ tools.ToolSet = (*Toolset)(nil)
2021

2122
// NewToolsetCommand creates a new MCP toolset from a command.
22-
func NewToolsetCommand(command string, args, env, toolFilter []string) *Toolset {
23+
func NewToolsetCommand(command string, args, env, toolFilter []string, cleanup func() error) *Toolset {
2324
slog.Debug("Creating MCP toolset", "command", command, "args", args, "toolFilter", toolFilter)
2425

2526
return &Toolset{
2627
c: NewStdioClient(command, args, env),
2728
toolFilter: toolFilter,
29+
cleanup: cleanup,
2830
}
2931
}
3032

@@ -86,6 +88,12 @@ func (t *Toolset) Stop() error {
8688
slog.Error("Failed to stop MCP toolset", "error", err)
8789
return err
8890
}
91+
if t.cleanup != nil {
92+
if err := t.cleanup(); err != nil {
93+
slog.Error("Failed to cleanup MCP toolset", "error", err)
94+
return err
95+
}
96+
}
8997
slog.Debug("Stopped MCP toolset successfully")
9098
return nil
9199
}

0 commit comments

Comments
 (0)