@mcpc-tech/unplugin-dev-inspector-mcp 0.0.31 → 0.0.34

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.
@@ -15,7 +15,7 @@ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
15
15
  import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
16
16
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
17
17
  import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
18
- import process2, { cwd } from "node:process";
18
+ import process$1, { cwd } from "node:process";
19
19
  import { homedir } from "os";
20
20
  import fs, { existsSync } from "fs";
21
21
  import * as http from "http";
@@ -24,7 +24,7 @@ import * as zlib from "zlib";
24
24
  import { fileURLToPath } from "node:url";
25
25
  import path$1, { dirname, join } from "path";
26
26
  import { fileURLToPath as fileURLToPath$1 } from "url";
27
- import { convertToModelMessages, jsonSchema, streamText, tool } from "ai";
27
+ import { asSchema, convertToModelMessages, jsonSchema, streamText, tool } from "ai";
28
28
  import { ClientSideConnection, PROTOCOL_VERSION, ndJsonStream, planEntrySchema } from "@agentclientprotocol/sdk";
29
29
  import { spawn } from "node:child_process";
30
30
  import { mkdir, readFile, writeFile } from "fs/promises";
@@ -58123,13 +58123,13 @@ var require_getMachineId_bsd$1 = /* @__PURE__ */ __commonJSMin(((exports) => {
58123
58123
  var require_getMachineId_win$1 = /* @__PURE__ */ __commonJSMin(((exports) => {
58124
58124
  Object.defineProperty(exports, "__esModule", { value: true });
58125
58125
  exports.getMachineId = void 0;
58126
- const process$4 = __require("process");
58126
+ const process$5 = __require("process");
58127
58127
  const execAsync_1 = require_execAsync$1();
58128
58128
  const api_1 = (init_esm$2(), __toCommonJS(esm_exports$2));
58129
58129
  async function getMachineId() {
58130
58130
  const args = "QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid";
58131
58131
  let command = "%windir%\\System32\\REG.exe";
58132
- if (process$4.arch === "ia32" && "PROCESSOR_ARCHITEW6432" in process$4.env) command = "%windir%\\sysnative\\cmd.exe /c " + command;
58132
+ if (process$5.arch === "ia32" && "PROCESSOR_ARCHITEW6432" in process$5.env) command = "%windir%\\sysnative\\cmd.exe /c " + command;
58133
58133
  try {
58134
58134
  const parts = (await (0, execAsync_1.execAsync)(`${command} ${args}`)).stdout.split("REG_SZ");
58135
58135
  if (parts.length === 2) return parts[1].trim();
@@ -58159,10 +58159,10 @@ var require_getMachineId_unsupported$1 = /* @__PURE__ */ __commonJSMin(((exports
58159
58159
  var require_getMachineId$1 = /* @__PURE__ */ __commonJSMin(((exports) => {
58160
58160
  Object.defineProperty(exports, "__esModule", { value: true });
58161
58161
  exports.getMachineId = void 0;
58162
- const process$3 = __require("process");
58162
+ const process$4 = __require("process");
58163
58163
  let getMachineId;
58164
58164
  exports.getMachineId = getMachineId;
58165
- switch (process$3.platform) {
58165
+ switch (process$4.platform) {
58166
58166
  case "darwin":
58167
58167
  exports.getMachineId = getMachineId = require_getMachineId_darwin$1().getMachineId;
58168
58168
  break;
@@ -66275,13 +66275,13 @@ var require_getMachineId_bsd = /* @__PURE__ */ __commonJSMin(((exports) => {
66275
66275
  var require_getMachineId_win = /* @__PURE__ */ __commonJSMin(((exports) => {
66276
66276
  Object.defineProperty(exports, "__esModule", { value: true });
66277
66277
  exports.getMachineId = void 0;
66278
- const process$2 = __require("process");
66278
+ const process$3 = __require("process");
66279
66279
  const execAsync_1 = require_execAsync();
66280
66280
  const api_1 = (init_esm$2(), __toCommonJS(esm_exports$2));
66281
66281
  async function getMachineId() {
66282
66282
  const args = "QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid";
66283
66283
  let command = "%windir%\\System32\\REG.exe";
66284
- if (process$2.arch === "ia32" && "PROCESSOR_ARCHITEW6432" in process$2.env) command = "%windir%\\sysnative\\cmd.exe /c " + command;
66284
+ if (process$3.arch === "ia32" && "PROCESSOR_ARCHITEW6432" in process$3.env) command = "%windir%\\sysnative\\cmd.exe /c " + command;
66285
66285
  try {
66286
66286
  const parts = (await (0, execAsync_1.execAsync)(`${command} ${args}`)).stdout.split("REG_SZ");
66287
66287
  if (parts.length === 2) return parts[1].trim();
@@ -66311,10 +66311,10 @@ var require_getMachineId_unsupported = /* @__PURE__ */ __commonJSMin(((exports)
66311
66311
  var require_getMachineId = /* @__PURE__ */ __commonJSMin(((exports) => {
66312
66312
  Object.defineProperty(exports, "__esModule", { value: true });
66313
66313
  exports.getMachineId = void 0;
66314
- const process$1 = __require("process");
66314
+ const process$2 = __require("process");
66315
66315
  let getMachineId;
66316
66316
  exports.getMachineId = getMachineId;
66317
- switch (process$1.platform) {
66317
+ switch (process$2.platform) {
66318
66318
  case "darwin":
66319
66319
  exports.getMachineId = getMachineId = require_getMachineId_darwin().getMachineId;
66320
66320
  break;
@@ -83385,7 +83385,7 @@ function parseTags(htmlString, tags) {
83385
83385
  }
83386
83386
  new TextEncoder();
83387
83387
  TransformStream;
83388
- var GEMINI_PREFERRED_FORMAT = process2.env.GEMINI_PREFERRED_FORMAT === "0" ? false : true;
83388
+ var GEMINI_PREFERRED_FORMAT = process$1.env.GEMINI_PREFERRED_FORMAT === "0" ? false : true;
83389
83389
  function sanitizePropertyKey(name) {
83390
83390
  return name.replace(/[@.,/\\:;!?#$%^&*()[\]{}]/g, "_").substring(0, 64);
83391
83391
  }
@@ -83439,7 +83439,7 @@ function createTransport(def) {
83439
83439
  command: defAny.command,
83440
83440
  args: defAny.args,
83441
83441
  env: {
83442
- ...process2.env,
83442
+ ...process$1.env,
83443
83443
  ...defAny.env ?? {}
83444
83444
  },
83445
83445
  cwd: cwd()
@@ -83504,11 +83504,11 @@ var cleanupAllPooledClients = async () => {
83504
83504
  }
83505
83505
  }));
83506
83506
  };
83507
- process2.once?.("exit", () => {
83507
+ process$1.once?.("exit", () => {
83508
83508
  cleanupAllPooledClients();
83509
83509
  });
83510
- process2.once?.("SIGINT", () => {
83511
- cleanupAllPooledClients().finally(() => process2.exit(0));
83510
+ process$1.once?.("SIGINT", () => {
83511
+ cleanupAllPooledClients().finally(() => process$1.exit(0));
83512
83512
  });
83513
83513
  async function composeMcpDepTools(mcpConfig, filterIn) {
83514
83514
  const allTools = {};
@@ -84068,12 +84068,12 @@ var AgenticExecutor = class {
84068
84068
  this.tracingEnabled = false;
84069
84069
  this.logger = createLogger(`mcpc.agentic.${name}`, server);
84070
84070
  try {
84071
- this.tracingEnabled = process2.env.MCPC_TRACING_ENABLED === "true";
84071
+ this.tracingEnabled = process$1.env.MCPC_TRACING_ENABLED === "true";
84072
84072
  if (this.tracingEnabled) initializeTracing({
84073
84073
  enabled: true,
84074
84074
  serviceName: `mcpc-agentic-${name}`,
84075
- exportTo: process2.env.MCPC_TRACING_EXPORT ?? "otlp",
84076
- otlpEndpoint: process2.env.MCPC_TRACING_OTLP_ENDPOINT ?? "http://localhost:4318/v1/traces"
84075
+ exportTo: process$1.env.MCPC_TRACING_EXPORT ?? "otlp",
84076
+ otlpEndpoint: process$1.env.MCPC_TRACING_OTLP_ENDPOINT ?? "http://localhost:4318/v1/traces"
84077
84077
  });
84078
84078
  } catch {
84079
84079
  this.tracingEnabled = false;
@@ -85006,10 +85006,10 @@ var BaseSamplingExecutor = class {
85006
85006
  this.logger = createLogger(`mcpc.sampling.${name}`, server);
85007
85007
  try {
85008
85008
  const tracingConfig = {
85009
- enabled: process2.env.MCPC_TRACING_ENABLED === "true",
85009
+ enabled: process$1.env.MCPC_TRACING_ENABLED === "true",
85010
85010
  serviceName: `mcpc-sampling-${name}`,
85011
- exportTo: process2.env.MCPC_TRACING_EXPORT ?? "otlp",
85012
- otlpEndpoint: process2.env.MCPC_TRACING_OTLP_ENDPOINT ?? "http://localhost:4318/v1/traces"
85011
+ exportTo: process$1.env.MCPC_TRACING_EXPORT ?? "otlp",
85012
+ otlpEndpoint: process$1.env.MCPC_TRACING_OTLP_ENDPOINT ?? "http://localhost:4318/v1/traces"
85013
85013
  };
85014
85014
  this.tracingEnabled = tracingConfig.enabled;
85015
85015
  if (this.tracingEnabled) initializeTracing(tracingConfig);
@@ -86415,10 +86415,10 @@ var ComposableMCPServer = class extends Server {
86415
86415
  if (!await this.pluginManager.triggerRegisterAgentTool(context2)) throw new Error(`No plugin registered to handle execution mode "${mode}". Did you override the default mode plugin, but in the wrong way?`);
86416
86416
  }
86417
86417
  };
86418
- var isSCF = () => Boolean(process2.env.SCF_RUNTIME || process2.env.PROD_SCF);
86418
+ var isSCF = () => Boolean(process$1.env.SCF_RUNTIME || process$1.env.PROD_SCF);
86419
86419
  if (isSCF()) console.log({
86420
86420
  isSCF: isSCF(),
86421
- SCF_RUNTIME: process2.env.SCF_RUNTIME
86421
+ SCF_RUNTIME: process$1.env.SCF_RUNTIME
86422
86422
  });
86423
86423
  function parseMcpcConfigs(conf) {
86424
86424
  return conf ?? [];
@@ -87261,7 +87261,7 @@ function setupInspectorMiddleware(middlewares, config) {
87261
87261
  }
87262
87262
 
87263
87263
  //#endregion
87264
- //#region ../../node_modules/.pnpm/@mcpc-tech+acp-ai-provider@0.1.37/node_modules/@mcpc-tech/acp-ai-provider/index.mjs
87264
+ //#region ../../node_modules/.pnpm/@mcpc-tech+acp-ai-provider@0.1.41/node_modules/@mcpc-tech/acp-ai-provider/index.mjs
87265
87265
  createRequire(import.meta.url);
87266
87266
  function formatToolError(toolResult) {
87267
87267
  if (!toolResult || toolResult.length === 0) return "Unknown tool error";
@@ -87418,8 +87418,13 @@ var ToolProxyHost = class {
87418
87418
  * Start TCP server and return MCP server config for ACP
87419
87419
  */
87420
87420
  async start() {
87421
- if (this.server) throw new Error("Tool proxy already started");
87422
- await this.startServer();
87421
+ if (!this.server) await this.startServer();
87422
+ return this.getServerConfig();
87423
+ }
87424
+ /**
87425
+ * Get MCP server configuration
87426
+ */
87427
+ getServerConfig() {
87423
87428
  return {
87424
87429
  name: this.serverName,
87425
87430
  command: "node",
@@ -87499,8 +87504,10 @@ var ToolProxyHost = class {
87499
87504
  text: typeof result === "string" ? result : JSON.stringify(result)
87500
87505
  }] };
87501
87506
  this.sendResponse(socket, createResponse(request.id, toolResult));
87502
- } else if (request.method === "getTools") this.sendResponse(socket, createResponse(request.id, this.getToolDefinitions()));
87503
- else this.sendResponse(socket, createErrorResponse(request.id, JsonRpcErrorCode.METHOD_NOT_FOUND, `Unknown method: ${request.method}`));
87507
+ } else if (request.method === "getTools") {
87508
+ const definitions = this.getToolDefinitions();
87509
+ this.sendResponse(socket, createResponse(request.id, definitions));
87510
+ } else this.sendResponse(socket, createErrorResponse(request.id, JsonRpcErrorCode.METHOD_NOT_FOUND, `Unknown method: ${request.method}`));
87504
87511
  } catch (error) {
87505
87512
  this.sendResponse(socket, createErrorResponse(request.id, JsonRpcErrorCode.INTERNAL_ERROR, error instanceof Error ? error.message : String(error)));
87506
87513
  }
@@ -87600,16 +87607,22 @@ function convertAiSdkMessagesToAcp(options, isFreshSession) {
87600
87607
  }
87601
87608
  return contentBlocks;
87602
87609
  }
87603
- function extractACPTools(tools) {
87610
+ function extractACPTools(tools, prepared = true) {
87604
87611
  const acpTools2 = [];
87605
87612
  if (!tools) return acpTools2;
87606
- for (const t of tools) if (t.type === "function") {
87613
+ const toolsArray = Array.isArray(tools) ? tools : Object.entries(tools).map(([name, tool2]) => ({
87614
+ type: "function",
87615
+ name,
87616
+ ...tool2
87617
+ }));
87618
+ for (const t of toolsArray) if (t.type === "function") {
87607
87619
  const toolInputSchema = t.inputSchema;
87608
87620
  if (hasRegisteredExecute(t.name) && toolInputSchema) {
87609
87621
  const execute = getExecuteByName(t.name);
87610
87622
  if (execute) acpTools2.push({
87611
87623
  ...t,
87612
87624
  name: t.name,
87625
+ inputSchema: prepared ? toolInputSchema : asSchema(toolInputSchema).jsonSchema,
87613
87626
  execute
87614
87627
  });
87615
87628
  }
@@ -87642,12 +87655,10 @@ var ACPAISDKClient = class {
87642
87655
  optionId: params.options[0]?.optionId || "allow"
87643
87656
  } };
87644
87657
  }
87645
- writeTextFile(params) {
87646
- console.log("[acp-ai-provider] Write file request (not implemented):", params.path);
87658
+ writeTextFile(_params) {
87647
87659
  throw new Error("File operations not implemented in language model client");
87648
87660
  }
87649
- readTextFile(params) {
87650
- console.log("[acp-ai-provider] Read file request (not implemented):", params.path);
87661
+ readTextFile(_params) {
87651
87662
  throw new Error("File operations not implemented in language model client");
87652
87663
  }
87653
87664
  };
@@ -87693,6 +87704,7 @@ var ACPLanguageModel = class {
87693
87704
  */
87694
87705
  parseToolCall(update$1) {
87695
87706
  if (update$1.sessionUpdate !== "tool_call") throw new Error("Invalid update type for parseToolCall");
87707
+ console.log("Parsing tool call update:", JSON.stringify(update$1, null, 2));
87696
87708
  return {
87697
87709
  toolCallId: update$1.toolCallId,
87698
87710
  toolName: update$1.title || update$1.toolCallId,
@@ -87729,7 +87741,7 @@ var ACPLanguageModel = class {
87729
87741
  async connectClient() {
87730
87742
  if (this.connection) return;
87731
87743
  if (!this.agentProcess) {
87732
- const sessionCwd = this.config.session?.cwd || (typeof process2.cwd === "function" ? process2.cwd() : "/");
87744
+ const sessionCwd = this.config.session?.cwd || (typeof process$1.cwd === "function" ? process$1.cwd() : "/");
87733
87745
  this.agentProcess = spawn(this.config.command, this.config.args ?? [], {
87734
87746
  stdio: [
87735
87747
  "pipe",
@@ -87737,7 +87749,7 @@ var ACPLanguageModel = class {
87737
87749
  "inherit"
87738
87750
  ],
87739
87751
  env: {
87740
- ...process2.env,
87752
+ ...process$1.env,
87741
87753
  ...this.config.env
87742
87754
  },
87743
87755
  cwd: sessionCwd
@@ -87765,7 +87777,7 @@ var ACPLanguageModel = class {
87765
87777
  if (initResult.authMethods?.length ?? false) {
87766
87778
  if (!this.config.authMethodId || !validAuthMethods) console.log("[acp-ai-provider] Warning: No authMethodId specified in config, skipping authentication step. If this is not desired, please set one of the authMethodId in the ACPProviderSettings.", JSON.stringify(initResult.authMethods, null, 2));
87767
87779
  if (this.config.authMethodId && validAuthMethods) await this.connection.authenticate({ methodId: this.config.authMethodId ?? initResult.authMethods?.[0].id });
87768
- } else console.log(`[acp-ai-provider] No authentication methods required by the ACP agent, skipping authentication step.`);
87780
+ }
87769
87781
  }
87770
87782
  /**
87771
87783
  * Starts a new session or updates the existing one.
@@ -87773,22 +87785,22 @@ var ACPLanguageModel = class {
87773
87785
  */
87774
87786
  async startSession(acpTools2) {
87775
87787
  if (!this.connection) throw new Error("Not connected");
87776
- console.log(`[acp-ai-provider] startSession called with ${acpTools2?.length ?? 0} tools`);
87777
87788
  const mcpServers = [...this.config.session?.mcpServers ?? []];
87778
87789
  let toolsAdded = false;
87779
87790
  if (acpTools2 && acpTools2.length > 0 && !this.toolProxyHost) {
87780
- console.log("[acp-ai-provider] Setting up tool proxy for client-side tools...");
87791
+ console.log("[acp-ai-provider] Setting up tool proxy for client-side tools...", acpTools2.map((t) => t.name));
87781
87792
  this.toolProxyHost = new ToolProxyHost("acp-ai-sdk-tools");
87782
87793
  for (const t of acpTools2) this.toolProxyHost.registerTool(t.name, t);
87794
+ toolsAdded = true;
87795
+ }
87796
+ if (this.toolProxyHost) {
87783
87797
  const proxyConfig = await this.toolProxyHost.start();
87784
87798
  mcpServers.push(proxyConfig);
87785
- toolsAdded = true;
87786
87799
  }
87787
87800
  if (this.sessionId && toolsAdded) {
87788
- console.log("[acp-ai-provider] Updating session to include new tools...");
87789
87801
  this.sessionResponse = await this.connection.newSession({
87790
87802
  ...this.config.session,
87791
- cwd: this.config.session?.cwd ?? process2.cwd(),
87803
+ cwd: this.config.session?.cwd ?? process$1.cwd(),
87792
87804
  mcpServers
87793
87805
  });
87794
87806
  this.sessionId = this.sessionResponse.sessionId;
@@ -87800,7 +87812,7 @@ var ACPLanguageModel = class {
87800
87812
  if (this.config.existingSessionId) {
87801
87813
  await this.connection.loadSession({
87802
87814
  sessionId: this.config.existingSessionId,
87803
- cwd: this.config.session?.cwd ?? process2.cwd(),
87815
+ cwd: this.config.session?.cwd ?? process$1.cwd(),
87804
87816
  mcpServers
87805
87817
  });
87806
87818
  this.sessionId = this.config.existingSessionId;
@@ -87809,7 +87821,7 @@ var ACPLanguageModel = class {
87809
87821
  } else {
87810
87822
  this.sessionResponse = await this.connection.newSession({
87811
87823
  ...this.config.session,
87812
- cwd: this.config.session?.cwd ?? process2.cwd(),
87824
+ cwd: this.config.session?.cwd ?? process$1.cwd(),
87813
87825
  mcpServers
87814
87826
  });
87815
87827
  this.sessionId = this.sessionResponse.sessionId;
@@ -87830,7 +87842,7 @@ var ACPLanguageModel = class {
87830
87842
  }
87831
87843
  async applySessionDelay() {
87832
87844
  if (this.config.sessionDelayMs) {
87833
- console.log(`[acp-ai-provider] Waiting for ${this.config.sessionDelayMs}ms after session setup...`);
87845
+ console.log(`[acp-ai-provider] Waiting ${this.config.sessionDelayMs}ms after session setup...`);
87834
87846
  await new Promise((resolve$2) => setTimeout(resolve$2, this.config.sessionDelayMs));
87835
87847
  }
87836
87848
  }
@@ -87865,14 +87877,9 @@ var ACPLanguageModel = class {
87865
87877
  *
87866
87878
  * @param acpTools - Optional list of tools to register during session initialization.
87867
87879
  */
87868
- async initSession(acpTools2) {
87869
- let toolsArray = [];
87870
- if (acpTools2) if (Array.isArray(acpTools2)) toolsArray = acpTools2;
87871
- else toolsArray = Object.entries(acpTools2).map(([name, tool2]) => ({
87872
- ...tool2,
87873
- name
87874
- }));
87875
- await this.ensureConnected(toolsArray);
87880
+ async initSession(tools) {
87881
+ const acpTools2 = extractACPTools(tools, false);
87882
+ await this.ensureConnected(acpTools2.length > 0 ? acpTools2 : void 0);
87876
87883
  return this.sessionResponse;
87877
87884
  }
87878
87885
  /**
@@ -87999,6 +88006,11 @@ var ACPLanguageModel = class {
87999
88006
  this.currentThinkingId = null;
88000
88007
  }
88001
88008
  const { toolCallId, toolName, toolInput } = this.parseToolCall(update$1);
88009
+ console.log(`Parsing tool call: ${JSON.stringify({
88010
+ toolCallId,
88011
+ toolName,
88012
+ toolInput
88013
+ }, null, 2)}`);
88002
88014
  const existingToolCall = this.toolCallsMap.get(toolCallId);
88003
88015
  const hasInput = toolInput && typeof toolInput === "object" && Object.keys(toolInput).length > 0;
88004
88016
  if (!existingToolCall) {
@@ -88039,7 +88051,6 @@ var ACPLanguageModel = class {
88039
88051
  break;
88040
88052
  }
88041
88053
  case "tool_call_update": {
88042
- console.log(`###[acp-ai-provider] tool_call_update received:`, JSON.stringify(update$1, null, 2));
88043
88054
  const { toolCallId, toolName, toolResult, isError, status } = this.parseToolResult(update$1);
88044
88055
  let toolInfo = this.toolCallsMap.get(toolCallId);
88045
88056
  if (status === "in_progress") {
@@ -88292,9 +88303,9 @@ var ACPProvider = class {
88292
88303
  * Initializes the session and returns session info (models, modes, meta).
88293
88304
  * Call this before prompting to discover available options.
88294
88305
  */
88295
- initSession(acpTools2) {
88306
+ initSession(tools) {
88296
88307
  if (!this.model) this.languageModel();
88297
- return this.model.initSession(acpTools2);
88308
+ return this.model.initSession(tools);
88298
88309
  }
88299
88310
  /**
88300
88311
  * Initializes the connection to the agent process without starting a session.
@@ -88333,6 +88344,22 @@ function createACPProvider(config) {
88333
88344
  //#endregion
88334
88345
  //#region src/middleware/acp-middleware.ts
88335
88346
  /**
88347
+ * Provider manager - stores one provider per agent config
88348
+ * Key: agentKey (command:args), Value: ProviderEntry
88349
+ */
88350
+ const providerManager = /* @__PURE__ */ new Map();
88351
+ /**
88352
+ * Session to provider mapping for quick lookup
88353
+ * Key: sessionId, Value: agentKey
88354
+ */
88355
+ const sessionToProvider = /* @__PURE__ */ new Map();
88356
+ /**
88357
+ * Generate a unique key for an agent configuration
88358
+ */
88359
+ function getAgentKey(command, args) {
88360
+ return `${command}:${(args || []).join(",")}`;
88361
+ }
88362
+ /**
88336
88363
  * Call MCP method via transport and wait for response
88337
88364
  */
88338
88365
  function callMcpMethodViaTransport(transport, method, params) {
@@ -88390,6 +88417,163 @@ function getActiveTransport() {
88390
88417
  return connectionManager.transports[sessionIds[0]];
88391
88418
  }
88392
88419
  function setupAcpMiddleware(middlewares, serverContext, acpOptions) {
88420
+ /**
88421
+ * Initialize a session for an agent
88422
+ * POST /api/acp/init-session
88423
+ * Body: { agent, envVars }
88424
+ * Returns: { sessionId }
88425
+ */
88426
+ middlewares.use("/api/acp/init-session", async (req, res) => {
88427
+ if (handleCors(res, req.method)) return;
88428
+ if (req.method !== "POST") {
88429
+ res.statusCode = 405;
88430
+ res.end("Method Not Allowed");
88431
+ return;
88432
+ }
88433
+ try {
88434
+ const body = await readBody(req);
88435
+ const { agent, envVars } = JSON.parse(body);
88436
+ const cwd$1 = process.cwd();
88437
+ const agentKey = getAgentKey(agent.command, agent.args);
88438
+ console.log(`[dev-inspector] [acp] Requesting session for agent: ${agent.name} (${agentKey})`);
88439
+ let providerEntry = providerManager.get(agentKey);
88440
+ let sessionId = "";
88441
+ if (providerEntry) {
88442
+ if (providerEntry.sessions.size > 0) {
88443
+ const firstSession = providerEntry.sessions.values().next().value;
88444
+ if (firstSession) {
88445
+ sessionId = firstSession.sessionId;
88446
+ console.log(`[dev-inspector] [acp] Reusing existing session: ${sessionId} for ${agent.name}`);
88447
+ }
88448
+ }
88449
+ if (!sessionId && providerEntry.initializationPromise) {
88450
+ console.log(`[dev-inspector] [acp] Joining pending initialization for ${agent.name}`);
88451
+ try {
88452
+ sessionId = await providerEntry.initializationPromise;
88453
+ } catch (e) {
88454
+ throw e;
88455
+ }
88456
+ }
88457
+ }
88458
+ if (!sessionId) {
88459
+ let provider;
88460
+ if (providerEntry) {
88461
+ console.log(`[dev-inspector] [acp] Reusing existing provider for ${agent.name}`);
88462
+ provider = providerEntry.provider;
88463
+ } else {
88464
+ console.log(`[dev-inspector] [acp] Creating new global provider for ${agent.name}`);
88465
+ provider = createACPProvider({
88466
+ command: agent.command,
88467
+ args: agent.args,
88468
+ env: {
88469
+ ...process.env,
88470
+ ...envVars
88471
+ },
88472
+ session: {
88473
+ cwd: cwd$1,
88474
+ mcpServers: []
88475
+ },
88476
+ authMethodId: agent.authMethodId,
88477
+ persistSession: true
88478
+ });
88479
+ providerEntry = {
88480
+ provider,
88481
+ agentKey,
88482
+ sessions: /* @__PURE__ */ new Map(),
88483
+ createdAt: Date.now(),
88484
+ initializationPromise: void 0
88485
+ };
88486
+ providerManager.set(agentKey, providerEntry);
88487
+ }
88488
+ console.log(`[dev-inspector] [acp] Spawning new process/session for ${agent.name}`);
88489
+ const initPromise = (async () => {
88490
+ const transport = getActiveTransport();
88491
+ let initialTools = {};
88492
+ if (transport) try {
88493
+ const rawTools = await loadMcpToolsV5(transport);
88494
+ initialTools = acpTools(rawTools);
88495
+ console.log(`[dev-inspector] [acp] Pre-loading ${Object.keys(rawTools).length} tools for session init`);
88496
+ } catch (e) {
88497
+ console.warn("[dev-inspector] [acp] Failed to pre-load tools:", e);
88498
+ }
88499
+ const sid = (await provider.initSession(initialTools)).sessionId || `session-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
88500
+ if (providerEntry) {
88501
+ providerEntry.sessions.set(sid, {
88502
+ sessionId: sid,
88503
+ createdAt: Date.now()
88504
+ });
88505
+ providerEntry.initializationPromise = void 0;
88506
+ }
88507
+ sessionToProvider.set(sid, agentKey);
88508
+ return sid;
88509
+ })();
88510
+ if (providerEntry) providerEntry.initializationPromise = initPromise;
88511
+ try {
88512
+ sessionId = await initPromise;
88513
+ console.log(`[dev-inspector] [acp] Session initialized: ${sessionId}`);
88514
+ } catch (error) {
88515
+ if (providerEntry) providerEntry.initializationPromise = void 0;
88516
+ throw error;
88517
+ }
88518
+ }
88519
+ res.setHeader("Content-Type", "application/json");
88520
+ res.end(JSON.stringify({ sessionId }));
88521
+ } catch (error) {
88522
+ console.error("ACP Init Session Error:", error);
88523
+ if (!res.headersSent) {
88524
+ res.statusCode = 500;
88525
+ res.end(JSON.stringify({ error: error instanceof Error ? error.message : "Internal Server Error" }));
88526
+ }
88527
+ }
88528
+ });
88529
+ /**
88530
+ * Cleanup a session
88531
+ * POST /api/acp/cleanup-session
88532
+ * Body: { sessionId }
88533
+ */
88534
+ middlewares.use("/api/acp/cleanup-session", async (req, res) => {
88535
+ if (handleCors(res, req.method)) return;
88536
+ if (req.method !== "POST") {
88537
+ res.statusCode = 405;
88538
+ res.end("Method Not Allowed");
88539
+ return;
88540
+ }
88541
+ try {
88542
+ const body = await readBody(req);
88543
+ const { sessionId } = JSON.parse(body);
88544
+ const agentKey = sessionToProvider.get(sessionId);
88545
+ if (agentKey) {
88546
+ const providerEntry = providerManager.get(agentKey);
88547
+ if (providerEntry) {
88548
+ console.log(`[dev-inspector] [acp] Cleaning up session: ${sessionId} (Provider sessions left: ${providerEntry.sessions.size - 1})`);
88549
+ providerEntry.sessions.delete(sessionId);
88550
+ if (providerEntry.sessions.size === 0) {
88551
+ console.log(`[dev-inspector] [acp] No active sessions for ${agentKey}, cleaning up provider`);
88552
+ try {
88553
+ providerEntry.provider.cleanup();
88554
+ } catch (e) {
88555
+ console.error("Error cleaning up provider:", e);
88556
+ }
88557
+ providerManager.delete(agentKey);
88558
+ }
88559
+ }
88560
+ sessionToProvider.delete(sessionId);
88561
+ }
88562
+ res.setHeader("Content-Type", "application/json");
88563
+ res.end(JSON.stringify({ success: true }));
88564
+ } catch (error) {
88565
+ console.error("ACP Cleanup Session Error:", error);
88566
+ if (!res.headersSent) {
88567
+ res.statusCode = 500;
88568
+ res.end(JSON.stringify({ error: "Internal Server Error" }));
88569
+ }
88570
+ }
88571
+ });
88572
+ /**
88573
+ * Chat endpoint
88574
+ * POST /api/acp/chat
88575
+ * Body: { messages, agent, envVars, sessionId? }
88576
+ */
88393
88577
  middlewares.use("/api/acp/chat", async (req, res) => {
88394
88578
  if (handleCors(res, req.method)) return;
88395
88579
  if (req.method !== "POST") {
@@ -88399,18 +88583,36 @@ function setupAcpMiddleware(middlewares, serverContext, acpOptions) {
88399
88583
  }
88400
88584
  try {
88401
88585
  const body = await readBody(req);
88402
- const { messages, agent, envVars } = JSON.parse(body);
88586
+ const { messages, agent, envVars, sessionId } = JSON.parse(body);
88403
88587
  const cwd$1 = process.cwd();
88404
- const provider = createACPProvider({
88405
- command: agent.command,
88406
- args: agent.args,
88407
- env: envVars,
88408
- session: {
88409
- cwd: cwd$1,
88410
- mcpServers: []
88411
- },
88412
- authMethodId: agent.authMethodId
88413
- });
88588
+ let provider;
88589
+ let shouldCleanupProvider = true;
88590
+ let existingProviderEntry;
88591
+ if (sessionId) {
88592
+ const agentKey = sessionToProvider.get(sessionId);
88593
+ if (agentKey) existingProviderEntry = providerManager.get(agentKey);
88594
+ }
88595
+ if (existingProviderEntry) {
88596
+ console.log(`[dev-inspector] [acp] Using existing global provider for session: ${sessionId}`);
88597
+ provider = existingProviderEntry.provider;
88598
+ shouldCleanupProvider = false;
88599
+ } else {
88600
+ console.log(`[dev-inspector] [acp] Creating new provider (no session found or provided)`);
88601
+ provider = createACPProvider({
88602
+ command: agent.command,
88603
+ args: agent.args,
88604
+ env: {
88605
+ ...process.env,
88606
+ ...envVars
88607
+ },
88608
+ session: {
88609
+ cwd: cwd$1,
88610
+ mcpServers: []
88611
+ },
88612
+ authMethodId: agent.authMethodId
88613
+ });
88614
+ await provider.initSession();
88615
+ }
88414
88616
  const transport = getActiveTransport();
88415
88617
  let mcpTools = {};
88416
88618
  if (transport) mcpTools = await loadMcpToolsV5(transport);
@@ -88426,7 +88628,7 @@ function setupAcpMiddleware(middlewares, serverContext, acpOptions) {
88426
88628
  req.on("close", () => {
88427
88629
  console.log("[dev-inspector] [acp] Client disconnected, aborting stream");
88428
88630
  abortController.abort();
88429
- provider.cleanup();
88631
+ if (shouldCleanupProvider) provider.cleanup();
88430
88632
  });
88431
88633
  const response = streamText({
88432
88634
  model: provider.languageModel(model, mode),
package/dist/index.cjs CHANGED
@@ -71,6 +71,25 @@ const createDevInspectorPlugin = (name, transformFactory) => {
71
71
  load(id) {
72
72
  if (id === "\0" + virtualModuleName) return `
73
73
  // Development-only code - removed in production builds
74
+
75
+ // Global tools registry
76
+ if (typeof window !== 'undefined') {
77
+ if (!window.__INSPECTOR_TOOLS__) {
78
+ window.__INSPECTOR_TOOLS__ = [];
79
+ }
80
+ // Exposed for the inspector client to retrieve tools
81
+ window.__getInspectorTools = () => window.__INSPECTOR_TOOLS__;
82
+ }
83
+
84
+ /**
85
+ * Register a custom tool for the inspector
86
+ */
87
+ export function registerInspectorTool(tool) {
88
+ if (typeof window === 'undefined') return;
89
+ window.__INSPECTOR_TOOLS__ = window.__INSPECTOR_TOOLS__ || [];
90
+ window.__INSPECTOR_TOOLS__.push(tool);
91
+ }
92
+
74
93
  if (typeof window !== 'undefined' && typeof document !== 'undefined') {
75
94
  // Create inspector element
76
95
  const inspector = document.createElement('dev-inspector-mcp');
package/dist/index.js CHANGED
@@ -67,6 +67,25 @@ const createDevInspectorPlugin = (name, transformFactory) => {
67
67
  load(id) {
68
68
  if (id === "\0" + virtualModuleName) return `
69
69
  // Development-only code - removed in production builds
70
+
71
+ // Global tools registry
72
+ if (typeof window !== 'undefined') {
73
+ if (!window.__INSPECTOR_TOOLS__) {
74
+ window.__INSPECTOR_TOOLS__ = [];
75
+ }
76
+ // Exposed for the inspector client to retrieve tools
77
+ window.__getInspectorTools = () => window.__INSPECTOR_TOOLS__;
78
+ }
79
+
80
+ /**
81
+ * Register a custom tool for the inspector
82
+ */
83
+ export function registerInspectorTool(tool) {
84
+ if (typeof window === 'undefined') return;
85
+ window.__INSPECTOR_TOOLS__ = window.__INSPECTOR_TOOLS__ || [];
86
+ window.__INSPECTOR_TOOLS__.push(tool);
87
+ }
88
+
70
89
  if (typeof window !== 'undefined' && typeof document !== 'undefined') {
71
90
  // Create inspector element
72
91
  const inspector = document.createElement('dev-inspector-mcp');