feishu-mcp 0.1.9-test.2 → 0.1.9-test.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/server.js +54 -3
  2. package/package.json +1 -1
package/dist/server.js CHANGED
@@ -29,6 +29,12 @@ export class FeishuMcpServer {
29
29
  writable: true,
30
30
  value: void 0
31
31
  });
32
+ Object.defineProperty(this, "callbackServer", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: null
37
+ }); // stdio 模式下的 callback 服务器实例
32
38
  this.connectionManager = new SSEConnectionManager();
33
39
  this.userAuthManager = UserAuthManager.getInstance();
34
40
  this.userContextManager = UserContextManager.getInstance();
@@ -50,10 +56,40 @@ export class FeishuMcpServer {
50
56
  }, async () => {
51
57
  await server.connect(transport);
52
58
  });
59
+ // 监听 transport 关闭事件,清理 callback 服务器
60
+ // 对于 stdio 模式,监听 stdin 关闭事件
61
+ if (process.stdin && typeof process.stdin.on === 'function') {
62
+ process.stdin.on('close', () => {
63
+ this.stopCallbackServer();
64
+ });
65
+ process.stdin.on('end', () => {
66
+ this.stopCallbackServer();
67
+ });
68
+ }
69
+ // 监听进程退出事件,确保清理资源
70
+ process.on('SIGINT', () => {
71
+ this.stopCallbackServer();
72
+ process.exit(0);
73
+ });
74
+ process.on('SIGTERM', () => {
75
+ this.stopCallbackServer();
76
+ process.exit(0);
77
+ });
53
78
  // 注意:在 stdio 模式下,Logger 会自动禁用输出,避免污染 MCP 协议
54
79
  // 如果需要日志,可以通过 MCP 协议的 logging 消息传递
55
80
  Logger.info('Server connected and ready to process requests');
56
81
  }
82
+ /**
83
+ * 停止 callback 服务器
84
+ */
85
+ stopCallbackServer() {
86
+ if (this.callbackServer) {
87
+ this.callbackServer.close(() => {
88
+ Logger.info('Callback server stopped');
89
+ });
90
+ this.callbackServer = null;
91
+ }
92
+ }
57
93
  async startHttpServer(port) {
58
94
  const app = express();
59
95
  const transports = {};
@@ -242,9 +278,24 @@ export class FeishuMcpServer {
242
278
  const app = express();
243
279
  // 只注册callback接口
244
280
  app.get('/callback', callback);
245
- app.listen(port, '0.0.0.0', () => {
246
- Logger.info(`Callback server listening on port ${port}`);
247
- Logger.info(`Callback endpoint available at http://localhost:${port}/callback`);
281
+ return new Promise((resolve, reject) => {
282
+ const server = app.listen(port, '0.0.0.0', () => {
283
+ this.callbackServer = server;
284
+ Logger.info(`Callback server listening on port ${port}`);
285
+ Logger.info(`Callback endpoint available at http://localhost:${port}/callback`);
286
+ resolve();
287
+ });
288
+ server.on('error', (err) => {
289
+ if (err.code === 'EADDRINUSE') {
290
+ // 端口被占用,说明其他进程已经启动了 callback 服务器
291
+ // 这是正常的,静默处理即可(多个 stdio 进程共享同一个 callback 服务器)
292
+ Logger.debug(`Port ${port} is already in use, callback server may already be running`);
293
+ resolve(); // 不抛出错误,因为 callback 服务器已经存在
294
+ }
295
+ else {
296
+ reject(err);
297
+ }
298
+ });
248
299
  });
249
300
  }
250
301
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feishu-mcp",
3
- "version": "0.1.9-test.2",
3
+ "version": "0.1.9-test.3",
4
4
  "description": "Model Context Protocol server for Feishu integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",