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

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 -12
  2. package/package.json +1 -1
package/dist/server.js CHANGED
@@ -8,7 +8,6 @@ import { SSEConnectionManager } from './manager/sseConnectionManager.js';
8
8
  import { FeishuMcp } from './mcp/feishuMcp.js';
9
9
  import { callback } from './services/callbackService.js';
10
10
  import { UserAuthManager, UserContextManager, getBaseUrl, TokenCacheManager, TokenRefreshManager } from './utils/auth/index.js';
11
- import { Config } from './utils/config.js';
12
11
  export class FeishuMcpServer {
13
12
  constructor() {
14
13
  Object.defineProperty(this, "connectionManager", {
@@ -29,6 +28,12 @@ export class FeishuMcpServer {
29
28
  writable: true,
30
29
  value: void 0
31
30
  });
31
+ Object.defineProperty(this, "callbackServer", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: null
36
+ }); // stdio 模式下的 callback 服务器实例
32
37
  this.connectionManager = new SSEConnectionManager();
33
38
  this.userAuthManager = UserAuthManager.getInstance();
34
39
  this.userContextManager = UserContextManager.getInstance();
@@ -41,19 +46,41 @@ export class FeishuMcpServer {
41
46
  }
42
47
  async connect(transport) {
43
48
  const server = new FeishuMcp();
44
- // stdio模式只能本地运行,使用localhost + 配置的端口
45
- const config = Config.getInstance();
46
- const baseUrl = `http://localhost:${config.server.port}`;
47
- await this.userContextManager.run({
48
- userKey: 'stdio',
49
- baseUrl: baseUrl
50
- }, async () => {
51
- await server.connect(transport);
49
+ await server.connect(transport);
50
+ // 监听 transport 关闭事件,清理 callback 服务器
51
+ // 对于 stdio 模式,监听 stdin 关闭事件
52
+ if (process.stdin && typeof process.stdin.on === 'function') {
53
+ process.stdin.on('close', () => {
54
+ this.stopCallbackServer();
55
+ });
56
+ process.stdin.on('end', () => {
57
+ this.stopCallbackServer();
58
+ });
59
+ }
60
+ // 监听进程退出事件,确保清理资源
61
+ process.on('SIGINT', () => {
62
+ this.stopCallbackServer();
63
+ process.exit(0);
64
+ });
65
+ process.on('SIGTERM', () => {
66
+ this.stopCallbackServer();
67
+ process.exit(0);
52
68
  });
53
69
  // 注意:在 stdio 模式下,Logger 会自动禁用输出,避免污染 MCP 协议
54
70
  // 如果需要日志,可以通过 MCP 协议的 logging 消息传递
55
71
  Logger.info('Server connected and ready to process requests');
56
72
  }
73
+ /**
74
+ * 停止 callback 服务器
75
+ */
76
+ stopCallbackServer() {
77
+ if (this.callbackServer) {
78
+ this.callbackServer.close(() => {
79
+ Logger.info('Callback server stopped');
80
+ });
81
+ this.callbackServer = null;
82
+ }
83
+ }
57
84
  async startHttpServer(port) {
58
85
  const app = express();
59
86
  const transports = {};
@@ -242,9 +269,24 @@ export class FeishuMcpServer {
242
269
  const app = express();
243
270
  // 只注册callback接口
244
271
  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`);
272
+ return new Promise((resolve, reject) => {
273
+ const server = app.listen(port, '0.0.0.0', () => {
274
+ this.callbackServer = server;
275
+ Logger.info(`Callback server listening on port ${port}`);
276
+ Logger.info(`Callback endpoint available at http://localhost:${port}/callback`);
277
+ resolve();
278
+ });
279
+ server.on('error', (err) => {
280
+ if (err.code === 'EADDRINUSE') {
281
+ // 端口被占用,说明其他进程已经启动了 callback 服务器
282
+ // 这是正常的,静默处理即可(多个 stdio 进程共享同一个 callback 服务器)
283
+ Logger.debug(`Port ${port} is already in use, callback server may already be running`);
284
+ resolve(); // 不抛出错误,因为 callback 服务器已经存在
285
+ }
286
+ else {
287
+ reject(err);
288
+ }
289
+ });
248
290
  });
249
291
  }
250
292
  }
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.4",
4
4
  "description": "Model Context Protocol server for Feishu integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",