Skip to content
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
28 changes: 28 additions & 0 deletions plugin/controller/lib/impl/mcp/MCPConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface MCPConfigOptions {
sseMessagePath: string;
streamPath: string;
statelessStreamPath: string;
ssePingEnabled?: boolean;
streamPingEnabled?: boolean;
pingElapsed?: number;
pingInterval?: number;
sessionIdGenerator?: (ctx: Context) => string;
Expand All @@ -27,6 +29,8 @@ export class MCPConfig {

private _pingElapsed: number;
private _pingInterval: number;
private _ssePingEnabled: boolean;
private _streamPingEnabled: boolean;

private _multipleServer: Record<string, Partial<MCPConfigOptions>>;

Expand All @@ -41,6 +45,8 @@ export class MCPConfig {
this._sseHeartTime = options.sseHeartTime ?? 25000;
this._pingElapsed = options.pingElapsed ?? 10 * 60 * 1000;
this._pingInterval = options.pingInterval ?? 5 * 1000;
this._ssePingEnabled = options.ssePingEnabled ?? false;
this._streamPingEnabled = options.streamPingEnabled ?? false;

this._multipleServer = options.multipleServer ?? {};
}
Expand Down Expand Up @@ -148,6 +154,28 @@ export class MCPConfig {
return this._pingInterval;
}

getSsePingEnabled(name?: string) {
if (name) {
const config = this._multipleServer[name];
if (config?.ssePingEnabled !== undefined) {
return config.ssePingEnabled;
}
return false;
}
return this._ssePingEnabled;
}

getStreamPingEnabled(name?: string) {
if (name) {
const config = this._multipleServer[name];
if (config?.streamPingEnabled !== undefined) {
return config.streamPingEnabled;
}
return false;
}
return this._streamPingEnabled;
}

setMultipleServerPath(app: Application, name: string) {
if (!(app.config.mcp as MCPConfigOptions).multipleServer) {
(app.config.mcp as MCPConfigOptions).multipleServer = {};
Expand Down
24 changes: 16 additions & 8 deletions plugin/controller/lib/impl/mcp/MCPControllerRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ export class MCPControllerRegister implements ControllerRegister {
);
}
}
self.mcpServerPing(mcpServerHelper.server.server, sessionId, name);
if (self.mcpConfig.getStreamPingEnabled(name)) {
self.mcpServerPing(mcpServerHelper.server.server, sessionId, name);
}
},
});

Expand Down Expand Up @@ -496,7 +498,9 @@ export class MCPControllerRegister implements ControllerRegister {
}
await mcpServerHelper.server.connect(transport);
self.mcpServerMap[id] = mcpServerHelper.server;
self.mcpServerPing(mcpServerHelper.server.server, transport.sessionId, name);
if (self.mcpConfig.getSsePingEnabled(name)) {
self.mcpServerPing(mcpServerHelper.server.server, transport.sessionId, name);
}
return self.sseCtxStorageRun.bind(self)(ctx, transport, name);
};
Reflect.apply(routerFunc, this.router, [
Expand Down Expand Up @@ -665,12 +669,16 @@ export class MCPControllerRegister implements ControllerRegister {

const timerId = setInterval(async () => {
const elapsed = Date.now() - startTime;
await server.ping();

if (elapsed >= duration) {
if (this.pingIntervals[sessionId]) {
clearInterval(this.pingIntervals[sessionId]);
delete this.pingIntervals[sessionId];
try {
await server.ping();
} catch (e) {
this.app.logger.warn('mcp server ping failed: ', e);
} finally {
if (elapsed >= duration) {
if (this.pingIntervals[sessionId]) {
clearInterval(this.pingIntervals[sessionId]);
delete this.pingIntervals[sessionId];
}
}
}
}, interval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports = function(appInfo) {
const config = {
keys: 'test key',
mcp: {
ssePingEnabled: true,
streamPingEnabled: true,
sessionIdGenerator: ctx => {
return ctx.request.headers['custom-session-id'] || randomUUID();
},
Expand Down
Loading