@agentrq/acp-gateway 0.1.7 → 0.1.9
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/README.md +6 -3
- package/dist/__tests__/index.test.js +104 -6
- package/dist/__tests__/index.test.js.map +1 -1
- package/dist/__tests__/mcpClient.test.js +106 -148
- package/dist/__tests__/mcpClient.test.js.map +1 -1
- package/dist/__tests__/taskIdentity.test.js +38 -0
- package/dist/__tests__/taskIdentity.test.js.map +1 -0
- package/dist/index.js +42 -14
- package/dist/index.js.map +1 -1
- package/dist/mcpClient.js +50 -24
- package/dist/mcpClient.js.map +1 -1
- package/dist/taskIdentity.js +28 -0
- package/dist/taskIdentity.js.map +1 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -42,7 +42,7 @@ npm install -g @agentrq/acp-gateway
|
|
|
42
42
|
|
|
43
43
|
## Current Version
|
|
44
44
|
|
|
45
|
-
`0.1.
|
|
45
|
+
`0.1.9`
|
|
46
46
|
|
|
47
47
|
## Usage
|
|
48
48
|
|
|
@@ -105,8 +105,11 @@ Example `.mcp.json`:
|
|
|
105
105
|
1. **Config Loading** — Reads `.mcp.json` to find the agentrq MCP server.
|
|
106
106
|
2. **MCP Connection** — Establishes a `StreamableHTTPClientTransport` to the MCP server with automatic retry and reconnection.
|
|
107
107
|
3. **Agent Spawning** — Launches the specified ACP agent as a subprocess with stdio piping.
|
|
108
|
-
4. **ACP Handshake** — Initializes the ACP connection
|
|
109
|
-
5. **Task Bridge
|
|
108
|
+
4. **ACP Handshake** — Initializes the ACP connection.
|
|
109
|
+
5. **Task Bridge & Multi-Session Isolation** — When a task is received from the MCP server:
|
|
110
|
+
- `acp-gateway` extracts the `chat_id` (Task ID).
|
|
111
|
+
- It ensures a dedicated ACP session for that specific task.
|
|
112
|
+
- If the task belongs to a different session than the current one, a new ACP session is initialized, providing clean state isolation between concurrent or sequential tasks.
|
|
110
113
|
6. **Permission Bridge** — Permission requests from the ACP agent are forwarded to the MCP server; verdicts are sent back.
|
|
111
114
|
7. **Recursive Execution** — After each task completes, `acp-gateway` checks for the next pending task automatically.
|
|
112
115
|
|
|
@@ -1,10 +1,108 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
|
|
3
|
-
// Since index.ts is mostly procedural, we'll just check if it's there for now.
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import { createAcpSessionSwitcher, checkForNextTask } from "../index.js";
|
|
4
3
|
describe("index", () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
describe("createAcpSessionSwitcher", () => {
|
|
5
|
+
let mockConnection;
|
|
6
|
+
let params;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
mockConnection = {
|
|
9
|
+
newSession: vi.fn().mockResolvedValue({ sessionId: "new-session-id" }),
|
|
10
|
+
};
|
|
11
|
+
params = { cwd: "/test", mcpServers: [{ name: "s1", url: "http://s1", headers: [] }] };
|
|
12
|
+
});
|
|
13
|
+
it("should return initial session ID", () => {
|
|
14
|
+
const switcher = createAcpSessionSwitcher(mockConnection, params, "initial-id");
|
|
15
|
+
expect(switcher.getSessionId()).toBe("initial-id");
|
|
16
|
+
});
|
|
17
|
+
it("should return current session ID if taskId is undefined", async () => {
|
|
18
|
+
const switcher = createAcpSessionSwitcher(mockConnection, params, "initial-id");
|
|
19
|
+
const id = await switcher.ensureForTask(undefined);
|
|
20
|
+
expect(id).toBe("initial-id");
|
|
21
|
+
expect(mockConnection.newSession).not.toHaveBeenCalled();
|
|
22
|
+
});
|
|
23
|
+
it("should create a new session on first call with taskId", async () => {
|
|
24
|
+
const switcher = createAcpSessionSwitcher(mockConnection, params, "initial-id");
|
|
25
|
+
const id = await switcher.ensureForTask("task-1");
|
|
26
|
+
expect(id).toBe("new-session-id");
|
|
27
|
+
expect(mockConnection.newSession).toHaveBeenCalled();
|
|
28
|
+
// Second call with same taskId should still return same ID
|
|
29
|
+
const id2 = await switcher.ensureForTask("task-1");
|
|
30
|
+
expect(id2).toBe("new-session-id");
|
|
31
|
+
expect(mockConnection.newSession).toHaveBeenCalledTimes(1);
|
|
32
|
+
});
|
|
33
|
+
it("should create a new session when taskId changes", async () => {
|
|
34
|
+
params = { cwd: "/test", mcpServers: [{ name: "s1", url: "http://s1", headers: [] }] };
|
|
35
|
+
const switcher = createAcpSessionSwitcher(mockConnection, params, "initial-id");
|
|
36
|
+
// Set initial task
|
|
37
|
+
await switcher.ensureForTask("task-1");
|
|
38
|
+
// Change task
|
|
39
|
+
const id = await switcher.ensureForTask("task-2");
|
|
40
|
+
expect(id).toBe("new-session-id");
|
|
41
|
+
expect(mockConnection.newSession).toHaveBeenCalledTimes(2);
|
|
42
|
+
expect(switcher.getSessionId()).toBe("new-session-id");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe("checkForNextTask", () => {
|
|
46
|
+
let mockMcpBridge;
|
|
47
|
+
let mockConnection;
|
|
48
|
+
let mockSessionSwitcher;
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
vi.clearAllMocks();
|
|
51
|
+
mockMcpBridge = {
|
|
52
|
+
callTool: vi.fn(),
|
|
53
|
+
};
|
|
54
|
+
mockConnection = {
|
|
55
|
+
prompt: vi.fn().mockResolvedValue({ stopReason: "complete" }),
|
|
56
|
+
};
|
|
57
|
+
mockSessionSwitcher = {
|
|
58
|
+
getSessionId: vi.fn().mockReturnValue("current-session"),
|
|
59
|
+
ensureForTask: vi.fn().mockResolvedValue("current-session"),
|
|
60
|
+
};
|
|
61
|
+
// Mock console.error to avoid cluttering test output
|
|
62
|
+
vi.spyOn(console, "error").mockImplementation(() => { });
|
|
63
|
+
});
|
|
64
|
+
it("should do nothing if no tasks are found", async () => {
|
|
65
|
+
mockMcpBridge.callTool.mockResolvedValue({
|
|
66
|
+
isError: false,
|
|
67
|
+
content: [{ type: "text", text: "no pending tasks exist" }],
|
|
68
|
+
});
|
|
69
|
+
await checkForNextTask(mockMcpBridge, mockConnection, mockSessionSwitcher);
|
|
70
|
+
expect(mockMcpBridge.callTool).toHaveBeenCalledWith("getNextTask");
|
|
71
|
+
expect(mockConnection.prompt).not.toHaveBeenCalled();
|
|
72
|
+
});
|
|
73
|
+
it("should handle error from MCP bridge", async () => {
|
|
74
|
+
mockMcpBridge.callTool.mockResolvedValue({
|
|
75
|
+
isError: true,
|
|
76
|
+
content: "some error",
|
|
77
|
+
});
|
|
78
|
+
await checkForNextTask(mockMcpBridge, mockConnection, mockSessionSwitcher);
|
|
79
|
+
expect(mockMcpBridge.callTool).toHaveBeenCalledWith("getNextTask");
|
|
80
|
+
expect(mockConnection.prompt).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
it("should process task and recurse if task is found", async () => {
|
|
83
|
+
// First call returns a task with ID in text, second call returns no task
|
|
84
|
+
mockMcpBridge.callTool
|
|
85
|
+
.mockResolvedValueOnce({
|
|
86
|
+
isError: false,
|
|
87
|
+
content: [{ type: "text", text: "Task ID: T1\ndo something" }],
|
|
88
|
+
})
|
|
89
|
+
.mockResolvedValueOnce({
|
|
90
|
+
isError: false,
|
|
91
|
+
content: [{ type: "text", text: "no pending tasks exist" }],
|
|
92
|
+
});
|
|
93
|
+
await checkForNextTask(mockMcpBridge, mockConnection, mockSessionSwitcher);
|
|
94
|
+
expect(mockMcpBridge.callTool).toHaveBeenCalledTimes(2);
|
|
95
|
+
expect(mockSessionSwitcher.ensureForTask).toHaveBeenCalledWith("T1");
|
|
96
|
+
expect(mockConnection.prompt).toHaveBeenCalledWith({
|
|
97
|
+
sessionId: "current-session",
|
|
98
|
+
prompt: [{ type: "text", text: "Task ID: T1\ndo something" }],
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
it("should handle exceptions during execution", async () => {
|
|
102
|
+
mockMcpBridge.callTool.mockRejectedValue(new Error("network error"));
|
|
103
|
+
await checkForNextTask(mockMcpBridge, mockConnection, mockSessionSwitcher);
|
|
104
|
+
expect(console.error).toHaveBeenCalledWith(expect.stringContaining("Failed to check for next task"), expect.any(Error));
|
|
105
|
+
});
|
|
8
106
|
});
|
|
9
107
|
});
|
|
10
108
|
//# sourceMappingURL=index.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/__tests__/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/__tests__/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,wBAAwB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEzE,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE;IACrB,QAAQ,CAAC,0BAA0B,EAAE,GAAG,EAAE;QACxC,IAAI,cAAmB,CAAC;QACxB,IAAI,MAAW,CAAC;QAEhB,UAAU,CAAC,GAAG,EAAE;YACd,cAAc,GAAG;gBACf,UAAU,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,EAAE,SAAS,EAAE,gBAAgB,EAAE,CAAC;aACvE,CAAC;YACF,MAAM,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,WAAW,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC;QACzF,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE;YAC1C,MAAM,QAAQ,GAAG,wBAAwB,CAAC,cAAc,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;YAChF,MAAM,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QACrD,CAAC,CAAC,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AAM,EAAE,IAAI,EAAE,wBAAwB,EAAE,CAAC;aAC5D,CAAC,CAAC;YAEL,MAAM,gBAAgB,CAAC,aAAa,EAAE,cAAc,EAAE,mBAAmB,CAAC,CAAC;YAE3E,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;YACxD,MAAM,CAAC,mBAAmB,CAAC,aAAa,CAAC,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;YACrE,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,oBAAoB,CAAC;gBACjD,SAAS,EAAE,iBAAiB;gBAC5B,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,2BAA2B,EAAE,CAAC;aAC9D,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,2CAA2C,EAAE,KAAK,IAAI,EAAE;YACzD,aAAa,CAAC,QAAQ,CAAC,iBAAiB,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC;YAErE,MAAM,gBAAgB,CAAC,aAAa,EAAE,cAAc,EAAE,mBAAmB,CAAC,CAAC;YAE3E,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,oBAAoB,CACxC,MAAM,CAAC,gBAAgB,CAAC,+BAA+B,CAAC,EACxD,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAClB,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -2,30 +2,34 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
|
2
2
|
import { MCPBridge } from "../mcpClient.js";
|
|
3
3
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
4
4
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
5
|
-
const
|
|
5
|
+
const createMockClient = () => ({
|
|
6
6
|
connect: vi.fn().mockResolvedValue(undefined),
|
|
7
7
|
close: vi.fn().mockResolvedValue(undefined),
|
|
8
8
|
callTool: vi.fn().mockResolvedValue({ result: "ok" }),
|
|
9
9
|
notification: vi.fn().mockResolvedValue(undefined),
|
|
10
10
|
setNotificationHandler: vi.fn(),
|
|
11
|
-
};
|
|
12
|
-
const
|
|
11
|
+
});
|
|
12
|
+
const createMockTransport = () => ({
|
|
13
13
|
close: vi.fn().mockResolvedValue(undefined),
|
|
14
|
-
onclose:
|
|
15
|
-
onerror:
|
|
14
|
+
onclose: undefined,
|
|
15
|
+
onerror: undefined,
|
|
16
16
|
_sessionId: "mock-session-id",
|
|
17
|
-
};
|
|
17
|
+
});
|
|
18
|
+
let lastMockClient;
|
|
19
|
+
let lastMockTransport;
|
|
18
20
|
vi.mock("@modelcontextprotocol/sdk/client/index.js", () => {
|
|
19
21
|
return {
|
|
20
22
|
Client: vi.fn().mockImplementation(function () {
|
|
21
|
-
|
|
23
|
+
lastMockClient = createMockClient();
|
|
24
|
+
return lastMockClient;
|
|
22
25
|
}),
|
|
23
26
|
};
|
|
24
27
|
});
|
|
25
28
|
vi.mock("@modelcontextprotocol/sdk/client/streamableHttp.js", () => {
|
|
26
29
|
return {
|
|
27
30
|
StreamableHTTPClientTransport: vi.fn().mockImplementation(function () {
|
|
28
|
-
|
|
31
|
+
lastMockTransport = createMockTransport();
|
|
32
|
+
return lastMockTransport;
|
|
29
33
|
}),
|
|
30
34
|
};
|
|
31
35
|
});
|
|
@@ -38,19 +42,17 @@ describe("MCPBridge", () => {
|
|
|
38
42
|
beforeEach(() => {
|
|
39
43
|
vi.clearAllMocks();
|
|
40
44
|
vi.useFakeTimers();
|
|
45
|
+
lastMockClient = undefined;
|
|
46
|
+
lastMockTransport = undefined;
|
|
41
47
|
});
|
|
42
48
|
afterEach(() => {
|
|
43
49
|
vi.useRealTimers();
|
|
44
50
|
});
|
|
45
|
-
it("should initialize
|
|
51
|
+
it("should initialize without connecting", () => {
|
|
46
52
|
const bridge = new MCPBridge(config);
|
|
47
53
|
expect(bridge).toBeDefined();
|
|
48
|
-
expect(StreamableHTTPClientTransport).toHaveBeenCalled();
|
|
49
|
-
expect(Client).toHaveBeenCalled();
|
|
50
|
-
});
|
|
51
|
-
it("should get session ID from transport", () => {
|
|
52
|
-
const bridge = new MCPBridge(config);
|
|
53
|
-
expect(bridge.getSessionId()).toBe("mock-session-id");
|
|
54
|
+
expect(StreamableHTTPClientTransport).not.toHaveBeenCalled();
|
|
55
|
+
expect(Client).not.toHaveBeenCalled();
|
|
54
56
|
});
|
|
55
57
|
it("should throw error if config has no URL", () => {
|
|
56
58
|
expect(() => new MCPBridge({ name: "fail", type: "http" })).toThrow("has no URL");
|
|
@@ -58,176 +60,132 @@ describe("MCPBridge", () => {
|
|
|
58
60
|
describe("connect", () => {
|
|
59
61
|
it("should connect successfully and set up handlers", async () => {
|
|
60
62
|
const bridge = new MCPBridge(config);
|
|
61
|
-
const mClient = bridge.client;
|
|
62
63
|
await bridge.connect();
|
|
63
|
-
expect(
|
|
64
|
-
expect(
|
|
64
|
+
expect(Client).toHaveBeenCalled();
|
|
65
|
+
expect(StreamableHTTPClientTransport).toHaveBeenCalled();
|
|
66
|
+
expect(lastMockClient.connect).toHaveBeenCalled();
|
|
67
|
+
expect(lastMockClient.setNotificationHandler).toHaveBeenCalledTimes(2);
|
|
65
68
|
expect(bridge.isConnected).toBe(true);
|
|
66
69
|
});
|
|
67
|
-
it("should retry on connection failure", async () => {
|
|
70
|
+
it("should retry on connection failure with exponential backoff", async () => {
|
|
68
71
|
const bridge = new MCPBridge(config);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
.
|
|
72
|
+
// We need to mock Client.connect to fail
|
|
73
|
+
Client.mockImplementationOnce(function () {
|
|
74
|
+
lastMockClient = createMockClient();
|
|
75
|
+
lastMockClient.connect.mockRejectedValue(new Error("fail 1"));
|
|
76
|
+
return lastMockClient;
|
|
77
|
+
}).mockImplementationOnce(function () {
|
|
78
|
+
lastMockClient = createMockClient();
|
|
79
|
+
lastMockClient.connect.mockRejectedValue(new Error("fail 2"));
|
|
80
|
+
return lastMockClient;
|
|
81
|
+
}).mockImplementationOnce(function () {
|
|
82
|
+
lastMockClient = createMockClient();
|
|
83
|
+
lastMockClient.connect.mockResolvedValue(undefined);
|
|
84
|
+
return lastMockClient;
|
|
85
|
+
});
|
|
73
86
|
const connectPromise = bridge.connect();
|
|
74
|
-
|
|
87
|
+
// First attempt (immediate)
|
|
88
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
89
|
+
expect(Client).toHaveBeenCalledTimes(1);
|
|
90
|
+
// Second attempt (after 1s)
|
|
91
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
92
|
+
expect(Client).toHaveBeenCalledTimes(2);
|
|
93
|
+
// Third attempt (after 2s)
|
|
94
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
95
|
+
expect(Client).toHaveBeenCalledTimes(3);
|
|
75
96
|
await connectPromise;
|
|
76
|
-
expect(mClient.connect).toHaveBeenCalledTimes(2);
|
|
77
97
|
expect(bridge.isConnected).toBe(true);
|
|
78
98
|
});
|
|
79
|
-
it("should
|
|
99
|
+
it("should cap exponential backoff at 15 minutes", async () => {
|
|
80
100
|
const bridge = new MCPBridge(config);
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
expect(bridge.isConnected).toBe(false);
|
|
88
|
-
expect(connectSpy).toHaveBeenCalled();
|
|
89
|
-
});
|
|
90
|
-
it("should start connection in ensureConnected if not already connecting", async () => {
|
|
91
|
-
const bridge = new MCPBridge(config);
|
|
92
|
-
const connectSpy = vi.spyOn(bridge, "connect");
|
|
93
|
-
// Mock connect to immediately set isConnected = true to avoid long wait
|
|
94
|
-
connectSpy.mockImplementation(async () => {
|
|
95
|
-
bridge.isConnected = true;
|
|
101
|
+
// Override default mock behavior for this test only
|
|
102
|
+
const clientMock = Client;
|
|
103
|
+
clientMock.mockImplementation(function () {
|
|
104
|
+
lastMockClient = createMockClient();
|
|
105
|
+
lastMockClient.connect.mockRejectedValue(new Error("fail"));
|
|
106
|
+
return lastMockClient;
|
|
96
107
|
});
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
// Initial connect
|
|
103
|
-
await bridge.connect();
|
|
104
|
-
expect(bridge.isConnected).toBe(true);
|
|
105
|
-
// Now replace the method on the instance
|
|
106
|
-
let resolveReconnect;
|
|
107
|
-
const reconnectPromise = new Promise((resolve) => { resolveReconnect = resolve; });
|
|
108
|
-
bridge._connectOnce = vi.fn().mockReturnValue(reconnectPromise);
|
|
109
|
-
// Manually trigger onclose
|
|
110
|
-
if (mockTransport.onclose) {
|
|
111
|
-
mockTransport.onclose();
|
|
112
|
-
}
|
|
113
|
-
// Ticking to allow onclose to call connect()
|
|
114
|
-
await vi.advanceTimersByTimeAsync(0);
|
|
115
|
-
// Reconnection should have been triggered
|
|
116
|
-
expect(bridge.isConnecting).toBe(true);
|
|
117
|
-
expect(bridge.isConnected).toBe(false);
|
|
118
|
-
// Clean up
|
|
119
|
-
resolveReconnect();
|
|
120
|
-
await vi.runAllTimersAsync();
|
|
121
|
-
});
|
|
122
|
-
it("should do nothing on transport close if not previously connected", async () => {
|
|
123
|
-
const bridge = new MCPBridge(config);
|
|
124
|
-
bridge.isConnected = false;
|
|
125
|
-
const connectSpy = vi.spyOn(bridge, "connect");
|
|
126
|
-
if (mockTransport.onclose) {
|
|
127
|
-
mockTransport.onclose();
|
|
128
|
-
}
|
|
129
|
-
expect(connectSpy).not.toHaveBeenCalled();
|
|
130
|
-
});
|
|
131
|
-
it("should log transport errors", async () => {
|
|
132
|
-
const consoleSpy = vi
|
|
133
|
-
.spyOn(console, "error")
|
|
134
|
-
.mockImplementation(() => { });
|
|
135
|
-
const bridge = new MCPBridge(config);
|
|
136
|
-
await bridge.connect();
|
|
137
|
-
const transport = bridge.transport;
|
|
138
|
-
if (transport.onerror) {
|
|
139
|
-
transport.onerror(new Error("transport error"));
|
|
108
|
+
const connectPromise = bridge.connect();
|
|
109
|
+
// Skip several attempts to reach near 900s
|
|
110
|
+
// 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024(cap to 900)
|
|
111
|
+
for (let i = 0; i < 11; i++) {
|
|
112
|
+
await vi.advanceTimersByTimeAsync(1000 * Math.pow(2, i));
|
|
140
113
|
}
|
|
141
|
-
|
|
114
|
+
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
115
|
+
// Advance by 900s (maxDelay)
|
|
116
|
+
await vi.advanceTimersByTimeAsync(900000);
|
|
117
|
+
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Retrying in 900s..."));
|
|
142
118
|
consoleSpy.mockRestore();
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const mClient = bridge.client;
|
|
149
|
-
let taskHandler;
|
|
150
|
-
mClient.setNotificationHandler.mockImplementation((schema, handler) => {
|
|
151
|
-
if (mClient.setNotificationHandler.mock.calls.length === 1) {
|
|
152
|
-
taskHandler = handler;
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
await bridge.connect();
|
|
156
|
-
const emitSpy = vi.spyOn(bridge, "emit");
|
|
157
|
-
if (taskHandler) {
|
|
158
|
-
taskHandler({ params: { content: "test task", meta: { foo: 1 } } });
|
|
159
|
-
}
|
|
160
|
-
expect(emitSpy).toHaveBeenCalledWith("task", {
|
|
161
|
-
content: "test task",
|
|
162
|
-
meta: { foo: 1 },
|
|
119
|
+
await bridge.close();
|
|
120
|
+
// Restore default mock behavior for other tests
|
|
121
|
+
clientMock.mockImplementation(function () {
|
|
122
|
+
lastMockClient = createMockClient();
|
|
123
|
+
return lastMockClient;
|
|
163
124
|
});
|
|
164
125
|
});
|
|
165
|
-
it("should
|
|
126
|
+
it("should handle transport close by reconnecting with a fresh client", async () => {
|
|
166
127
|
const bridge = new MCPBridge(config);
|
|
167
|
-
const mClient = bridge.client;
|
|
168
|
-
let verdictHandler;
|
|
169
|
-
mClient.setNotificationHandler.mockImplementation((schema, handler) => {
|
|
170
|
-
if (mClient.setNotificationHandler.mock.calls.length === 2) {
|
|
171
|
-
verdictHandler = handler;
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
128
|
await bridge.connect();
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
129
|
+
const firstClient = lastMockClient;
|
|
130
|
+
const firstTransport = lastMockTransport;
|
|
131
|
+
expect(bridge.isConnected).toBe(true);
|
|
132
|
+
// Simulate disconnection
|
|
133
|
+
if (firstTransport.onclose) {
|
|
134
|
+
firstTransport.onclose();
|
|
178
135
|
}
|
|
179
|
-
expect(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
136
|
+
expect(bridge.isConnected).toBe(false);
|
|
137
|
+
expect(bridge.isConnecting).toBe(true);
|
|
138
|
+
// Reconnection attempt (after 1s)
|
|
139
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
140
|
+
expect(Client).toHaveBeenCalledTimes(2);
|
|
141
|
+
expect(lastMockClient).not.toBe(firstClient);
|
|
142
|
+
expect(bridge.isConnected).toBe(true);
|
|
183
143
|
});
|
|
184
144
|
});
|
|
185
145
|
describe("ensureConnected", () => {
|
|
186
146
|
it("should wait for connection if currently connecting", async () => {
|
|
187
147
|
const bridge = new MCPBridge(config);
|
|
188
148
|
bridge.isConnecting = true;
|
|
189
|
-
|
|
149
|
+
// Mock isConnected to true after 2s
|
|
190
150
|
setTimeout(() => {
|
|
191
151
|
bridge.isConnected = true;
|
|
192
|
-
},
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
152
|
+
}, 2000);
|
|
153
|
+
const ensurePromise = bridge.ensureConnected();
|
|
154
|
+
await vi.advanceTimersByTimeAsync(2500);
|
|
155
|
+
await ensurePromise;
|
|
156
|
+
expect(bridge.isConnected).toBe(true);
|
|
196
157
|
});
|
|
197
158
|
it("should throw if connection times out", async () => {
|
|
198
159
|
const bridge = new MCPBridge(config);
|
|
199
160
|
bridge.isConnecting = true;
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
// Advance time enough to trigger the timeout
|
|
203
|
-
await vi.runAllTimersAsync();
|
|
161
|
+
const expectPromise = expect(bridge.ensureConnected()).rejects.toThrow("MCP not connected after 10s timeout");
|
|
162
|
+
await vi.advanceTimersByTimeAsync(11000);
|
|
204
163
|
await expectPromise;
|
|
205
164
|
});
|
|
206
165
|
});
|
|
207
166
|
describe("tool calls and notifications", () => {
|
|
208
|
-
it("should call tools correctly", async () => {
|
|
167
|
+
it("should call tools correctly after ensuring connection", async () => {
|
|
209
168
|
const bridge = new MCPBridge(config);
|
|
210
|
-
|
|
211
|
-
const
|
|
169
|
+
// Start connect but don't finish yet
|
|
170
|
+
const callPromise = bridge.callTool("test-tool", { arg: 1 });
|
|
171
|
+
// Advance to trigger _connectOnce and ensureConnected loop
|
|
172
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
173
|
+
const result = await callPromise;
|
|
212
174
|
expect(result).toEqual({ result: "ok" });
|
|
175
|
+
expect(lastMockClient.callTool).toHaveBeenCalledWith({
|
|
176
|
+
name: "test-tool",
|
|
177
|
+
arguments: { arg: 1 },
|
|
178
|
+
});
|
|
213
179
|
});
|
|
214
|
-
it("should send notifications correctly", async () => {
|
|
215
|
-
const bridge = new MCPBridge(config);
|
|
216
|
-
bridge.isConnected = true;
|
|
217
|
-
await bridge.sendNotification("test-method", { foo: "bar" });
|
|
218
|
-
expect(bridge.client.notification).toHaveBeenCalled();
|
|
219
|
-
});
|
|
220
|
-
it("should handle transport close rejection in _connectOnce", async () => {
|
|
221
|
-
const bridge = new MCPBridge(config);
|
|
222
|
-
mockTransport.close.mockRejectedValue(new Error("close failed"));
|
|
223
|
-
await bridge._connectOnce();
|
|
224
|
-
expect(mockTransport.close).toHaveBeenCalled();
|
|
225
|
-
expect(bridge.isConnected).toBe(false);
|
|
226
|
-
});
|
|
227
|
-
it("should close the client", async () => {
|
|
180
|
+
it("should send notifications correctly after ensuring connection", async () => {
|
|
228
181
|
const bridge = new MCPBridge(config);
|
|
229
|
-
|
|
230
|
-
|
|
182
|
+
const notifyPromise = bridge.sendNotification("test-method", { foo: "bar" });
|
|
183
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
184
|
+
await notifyPromise;
|
|
185
|
+
expect(lastMockClient.notification).toHaveBeenCalledWith({
|
|
186
|
+
method: "test-method",
|
|
187
|
+
params: { foo: "bar" },
|
|
188
|
+
});
|
|
231
189
|
});
|
|
232
190
|
});
|
|
233
191
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcpClient.test.js","sourceRoot":"","sources":["../../src/__tests__/mcpClient.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AAEnG,MAAM,
|
|
1
|
+
{"version":3,"file":"mcpClient.test.js","sourceRoot":"","sources":["../../src/__tests__/mcpClient.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AAEnG,MAAM,gBAAgB,GAAG,GAAG,EAAE,CAAC,CAAC;IAC9B,OAAO,EAAE,E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AAM,EAAE,CAAC,wBAAwB,CAAC,IAAI,CAAC,CAAC;YAExC,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC;YAEjC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YACzC,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC,oBAAoB,CAAC;gBACnD,IAAI,EAAE,WAAW;gBACjB,SAAS,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE;aACtB,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,+DAA+D,EAAE,KAAK,IAAI,EAAE;YAC7E,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC,MAAM,CAAC,CAAC;YAErC,MAAM,aAAa,GAAG,MAAM,CAAC,gBAAgB,CAAC,aAAa,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;YAE7E,MAAM,EAAE,CAAC,wBAAwB,CAAC,IAAI,CAAC,CAAC;YACxC,MAAM,aAAa,CAAC;YAEpB,MAAM,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,oBAAoB,CAAC;gBACvD,MAAM,EAAE,aAAa;gBACrB,MAAM,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE;aACvB,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { extractTaskIdFromMeta, extractTaskIdFromText, } from "../taskIdentity.js";
|
|
3
|
+
describe("extractTaskIdFromMeta", () => {
|
|
4
|
+
it("returns undefined for non-objects", () => {
|
|
5
|
+
expect(extractTaskIdFromMeta(undefined)).toBeUndefined();
|
|
6
|
+
expect(extractTaskIdFromMeta(null)).toBeUndefined();
|
|
7
|
+
expect(extractTaskIdFromMeta("x")).toBeUndefined();
|
|
8
|
+
});
|
|
9
|
+
it("reads only chat_id", () => {
|
|
10
|
+
expect(extractTaskIdFromMeta({ chat_id: "chat-1" })).toBe("chat-1");
|
|
11
|
+
expect(extractTaskIdFromMeta({ taskId: "a" })).toBeUndefined();
|
|
12
|
+
expect(extractTaskIdFromMeta({ task_id: "b" })).toBeUndefined();
|
|
13
|
+
expect(extractTaskIdFromMeta({ id: "c" })).toBeUndefined();
|
|
14
|
+
});
|
|
15
|
+
it("ignores empty chat_id", () => {
|
|
16
|
+
expect(extractTaskIdFromMeta({ chat_id: "" })).toBeUndefined();
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
describe("extractTaskIdFromText", () => {
|
|
20
|
+
it("extracts from Task ID", () => {
|
|
21
|
+
expect(extractTaskIdFromText("Task ID: 0amnlepEi1J")).toBe("0amnlepEi1J");
|
|
22
|
+
expect(extractTaskIdFromText("task ID 0amnlepEi1J")).toBe("0amnlepEi1J");
|
|
23
|
+
expect(extractTaskIdFromText("TASK ID: abc-123")).toBe("abc-123");
|
|
24
|
+
});
|
|
25
|
+
it("extracts from Response to task", () => {
|
|
26
|
+
expect(extractTaskIdFromText("Response to task 0amnlepEi1J")).toBe("0amnlepEi1J");
|
|
27
|
+
expect(extractTaskIdFromText("response to task: xyz_789")).toBe("xyz_789");
|
|
28
|
+
});
|
|
29
|
+
it("extracts from task keyword", () => {
|
|
30
|
+
expect(extractTaskIdFromText("task 0amnlepEi1J")).toBe("0amnlepEi1J");
|
|
31
|
+
expect(extractTaskIdFromText("Working on task abc")).toBe("abc");
|
|
32
|
+
});
|
|
33
|
+
it("returns undefined if no match", () => {
|
|
34
|
+
expect(extractTaskIdFromText("Hello world")).toBeUndefined();
|
|
35
|
+
expect(extractTaskIdFromText("")).toBeUndefined();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
//# sourceMappingURL=taskIdentity.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"taskIdentity.test.js","sourceRoot":"","sources":["../../src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AAE;IACrC,EAAE,CAAC,uBAAuB,EAAE,GAAG,EAAE;QAC/B,MAAM,CAAC,qBAAqB,CAAC,sBAAsB,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QAC1E,MAAM,CAAC,qBAAqB,CAAC,qBAAqB,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QACzE,MAAM,CAAC,qBAAqB,CAAC,kBAAkB,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACpE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gCAAgC,EAAE,GAAG,EAAE;QACxC,MAAM,CAAC,qBAAqB,CAAC,8BAA8B,CAAC,CAAC,CAAC,IAAI,CAChE,aAAa,CACd,CAAC;QACF,MAAM,CAAC,qBAAqB,CAAC,2BAA2B,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC7E,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,4BAA4B,EAAE,GAAG,EAAE;QACpC,MAAM,CAAC,qBAAqB,CAAC,kBAAkB,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QACtE,MAAM,CAAC,qBAAqB,CAAC,qBAAqB,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+BAA+B,EAAE,GAAG,EAAE;QACvC,MAAM,CAAC,qBAAqB,CAAC,aAAa,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;QAC7D,MAAM,CAAC,qBAAqB,CAAC,EAAE,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;IACpD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,26 @@ const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url),
|
|
|
13
13
|
import { loadMcpConfig, pickAgentrqServer } from "./config.js";
|
|
14
14
|
import { MCPBridge } from "./mcpClient.js";
|
|
15
15
|
import { AgentRQACPClient } from "./acpClient.js";
|
|
16
|
+
import { extractTaskIdFromMeta, extractTaskIdFromText, } from "./taskIdentity.js";
|
|
17
|
+
export function createAcpSessionSwitcher(connection, params, initialSessionId) {
|
|
18
|
+
let sessionId = initialSessionId;
|
|
19
|
+
let activeTaskId = undefined;
|
|
20
|
+
return {
|
|
21
|
+
getSessionId() {
|
|
22
|
+
return sessionId;
|
|
23
|
+
},
|
|
24
|
+
async ensureForTask(taskId) {
|
|
25
|
+
if (taskId === activeTaskId) {
|
|
26
|
+
return sessionId;
|
|
27
|
+
}
|
|
28
|
+
const next = await connection.newSession(params);
|
|
29
|
+
sessionId = next.sessionId;
|
|
30
|
+
activeTaskId = taskId;
|
|
31
|
+
console.error(`[acp] New ACP session for task ${taskId ?? "anonymous"} (MCP connection unchanged): ${sessionId}`);
|
|
32
|
+
return sessionId;
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
16
36
|
async function main() {
|
|
17
37
|
console.log(`Starting [acp-gateway] ${pkg.name} v${pkg.version}`);
|
|
18
38
|
const args = process.argv.slice(2);
|
|
@@ -58,7 +78,7 @@ async function main() {
|
|
|
58
78
|
// 6. Create ACP Session
|
|
59
79
|
// We pass our agentrq MCP server to the agent so it can use our tools directly.
|
|
60
80
|
// The McpServer object must follow the ACP protocol schema (type, name, url, headers).
|
|
61
|
-
const
|
|
81
|
+
const newSessionParams = {
|
|
62
82
|
cwd: process.cwd(),
|
|
63
83
|
mcpServers: [
|
|
64
84
|
{
|
|
@@ -68,15 +88,18 @@ async function main() {
|
|
|
68
88
|
headers: [],
|
|
69
89
|
},
|
|
70
90
|
],
|
|
71
|
-
}
|
|
72
|
-
const
|
|
73
|
-
|
|
91
|
+
};
|
|
92
|
+
const sessionResult = await connection.newSession(newSessionParams);
|
|
93
|
+
const sessionSwitcher = createAcpSessionSwitcher(connection, newSessionParams, sessionResult.sessionId);
|
|
94
|
+
console.error(`[acp] Created session: ${sessionSwitcher.getSessionId()}`);
|
|
74
95
|
// Bridge: MCP -> ACP
|
|
75
96
|
// When the MCP server sends a notification to 'notifications/claude/channel',
|
|
76
97
|
// it contains a new task content.
|
|
77
|
-
mcpBridge.on("task", async ({ content }) => {
|
|
98
|
+
mcpBridge.on("task", async ({ content, meta }) => {
|
|
78
99
|
console.error("\n[bridge] Incoming task from MCP server. Forwarding to ACP agent...");
|
|
79
100
|
try {
|
|
101
|
+
const taskId = extractTaskIdFromMeta(meta);
|
|
102
|
+
const sessionId = await sessionSwitcher.ensureForTask(taskId);
|
|
80
103
|
const result = await connection.prompt({
|
|
81
104
|
sessionId,
|
|
82
105
|
prompt: [{ type: "text", text: content }],
|
|
@@ -88,7 +111,7 @@ async function main() {
|
|
|
88
111
|
}
|
|
89
112
|
});
|
|
90
113
|
// Initial check for a pending task
|
|
91
|
-
await checkForNextTask(mcpBridge, connection,
|
|
114
|
+
await checkForNextTask(mcpBridge, connection, sessionSwitcher);
|
|
92
115
|
// Keep the process alive
|
|
93
116
|
await new Promise(() => { });
|
|
94
117
|
}
|
|
@@ -105,7 +128,7 @@ async function main() {
|
|
|
105
128
|
* Checks for the next pending task using the 'getNextTask' tool on the MCP server.
|
|
106
129
|
* If found, sends it to the ACP agent.
|
|
107
130
|
*/
|
|
108
|
-
async function checkForNextTask(mcpBridge, connection,
|
|
131
|
+
export async function checkForNextTask(mcpBridge, connection, sessionSwitcher) {
|
|
109
132
|
console.error("[bridge] Checking for next task via MCP server...");
|
|
110
133
|
try {
|
|
111
134
|
const result = await mcpBridge.callTool("getNextTask");
|
|
@@ -118,14 +141,17 @@ async function checkForNextTask(mcpBridge, connection, sessionId) {
|
|
|
118
141
|
if (content &&
|
|
119
142
|
content.text &&
|
|
120
143
|
!content.text.includes("no pending tasks exist")) {
|
|
121
|
-
|
|
144
|
+
const text = content.text;
|
|
145
|
+
console.error(`[bridge] Found task: "${text.slice(0, 50).replace(/\n/g, " ")}..."`);
|
|
146
|
+
const taskId = extractTaskIdFromText(text);
|
|
147
|
+
const sessionId = await sessionSwitcher.ensureForTask(taskId);
|
|
122
148
|
const promptResult = await connection.prompt({
|
|
123
149
|
sessionId,
|
|
124
|
-
prompt: [{ type: "text", text
|
|
150
|
+
prompt: [{ type: "text", text }],
|
|
125
151
|
});
|
|
126
152
|
console.error(`\n[acp] Agent completed with: ${promptResult.stopReason}`);
|
|
127
153
|
// Recursively check for next task
|
|
128
|
-
await checkForNextTask(mcpBridge, connection,
|
|
154
|
+
await checkForNextTask(mcpBridge, connection, sessionSwitcher);
|
|
129
155
|
}
|
|
130
156
|
else {
|
|
131
157
|
console.error("[bridge] No pending tasks available.");
|
|
@@ -135,8 +161,10 @@ async function checkForNextTask(mcpBridge, connection, sessionId) {
|
|
|
135
161
|
console.error("[bridge] Failed to check for next task:", err);
|
|
136
162
|
}
|
|
137
163
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
164
|
+
if (process.env.NODE_ENV !== "test") {
|
|
165
|
+
main().catch((err) => {
|
|
166
|
+
console.error("[fatal]", err);
|
|
167
|
+
process.exit(1);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
142
170
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA;;;;;GAKG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,KAAK,GAAG,MAAM,0BAA0B,CAAC;AAChD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CACpB,YAAY,CAAC,IAAI,GAAG,CAAC,iBAAiB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,OAAO,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YAAY,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC;gBAC3C,SAAS;gBACT,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;aACjC,CAAC,CAAC;YAEH,OAAO,CAAC,KAAK,CAAC,iCAAiC,YAAY,CAAC,UAAU,EAAE,CAAC,CAAC;YAE1E,kCAAkC;YAClC,MAAM,gBAAgB,CAAC,SAAS,EAAE,UAAU,EAAE,eAAe,CAAC,CAAC;QACjE,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,sCAAsC,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,yCAAyC,EAAE,GAAG,CAAC,CAAC;IAChE,CAAC;AACH,CAAC;AAED,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;IACpC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QACnB,OAAO,CAAC,KAAK,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;QAC9B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/mcpClient.js
CHANGED
|
@@ -10,10 +10,13 @@ import { EventEmitter } from "node:events";
|
|
|
10
10
|
import { z } from "zod";
|
|
11
11
|
export class MCPBridge extends EventEmitter {
|
|
12
12
|
config;
|
|
13
|
-
client;
|
|
14
|
-
transport;
|
|
13
|
+
client = null;
|
|
14
|
+
transport = null;
|
|
15
|
+
isConnected = false;
|
|
16
|
+
isConnecting = false;
|
|
17
|
+
isClosed = false;
|
|
15
18
|
getSessionId() {
|
|
16
|
-
return this.transport
|
|
19
|
+
return this.transport?._sessionId;
|
|
17
20
|
}
|
|
18
21
|
constructor(config) {
|
|
19
22
|
super();
|
|
@@ -21,55 +24,60 @@ export class MCPBridge extends EventEmitter {
|
|
|
21
24
|
if (!config.url) {
|
|
22
25
|
throw new Error(`MCP server ${config.name} has no URL`);
|
|
23
26
|
}
|
|
24
|
-
const url = new URL(config.url);
|
|
25
|
-
this.transport = new StreamableHTTPClientTransport(url);
|
|
26
|
-
this.client = new Client({
|
|
27
|
-
name: "acp-gateway",
|
|
28
|
-
version: "0.1.0",
|
|
29
|
-
}, {
|
|
30
|
-
capabilities: {},
|
|
31
|
-
});
|
|
32
27
|
}
|
|
33
|
-
isConnected = false;
|
|
34
|
-
isConnecting = false;
|
|
35
28
|
async connect() {
|
|
36
29
|
if (this.isConnecting || this.isConnected)
|
|
37
30
|
return;
|
|
38
31
|
this.isConnecting = true;
|
|
32
|
+
this.isClosed = false;
|
|
39
33
|
let attempt = 0;
|
|
40
34
|
const initialDelay = 1000;
|
|
41
|
-
const maxDelay =
|
|
42
|
-
while (!this.isConnected) {
|
|
35
|
+
const maxDelay = 900000; // 15 minutes
|
|
36
|
+
while (!this.isConnected && !this.isClosed) {
|
|
43
37
|
try {
|
|
44
38
|
await this._connectOnce();
|
|
45
39
|
this.isConnected = true;
|
|
46
|
-
this.isConnecting = false;
|
|
47
40
|
console.error(`[mcp] Connected to ${this.config.name}`);
|
|
48
41
|
}
|
|
49
42
|
catch (error) {
|
|
43
|
+
if (this.isClosed)
|
|
44
|
+
break;
|
|
50
45
|
const delay = Math.min(initialDelay * Math.pow(2, attempt), maxDelay);
|
|
51
|
-
console.error(`[mcp] Connection failed to ${this.config.name}. Retrying in ${delay / 1000}s...`);
|
|
46
|
+
console.error(`[mcp] Connection failed to ${this.config.name} (attempt ${attempt + 1}): ${error.message || error}. Retrying in ${delay / 1000}s...`);
|
|
52
47
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
53
48
|
attempt++;
|
|
54
49
|
}
|
|
55
50
|
}
|
|
51
|
+
this.isConnecting = false;
|
|
56
52
|
}
|
|
57
53
|
async _connectOnce() {
|
|
54
|
+
// Clean up existing transport and client
|
|
58
55
|
if (this.transport) {
|
|
56
|
+
this.transport.onclose = undefined;
|
|
57
|
+
this.transport.onerror = undefined;
|
|
59
58
|
await this.transport.close().catch(() => { });
|
|
59
|
+
this.transport = null;
|
|
60
|
+
}
|
|
61
|
+
if (this.client) {
|
|
62
|
+
await this.client.close().catch(() => { });
|
|
63
|
+
this.client = null;
|
|
60
64
|
}
|
|
61
65
|
if (!this.config.url) {
|
|
62
66
|
throw new Error("MCP server URL is not configured");
|
|
63
67
|
}
|
|
64
68
|
const url = new URL(this.config.url);
|
|
65
69
|
this.transport = new StreamableHTTPClientTransport(url);
|
|
70
|
+
this.client = new Client({
|
|
71
|
+
name: "acp-gateway",
|
|
72
|
+
version: "0.1.0",
|
|
73
|
+
}, {
|
|
74
|
+
capabilities: {},
|
|
75
|
+
});
|
|
66
76
|
// Set up transport hooks for disconnection
|
|
67
77
|
this.transport.onclose = () => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this.connect(); // Start reconnection loop
|
|
72
|
-
}
|
|
78
|
+
console.error(`[mcp] Connection to ${this.config.name} lost.`);
|
|
79
|
+
this.isConnected = false;
|
|
80
|
+
this.connect(); // Start reconnection loop
|
|
73
81
|
};
|
|
74
82
|
this.transport.onerror = (error) => {
|
|
75
83
|
console.error(`[mcp] Transport error:`, error.message || error);
|
|
@@ -104,7 +112,10 @@ export class MCPBridge extends EventEmitter {
|
|
|
104
112
|
if (this.isConnected)
|
|
105
113
|
return;
|
|
106
114
|
if (!this.isConnecting) {
|
|
107
|
-
|
|
115
|
+
// Fire and forget connect loop if not already running
|
|
116
|
+
this.connect().catch((err) => {
|
|
117
|
+
console.error(`[mcp] Unexpected error in connect loop:`, err);
|
|
118
|
+
});
|
|
108
119
|
}
|
|
109
120
|
// Wait up to 10 seconds for connection
|
|
110
121
|
let waited = 0;
|
|
@@ -118,6 +129,8 @@ export class MCPBridge extends EventEmitter {
|
|
|
118
129
|
}
|
|
119
130
|
async callTool(name, args = {}) {
|
|
120
131
|
await this.ensureConnected();
|
|
132
|
+
if (!this.client)
|
|
133
|
+
throw new Error("MCP client not initialized");
|
|
121
134
|
return await this.client.callTool({
|
|
122
135
|
name,
|
|
123
136
|
arguments: args,
|
|
@@ -125,13 +138,26 @@ export class MCPBridge extends EventEmitter {
|
|
|
125
138
|
}
|
|
126
139
|
async sendNotification(method, params) {
|
|
127
140
|
await this.ensureConnected();
|
|
141
|
+
if (!this.client)
|
|
142
|
+
throw new Error("MCP client not initialized");
|
|
128
143
|
await this.client.notification({
|
|
129
144
|
method,
|
|
130
145
|
params,
|
|
131
146
|
});
|
|
132
147
|
}
|
|
133
148
|
async close() {
|
|
134
|
-
|
|
149
|
+
this.isClosed = true;
|
|
150
|
+
this.isConnected = false;
|
|
151
|
+
if (this.transport) {
|
|
152
|
+
this.transport.onclose = undefined;
|
|
153
|
+
this.transport.onerror = undefined;
|
|
154
|
+
await this.transport.close().catch(() => { });
|
|
155
|
+
this.transport = null;
|
|
156
|
+
}
|
|
157
|
+
if (this.client) {
|
|
158
|
+
await this.client.close().catch(() => { });
|
|
159
|
+
this.client = null;
|
|
160
|
+
}
|
|
135
161
|
}
|
|
136
162
|
}
|
|
137
163
|
//# sourceMappingURL=mcpClient.js.map
|
package/dist/mcpClient.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcpClient.js","sourceRoot":"","sources":["../src/mcpClient.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AACnG,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,MAAM,OAAO,SAAU,SAAQ,YAAY;
|
|
1
|
+
{"version":3,"file":"mcpClient.js","sourceRoot":"","sources":["../src/mcpClient.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA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YAC7C,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACxB,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;YAC1C,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACrB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Task identity from MCP channel `meta` for ACP session switching (agentrq `chat_id`).
|
|
3
|
+
*/
|
|
4
|
+
/** Task identity from `notifications/claude/channel` `meta` (agentrq uses `chat_id`). */
|
|
5
|
+
export function extractTaskIdFromMeta(meta) {
|
|
6
|
+
if (!meta || typeof meta !== "object")
|
|
7
|
+
return undefined;
|
|
8
|
+
const m = meta;
|
|
9
|
+
if (typeof m.chat_id === "string" && m.chat_id.length > 0)
|
|
10
|
+
return m.chat_id;
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
/** Try to extract taskId from text content as a fallback. */
|
|
14
|
+
export function extractTaskIdFromText(text) {
|
|
15
|
+
// Try to match "Task ID: <id>" or "Response to task <id>" or just "task <id>"
|
|
16
|
+
const patterns = [
|
|
17
|
+
/Task ID[:\s]+([a-zA-Z0-9_-]+)/i,
|
|
18
|
+
/Response to task[:\s]+([a-zA-Z0-9_-]+)/i,
|
|
19
|
+
/task[:\s]+([a-zA-Z0-9_-]+)/i,
|
|
20
|
+
];
|
|
21
|
+
for (const pattern of patterns) {
|
|
22
|
+
const match = text.match(pattern);
|
|
23
|
+
if (match && match[1])
|
|
24
|
+
return match[1];
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=taskIdentity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"taskIdentity.js","sourceRoot":"","sources":["../src/taskIdentity.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,yFAAyF;AACzF,MAAM,UAAU,qBAAqB,CAAC,IAAa;IACjD,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IACxD,MAAM,CAAC,GAAG,IAA+B,CAAC;IAC1C,IAAI,OAAO,CAAC,CAAC,OAAO,KAAK,QAAQ,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC,OAAO,CAAC;IAC5E,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,8EAA8E;IAC9E,MAAM,QAAQ,GAAG;QACf,gCAAgC;QAChC,yCAAyC;QACzC,6BAA6B;KAC9B,CAAC;IACF,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAClC,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;IACzC,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentrq/acp-gateway",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "ACP+MCP bridge CLI for agentrq workspaces",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"dev": "tsx src/index.ts",
|
|
16
16
|
"typecheck": "tsc --noEmit",
|
|
17
17
|
"test": "vitest run",
|
|
18
|
+
"test:coverage": "vitest run --coverage",
|
|
18
19
|
"prepare": "npm run build",
|
|
19
20
|
"prepublishOnly": "npm run build"
|
|
20
21
|
},
|