|
| 1 | +import { Tool } from '@langchain/core/tools' |
| 2 | +import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../../src/Interface' |
| 3 | +import { getNodeModulesPackagePath } from '../../../../src/utils' |
| 4 | +import { MCPToolkit } from '../core' |
| 5 | + |
| 6 | +class SequentialThinking_MCP implements INode { |
| 7 | + label: string |
| 8 | + name: string |
| 9 | + version: number |
| 10 | + description: string |
| 11 | + type: string |
| 12 | + icon: string |
| 13 | + category: string |
| 14 | + baseClasses: string[] |
| 15 | + documentation: string |
| 16 | + inputs: INodeParams[] |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.label = 'Sequential Thinking MCP' |
| 20 | + this.name = 'sequentialThinkingMCP' |
| 21 | + this.version = 1.0 |
| 22 | + this.type = 'Sequential Thinking MCP Tool' |
| 23 | + this.icon = 'sequentialthinking.svg' |
| 24 | + this.category = 'Tools (MCP)' |
| 25 | + this.description = |
| 26 | + 'MCP server that provides a tool for dynamic and reflective problem-solving through a structured thinking process' |
| 27 | + this.documentation = 'https://github.com/modelcontextprotocol/servers/tree/main/src/sequentialthinking' |
| 28 | + this.inputs = [ |
| 29 | + { |
| 30 | + label: 'Available Actions', |
| 31 | + name: 'mcpActions', |
| 32 | + type: 'asyncMultiOptions', |
| 33 | + loadMethod: 'listActions', |
| 34 | + refresh: true |
| 35 | + } |
| 36 | + ] |
| 37 | + this.baseClasses = ['Tool'] |
| 38 | + } |
| 39 | + |
| 40 | + //@ts-ignore |
| 41 | + loadMethods = { |
| 42 | + listActions: async (nodeData: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> => { |
| 43 | + try { |
| 44 | + const toolset = await this.getTools(nodeData, options) |
| 45 | + toolset.sort((a: any, b: any) => a.name.localeCompare(b.name)) |
| 46 | + |
| 47 | + return toolset.map(({ name, ...rest }) => ({ |
| 48 | + label: name.toUpperCase(), |
| 49 | + name: name, |
| 50 | + description: rest.description || name |
| 51 | + })) |
| 52 | + } catch (error) { |
| 53 | + console.error('Error listing actions:', error) |
| 54 | + return [ |
| 55 | + { |
| 56 | + label: 'No Available Actions', |
| 57 | + name: 'error', |
| 58 | + description: 'No available actions, please refresh' |
| 59 | + } |
| 60 | + ] |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 66 | + const tools = await this.getTools(nodeData, options) |
| 67 | + |
| 68 | + const _mcpActions = nodeData.inputs?.mcpActions |
| 69 | + let mcpActions = [] |
| 70 | + if (_mcpActions) { |
| 71 | + try { |
| 72 | + mcpActions = typeof _mcpActions === 'string' ? JSON.parse(_mcpActions) : _mcpActions |
| 73 | + } catch (error) { |
| 74 | + console.error('Error parsing mcp actions:', error) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return tools.filter((tool: any) => mcpActions.includes(tool.name)) |
| 79 | + } |
| 80 | + |
| 81 | + async getTools(_nodeData: INodeData, _options: ICommonObject): Promise<Tool[]> { |
| 82 | + const packagePath = getNodeModulesPackagePath('@modelcontextprotocol/server-sequential-thinking/dist/index.js') |
| 83 | + |
| 84 | + const serverParams = { |
| 85 | + command: 'node', |
| 86 | + args: [packagePath] |
| 87 | + } |
| 88 | + |
| 89 | + const toolkit = new MCPToolkit(serverParams, 'stdio') |
| 90 | + await toolkit.initialize() |
| 91 | + |
| 92 | + const tools = toolkit.tools ?? [] |
| 93 | + |
| 94 | + return tools as Tool[] |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +module.exports = { nodeClass: SequentialThinking_MCP } |
0 commit comments