Skip to content

Commit 40ff84c

Browse files
committed
fix mcp restart
1 parent 4eba4b5 commit 40ff84c

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/services/mcp/connection/ConnectionFactory.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export class ConnectionFactory {
102102
this.setupFileWatcher(connection, patchedConfig)
103103
}
104104

105+
// Remove any existing object with the same name and source to avoid duplicates
106+
this.connections = this.connections.filter(
107+
(conn) => !(conn.server.name === name && conn.server.source === source),
108+
)
105109
this.connections.push(connection)
106110
return connection
107111
}
@@ -111,7 +115,7 @@ export class ConnectionFactory {
111115
* @param name Server name
112116
* @param source Optional config source
113117
*/
114-
async closeConnection(name: string, source?: ConfigSource): Promise<void> {
118+
async closeConnection(name: string, source?: ConfigSource, allowKeep?: boolean): Promise<void> {
115119
// Find and close connections
116120
const connections = source ? this.findConnections(name, source) : this.findConnections(name)
117121

@@ -126,12 +130,14 @@ export class ConnectionFactory {
126130
}
127131
}
128132

129-
// Remove from array
130-
this.connections = this.connections.filter((conn) => {
131-
if (conn.server.name !== name) return true
132-
if (source && conn.server.source !== source) return true
133-
return false
134-
})
133+
// Remove from array unless allowKeep is true
134+
if (!allowKeep) {
135+
this.connections = this.connections.filter((conn) => {
136+
if (conn.server.name !== name) return true
137+
if (source && conn.server.source !== source) return true
138+
return false
139+
})
140+
}
135141
}
136142

137143
/**
@@ -180,11 +186,20 @@ export class ConnectionFactory {
180186
}
181187

182188
for (const conn of connections) {
189+
// Set status to connecting
190+
conn.server.status = "connecting"
191+
conn.server.error = ""
192+
193+
// Notify status change if callback exists
194+
if (this.onStatusChange) {
195+
this.onStatusChange(conn.server)
196+
}
197+
183198
const config = JSON.parse(conn.server.config)
184199
const connSource = conn.server.source || "global"
185200

186-
// Close existing connection
187-
await this.closeConnection(name, connSource)
201+
// Close existing connection but do not remove the object, so notifyServersChanged can find "connecting"
202+
await this.closeConnection(name, connSource, true)
188203

189204
// Create new connection
190205
await this.createConnection(name, config, connSource)

src/services/mcp/connection/ConnectionManager.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class ConnectionManager {
126126

127127
const stripNonConnectionFields = (configObj: any) => {
128128
// Exclude alwaysAllow and timeout, timeout changes do not trigger reconnection
129-
const { alwaysAllow: _alwaysAllow, timeout: _timeout, ...rest } = configObj
129+
const { alwaysAllow: _alwaysAllow, timeout: _timeout, disabled: _disabled, ...rest } = configObj
130130
return rest
131131
}
132132

@@ -136,11 +136,7 @@ export class ConnectionManager {
136136
// Use deep comparison from fast-deep-equal instead of JSON.stringify
137137
if (!deepEqual(strippedCurrent, strippedValidated)) {
138138
await this.factory.closeConnection(serverName, source)
139-
140-
// If server is not disabled, create new connection
141-
if (!validatedConfig.disabled) {
142-
await this.factory.createConnection(serverName, validatedConfig, source)
143-
}
139+
await this.factory.createConnection(serverName, validatedConfig, source)
144140
} else {
145141
// No connection parameter change, but dynamic parameters like timeout may change, need to sync config field
146142
// Ensure callTool always reads the latest config
@@ -151,7 +147,7 @@ export class ConnectionManager {
151147
}
152148
}
153149
}
154-
} else if (!validatedConfig.disabled) {
150+
} else {
155151
// Create new connection
156152
await this.factory.createConnection(serverName, validatedConfig, source)
157153
}

0 commit comments

Comments
 (0)