@nest-mcp/client 0.2.11 → 0.3.1

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.
@@ -1,8 +1,39 @@
1
+ import type { McpClientConnection } from './interfaces/client-options.interface';
1
2
  import { McpClient } from './mcp-client.service';
2
3
  export declare class McpClientsService {
3
4
  private readonly clients;
5
+ private readonly logger;
6
+ private readonly pending;
4
7
  constructor(clients: McpClient[]);
5
8
  getClient(name: string): McpClient;
6
9
  getClients(): McpClient[];
10
+ /** Whether a client is currently registered under `name` (regardless of connection state). */
11
+ has(name: string): boolean;
12
+ /**
13
+ * Idempotently ensure a connected client for `connection.name`, creating and connecting one if
14
+ * absent. This is the runtime counterpart to the static `connections` passed to
15
+ * `McpClientModule.forRoot` — use it when upstreams are discovered at runtime (e.g. per-tenant
16
+ * MCP servers registered by users).
17
+ *
18
+ * - If a client with that name exists and is connected, it is returned unchanged.
19
+ * - If a client with that name exists but is disconnected, it is dropped and replaced with a
20
+ * fresh connection.
21
+ * - Concurrent calls for the same name share a single connect.
22
+ *
23
+ * The new client is registered in the same collection as the static ones, so it is returned by
24
+ * `getClient`/`getClients` and torn down on application shutdown. A client that fails to connect
25
+ * is NOT registered (the rejection propagates).
26
+ *
27
+ * @throws the underlying connect error if the upstream cannot be reached.
28
+ */
29
+ addConnection(connection: McpClientConnection): Promise<McpClient>;
30
+ /** Alias for {@link addConnection}: return the connected client for `connection`, creating it if needed. */
31
+ getOrCreate(connection: McpClientConnection): Promise<McpClient>;
32
+ /**
33
+ * Disconnect and unregister every client registered under `name` (idempotent — a no-op if none
34
+ * exist). Mutates the registered collection in place so existing `getClients()` references stay
35
+ * valid.
36
+ */
37
+ removeConnection(name: string): Promise<void>;
7
38
  }
8
39
  //# sourceMappingURL=mcp-clients.service.d.ts.map
@@ -14,8 +14,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.McpClientsService = void 0;
16
16
  const common_1 = require("@nestjs/common");
17
+ const mcp_client_service_1 = require("./mcp-client.service");
17
18
  let McpClientsService = class McpClientsService {
18
19
  clients;
20
+ logger = new common_1.Logger('McpClientsService');
21
+ // In-flight connects keyed by connection name, so concurrent addConnection/getOrCreate calls
22
+ // for the same name share a single connect instead of racing to create duplicate clients.
23
+ pending = new Map();
19
24
  constructor(clients) {
20
25
  this.clients = clients;
21
26
  }
@@ -29,6 +34,66 @@ let McpClientsService = class McpClientsService {
29
34
  getClients() {
30
35
  return this.clients;
31
36
  }
37
+ /** Whether a client is currently registered under `name` (regardless of connection state). */
38
+ has(name) {
39
+ return this.clients.some((c) => c.name === name);
40
+ }
41
+ /**
42
+ * Idempotently ensure a connected client for `connection.name`, creating and connecting one if
43
+ * absent. This is the runtime counterpart to the static `connections` passed to
44
+ * `McpClientModule.forRoot` — use it when upstreams are discovered at runtime (e.g. per-tenant
45
+ * MCP servers registered by users).
46
+ *
47
+ * - If a client with that name exists and is connected, it is returned unchanged.
48
+ * - If a client with that name exists but is disconnected, it is dropped and replaced with a
49
+ * fresh connection.
50
+ * - Concurrent calls for the same name share a single connect.
51
+ *
52
+ * The new client is registered in the same collection as the static ones, so it is returned by
53
+ * `getClient`/`getClients` and torn down on application shutdown. A client that fails to connect
54
+ * is NOT registered (the rejection propagates).
55
+ *
56
+ * @throws the underlying connect error if the upstream cannot be reached.
57
+ */
58
+ async addConnection(connection) {
59
+ const existing = this.clients.find((c) => c.name === connection.name);
60
+ if (existing?.isConnected())
61
+ return existing;
62
+ const inFlight = this.pending.get(connection.name);
63
+ if (inFlight)
64
+ return inFlight;
65
+ const promise = (async () => {
66
+ // Replace a stale (disconnected) client with the same name before reconnecting fresh.
67
+ if (existing)
68
+ await this.removeConnection(connection.name);
69
+ const client = new mcp_client_service_1.McpClient(connection.name, connection);
70
+ await client.connect();
71
+ this.clients.push(client);
72
+ this.logger.log(`Registered runtime MCP client "${connection.name}"`);
73
+ return client;
74
+ })().finally(() => this.pending.delete(connection.name));
75
+ this.pending.set(connection.name, promise);
76
+ return promise;
77
+ }
78
+ /** Alias for {@link addConnection}: return the connected client for `connection`, creating it if needed. */
79
+ getOrCreate(connection) {
80
+ return this.addConnection(connection);
81
+ }
82
+ /**
83
+ * Disconnect and unregister every client registered under `name` (idempotent — a no-op if none
84
+ * exist). Mutates the registered collection in place so existing `getClients()` references stay
85
+ * valid.
86
+ */
87
+ async removeConnection(name) {
88
+ const removed = [];
89
+ for (let i = this.clients.length - 1; i >= 0; i--) {
90
+ if (this.clients[i].name === name)
91
+ removed.push(...this.clients.splice(i, 1));
92
+ }
93
+ await Promise.all(removed.map((c) => c
94
+ .disconnect()
95
+ .catch((e) => this.logger.warn(`Failed to disconnect "${name}": ${String(e)}`))));
96
+ }
32
97
  };
33
98
  exports.McpClientsService = McpClientsService;
34
99
  exports.McpClientsService = McpClientsService = __decorate([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nest-mcp/client",
3
- "version": "0.2.11",
3
+ "version": "0.3.1",
4
4
  "description": "NestJS MCP Client - Connect to and consume MCP servers from NestJS",
5
5
  "license": "BSD-3-Clause",
6
6
  "main": "./dist/index.js",
@@ -40,7 +40,7 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
- "@nest-mcp/common": "0.5.1"
43
+ "@nest-mcp/common": "0.6.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "@modelcontextprotocol/sdk": "^1.26.0",