Skip to content

Feature/MCP (Model Context Protocol) #4134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/components/credentials/PostgresUrl.credential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { INodeParams, INodeCredential } from '../src/Interface'

class PostgresUrl implements INodeCredential {
label: string
name: string
version: number
description: string
inputs: INodeParams[]

constructor() {
this.label = 'Postgres URL'
this.name = 'PostgresUrl'
this.version = 1.0
this.inputs = [
{
label: 'Postgres URL',
name: 'postgresUrl',
type: 'string',
placeholder: 'postgresql://localhost/mydb'
}
]
}
}

module.exports = { credClass: PostgresUrl }
32 changes: 32 additions & 0 deletions packages/components/credentials/SlackApi.credential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { INodeParams, INodeCredential } from '../src/Interface'

class SlackApi implements INodeCredential {
label: string
name: string
version: number
description: string
inputs: INodeParams[]

constructor() {
this.label = 'Slack API'
this.name = 'slackApi'
this.version = 1.0
this.description =
'Refer to <a target="_blank" href="https://github.com/modelcontextprotocol/servers/tree/main/src/slack">official guide</a> on how to get botToken and teamId on Slack'
this.inputs = [
{
label: 'Bot Token',
name: 'botToken',
type: 'password'
},
{
label: 'Team ID',
name: 'teamId',
type: 'string',
placeholder: '<SLACK_TEAM_ID>'
}
]
}
}

module.exports = { credClass: SlackApi }
74 changes: 74 additions & 0 deletions packages/components/nodes/tools/CurrentDateTime/CurrentDateTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { z } from 'zod'
import { INode } from '../../../src/Interface'
import { DynamicStructuredTool } from '../CustomTool/core'

const code = `
const now = new Date();

// Format date as YYYY-MM-DD
const date = now.toISOString().split('T')[0];

// Get time in HH:MM:SS format
const time = now.toTimeString().split(' ')[0];

// Get day of week
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const day = days[now.getDay()];

// Get timezone information
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const timezoneOffset = now.getTimezoneOffset();
const timezoneOffsetHours = Math.abs(Math.floor(timezoneOffset / 60));
const timezoneOffsetMinutes = Math.abs(timezoneOffset % 60);
const timezoneOffsetFormatted =
(timezoneOffset <= 0 ? '+' : '-') +
timezoneOffsetHours.toString().padStart(2, '0') + ':' +
timezoneOffsetMinutes.toString().padStart(2, '0');

return {
date,
time,
day,
timezone,
timezoneOffset: timezoneOffsetFormatted,
iso8601: now.toISOString(),
unix_timestamp: Math.floor(now.getTime() / 1000)
};
`

class CurrentDateTime_Tools implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]

constructor() {
this.label = 'CurrentDateTime'
this.name = 'currentDateTime'
this.version = 1.0
this.type = 'CurrentDateTime'
this.icon = 'currentDateTime.svg'
this.category = 'Tools'
this.description = 'Get todays day, date and time.'
this.baseClasses = [this.type, 'Tool']
}

async init(): Promise<any> {
const obj = {
name: 'current_date_time',
description: 'Useful to get current day, date and time.',
schema: z.object({}),
code: code
}

let dynamicStructuredTool = new DynamicStructuredTool(obj)

return dynamicStructuredTool
}
}

module.exports = { nodeClass: CurrentDateTime_Tools }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions packages/components/nodes/tools/MCP/BraveSearch/BraveSearchMCP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Tool } from '@langchain/core/tools'
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../../src/Interface'
import { getCredentialData, getCredentialParam, getNodeModulesPackagePath } from '../../../../src/utils'
import { MCPToolkit } from '../core'

class BraveSearch_MCP implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
documentation: string
credential: INodeParams
inputs: INodeParams[]

constructor() {
this.label = 'Brave Search MCP'
this.name = 'braveSearchMCP'
this.version = 1.0
this.type = 'BraveSearch MCP Tool'
this.icon = 'brave.svg'
this.category = 'Tools (MCP)'
this.description = 'MCP server that integrates the Brave Search API - a real-time API to access web search capabilities'
this.documentation = 'https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search'
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['braveSearchApi']
}
this.inputs = [
{
label: 'Available Actions',
name: 'mcpActions',
type: 'asyncMultiOptions',
loadMethod: 'listActions',
refresh: true
}
]
this.baseClasses = ['Tool']
}

//@ts-ignore
loadMethods = {
listActions: async (nodeData: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> => {
try {
const toolset = await this.getTools(nodeData, options)
toolset.sort((a: any, b: any) => a.name.localeCompare(b.name))

return toolset.map(({ name, ...rest }) => ({
label: name.toUpperCase(),
name: name,
description: rest.description || name
}))
} catch (error) {
return [
{
label: 'No Available Actions',
name: 'error',
description: 'No available actions, please check your API key and refresh'
}
]
}
}
}

async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const tools = await this.getTools(nodeData, options)

const _mcpActions = nodeData.inputs?.mcpActions
let mcpActions = []
if (_mcpActions) {
try {
mcpActions = typeof _mcpActions === 'string' ? JSON.parse(_mcpActions) : _mcpActions
} catch (error) {
console.error('Error parsing mcp actions:', error)
}
}

return tools.filter((tool: any) => mcpActions.includes(tool.name))
}

async getTools(nodeData: INodeData, options: ICommonObject): Promise<Tool[]> {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const braveApiKey = getCredentialParam('braveApiKey', credentialData, nodeData)
const packagePath = getNodeModulesPackagePath('@modelcontextprotocol/server-brave-search/dist/index.js')

const serverParams = {
command: 'node',
args: [packagePath],
env: {
BRAVE_API_KEY: braveApiKey
}
}

const toolkit = new MCPToolkit(serverParams, 'stdio')
await toolkit.initialize()

const tools = toolkit.tools ?? []

return tools as Tool[]
}
}

module.exports = { nodeClass: BraveSearch_MCP }
8 changes: 8 additions & 0 deletions packages/components/nodes/tools/MCP/BraveSearch/brave.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions packages/components/nodes/tools/MCP/Github/GithubMCP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Tool } from '@langchain/core/tools'
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../../src/Interface'
import { getCredentialData, getCredentialParam, getNodeModulesPackagePath } from '../../../../src/utils'
import { MCPToolkit } from '../core'

class Github_MCP implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
documentation: string
credential: INodeParams
inputs: INodeParams[]

constructor() {
this.label = 'Github MCP'
this.name = 'githubMCP'
this.version = 1.0
this.type = 'Github MCP Tool'
this.icon = 'github.svg'
this.category = 'Tools (MCP)'
this.description = 'MCP Server for the GitHub API'
this.documentation = 'https://github.com/modelcontextprotocol/servers/tree/main/src/github'
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['githubApi']
}
this.inputs = [
{
label: 'Available Actions',
name: 'mcpActions',
type: 'asyncMultiOptions',
loadMethod: 'listActions',
refresh: true
}
]
this.baseClasses = ['Tool']
}

//@ts-ignore
loadMethods = {
listActions: async (nodeData: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> => {
try {
const toolset = await this.getTools(nodeData, options)
toolset.sort((a: any, b: any) => a.name.localeCompare(b.name))

return toolset.map(({ name, ...rest }) => ({
label: name.toUpperCase(),
name: name,
description: rest.description || name
}))
} catch (error) {
console.error('Error listing actions:', error)
return [
{
label: 'No Available Actions',
name: 'error',
description: 'No available actions, please check your Github Access Token and refresh'
}
]
}
}
}

async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const tools = await this.getTools(nodeData, options)

const _mcpActions = nodeData.inputs?.mcpActions
let mcpActions = []
if (_mcpActions) {
try {
mcpActions = typeof _mcpActions === 'string' ? JSON.parse(_mcpActions) : _mcpActions
} catch (error) {
console.error('Error parsing mcp actions:', error)
}
}

return tools.filter((tool: any) => mcpActions.includes(tool.name))
}

async getTools(nodeData: INodeData, options: ICommonObject): Promise<Tool[]> {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const accessToken = getCredentialParam('accessToken', credentialData, nodeData)

if (!accessToken) {
throw new Error('Missing Github Access Token')
}

const packagePath = getNodeModulesPackagePath('@modelcontextprotocol/server-github/dist/index.js')

const serverParams = {
command: 'node',
args: [packagePath],
env: {
GITHUB_PERSONAL_ACCESS_TOKEN: accessToken
}
}

const toolkit = new MCPToolkit(serverParams, 'stdio')
await toolkit.initialize()

const tools = toolkit.tools ?? []

return tools as Tool[]
}
}

module.exports = { nodeClass: Github_MCP }
13 changes: 13 additions & 0 deletions packages/components/nodes/tools/MCP/Github/github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading