|
| 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 Supergateway_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 | + credential: INodeParams |
| 17 | + inputs: INodeParams[] |
| 18 | + |
| 19 | + constructor() { |
| 20 | + this.label = 'Supergateway MCP' |
| 21 | + this.name = 'supergatewayMCP' |
| 22 | + this.version = 1.0 |
| 23 | + this.type = 'Supergateway MCP Tool' |
| 24 | + this.icon = 'supermachine-logo.png' |
| 25 | + this.category = 'Tools (MCP)' |
| 26 | + this.description = 'Runs MCP stdio-based servers over SSE (Server-Sent Events) or WebSockets (WS)' |
| 27 | + this.documentation = 'https://github.com/supercorp-ai/supergateway' |
| 28 | + this.inputs = [ |
| 29 | + { |
| 30 | + label: 'Arguments', |
| 31 | + name: 'arguments', |
| 32 | + type: 'string', |
| 33 | + rows: 4, |
| 34 | + placeholder: '--sse "https://mcp-server-ab71a6b2-cd55-49d0-adba-562bc85956e3.supermachine.app"', |
| 35 | + description: |
| 36 | + 'Arguments to pass to the supergateway server. Refer to the <a href="https://github.com/supercorp-ai/supergateway/blob/main/README.md" target="_blank">documentation</a> for more information.' |
| 37 | + }, |
| 38 | + { |
| 39 | + label: 'Available Actions', |
| 40 | + name: 'mcpActions', |
| 41 | + type: 'asyncMultiOptions', |
| 42 | + loadMethod: 'listActions', |
| 43 | + refresh: true |
| 44 | + } |
| 45 | + ] |
| 46 | + this.baseClasses = ['Tool'] |
| 47 | + } |
| 48 | + |
| 49 | + //@ts-ignore |
| 50 | + loadMethods = { |
| 51 | + listActions: async (nodeData: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> => { |
| 52 | + try { |
| 53 | + const toolset = await this.getTools(nodeData, options) |
| 54 | + toolset.sort((a: any, b: any) => a.name.localeCompare(b.name)) |
| 55 | + |
| 56 | + return toolset.map(({ name, ...rest }) => ({ |
| 57 | + label: name.toUpperCase(), |
| 58 | + name: name, |
| 59 | + description: rest.description || name |
| 60 | + })) |
| 61 | + } catch (error) { |
| 62 | + return [ |
| 63 | + { |
| 64 | + label: 'No Available Actions', |
| 65 | + name: 'error', |
| 66 | + description: 'No available actions, please check the arguments again and refresh' |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 74 | + const tools = await this.getTools(nodeData, options) |
| 75 | + |
| 76 | + const _mcpActions = nodeData.inputs?.mcpActions |
| 77 | + let mcpActions = [] |
| 78 | + if (_mcpActions) { |
| 79 | + try { |
| 80 | + mcpActions = typeof _mcpActions === 'string' ? JSON.parse(_mcpActions) : _mcpActions |
| 81 | + } catch (error) { |
| 82 | + console.error('Error parsing mcp actions:', error) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return tools.filter((tool: any) => mcpActions.includes(tool.name)) |
| 87 | + } |
| 88 | + |
| 89 | + async getTools(nodeData: INodeData, _: ICommonObject): Promise<Tool[]> { |
| 90 | + const _args = nodeData.inputs?.arguments as string |
| 91 | + const packagePath = getNodeModulesPackagePath('supergateway/dist/index.js') |
| 92 | + |
| 93 | + const serverParams = { |
| 94 | + command: 'node', |
| 95 | + args: [ |
| 96 | + packagePath, |
| 97 | + ..._args |
| 98 | + .trim() |
| 99 | + .split(/\s+/) |
| 100 | + .map((arg) => { |
| 101 | + // Remove surrounding double or single quotes if they exist |
| 102 | + if ((arg.startsWith('"') && arg.endsWith('"')) || (arg.startsWith("'") && arg.endsWith("'"))) { |
| 103 | + return arg.slice(1, -1) |
| 104 | + } |
| 105 | + return arg |
| 106 | + }) |
| 107 | + ] |
| 108 | + } |
| 109 | + |
| 110 | + const toolkit = new MCPToolkit(serverParams, 'stdio') |
| 111 | + await toolkit.initialize() |
| 112 | + |
| 113 | + const tools = toolkit.tools ?? [] |
| 114 | + |
| 115 | + return tools as Tool[] |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +module.exports = { nodeClass: Supergateway_MCP } |
0 commit comments