siyuan-note/siyuan · error

MCP request operation scope is closed

Error message

MCP request operation scope is closed

What it means

errMCPRequestOperationScopeClosed is returned when the per-request operation scope (registered per HTTP request ID in requestOperationScopes) can no longer be found or its stored value is not a context.Context. It means the encrypted-notebook operation scope tied to this MCP request has already been closed/cleaned up, so no scoped context can be handed to the operation.

Solutions

  1. Ensure the request goes through the middleware that registers the scope (withEncryptedBoxOperationScope) before any code calls requestOperationContext
  2. Perform encrypted-notebook operations while the original HTTP request is still in flight; capture the context before the handler returns
  3. For background work, derive and pass an explicit context instead of relying on the request scope
  4. Fix any code that stores a non-context value under the request ID

Example fix

// before
ctx, err := requestOperationContext(staleRequestID) // scope already closed
// after
// call within the handler lifetime, or:
ctx := context.Background() // for out-of-request work
Defensive patterns

Strategy: fallback

Validate before calling

if _, ok := requestOperationScopes.Load(requestID); !ok {
    // scope not registered or already closed — use an explicit context instead
    ctx = context.Background()
}

Try / catch

ctx, err := requestOperationContext(requestID)
if errors.Is(err, errMCPRequestOperationScopeClosed) {
    ctx = context.Background() // out-of-request fallback
}

Prevention

When it happens

Trigger: requestOperationContext is called with a request ID that was never registered (middleware withEncryptedBoxOperationScope did not run) or was already removed when the response finished; or the sync.Map value fails the context.Context type assertion.

Common situations: Calling MCP operation code outside the HTTP handler chain (background jobs, tests) where the scope middleware never wrapped the request; reusing a stale request ID after the response completed; concurrent cleanup racing an operation.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/111d848bef8301ba. Report an issue: GitHub.

Appendix: source

Thrown at kernel/mcp/request_scope.go:33

// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package mcp

import (
	"context"
	"errors"
	"net/http"
	"sync"

	"github.com/google/uuid"
	mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp"
	"github.com/siyuan-note/siyuan/kernel/model"
)

const requestOperationScopeHeader = "X-Siyuan-Mcp-Request-Scope"

var (
	errMCPRequestOperationScopeClosed = errors.New("MCP request operation scope is closed")
	requestOperationScopes            sync.Map // 键为请求 ID,值为 context.Context。
)

// withEncryptedBoxOperationScope 为当前 HTTP 响应注册加密笔记本操作作用域。
func withEncryptedBoxOperationScope(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		requestContext, release := model.WithEncryptedBoxOperationScope(request.Context())
		requestID := uuid.NewString()
		scopedRequest := request.WithContext(requestContext)
		scopedRequest.Header = request.Header.Clone()
		scopedRequest.Header.Set(requestOperationScopeHeader, requestID)
		requestOperationScopes.Store(requestID, requestContext)
		defer func() {
			requestOperationScopes.Delete(requestID)
			release()
		}()

		handler.ServeHTTP(writer, scopedRequest)

View on GitHub (pinned to 9f775e8a12)