@agiflowai/one-mcp 0.3.15 → 0.3.16

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.
@@ -1495,23 +1495,11 @@ var McpClientManagerService = class {
1495
1495
  clients = /* @__PURE__ */ new Map();
1496
1496
  serverConfigs = /* @__PURE__ */ new Map();
1497
1497
  connectionPromises = /* @__PURE__ */ new Map();
1498
- constructor() {
1499
- process.on("exit", () => {
1500
- this.cleanupOnExit();
1501
- });
1502
- process.on("SIGINT", () => {
1503
- this.cleanupOnExit();
1504
- process.exit(0);
1505
- });
1506
- process.on("SIGTERM", () => {
1507
- this.cleanupOnExit();
1508
- process.exit(0);
1509
- });
1510
- }
1511
1498
  /**
1512
- * Cleanup all resources on exit (child processes)
1499
+ * Synchronously kill all stdio MCP server child processes.
1500
+ * Must be called by the owner (e.g. transport/command layer) during shutdown.
1513
1501
  */
1514
- cleanupOnExit() {
1502
+ cleanupChildProcesses() {
1515
1503
  for (const [serverName, client] of this.clients) try {
1516
1504
  const childProcess = client["childProcess"];
1517
1505
  if (childProcess && !childProcess.killed) {
@@ -2779,7 +2767,7 @@ IMPORTANT: Only use tools discovered from describe_tools with id="${this.serverI
2779
2767
 
2780
2768
  //#endregion
2781
2769
  //#region package.json
2782
- var version = "0.3.14";
2770
+ var version = "0.3.15";
2783
2771
 
2784
2772
  //#endregion
2785
2773
  //#region src/server/index.ts
@@ -2790,6 +2778,7 @@ var version = "0.3.14";
2790
2778
  * - Factory pattern for server creation
2791
2779
  * - Tool registration pattern
2792
2780
  * - Dependency injection for services
2781
+ * - Shared services pattern for multi-session HTTP transport
2793
2782
  *
2794
2783
  * CODING STANDARDS:
2795
2784
  * - Register all tools, resources, and prompts here
@@ -2893,7 +2882,13 @@ function buildProxyInstructions(serverDefinitions, mode, includeSkillsTool) {
2893
2882
  "Use describe_tools to inspect capabilities and use_tool to execute them."
2894
2883
  ].join("\n\n");
2895
2884
  }
2896
- async function createServer(options) {
2885
+ /**
2886
+ * Initialize shared services and tools once for use across multiple sessions.
2887
+ * Use with createSessionServer() for HTTP transport where multiple agents
2888
+ * connect concurrently. This avoids duplicating downstream connections,
2889
+ * file watchers, caches, and tool instances per session.
2890
+ */
2891
+ async function initializeSharedServices(options) {
2897
2892
  const clientManager = new McpClientManagerService();
2898
2893
  let configSkills;
2899
2894
  let configId;
@@ -2966,13 +2961,47 @@ async function createServer(options) {
2966
2961
  definitionsCacheService = new DefinitionsCacheService(clientManager, skillService);
2967
2962
  }
2968
2963
  else definitionsCacheService = new DefinitionsCacheService(clientManager, skillService);
2964
+ const proxyMode = options?.proxyMode || "meta";
2969
2965
  const describeTools = new DescribeToolsTool(clientManager, skillService, serverId, definitionsCacheService);
2970
- const useToolWithCache = new UseToolTool(clientManager, skillService, serverId, definitionsCacheService);
2966
+ const useTool = new UseToolTool(clientManager, skillService, serverId, definitionsCacheService);
2971
2967
  const searchListTools = new SearchListToolsTool(clientManager, definitionsCacheService);
2972
2968
  toolsRef.describeTools = describeTools;
2973
- const serverDefinitions = await definitionsCacheService.getServerDefinitions();
2974
- const includeSkillsTool = await hasAnySkills(definitionsCacheService, skillService);
2975
- const proxyMode = options?.proxyMode || "meta";
2969
+ if (skillService) skillService.startWatching().catch((error) => {
2970
+ console.error(`[skill-watcher] File watcher failed (non-critical): ${error instanceof Error ? error.message : "Unknown error"}`);
2971
+ });
2972
+ if (!shouldStartFromCache && effectiveDefinitionsCachePath && options?.configFilePath) definitionsCacheService.collectForCache({
2973
+ configPath: options.configFilePath,
2974
+ configHash,
2975
+ oneMcpVersion: version,
2976
+ serverId
2977
+ }).then((definitionsCache) => DefinitionsCacheService.writeToFile(effectiveDefinitionsCachePath, definitionsCache)).then(() => {
2978
+ console.error(`[definitions-cache] Wrote ${effectiveDefinitionsCachePath}`);
2979
+ }).catch((error) => {
2980
+ console.error(`[definitions-cache] Failed to persist ${effectiveDefinitionsCachePath}: ${error instanceof Error ? error.message : "Unknown error"}`);
2981
+ });
2982
+ const dispose = async () => {
2983
+ await clientManager.disconnectAll();
2984
+ if (skillService) skillService.stopWatching();
2985
+ };
2986
+ return {
2987
+ clientManager,
2988
+ definitionsCacheService,
2989
+ skillService,
2990
+ describeTools,
2991
+ useTool,
2992
+ searchListTools,
2993
+ serverId,
2994
+ proxyMode,
2995
+ dispose
2996
+ };
2997
+ }
2998
+ /**
2999
+ * Create a lightweight per-session MCP Server instance that delegates
3000
+ * to shared services and tools. Use with initializeSharedServices()
3001
+ * for multi-session HTTP transport.
3002
+ */
3003
+ async function createSessionServer(shared) {
3004
+ const { clientManager, definitionsCacheService, skillService, describeTools, useTool: useToolWithCache, searchListTools, serverId, proxyMode } = shared;
2976
3005
  const server = new Server({
2977
3006
  name: "@agiflowai/one-mcp",
2978
3007
  version: "0.1.0"
@@ -2982,10 +3011,7 @@ async function createServer(options) {
2982
3011
  resources: {},
2983
3012
  prompts: {}
2984
3013
  },
2985
- instructions: buildProxyInstructions(serverDefinitions, proxyMode, includeSkillsTool)
2986
- });
2987
- if (skillService) skillService.startWatching().catch((error) => {
2988
- console.error(`[skill-watcher] File watcher failed (non-critical): ${error instanceof Error ? error.message : "Unknown error"}`);
3014
+ instructions: buildProxyInstructions(await definitionsCacheService.getServerDefinitions(), proxyMode, await hasAnySkills(definitionsCacheService, skillService))
2989
3015
  });
2990
3016
  server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: proxyMode === "flat" ? await (async () => {
2991
3017
  const currentServerDefinitions = await definitionsCacheService.getServerDefinitions();
@@ -3022,14 +3048,14 @@ async function createServer(options) {
3022
3048
  throw new Error(`Unknown tool: ${name}`);
3023
3049
  });
3024
3050
  server.setRequestHandler(ListResourcesRequestSchema, async () => {
3025
- const serverDefinitions$1 = await definitionsCacheService.getServerDefinitions();
3051
+ const currentServerDefinitions = await definitionsCacheService.getServerDefinitions();
3026
3052
  const resourceToServers = /* @__PURE__ */ new Map();
3027
- for (const serverDefinition of serverDefinitions$1) for (const resource of serverDefinition.resources) {
3053
+ for (const serverDefinition of currentServerDefinitions) for (const resource of serverDefinition.resources) {
3028
3054
  if (!resourceToServers.has(resource.uri)) resourceToServers.set(resource.uri, []);
3029
3055
  resourceToServers.get(resource.uri)?.push(serverDefinition.serverName);
3030
3056
  }
3031
3057
  const resources = [];
3032
- for (const serverDefinition of serverDefinitions$1) for (const resource of serverDefinition.resources) {
3058
+ for (const serverDefinition of currentServerDefinitions) for (const resource of serverDefinition.resources) {
3033
3059
  const hasClash = (resourceToServers.get(resource.uri) || []).length > 1;
3034
3060
  resources.push({
3035
3061
  ...resource,
@@ -3048,10 +3074,10 @@ async function createServer(options) {
3048
3074
  return await (await clientManager.ensureConnected(matchingServers[0])).readResource(actualUri);
3049
3075
  });
3050
3076
  server.setRequestHandler(ListPromptsRequestSchema, async () => {
3051
- const serverDefinitions$1 = await definitionsCacheService.getServerDefinitions();
3077
+ const currentServerDefinitions = await definitionsCacheService.getServerDefinitions();
3052
3078
  const promptToServers = /* @__PURE__ */ new Map();
3053
3079
  const serverPromptsMap = /* @__PURE__ */ new Map();
3054
- for (const serverDefinition of serverDefinitions$1) {
3080
+ for (const serverDefinition of currentServerDefinitions) {
3055
3081
  serverPromptsMap.set(serverDefinition.serverName, serverDefinition.prompts);
3056
3082
  for (const prompt of serverDefinition.prompts) {
3057
3083
  if (!promptToServers.has(prompt.name)) promptToServers.set(prompt.name, []);
@@ -3059,7 +3085,7 @@ async function createServer(options) {
3059
3085
  }
3060
3086
  }
3061
3087
  const aggregatedPrompts = [];
3062
- for (const serverDefinition of serverDefinitions$1) {
3088
+ for (const serverDefinition of currentServerDefinitions) {
3063
3089
  const prompts = serverPromptsMap.get(serverDefinition.serverName) || [];
3064
3090
  for (const prompt of prompts) {
3065
3091
  const hasClash = (promptToServers.get(prompt.name) || []).length > 1;
@@ -3074,29 +3100,26 @@ async function createServer(options) {
3074
3100
  });
3075
3101
  server.setRequestHandler(GetPromptRequestSchema, async (request) => {
3076
3102
  const { name, arguments: args } = request.params;
3077
- const serverDefinitions$1 = await definitionsCacheService.getServerDefinitions();
3103
+ const currentServerDefinitions = await definitionsCacheService.getServerDefinitions();
3078
3104
  const { serverName, actualToolName: actualPromptName } = parseToolName(name);
3079
3105
  if (serverName) return await (await clientManager.ensureConnected(serverName)).getPrompt(actualPromptName, args);
3080
3106
  const serversWithPrompt = [];
3081
- for (const serverDefinition of serverDefinitions$1) if (serverDefinition.prompts.some((prompt) => prompt.name === name)) serversWithPrompt.push(serverDefinition.serverName);
3107
+ for (const serverDefinition of currentServerDefinitions) if (serverDefinition.prompts.some((prompt) => prompt.name === name)) serversWithPrompt.push(serverDefinition.serverName);
3082
3108
  if (serversWithPrompt.length === 0) throw new Error(`Prompt not found: ${name}`);
3083
3109
  if (serversWithPrompt.length > 1) throw new Error(`Prompt "${name}" exists on multiple servers: ${serversWithPrompt.join(", ")}. Use the prefixed format (e.g., "${serversWithPrompt[0]}__${name}") to specify which server to use.`);
3084
3110
  const client = clientManager.getClient(serversWithPrompt[0]);
3085
3111
  if (!client) return await (await clientManager.ensureConnected(serversWithPrompt[0])).getPrompt(name, args);
3086
3112
  return await client.getPrompt(name, args);
3087
3113
  });
3088
- if (!shouldStartFromCache && effectiveDefinitionsCachePath && options?.configFilePath) definitionsCacheService.collectForCache({
3089
- configPath: options.configFilePath,
3090
- configHash,
3091
- oneMcpVersion: version,
3092
- serverId
3093
- }).then((definitionsCache) => DefinitionsCacheService.writeToFile(effectiveDefinitionsCachePath, definitionsCache)).then(() => {
3094
- console.error(`[definitions-cache] Wrote ${effectiveDefinitionsCachePath}`);
3095
- }).catch((error) => {
3096
- console.error(`[definitions-cache] Failed to persist ${effectiveDefinitionsCachePath}: ${error instanceof Error ? error.message : "Unknown error"}`);
3097
- });
3098
3114
  return server;
3099
3115
  }
3116
+ /**
3117
+ * Create a single MCP server instance (backward-compatible wrapper).
3118
+ * For multi-session HTTP transport, use initializeSharedServices() + createSessionServer() instead.
3119
+ */
3120
+ async function createServer(options) {
3121
+ return createSessionServer(await initializeSharedServices(options));
3122
+ }
3100
3123
 
3101
3124
  //#endregion
3102
3125
  //#region src/types/index.ts
@@ -4080,4 +4103,4 @@ var StopServerService = class {
4080
4103
  };
4081
4104
 
4082
4105
  //#endregion
4083
- export { findConfigFile as _, SseTransportHandler as a, createServer as c, SearchListToolsTool as d, DescribeToolsTool as f, generateServerId as g, DefinitionsCacheService as h, StdioTransportHandler as i, version as l, McpClientManagerService as m, RuntimeStateService as n, HttpTransportHandler as o, SkillService as p, StdioHttpTransportHandler as r, TRANSPORT_MODE as s, StopServerService as t, UseToolTool as u, ConfigFetcherService as v };
4106
+ export { DefinitionsCacheService as _, SseTransportHandler as a, ConfigFetcherService as b, createServer as c, version as d, UseToolTool as f, McpClientManagerService as g, SkillService as h, StdioTransportHandler as i, createSessionServer as l, DescribeToolsTool as m, RuntimeStateService as n, HttpTransportHandler as o, SearchListToolsTool as p, StdioHttpTransportHandler as r, TRANSPORT_MODE as s, StopServerService as t, initializeSharedServices as u, generateServerId as v, findConfigFile as y };
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.15",
4
+ "version": "0.3.16",
5
5
  "license": "AGPL-3.0",
6
6
  "keywords": [
7
7
  "mcp",