{"record":{"id":"4258e5310af6f210","repo":"FlowiseAI/Flowise","slug":"must-initialize-the-toolkit-first","errorCode":null,"errorMessage":"Must initialize the toolkit first","messagePattern":"Must initialize the toolkit first","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/components/nodes/tools/MCP/core.ts","lineNumber":186,"sourceCode":"        return client\n    }\n\n    async initialize() {\n        if (this._tools === null) {\n            this.client = await this.createClient()\n\n            this._tools = await this.client.request({ method: 'tools/list' }, ListToolsResultSchema)\n\n            this.tools = await this.get_tools()\n\n            // Close the initial client after initialization\n            await this.client.close()\n        }\n    }\n\n    async get_tools(): Promise<Tool[]> {\n        if (this._tools === null || this.client === null) {\n            throw new Error('Must initialize the toolkit first')\n        }\n        const toolsPromises = this._tools.tools.map(async (tool: any) => {\n            if (this.client === null) {\n                throw new Error('Client is not initialized')\n            }\n            const argsSchema = tool.inputSchema ?? { type: 'object', properties: {} }\n            const safeName = sanitizeMCPToolName(tool.name)\n            const safeDescription = sanitizeMCPToolDescription(tool.description || tool.name)\n            return await MCPTool({\n                toolkit: this,\n                name: safeName,\n                description: safeDescription,\n                argsSchema\n            })\n        })\n        const res = await Promise.allSettled(toolsPromises)\n        const errors = res.filter((r) => r.status === 'rejected')\n        if (errors.length !== 0) {","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/FlowiseAI/Flowise/blob/abe4a8601a058047b350c260676826e21dd14101/packages/components/nodes/tools/MCP/core.ts#L168-L204","documentation":"Thrown by MCPToolkit.get_tools when this._tools === null OR this.client === null. get_tools reads this._tools.tools and maps over them, so both the cached tool list and a live client must exist. initialize() is the only method that sets this._tools and this.client; calling get_tools() before initialize() (or after the client was closed and nulled) violates that precondition.","triggerScenarios":"Calling toolkit.get_tools() before toolkit.initialize(); or calling it again after the toolkit's client was closed/null. Note initialize() itself calls get_tools() internally, so manual calls are usually redundant.","commonSituations":"Subclass or caller that calls get_tools() directly to refresh the tool list; race where initialize() threw midway leaving _tools null; reusing a toolkit across requests after close().","solutions":["Always await toolkit.initialize() before calling get_tools() (or rely on initialize() which already populates toolkit.tools).","Read toolkit.tools (the cached array) instead of calling get_tools() again.","Construct a fresh MCPToolkit per session rather than reusing one whose client was closed."],"exampleFix":"// before\nconst toolkit = new MCPToolkit(params, 'stdio')\nconst tools = await toolkit.get_tools() // throws: not initialized\n\n// after\nconst toolkit = new MCPToolkit(params, 'stdio')\nawait toolkit.initialize()\nconst tools = toolkit.tools ?? []","handlingStrategy":"try-catch","validationCode":"if (toolkit._tools === null || toolkit.client === null) {\n  await toolkit.initialize()\n}\nconst tools = toolkit.tools ?? []","typeGuard":"const isInitialized = (tk: MCPToolkit): boolean => tk._tools !== null && tk.client !== null","tryCatchPattern":"try {\n  return await toolkit.get_tools()\n} catch (e) {\n  if (e.message === 'Must initialize the toolkit first') {\n    await toolkit.initialize()\n    return toolkit.tools ?? []\n  }\n  throw e\n}","preventionTips":["Always await initialize() before any access; read toolkit.tools instead of get_tools().","Treat MCPToolkit as single-use: one instance per session.","Add an assertion helper that fails fast if _tools is null."],"tags":["mcp","lifecycle","state"],"backgroundTag":null,"analyzedSha":"abe4a8601a058047b350c260676826e21dd14101","analyzedAt":"2026-08-12T16:04:40.823Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}