Skip to content

Commit 5151dd9

Browse files
author
guwenjia
committed
Upgrade MCP version and support SSE mode.
1 parent ddba891 commit 5151dd9

File tree

4 files changed

+36533
-36470
lines changed

4 files changed

+36533
-36470
lines changed

packages/components/nodes/tools/MCP/CustomMCP/CustomMCP.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ class Custom_MCP implements INode {
104104
serverParams = JSON.parse(serverParamsString)
105105
}
106106

107-
const toolkit = new MCPToolkit(serverParams, 'stdio')
107+
// Compatible with stdio and SSE
108+
let toolkit: MCPToolkit
109+
if (serverParams?.command === undefined) {
110+
toolkit = new MCPToolkit(serverParams, 'sse')
111+
} else {
112+
toolkit = new MCPToolkit(serverParams, 'stdio')
113+
}
114+
108115
await toolkit.initialize()
109116

110117
const tools = toolkit.tools ?? []

packages/components/nodes/tools/MCP/core.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33
import { StdioClientTransport, StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js'
44
import { BaseToolkit, tool, Tool } from '@langchain/core/tools'
55
import { z } from 'zod'
6+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
7+
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
68

79
export class MCPToolkit extends BaseToolkit {
810
tools: Tool[] = []
911
_tools: ListToolsResult | null = null
1012
model_config: any
11-
transport: StdioClientTransport | null = null
13+
transport: StdioClientTransport | SSEClientTransport | StreamableHTTPClientTransport | null = null
1214
client: Client | null = null
13-
constructor(serverParams: StdioServerParameters | any, transport: 'stdio' | 'sse') {
15+
serverParams: StdioServerParameters | any
16+
transportType: 'stdio' | 'sse'
17+
constructor(serverParams: StdioServerParameters | any, transportType: 'stdio' | 'sse') {
1418
super()
15-
if (transport === 'stdio') {
16-
this.transport = new StdioClientTransport(serverParams as StdioServerParameters)
17-
} else {
18-
// TODO: this.transport = new SSEClientTransport(serverParams.url);
19-
}
19+
this.serverParams = serverParams
20+
this.transportType = transportType
2021
}
2122
async initialize() {
2223
if (this._tools === null) {
@@ -29,10 +30,30 @@ export class MCPToolkit extends BaseToolkit {
2930
capabilities: {}
3031
}
3132
)
32-
if (this.transport === null) {
33-
throw new Error('Transport is not initialized')
33+
if (this.transportType === 'stdio') {
34+
// Compatible with overridden PATH configuration
35+
this.serverParams.env = {
36+
...(this.serverParams.env || {}),
37+
PATH: process.env.PATH
38+
}
39+
40+
this.transport = new StdioClientTransport(this.serverParams as StdioServerParameters)
41+
await this.client.connect(this.transport)
42+
} else {
43+
if (this.serverParams.url === undefined) {
44+
throw new Error('URL is required for SSE transport')
45+
}
46+
47+
const baseUrl = new URL(this.serverParams.url)
48+
try {
49+
this.transport = new StreamableHTTPClientTransport(baseUrl)
50+
await this.client.connect(this.transport)
51+
} catch (error) {
52+
this.transport = new SSEClientTransport(baseUrl)
53+
await this.client.connect(this.transport)
54+
}
3455
}
35-
await this.client.connect(this.transport)
56+
3657
this._tools = await this.client.request({ method: 'tools/list' }, ListToolsResultSchema)
3758

3859
this.tools = await this.get_tools()

packages/components/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@mem0/community": "^0.0.1",
6161
"@mendable/firecrawl-js": "^0.0.28",
6262
"@mistralai/mistralai": "0.1.3",
63-
"@modelcontextprotocol/sdk": "^1.6.1",
63+
"@modelcontextprotocol/sdk": "^1.10.1",
6464
"@modelcontextprotocol/server-brave-search": "^0.6.2",
6565
"@modelcontextprotocol/server-github": "^2025.1.23",
6666
"@modelcontextprotocol/server-postgres": "^0.6.2",

0 commit comments

Comments
 (0)