Skip to content

Using mcp.StdioTransport{} breaks code coverage reporting during tests #548

@michaelmcdonnellmw

Description

@michaelmcdonnellmw

Describe the bug

Using mcp.StdioTransport{} for the server transport protocol causes os.Stout to be closed when the server is stopped which prevents go test code coverage from being reported:

$ go test -coverprofile cover.out ./...
error generating coverage report: write /dev/stdout: file already closed

To Reproduce

Create the following main and test files:

package main

import (
	"context"
	"time"

	"github.com/modelcontextprotocol/go-sdk/mcp"
)

func main() {
	RunServer()
}

func RunServer() {
	impl := &mcp.Implementation{}
	options := &mcp.ServerOptions{}
	server := mcp.NewServer(impl, options)

	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
	defer cancel()

	server.Run(ctx, &mcp.InMemoryTransport{})
}
package main

import "testing"

func Test_RunServer(t *testing.T) {
	RunServer()
}

Then run the following to test the code:

$ go test -coverprofile cover.out ./...

Expected behavior

Coverage should be reported and the tests shouldn't fail

$ go test -coverprofile cover.out ./...
ok      mcp-test        0.114s  coverage: 87.5% of statements

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions