Skip to content

SSE: Context may be canceled in HandleMessage #248

@w-zr

Description

@w-zr

In github.com/mark3labs/mcp-go/server/sse.go:

func (s *SSEServer) handleMessage(w http.ResponseWriter, r *http.Request) {
	...
        ctx := s.server.WithContext(r.Context(), session)
        ...
        // quick return request, send 202 Accepted with no body, then deal the message and sent response via SSE
	w.WriteHeader(http.StatusAccepted)

	go func() {
		// Process message through MCPServer
		response := s.server.HandleMessage(ctx, rawMessage)
                ...
        }()
}

r.Context() is canceled by golang after handleMessage returns. HandleMessage in the goroutine should not use context derived from r.Context().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions