|
| 1 | +import { Tool } from '@langchain/core/tools' |
| 2 | +import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../../src/Interface' |
| 3 | +import { getCredentialData, getCredentialParam, getNodeModulesPackagePath } from '../../../../src/utils' |
| 4 | +import { MCPToolkit } from '../core' |
| 5 | + |
| 6 | +class Github_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 = 'Github MCP' |
| 21 | + this.name = 'githubMCP' |
| 22 | + this.version = 1.0 |
| 23 | + this.type = 'Github MCP Tool' |
| 24 | + this.icon = 'github.svg' |
| 25 | + this.category = 'Tools (MCP)' |
| 26 | + this.description = 'MCP Server for the GitHub API' |
| 27 | + this.documentation = 'https://github.com/modelcontextprotocol/servers/tree/main/src/github' |
| 28 | + this.credential = { |
| 29 | + label: 'Connect Credential', |
| 30 | + name: 'credential', |
| 31 | + type: 'credential', |
| 32 | + credentialNames: ['githubApi'] |
| 33 | + } |
| 34 | + this.inputs = [ |
| 35 | + { |
| 36 | + label: 'Available Actions', |
| 37 | + name: 'mcpActions', |
| 38 | + type: 'asyncMultiOptions', |
| 39 | + loadMethod: 'listActions', |
| 40 | + refresh: true |
| 41 | + } |
| 42 | + ] |
| 43 | + this.baseClasses = ['Tool'] |
| 44 | + } |
| 45 | + |
| 46 | + //@ts-ignore |
| 47 | + loadMethods = { |
| 48 | + listActions: async (nodeData: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> => { |
| 49 | + try { |
| 50 | + const toolset = await this.getTools(nodeData, options) |
| 51 | + toolset.sort((a: any, b: any) => a.name.localeCompare(b.name)) |
| 52 | + |
| 53 | + return toolset.map(({ name, ...rest }) => ({ |
| 54 | + label: name.toUpperCase(), |
| 55 | + name: name, |
| 56 | + description: rest.description || name |
| 57 | + })) |
| 58 | + } catch (error) { |
| 59 | + console.error('Error listing actions:', error) |
| 60 | + return [ |
| 61 | + { |
| 62 | + label: 'No Available Actions', |
| 63 | + name: 'error', |
| 64 | + description: 'No available actions, please check your Github Access Token and refresh' |
| 65 | + } |
| 66 | + ] |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 72 | + const tools = await this.getTools(nodeData, options) |
| 73 | + |
| 74 | + const _mcpActions = nodeData.inputs?.mcpActions |
| 75 | + let mcpActions = [] |
| 76 | + if (_mcpActions) { |
| 77 | + try { |
| 78 | + mcpActions = typeof _mcpActions === 'string' ? JSON.parse(_mcpActions) : _mcpActions |
| 79 | + } catch (error) { |
| 80 | + console.error('Error parsing mcp actions:', error) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return tools.filter((tool: any) => mcpActions.includes(tool.name)) |
| 85 | + } |
| 86 | + |
| 87 | + async getTools(nodeData: INodeData, options: ICommonObject): Promise<Tool[]> { |
| 88 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 89 | + const accessToken = getCredentialParam('accessToken', credentialData, nodeData) |
| 90 | + |
| 91 | + if (!accessToken) { |
| 92 | + throw new Error('Missing Github Access Token') |
| 93 | + } |
| 94 | + |
| 95 | + const packagePath = getNodeModulesPackagePath('@modelcontextprotocol/server-github/dist/index.js') |
| 96 | + |
| 97 | + const serverParams = { |
| 98 | + command: 'node', |
| 99 | + args: [packagePath], |
| 100 | + env: { |
| 101 | + GITHUB_PERSONAL_ACCESS_TOKEN: accessToken |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + const toolkit = new MCPToolkit(serverParams, 'stdio') |
| 106 | + await toolkit.initialize() |
| 107 | + |
| 108 | + const tools = toolkit.tools ?? [] |
| 109 | + |
| 110 | + return tools as Tool[] |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +module.exports = { nodeClass: Github_MCP } |
0 commit comments