Skip to content

Commit c2757fc

Browse files
authored
fix: add ping config (#364)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. --> <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added configurable ping controls for SSE and stream server connections, allowing per-server and global-level configuration. * **Bug Fixes** * Improved error handling for server ping operations to prevent connection disruptions and ensure proper cleanup. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 3e944e8 commit c2757fc

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

plugin/controller/lib/impl/mcp/MCPConfig.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface MCPConfigOptions {
88
sseMessagePath: string;
99
streamPath: string;
1010
statelessStreamPath: string;
11+
ssePingEnabled?: boolean;
12+
streamPingEnabled?: boolean;
1113
pingElapsed?: number;
1214
pingInterval?: number;
1315
sessionIdGenerator?: (ctx: Context) => string;
@@ -27,6 +29,8 @@ export class MCPConfig {
2729

2830
private _pingElapsed: number;
2931
private _pingInterval: number;
32+
private _ssePingEnabled: boolean;
33+
private _streamPingEnabled: boolean;
3034

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

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

4551
this._multipleServer = options.multipleServer ?? {};
4652
}
@@ -148,6 +154,28 @@ export class MCPConfig {
148154
return this._pingInterval;
149155
}
150156

157+
getSsePingEnabled(name?: string) {
158+
if (name) {
159+
const config = this._multipleServer[name];
160+
if (config?.ssePingEnabled !== undefined) {
161+
return config.ssePingEnabled;
162+
}
163+
return false;
164+
}
165+
return this._ssePingEnabled;
166+
}
167+
168+
getStreamPingEnabled(name?: string) {
169+
if (name) {
170+
const config = this._multipleServer[name];
171+
if (config?.streamPingEnabled !== undefined) {
172+
return config.streamPingEnabled;
173+
}
174+
return false;
175+
}
176+
return this._streamPingEnabled;
177+
}
178+
151179
setMultipleServerPath(app: Application, name: string) {
152180
if (!(app.config.mcp as MCPConfigOptions).multipleServer) {
153181
(app.config.mcp as MCPConfigOptions).multipleServer = {};

plugin/controller/lib/impl/mcp/MCPControllerRegister.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,9 @@ export class MCPControllerRegister implements ControllerRegister {
344344
);
345345
}
346346
}
347-
self.mcpServerPing(mcpServerHelper.server.server, sessionId, name);
347+
if (self.mcpConfig.getStreamPingEnabled(name)) {
348+
self.mcpServerPing(mcpServerHelper.server.server, sessionId, name);
349+
}
348350
},
349351
});
350352

@@ -496,7 +498,9 @@ export class MCPControllerRegister implements ControllerRegister {
496498
}
497499
await mcpServerHelper.server.connect(transport);
498500
self.mcpServerMap[id] = mcpServerHelper.server;
499-
self.mcpServerPing(mcpServerHelper.server.server, transport.sessionId, name);
501+
if (self.mcpConfig.getSsePingEnabled(name)) {
502+
self.mcpServerPing(mcpServerHelper.server.server, transport.sessionId, name);
503+
}
500504
return self.sseCtxStorageRun.bind(self)(ctx, transport, name);
501505
};
502506
Reflect.apply(routerFunc, this.router, [
@@ -665,12 +669,16 @@ export class MCPControllerRegister implements ControllerRegister {
665669

666670
const timerId = setInterval(async () => {
667671
const elapsed = Date.now() - startTime;
668-
await server.ping();
669-
670-
if (elapsed >= duration) {
671-
if (this.pingIntervals[sessionId]) {
672-
clearInterval(this.pingIntervals[sessionId]);
673-
delete this.pingIntervals[sessionId];
672+
try {
673+
await server.ping();
674+
} catch (e) {
675+
this.app.logger.warn('mcp server ping failed: ', e);
676+
} finally {
677+
if (elapsed >= duration) {
678+
if (this.pingIntervals[sessionId]) {
679+
clearInterval(this.pingIntervals[sessionId]);
680+
delete this.pingIntervals[sessionId];
681+
}
674682
}
675683
}
676684
}, interval);

plugin/controller/test/fixtures/apps/mcp-app/config/config.default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module.exports = function(appInfo) {
88
const config = {
99
keys: 'test key',
1010
mcp: {
11+
ssePingEnabled: true,
12+
streamPingEnabled: true,
1113
sessionIdGenerator: ctx => {
1214
return ctx.request.headers['custom-session-id'] || randomUUID();
1315
},

0 commit comments

Comments
 (0)