mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 23:01:09 +03:00
Feature: Upgrade MCP version and support SSE mode. (#4329)
Upgrade MCP version and support SSE mode. Co-authored-by: guwenjia <guwenjia@bilibili.com>
This commit is contained in:
@@ -104,7 +104,14 @@ class Custom_MCP implements INode {
|
|||||||
serverParams = JSON.parse(serverParamsString)
|
serverParams = JSON.parse(serverParamsString)
|
||||||
}
|
}
|
||||||
|
|
||||||
const toolkit = new MCPToolkit(serverParams, 'stdio')
|
// Compatible with stdio and SSE
|
||||||
|
let toolkit: MCPToolkit
|
||||||
|
if (serverParams?.command === undefined) {
|
||||||
|
toolkit = new MCPToolkit(serverParams, 'sse')
|
||||||
|
} else {
|
||||||
|
toolkit = new MCPToolkit(serverParams, 'stdio')
|
||||||
|
}
|
||||||
|
|
||||||
await toolkit.initialize()
|
await toolkit.initialize()
|
||||||
|
|
||||||
const tools = toolkit.tools ?? []
|
const tools = toolkit.tools ?? []
|
||||||
|
|||||||
@@ -3,20 +3,21 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js'
|
|||||||
import { StdioClientTransport, StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js'
|
import { StdioClientTransport, StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js'
|
||||||
import { BaseToolkit, tool, Tool } from '@langchain/core/tools'
|
import { BaseToolkit, tool, Tool } from '@langchain/core/tools'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
|
||||||
|
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
|
||||||
|
|
||||||
export class MCPToolkit extends BaseToolkit {
|
export class MCPToolkit extends BaseToolkit {
|
||||||
tools: Tool[] = []
|
tools: Tool[] = []
|
||||||
_tools: ListToolsResult | null = null
|
_tools: ListToolsResult | null = null
|
||||||
model_config: any
|
model_config: any
|
||||||
transport: StdioClientTransport | null = null
|
transport: StdioClientTransport | SSEClientTransport | StreamableHTTPClientTransport | null = null
|
||||||
client: Client | null = null
|
client: Client | null = null
|
||||||
constructor(serverParams: StdioServerParameters | any, transport: 'stdio' | 'sse') {
|
serverParams: StdioServerParameters | any
|
||||||
|
transportType: 'stdio' | 'sse'
|
||||||
|
constructor(serverParams: StdioServerParameters | any, transportType: 'stdio' | 'sse') {
|
||||||
super()
|
super()
|
||||||
if (transport === 'stdio') {
|
this.serverParams = serverParams
|
||||||
this.transport = new StdioClientTransport(serverParams as StdioServerParameters)
|
this.transportType = transportType
|
||||||
} else {
|
|
||||||
// TODO: this.transport = new SSEClientTransport(serverParams.url);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
async initialize() {
|
async initialize() {
|
||||||
if (this._tools === null) {
|
if (this._tools === null) {
|
||||||
@@ -29,10 +30,30 @@ export class MCPToolkit extends BaseToolkit {
|
|||||||
capabilities: {}
|
capabilities: {}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
if (this.transport === null) {
|
if (this.transportType === 'stdio') {
|
||||||
throw new Error('Transport is not initialized')
|
// Compatible with overridden PATH configuration
|
||||||
|
this.serverParams.env = {
|
||||||
|
...(this.serverParams.env || {}),
|
||||||
|
PATH: process.env.PATH
|
||||||
|
}
|
||||||
|
|
||||||
|
this.transport = new StdioClientTransport(this.serverParams as StdioServerParameters)
|
||||||
|
await this.client.connect(this.transport)
|
||||||
|
} else {
|
||||||
|
if (this.serverParams.url === undefined) {
|
||||||
|
throw new Error('URL is required for SSE transport')
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseUrl = new URL(this.serverParams.url)
|
||||||
|
try {
|
||||||
|
this.transport = new StreamableHTTPClientTransport(baseUrl)
|
||||||
|
await this.client.connect(this.transport)
|
||||||
|
} catch (error) {
|
||||||
|
this.transport = new SSEClientTransport(baseUrl)
|
||||||
|
await this.client.connect(this.transport)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
await this.client.connect(this.transport)
|
|
||||||
this._tools = await this.client.request({ method: 'tools/list' }, ListToolsResultSchema)
|
this._tools = await this.client.request({ method: 'tools/list' }, ListToolsResultSchema)
|
||||||
|
|
||||||
this.tools = await this.get_tools()
|
this.tools = await this.get_tools()
|
||||||
|
|||||||
@@ -60,7 +60,7 @@
|
|||||||
"@mem0/community": "^0.0.1",
|
"@mem0/community": "^0.0.1",
|
||||||
"@mendable/firecrawl-js": "^0.0.28",
|
"@mendable/firecrawl-js": "^0.0.28",
|
||||||
"@mistralai/mistralai": "0.1.3",
|
"@mistralai/mistralai": "0.1.3",
|
||||||
"@modelcontextprotocol/sdk": "^1.6.1",
|
"@modelcontextprotocol/sdk": "^1.10.1",
|
||||||
"@modelcontextprotocol/server-brave-search": "^0.6.2",
|
"@modelcontextprotocol/server-brave-search": "^0.6.2",
|
||||||
"@modelcontextprotocol/server-github": "^2025.1.23",
|
"@modelcontextprotocol/server-github": "^2025.1.23",
|
||||||
"@modelcontextprotocol/server-postgres": "^0.6.2",
|
"@modelcontextprotocol/server-postgres": "^0.6.2",
|
||||||
|
|||||||
Generated
+36698
-36663
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user