{"record":{"id":"111d848bef8301ba","repo":"siyuan-note/siyuan","slug":"mcp-request-operation-scope-is-closed","errorCode":null,"errorMessage":"MCP request operation scope is closed","messagePattern":"MCP request operation scope is closed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/request_scope.go","lineNumber":33,"sourceCode":"// along with this program.  If not, see <https://www.gnu.org/licenses/>.\n\npackage mcp\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"net/http\"\n\t\"sync\"\n\n\t\"github.com/google/uuid\"\n\tmcpsdk \"github.com/modelcontextprotocol/go-sdk/mcp\"\n\t\"github.com/siyuan-note/siyuan/kernel/model\"\n)\n\nconst requestOperationScopeHeader = \"X-Siyuan-Mcp-Request-Scope\"\n\nvar (\n\terrMCPRequestOperationScopeClosed = errors.New(\"MCP request operation scope is closed\")\n\trequestOperationScopes            sync.Map // 键为请求 ID，值为 context.Context。\n)\n\n// withEncryptedBoxOperationScope 为当前 HTTP 响应注册加密笔记本操作作用域。\nfunc withEncryptedBoxOperationScope(handler http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {\n\t\trequestContext, release := model.WithEncryptedBoxOperationScope(request.Context())\n\t\trequestID := uuid.NewString()\n\t\tscopedRequest := request.WithContext(requestContext)\n\t\tscopedRequest.Header = request.Header.Clone()\n\t\tscopedRequest.Header.Set(requestOperationScopeHeader, requestID)\n\t\trequestOperationScopes.Store(requestID, requestContext)\n\t\tdefer func() {\n\t\t\trequestOperationScopes.Delete(requestID)\n\t\t\trelease()\n\t\t}()\n\n\t\thandler.ServeHTTP(writer, scopedRequest)","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/request_scope.go#L15-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the request goes through the middleware that registers the scope (withEncryptedBoxOperationScope) before any code calls requestOperationContext","Perform encrypted-notebook operations while the original HTTP request is still in flight; capture the context before the handler returns","For background work, derive and pass an explicit context instead of relying on the request scope","Fix any code that stores a non-context value under the request ID"],"exampleFix":"// before\nctx, err := requestOperationContext(staleRequestID) // scope already closed\n// after\n// call within the handler lifetime, or:\nctx := context.Background() // for out-of-request work","handlingStrategy":"fallback","validationCode":"if _, ok := requestOperationScopes.Load(requestID); !ok {\n    // scope not registered or already closed — use an explicit context instead\n    ctx = context.Background()\n}","typeGuard":null,"tryCatchPattern":"ctx, err := requestOperationContext(requestID)\nif errors.Is(err, errMCPRequestOperationScopeClosed) {\n    ctx = context.Background() // out-of-request fallback\n}","preventionTips":["Route all MCP calls through the scope-registering middleware","Never retain request scopes beyond response completion; capture needed values first","For async work, propagate an explicit context rather than reading the request scope"],"tags":["mcp","context","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}