Skip to content

mcp process leak when server is started via command (stdio transport) and /mcp refresh is triggered #5318

@danicat

Description

@danicat

What happened?

The CLI doesn't clean up MCP Servers that are spin up via commands (using stdio transport) when performing an /mcp refresh command.

Steps to reproduce:

  • Configure one stdio mcp server in settings.json (via "command" property)
  • Start the CLI
  • Run !ps -ef | grep your-mcp-server-command (should have one count)
  • Run /mcp refresh
  • Run !ps -ef | grep your-mcp-server-command (should have two counts)
  • Run /mcp refresh
  • Run !ps -ef | grep your-mcp-server-command (should have three counts)

When started:

Image

After 2 refreshes:

Image

Source code for the server:

package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/danicat/speedgrapher/internal/prompts"
	"github.com/modelcontextprotocol/go-sdk/mcp"
)

func main() {
	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
	defer stop()
	if err := run(ctx, os.Args[1:]); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func run(ctx context.Context, args []string) error {
	server := mcp.NewServer(
		&mcp.Implementation{Name: "speedgrapher"},
		nil,
	)
	server.AddPrompt(prompts.Haiku(), prompts.HaikuHandler)
	server.AddPrompt(prompts.Interview(), prompts.InterviewHandler)
	server.AddPrompt(prompts.Localize(), prompts.LocalizeHandler)
	server.AddPrompt(prompts.Review(), prompts.ReviewHandler)
	return server.Run(ctx, mcp.NewStdioTransport())
}

What did you expect to happen?

No process leak when doing a manual refresh.

Client information

  • CLI Version: 0.1.15
  • Git Commit: c45c14e (local modifications)
  • Operating System: darwin v22.16.0
  • Sandbox Environment: no sandbox
  • Model Version: gemini-2.5-pro
  • Memory Usage: 208.5 MB

Login information

No response

Anything else we need to know?

Currently working on a fix, PR will come soon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/toolsBuilt-in tools and the MCP ecosystem.kind/bugSomething isn't workingmaintainerFor Roadmap Itemspriority/p1Important and should be addressed in the near term.sub-area/performanceIssues related to performance.

    Type

    Projects

    Status

    Ready for Work

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions