@artinet/sdk 0.6.6 → 0.6.7

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.
@@ -72,9 +72,9 @@ export class Manager {
72
72
  async list() {
73
73
  const listed = Array.from(this.cache.values());
74
74
  if (this.storage) {
75
+ const storedList = (await this.storage.list?.())?.filter((item) => item !== undefined && !listed.includes(item));
75
76
  /** Could be an expensive operation */
76
- listed.push(...((await this.storage.list?.())?.filter((item) => item !== undefined && !listed.includes(item)) ??
77
- []));
77
+ listed.push(...(storedList ?? []));
78
78
  }
79
79
  return listed;
80
80
  }
@@ -88,7 +88,14 @@ export class Manager {
88
88
  }
89
89
  const results = [];
90
90
  if (filter) {
91
- results.push(...Array.from(this.cache.values()).filter(filter));
91
+ const items = Array.from(this.cache.values());
92
+ const filterResults = await Promise.all(items.map(async (item) => {
93
+ if (await filter(item)) {
94
+ return item;
95
+ }
96
+ return undefined;
97
+ }));
98
+ results.push(...filterResults.filter((item) => item !== undefined));
92
99
  }
93
100
  if (this.storage) {
94
101
  const storageFilter = async (item) => {
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Copyright 2026 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
6
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
7
+ import { z } from 'zod/v4';
8
+ /**
9
+ * Configuration for mounting an in-memory MCP server.
10
+ */
11
+ declare const InMemoryParamsSchema: z.ZodObject<{
12
+ type: z.ZodEnum<{
13
+ factory: "factory";
14
+ constructor: "constructor";
15
+ }>;
16
+ target: z.ZodString;
17
+ module: z.ZodString;
18
+ args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
19
+ }, z.core.$strip>;
20
+ export type InMemoryParams = z.infer<typeof InMemoryParamsSchema>;
21
+ /**
22
+ * Mounts an MCP server in-memory for direct integration.
23
+ * Creates a linked transport pair for client-server communication without network overhead.
24
+ * @docs https://modelcontextprotocol.io/docs/concepts/transports#in-memory
25
+ *
26
+ * @param params - {@link InMemoryParams} Configuration for the server module
27
+ * @param extract - Optional function to extract the McpServer from the imported module.
28
+ * Use when the module's export structure doesn't match standard factory/constructor patterns.
29
+ * @example
30
+ * ```typescript
31
+ * import { mountMemServer } from "@artinet/sdk/mcp/mem";
32
+ * import { Client } from "@modelcontextprotocol/sdk/client/index.js";
33
+ *
34
+ * // Mount a server using a factory function with custom extraction
35
+ * const { server, clientTransport } = await mountMemServer(
36
+ * {
37
+ * type: "factory",
38
+ * target: "createServer",
39
+ * module: "@modelcontextprotocol/server-everything/dist/everything.js",
40
+ * },
41
+ * (module) => module.createServer().server
42
+ * );
43
+ *
44
+ * // Connect a client to the in-memory server
45
+ * const client = new Client({ name: "my-client", version: "1.0.0" });
46
+ * await client.connect(clientTransport);
47
+ *
48
+ * // Use the client
49
+ * const tools = await client.listTools();
50
+ * ```
51
+ * @returns Promise containing the {@link McpServer} instance and linked transport pair
52
+ */
53
+ export declare function mountMemServer(params: InMemoryParams, extract?: (module: any) => McpServer): Promise<{
54
+ serverTransport: InMemoryTransport;
55
+ clientTransport: InMemoryTransport;
56
+ server: McpServer;
57
+ }>;
58
+ export {};
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Copyright 2026 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
6
+ import { logger } from "../../../config/index.js";
7
+ import { z } from 'zod/v4';
8
+ /**
9
+ * Configuration for mounting an in-memory MCP server.
10
+ */
11
+ const InMemoryParamsSchema = z.object({
12
+ /** Whether to invoke the target as a factory function or constructor */
13
+ type: z.enum(['factory', 'constructor']),
14
+ /** The name of the exported factory function or constructor class */
15
+ target: z.string(),
16
+ /** The module path to dynamically import (e.g., '@modelcontextprotocol/server-everything/dist/everything.js') */
17
+ module: z.string(),
18
+ /** Optional arguments to pass to the factory function or constructor */
19
+ args: z.record(z.string(), z.unknown()).optional(),
20
+ });
21
+ /**
22
+ * Mounts an MCP server in-memory for direct integration.
23
+ * Creates a linked transport pair for client-server communication without network overhead.
24
+ * @docs https://modelcontextprotocol.io/docs/concepts/transports#in-memory
25
+ *
26
+ * @param params - {@link InMemoryParams} Configuration for the server module
27
+ * @param extract - Optional function to extract the McpServer from the imported module.
28
+ * Use when the module's export structure doesn't match standard factory/constructor patterns.
29
+ * @example
30
+ * ```typescript
31
+ * import { mountMemServer } from "@artinet/sdk/mcp/mem";
32
+ * import { Client } from "@modelcontextprotocol/sdk/client/index.js";
33
+ *
34
+ * // Mount a server using a factory function with custom extraction
35
+ * const { server, clientTransport } = await mountMemServer(
36
+ * {
37
+ * type: "factory",
38
+ * target: "createServer",
39
+ * module: "@modelcontextprotocol/server-everything/dist/everything.js",
40
+ * },
41
+ * (module) => module.createServer().server
42
+ * );
43
+ *
44
+ * // Connect a client to the in-memory server
45
+ * const client = new Client({ name: "my-client", version: "1.0.0" });
46
+ * await client.connect(clientTransport);
47
+ *
48
+ * // Use the client
49
+ * const tools = await client.listTools();
50
+ * ```
51
+ * @returns Promise containing the {@link McpServer} instance and linked transport pair
52
+ */
53
+ export async function mountMemServer(params, extract) {
54
+ const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
55
+ //IFFE
56
+ const modules = (() => {
57
+ const modules = new Map();
58
+ return async (moduleName) => {
59
+ if (modules.has(moduleName)) {
60
+ return modules.get(moduleName);
61
+ }
62
+ const module = await import(moduleName).catch((error) => {
63
+ logger.error(`Failed to import module ${moduleName}`, error);
64
+ throw error;
65
+ });
66
+ if (module) {
67
+ modules.set(moduleName, module);
68
+ }
69
+ return module;
70
+ };
71
+ })();
72
+ const module = await modules(params.module).catch((error) => {
73
+ logger.error(`Failed to import module ${params.module}`, error);
74
+ throw error;
75
+ });
76
+ if (!module) {
77
+ logger.warn(`Failed to import module ${params.module}`);
78
+ throw new Error(`Failed to import module ${params.module}`);
79
+ }
80
+ let server;
81
+ if (extract) {
82
+ server = extract(module);
83
+ }
84
+ else if (params.type === 'factory') {
85
+ const factory = module[params.target];
86
+ if (!factory) {
87
+ const error = new Error(`Failed to find factory ${params.target} in module ${params.module}`);
88
+ logger.error(error.message, error);
89
+ throw error;
90
+ }
91
+ server = factory(params.args);
92
+ }
93
+ else {
94
+ const constructor = module[params.target];
95
+ if (!constructor) {
96
+ const error = new Error(`Failed to find constructor ${params.target} in module ${params.module}`);
97
+ logger.error(error.message, error);
98
+ throw error;
99
+ }
100
+ server = new constructor(params.args);
101
+ }
102
+ await server.connect(serverTransport).catch((error) => {
103
+ logger.error(`Failed to connect to server ${params.target}`, error);
104
+ throw error;
105
+ });
106
+ return { serverTransport, clientTransport, server };
107
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artinet/sdk",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "description": "A TypeScript SDK for building collaborative AI agents.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,6 +22,11 @@
22
22
  "import": "./dist/services/mcp/index.js",
23
23
  "default": "./dist/services/mcp/index.js"
24
24
  },
25
+ "./mcp/mem": {
26
+ "types": "./dist/services/mcp/modules/mem.d.ts",
27
+ "import": "./dist/services/mcp/modules/mem.js",
28
+ "default": "./dist/services/mcp/modules/mem.js"
29
+ },
25
30
  "./serverless": {
26
31
  "types": "./dist/server/express/serverless/index.d.ts",
27
32
  "import": "./dist/server/express/serverless/index.js",