@agiflowai/one-mcp 0.3.16 → 0.3.17

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.
package/dist/cli.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- const require_src = require('./src-BRqEdbha.cjs');
2
+ const require_src = require('./src-B8Rb84GQ.cjs');
3
3
  let node_fs_promises = require("node:fs/promises");
4
4
  let node_fs = require("node:fs");
5
5
  let node_crypto = require("node:crypto");
package/dist/cli.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { _ as DefinitionsCacheService, a as SseTransportHandler, b as ConfigFetcherService, c as createServer, d as version, g as McpClientManagerService, h as SkillService, i as StdioTransportHandler, l as createSessionServer, n as RuntimeStateService, o as HttpTransportHandler, p as SearchListToolsTool, r as StdioHttpTransportHandler, s as TRANSPORT_MODE, t as StopServerService, u as initializeSharedServices, v as generateServerId, y as findConfigFile } from "./src-Dn6vMZIk.mjs";
2
+ import { _ as DefinitionsCacheService, a as SseTransportHandler, b as ConfigFetcherService, c as createServer, d as version, g as McpClientManagerService, h as SkillService, i as StdioTransportHandler, l as createSessionServer, n as RuntimeStateService, o as HttpTransportHandler, p as SearchListToolsTool, r as StdioHttpTransportHandler, s as TRANSPORT_MODE, t as StopServerService, u as initializeSharedServices, v as generateServerId, y as findConfigFile } from "./src-D5YMjGgs.mjs";
3
3
  import { access, writeFile } from "node:fs/promises";
4
4
  import { constants } from "node:fs";
5
5
  import { randomUUID } from "node:crypto";
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_src = require('./src-BRqEdbha.cjs');
1
+ const require_src = require('./src-B8Rb84GQ.cjs');
2
2
 
3
3
  exports.ConfigFetcherService = require_src.ConfigFetcherService;
4
4
  exports.DefinitionsCacheService = require_src.DefinitionsCacheService;
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { _ as DefinitionsCacheService, a as SseTransportHandler, b as ConfigFetcherService, c as createServer, f as UseToolTool, g as McpClientManagerService, h as SkillService, i as StdioTransportHandler, l as createSessionServer, m as DescribeToolsTool, n as RuntimeStateService, o as HttpTransportHandler, p as SearchListToolsTool, r as StdioHttpTransportHandler, s as TRANSPORT_MODE, t as StopServerService, u as initializeSharedServices, v as generateServerId, y as findConfigFile } from "./src-Dn6vMZIk.mjs";
1
+ import { _ as DefinitionsCacheService, a as SseTransportHandler, b as ConfigFetcherService, c as createServer, f as UseToolTool, g as McpClientManagerService, h as SkillService, i as StdioTransportHandler, l as createSessionServer, m as DescribeToolsTool, n as RuntimeStateService, o as HttpTransportHandler, p as SearchListToolsTool, r as StdioHttpTransportHandler, s as TRANSPORT_MODE, t as StopServerService, u as initializeSharedServices, v as generateServerId, y as findConfigFile } from "./src-D5YMjGgs.mjs";
2
2
 
3
3
  export { ConfigFetcherService, DefinitionsCacheService, DescribeToolsTool, HttpTransportHandler, McpClientManagerService, RuntimeStateService, SearchListToolsTool, SkillService, SseTransportHandler, StdioHttpTransportHandler, StdioTransportHandler, StopServerService, TRANSPORT_MODE, UseToolTool, createServer, createSessionServer, findConfigFile, generateServerId, initializeSharedServices };
@@ -1453,6 +1453,14 @@ var DefinitionsCacheService = class {
1453
1453
  /** Default connection timeout in milliseconds (30 seconds) */
1454
1454
  const DEFAULT_CONNECTION_TIMEOUT_MS = 3e4;
1455
1455
  /**
1456
+ * Checks if an error is a session-related error from an HTTP backend
1457
+ * (e.g., downstream server restarted and no longer recognizes the session ID).
1458
+ */
1459
+ function isSessionError(error) {
1460
+ const message = error instanceof Error ? error.message : String(error);
1461
+ return message.includes("unknown session") || message.includes("Session not found");
1462
+ }
1463
+ /**
1456
1464
  * MCP Client wrapper for managing individual server connections
1457
1465
  * This is an internal class used by McpClientManagerService
1458
1466
  */
@@ -1466,6 +1474,7 @@ var McpClient = class {
1466
1474
  client;
1467
1475
  childProcess;
1468
1476
  connected = false;
1477
+ reconnectFn;
1469
1478
  constructor(serverName, transport, client, config) {
1470
1479
  this.serverName = serverName;
1471
1480
  this.serverInstruction = config.instruction;
@@ -1481,34 +1490,76 @@ var McpClient = class {
1481
1490
  setConnected(connected) {
1482
1491
  this.connected = connected;
1483
1492
  }
1493
+ /**
1494
+ * Sets a reconnection function that creates a fresh Client and transport.
1495
+ * Called automatically by withSessionRetry when a session error is detected
1496
+ * (e.g., downstream HTTP server restarted and the old session ID is invalid).
1497
+ */
1498
+ setReconnectFn(fn) {
1499
+ this.reconnectFn = fn;
1500
+ }
1501
+ /**
1502
+ * Wraps an operation with automatic retry on session errors.
1503
+ * If the operation fails with a session error (e.g., downstream server restarted),
1504
+ * reconnects and retries once.
1505
+ */
1506
+ async withSessionRetry(operation) {
1507
+ try {
1508
+ return await operation();
1509
+ } catch (error) {
1510
+ if (!this.reconnectFn || !isSessionError(error)) throw error;
1511
+ console.error(`Session error for ${this.serverName}, reconnecting: ${error instanceof Error ? error.message : String(error)}`);
1512
+ try {
1513
+ await this.client.close();
1514
+ } catch (closeError) {
1515
+ console.error(`Failed to close stale client for ${this.serverName}:`, closeError);
1516
+ }
1517
+ const result = await this.reconnectFn();
1518
+ this.client = result.client;
1519
+ if (result.childProcess) this.childProcess = result.childProcess;
1520
+ return await operation();
1521
+ }
1522
+ }
1484
1523
  async listTools() {
1485
1524
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1486
- return (await this.client.listTools()).tools;
1525
+ return this.withSessionRetry(async () => {
1526
+ return (await this.client.listTools()).tools;
1527
+ });
1487
1528
  }
1488
1529
  async listResources() {
1489
1530
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1490
- return (await this.client.listResources()).resources;
1531
+ return this.withSessionRetry(async () => {
1532
+ return (await this.client.listResources()).resources;
1533
+ });
1491
1534
  }
1492
1535
  async listPrompts() {
1493
1536
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1494
- return (await this.client.listPrompts()).prompts;
1537
+ return this.withSessionRetry(async () => {
1538
+ return (await this.client.listPrompts()).prompts;
1539
+ });
1495
1540
  }
1496
1541
  async callTool(name, args) {
1497
1542
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1498
- return await this.client.callTool({
1499
- name,
1500
- arguments: args
1543
+ return this.withSessionRetry(async () => {
1544
+ return await this.client.callTool({
1545
+ name,
1546
+ arguments: args
1547
+ });
1501
1548
  });
1502
1549
  }
1503
1550
  async readResource(uri) {
1504
1551
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1505
- return await this.client.readResource({ uri });
1552
+ return this.withSessionRetry(async () => {
1553
+ return await this.client.readResource({ uri });
1554
+ });
1506
1555
  }
1507
1556
  async getPrompt(name, args) {
1508
1557
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1509
- return await this.client.getPrompt({
1510
- name,
1511
- arguments: args
1558
+ return this.withSessionRetry(async () => {
1559
+ return await this.client.getPrompt({
1560
+ name,
1561
+ arguments: args
1562
+ });
1512
1563
  });
1513
1564
  }
1514
1565
  async close() {
@@ -1589,6 +1640,20 @@ var McpClientManagerService = class {
1589
1640
  try {
1590
1641
  await Promise.race([this.performConnection(mcpClient, config), new Promise((_, reject) => setTimeout(() => reject(/* @__PURE__ */ new Error(`Connection timeout after ${timeoutMs}ms`)), timeoutMs))]);
1591
1642
  mcpClient.setConnected(true);
1643
+ if (config.transport === "http" || config.transport === "sse") mcpClient.setReconnectFn(async () => {
1644
+ try {
1645
+ const newClient = new __modelcontextprotocol_sdk_client_index_js.Client({
1646
+ name: "@agiflowai/one-mcp-client",
1647
+ version: "0.1.0"
1648
+ }, { capabilities: {} });
1649
+ const newMcpClient = new McpClient(serverName, config.transport, newClient, {});
1650
+ await this.performConnection(newMcpClient, config);
1651
+ return { client: newClient };
1652
+ } catch (error) {
1653
+ console.error(`Failed to reconnect to ${serverName}:`, error);
1654
+ throw error;
1655
+ }
1656
+ });
1592
1657
  if (!mcpClient.serverInstruction) try {
1593
1658
  const serverInstruction = mcpClient["client"].getInstructions();
1594
1659
  if (serverInstruction) mcpClient.serverInstruction = serverInstruction;
@@ -2796,7 +2861,7 @@ IMPORTANT: Only use tools discovered from describe_tools with id="${this.serverI
2796
2861
 
2797
2862
  //#endregion
2798
2863
  //#region package.json
2799
- var version = "0.3.15";
2864
+ var version = "0.3.16";
2800
2865
 
2801
2866
  //#endregion
2802
2867
  //#region src/server/index.ts
@@ -1424,6 +1424,14 @@ var DefinitionsCacheService = class {
1424
1424
  /** Default connection timeout in milliseconds (30 seconds) */
1425
1425
  const DEFAULT_CONNECTION_TIMEOUT_MS = 3e4;
1426
1426
  /**
1427
+ * Checks if an error is a session-related error from an HTTP backend
1428
+ * (e.g., downstream server restarted and no longer recognizes the session ID).
1429
+ */
1430
+ function isSessionError(error) {
1431
+ const message = error instanceof Error ? error.message : String(error);
1432
+ return message.includes("unknown session") || message.includes("Session not found");
1433
+ }
1434
+ /**
1427
1435
  * MCP Client wrapper for managing individual server connections
1428
1436
  * This is an internal class used by McpClientManagerService
1429
1437
  */
@@ -1437,6 +1445,7 @@ var McpClient = class {
1437
1445
  client;
1438
1446
  childProcess;
1439
1447
  connected = false;
1448
+ reconnectFn;
1440
1449
  constructor(serverName, transport, client, config) {
1441
1450
  this.serverName = serverName;
1442
1451
  this.serverInstruction = config.instruction;
@@ -1452,34 +1461,76 @@ var McpClient = class {
1452
1461
  setConnected(connected) {
1453
1462
  this.connected = connected;
1454
1463
  }
1464
+ /**
1465
+ * Sets a reconnection function that creates a fresh Client and transport.
1466
+ * Called automatically by withSessionRetry when a session error is detected
1467
+ * (e.g., downstream HTTP server restarted and the old session ID is invalid).
1468
+ */
1469
+ setReconnectFn(fn) {
1470
+ this.reconnectFn = fn;
1471
+ }
1472
+ /**
1473
+ * Wraps an operation with automatic retry on session errors.
1474
+ * If the operation fails with a session error (e.g., downstream server restarted),
1475
+ * reconnects and retries once.
1476
+ */
1477
+ async withSessionRetry(operation) {
1478
+ try {
1479
+ return await operation();
1480
+ } catch (error) {
1481
+ if (!this.reconnectFn || !isSessionError(error)) throw error;
1482
+ console.error(`Session error for ${this.serverName}, reconnecting: ${error instanceof Error ? error.message : String(error)}`);
1483
+ try {
1484
+ await this.client.close();
1485
+ } catch (closeError) {
1486
+ console.error(`Failed to close stale client for ${this.serverName}:`, closeError);
1487
+ }
1488
+ const result = await this.reconnectFn();
1489
+ this.client = result.client;
1490
+ if (result.childProcess) this.childProcess = result.childProcess;
1491
+ return await operation();
1492
+ }
1493
+ }
1455
1494
  async listTools() {
1456
1495
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1457
- return (await this.client.listTools()).tools;
1496
+ return this.withSessionRetry(async () => {
1497
+ return (await this.client.listTools()).tools;
1498
+ });
1458
1499
  }
1459
1500
  async listResources() {
1460
1501
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1461
- return (await this.client.listResources()).resources;
1502
+ return this.withSessionRetry(async () => {
1503
+ return (await this.client.listResources()).resources;
1504
+ });
1462
1505
  }
1463
1506
  async listPrompts() {
1464
1507
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1465
- return (await this.client.listPrompts()).prompts;
1508
+ return this.withSessionRetry(async () => {
1509
+ return (await this.client.listPrompts()).prompts;
1510
+ });
1466
1511
  }
1467
1512
  async callTool(name, args) {
1468
1513
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1469
- return await this.client.callTool({
1470
- name,
1471
- arguments: args
1514
+ return this.withSessionRetry(async () => {
1515
+ return await this.client.callTool({
1516
+ name,
1517
+ arguments: args
1518
+ });
1472
1519
  });
1473
1520
  }
1474
1521
  async readResource(uri) {
1475
1522
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1476
- return await this.client.readResource({ uri });
1523
+ return this.withSessionRetry(async () => {
1524
+ return await this.client.readResource({ uri });
1525
+ });
1477
1526
  }
1478
1527
  async getPrompt(name, args) {
1479
1528
  if (!this.connected) throw new Error(`Client for ${this.serverName} is not connected`);
1480
- return await this.client.getPrompt({
1481
- name,
1482
- arguments: args
1529
+ return this.withSessionRetry(async () => {
1530
+ return await this.client.getPrompt({
1531
+ name,
1532
+ arguments: args
1533
+ });
1483
1534
  });
1484
1535
  }
1485
1536
  async close() {
@@ -1560,6 +1611,20 @@ var McpClientManagerService = class {
1560
1611
  try {
1561
1612
  await Promise.race([this.performConnection(mcpClient, config), new Promise((_, reject) => setTimeout(() => reject(/* @__PURE__ */ new Error(`Connection timeout after ${timeoutMs}ms`)), timeoutMs))]);
1562
1613
  mcpClient.setConnected(true);
1614
+ if (config.transport === "http" || config.transport === "sse") mcpClient.setReconnectFn(async () => {
1615
+ try {
1616
+ const newClient = new Client({
1617
+ name: "@agiflowai/one-mcp-client",
1618
+ version: "0.1.0"
1619
+ }, { capabilities: {} });
1620
+ const newMcpClient = new McpClient(serverName, config.transport, newClient, {});
1621
+ await this.performConnection(newMcpClient, config);
1622
+ return { client: newClient };
1623
+ } catch (error) {
1624
+ console.error(`Failed to reconnect to ${serverName}:`, error);
1625
+ throw error;
1626
+ }
1627
+ });
1563
1628
  if (!mcpClient.serverInstruction) try {
1564
1629
  const serverInstruction = mcpClient["client"].getInstructions();
1565
1630
  if (serverInstruction) mcpClient.serverInstruction = serverInstruction;
@@ -2767,7 +2832,7 @@ IMPORTANT: Only use tools discovered from describe_tools with id="${this.serverI
2767
2832
 
2768
2833
  //#endregion
2769
2834
  //#region package.json
2770
- var version = "0.3.15";
2835
+ var version = "0.3.16";
2771
2836
 
2772
2837
  //#endregion
2773
2838
  //#region src/server/index.ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agiflowai/one-mcp",
3
3
  "description": "One MCP server package",
4
- "version": "0.3.16",
4
+ "version": "0.3.17",
5
5
  "license": "AGPL-3.0",
6
6
  "keywords": [
7
7
  "mcp",