|
| 1 | +import { Tool } from '@langchain/core/tools' |
| 2 | +import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../../src/Interface' |
| 3 | +import { MCPToolkit } from '../core' |
| 4 | +import { decryptCredentialData } from '../../../../src/utils' |
| 5 | +import { DataSource } from 'typeorm' |
| 6 | + |
| 7 | +class CustomMcpServerTool implements INode { |
| 8 | + label: string |
| 9 | + name: string |
| 10 | + version: number |
| 11 | + description: string |
| 12 | + type: string |
| 13 | + icon: string |
| 14 | + category: string |
| 15 | + baseClasses: string[] |
| 16 | + inputs: INodeParams[] |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.label = 'Custom MCP Server' |
| 20 | + this.name = 'customMcpServerTool' |
| 21 | + this.version = 1.0 |
| 22 | + this.type = 'Custom MCP Server Tool' |
| 23 | + this.icon = 'customMCP.png' |
| 24 | + this.category = 'Tools (MCP)' |
| 25 | + this.description = 'Use tools from authorized MCP servers configured in workspace' |
| 26 | + this.inputs = [ |
| 27 | + { |
| 28 | + label: 'Custom MCP Server', |
| 29 | + name: 'mcpServerId', |
| 30 | + type: 'asyncOptions', |
| 31 | + loadMethod: 'listServers' |
| 32 | + }, |
| 33 | + { |
| 34 | + label: 'Available Actions', |
| 35 | + name: 'mcpActions', |
| 36 | + type: 'asyncMultiOptions', |
| 37 | + loadMethod: 'listActions', |
| 38 | + refresh: true |
| 39 | + } |
| 40 | + ] |
| 41 | + this.baseClasses = ['Tool'] |
| 42 | + } |
| 43 | + |
| 44 | + //@ts-ignore |
| 45 | + loadMethods = { |
| 46 | + listServers: async (_: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> => { |
| 47 | + try { |
| 48 | + const appDataSource = options.appDataSource as DataSource |
| 49 | + const databaseEntities = options.databaseEntities as IDatabaseEntity |
| 50 | + if (!appDataSource || !databaseEntities?.['CustomMcpServer']) { |
| 51 | + return [] |
| 52 | + } |
| 53 | + |
| 54 | + const workspaceId = (options.searchOptions as ICommonObject | undefined)?.workspaceId as string | undefined |
| 55 | + if (!workspaceId) return [] |
| 56 | + |
| 57 | + const mcpServers = await appDataSource.getRepository(databaseEntities['CustomMcpServer']).find({ |
| 58 | + where: { workspaceId, status: 'AUTHORIZED' }, |
| 59 | + order: { updatedDate: 'DESC' } |
| 60 | + }) |
| 61 | + |
| 62 | + return mcpServers.map((server: any) => { |
| 63 | + let maskedUrl: string |
| 64 | + try { |
| 65 | + const parsed = new URL(server.serverUrl) |
| 66 | + maskedUrl = parsed.pathname && parsed.pathname !== '/' ? `${parsed.origin}/************` : parsed.origin |
| 67 | + } catch { |
| 68 | + maskedUrl = '************' |
| 69 | + } |
| 70 | + return { |
| 71 | + label: server.name, |
| 72 | + name: server.id, |
| 73 | + description: maskedUrl |
| 74 | + } |
| 75 | + }) |
| 76 | + } catch (error) { |
| 77 | + return [] |
| 78 | + } |
| 79 | + }, |
| 80 | + listActions: async (nodeData: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> => { |
| 81 | + try { |
| 82 | + const toolset = await this.getTools(nodeData, options) |
| 83 | + toolset.sort((a: any, b: any) => a.name.localeCompare(b.name)) |
| 84 | + |
| 85 | + return toolset.map(({ name, ...rest }) => ({ |
| 86 | + label: name.toUpperCase(), |
| 87 | + name: name, |
| 88 | + description: rest.description || name |
| 89 | + })) |
| 90 | + } catch (error) { |
| 91 | + return [ |
| 92 | + { |
| 93 | + label: 'No Available Actions', |
| 94 | + name: 'error', |
| 95 | + description: 'Select an authorized MCP server first, then refresh' |
| 96 | + } |
| 97 | + ] |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 103 | + const tools = await this.getTools(nodeData, options) |
| 104 | + |
| 105 | + const _mcpActions = nodeData.inputs?.mcpActions |
| 106 | + let mcpActions: string[] = [] |
| 107 | + if (_mcpActions) { |
| 108 | + try { |
| 109 | + mcpActions = typeof _mcpActions === 'string' ? JSON.parse(_mcpActions) : _mcpActions |
| 110 | + } catch (error) { |
| 111 | + console.error('Error parsing mcp actions:', error) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return tools.filter((tool: any) => mcpActions.includes(tool.name)) |
| 116 | + } |
| 117 | + |
| 118 | + async getTools(nodeData: INodeData, options: ICommonObject): Promise<Tool[]> { |
| 119 | + const serverId = nodeData.inputs?.mcpServerId as string |
| 120 | + if (!serverId) { |
| 121 | + throw new Error('MCP Server is required') |
| 122 | + } |
| 123 | + |
| 124 | + const appDataSource = options.appDataSource as DataSource |
| 125 | + const databaseEntities = options.databaseEntities as IDatabaseEntity |
| 126 | + if (!appDataSource || !databaseEntities?.['CustomMcpServer']) { |
| 127 | + throw new Error('Database not available') |
| 128 | + } |
| 129 | + |
| 130 | + const workspaceId = |
| 131 | + (options.workspaceId as string | undefined) ?? |
| 132 | + ((options.searchOptions as ICommonObject | undefined)?.workspaceId as string | undefined) |
| 133 | + if (!workspaceId) { |
| 134 | + throw new Error('Workspace context is required to load MCP server') |
| 135 | + } |
| 136 | + |
| 137 | + const serverRecord = await appDataSource.getRepository(databaseEntities['CustomMcpServer']).findOneBy({ id: serverId, workspaceId }) |
| 138 | + if (!serverRecord) { |
| 139 | + throw new Error(`MCP server ${serverId} not found`) |
| 140 | + } |
| 141 | + if (serverRecord.status !== 'AUTHORIZED') { |
| 142 | + throw new Error(`MCP server "${serverRecord.name}" is not authorized. Please authorize it in the Tools page first.`) |
| 143 | + } |
| 144 | + |
| 145 | + // Build headers from encrypted authConfig — only when authType explicitly requires them |
| 146 | + let headers: Record<string, string> = {} |
| 147 | + if (serverRecord.authType === 'CUSTOM_HEADERS' && serverRecord.authConfig) { |
| 148 | + try { |
| 149 | + const decrypted = await decryptCredentialData(serverRecord.authConfig) |
| 150 | + if (decrypted?.headers && typeof decrypted.headers === 'object') { |
| 151 | + headers = decrypted.headers as Record<string, string> |
| 152 | + } |
| 153 | + } catch { |
| 154 | + // authConfig decryption failed — proceed without headers |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + const serverParams: any = { |
| 159 | + url: serverRecord.serverUrl, |
| 160 | + ...(Object.keys(headers).length > 0 ? { headers } : {}) |
| 161 | + } |
| 162 | + |
| 163 | + if (options.cachePool) { |
| 164 | + const cacheKey = `mcpServer_${serverId}` |
| 165 | + const cachedResult = await options.cachePool.getMCPCache(cacheKey) |
| 166 | + if (cachedResult) { |
| 167 | + return cachedResult.tools |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + const toolkit = new MCPToolkit(serverParams, 'sse') |
| 172 | + await toolkit.initialize() |
| 173 | + |
| 174 | + const tools = toolkit.tools ?? [] |
| 175 | + |
| 176 | + if (options.cachePool) { |
| 177 | + const cacheKey = `mcpServer_${serverId}` |
| 178 | + await options.cachePool.addMCPCache(cacheKey, { toolkit, tools }) |
| 179 | + } |
| 180 | + |
| 181 | + return tools.map((tool: Tool) => { |
| 182 | + tool.name = this.formatToolName(tool.name) |
| 183 | + return tool |
| 184 | + }) as Tool[] |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Formats the tool name to ensure it is a valid identifier by replacing spaces and special characters with underscores. |
| 189 | + * This is necessary because tool names may be used as identifiers in various contexts where special characters could cause issues. |
| 190 | + * For example, a tool named "Get User Info" would be formatted to "Get_User_Info". |
| 191 | + * This method can be enhanced further to handle edge cases as needed. |
| 192 | + */ |
| 193 | + private formatToolName = (name: string): string => name.trim().replace(/[^a-zA-Z0-9_-]/g, '_') |
| 194 | +} |
| 195 | + |
| 196 | +module.exports = { nodeClass: CustomMcpServerTool } |
0 commit comments