@thinkwell/acp 0.1.0

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-over-acp-handler.js","sourceRoot":"","sources":["../src/mcp-over-acp-handler.ts"],"names":[],"mappings":"AA0BA;;;;;;;;;;GAUG;AACH,MAAM,OAAO,iBAAiB;IAC5B,mDAAmD;IAClC,aAAa,GAA2B,IAAI,GAAG,EAAE,CAAC;IACnE,gDAAgD;IAC/B,YAAY,GAA+B,IAAI,GAAG,EAAE,CAAC;IACtE,qCAAqC;IAC7B,UAAU,GAAW,EAAE,CAAC;IAChC,2DAA2D;IAC1C,wBAAwB,GAAqC,IAAI,GAAG,EAAE,CAAC;IAExF;;OAEG;IACH,QAAQ,CAAC,MAAiB;QACxB,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChD,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,MAAiB;QAC1B,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,YAAY,CAAC,SAAiB;QAC5B,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED;;OAEG;IACH,aAAa,CAAC,MAA+B;QAC3C,oDAAoD;QACpD,qDAAqD;QACrD,MAAM,YAAY,GAAI,MAAM,CAAC,YAAuB,IAAI,QAAQ,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;QACpH,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,GAAG,CAAW,CAAC;QAErD,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CA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AAM,KAAK,GAAG,IAAI,CAAC,wBAAwB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC3D,IAAI,KAAK,EAAE,CAAC;YACV,KAAK,CAAC,OAAO,EAAE,CAAC;QAClB,CAAC;IACH,CAAC;IAED;;OAEG;IACH,gBAAgB,CAAC,MAA+B;QAC9C,MAAM,YAAY,GAAG,MAAM,CAAC,YAAsB,CAAC;QACnD,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IACzC,CAAC;IAED;;;OAGG;IACH,YAAY,CAAC,MAAc;QACzB,OAAO,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,YAAY,CAChB,MAAc,EACd,MAA+B;QAE/B,QAAQ,MAAM,EAAE,CAAC;YACf,KAAK,aAAa;gBAChB,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;YACpC,KAAK,aAAa;gBAChB,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;YACpC,KAAK,gBAAgB;gBACnB,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;gBAC9B,OAAO,SAAS,CAAC;YACnB;gBACE,MAAM,IAAI,KAAK,CAAC,gCAAgC,MAAM,EAAE,CAAC,CAAC;QAC9D,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,56 @@
1
+ import type { JsonSchema, McpContext, McpServerConfig, McpToolDefinition, RegisteredTool, ToolHandler } from "./types.js";
2
+ /**
3
+ * Fluent builder for creating MCP servers with registered tools
4
+ */
5
+ export declare class McpServerBuilder {
6
+ private _name;
7
+ private _instructions;
8
+ private _tools;
9
+ constructor(name: string);
10
+ /**
11
+ * Set instructions for the MCP server
12
+ */
13
+ instructions(text: string): this;
14
+ /**
15
+ * Register a tool with input/output JSON schemas
16
+ */
17
+ tool<I, O>(name: string, description: string, inputSchema: JsonSchema, outputSchema: JsonSchema, handler: ToolHandler<I, O>): this;
18
+ /**
19
+ * Build the MCP server
20
+ */
21
+ build(): McpServer;
22
+ }
23
+ /**
24
+ * MCP server that handles tool calls via MCP-over-ACP
25
+ */
26
+ export declare class McpServer {
27
+ readonly id: string;
28
+ readonly name: string;
29
+ private readonly _instructions;
30
+ private readonly _tools;
31
+ constructor(name: string, instructions: string | undefined, tools: Map<string, RegisteredTool>);
32
+ /**
33
+ * Get the acp: URL for this server
34
+ */
35
+ get acpUrl(): string;
36
+ /**
37
+ * Get MCP server config for session requests
38
+ */
39
+ toSessionConfig(): McpServerConfig;
40
+ /**
41
+ * Get the list of tool definitions for tools/list
42
+ */
43
+ getToolDefinitions(): McpToolDefinition[];
44
+ /**
45
+ * Handle an MCP method call or notification
46
+ */
47
+ handleMethod(method: string, params: unknown, context: McpContext): Promise<unknown>;
48
+ private handleInitialize;
49
+ private handleToolsList;
50
+ private handleToolsCall;
51
+ }
52
+ /**
53
+ * Create a new MCP server builder
54
+ */
55
+ export declare function mcpServer(name: string): McpServerBuilder;
56
+ //# sourceMappingURL=mcp-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-server.d.ts","sourceRoot":"","sources":["../src/mcp-server.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,UAAU,EACV,UAAU,EACV,eAAe,EACf,iBAAiB,EAIjB,cAAc,EACd,WAAW,EACZ,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,MAAM,CAA0C;gBAE5C,IAAI,EAAE,MAAM;IAIxB;;OAEG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAKhC;;OAEG;IACH,IAAI,CAAC,CAAC,EAAE,CAAC,EACP,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,UAAU,EACvB,YAAY,EAAE,UAAU,EACxB,OAAO,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,GACzB,IAAI;IAWP;;OAEG;IACH,KAAK,IAAI,SAAS;CAOnB;AAED;;GAEG;AACH,qBAAa,SAAS;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA8B;gBAGnD,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,GAAG,SAAS,EAChC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC;IAQpC;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;OAEG;IACH,eAAe,IAAI,eAAe;IAQlC;;OAEG;IACH,kBAAkB,IAAI,iBAAiB,EAAE;IAQzC;;OAEG;IACG,YAAY,CAChB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,OAAO,EACf,OAAO,EAAE,UAAU,GAClB,OAAO,CAAC,OAAO,CAAC;IAgBnB,OAAO,CAAC,gBAAgB;IAsBxB,OAAO,CAAC,eAAe;YAMT,eAAe;CA2B9B;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,CAExD"}
@@ -0,0 +1,148 @@
1
+ import { v4 as uuidv4 } from "uuid";
2
+ /**
3
+ * Fluent builder for creating MCP servers with registered tools
4
+ */
5
+ export class McpServerBuilder {
6
+ _name;
7
+ _instructions;
8
+ _tools = new Map();
9
+ constructor(name) {
10
+ this._name = name;
11
+ }
12
+ /**
13
+ * Set instructions for the MCP server
14
+ */
15
+ instructions(text) {
16
+ this._instructions = text;
17
+ return this;
18
+ }
19
+ /**
20
+ * Register a tool with input/output JSON schemas
21
+ */
22
+ tool(name, description, inputSchema, outputSchema, handler) {
23
+ this._tools.set(name, {
24
+ name,
25
+ description,
26
+ inputSchema,
27
+ outputSchema,
28
+ handler: handler,
29
+ });
30
+ return this;
31
+ }
32
+ /**
33
+ * Build the MCP server
34
+ */
35
+ build() {
36
+ return new McpServer(this._name, this._instructions, new Map(this._tools));
37
+ }
38
+ }
39
+ /**
40
+ * MCP server that handles tool calls via MCP-over-ACP
41
+ */
42
+ export class McpServer {
43
+ id;
44
+ name;
45
+ _instructions;
46
+ _tools;
47
+ constructor(name, instructions, tools) {
48
+ this.id = uuidv4();
49
+ this.name = name;
50
+ this._instructions = instructions;
51
+ this._tools = tools;
52
+ }
53
+ /**
54
+ * Get the acp: URL for this server
55
+ */
56
+ get acpUrl() {
57
+ return `acp:${this.id}`;
58
+ }
59
+ /**
60
+ * Get MCP server config for session requests
61
+ */
62
+ toSessionConfig() {
63
+ return {
64
+ type: "http",
65
+ name: this.name,
66
+ url: this.acpUrl,
67
+ };
68
+ }
69
+ /**
70
+ * Get the list of tool definitions for tools/list
71
+ */
72
+ getToolDefinitions() {
73
+ return Array.from(this._tools.values()).map((tool) => ({
74
+ name: tool.name,
75
+ description: tool.description,
76
+ inputSchema: tool.inputSchema,
77
+ }));
78
+ }
79
+ /**
80
+ * Handle an MCP method call or notification
81
+ */
82
+ async handleMethod(method, params, context) {
83
+ switch (method) {
84
+ case "tools/list":
85
+ return this.handleToolsList();
86
+ case "tools/call":
87
+ return this.handleToolsCall(params, context);
88
+ case "initialize":
89
+ return this.handleInitialize();
90
+ case "notifications/initialized":
91
+ // Client notification after initialize - no response needed
92
+ return undefined;
93
+ default:
94
+ throw new Error(`Unknown MCP method: ${method}`);
95
+ }
96
+ }
97
+ handleInitialize() {
98
+ // Use protocol version 2025-03-26 to match rmcp's behavior
99
+ // Always include instructions to help Claude Code understand how to use tools
100
+ const instructions = this._instructions ?? "You have access to tools. Call return_result when done.";
101
+ return {
102
+ protocolVersion: "2025-03-26",
103
+ serverInfo: {
104
+ name: this.name,
105
+ version: "0.1.0",
106
+ },
107
+ capabilities: {
108
+ tools: {},
109
+ },
110
+ instructions,
111
+ };
112
+ }
113
+ handleToolsList() {
114
+ return {
115
+ tools: this.getToolDefinitions(),
116
+ };
117
+ }
118
+ async handleToolsCall(params, context) {
119
+ const tool = this._tools.get(params.name);
120
+ if (!tool) {
121
+ return {
122
+ content: [{ type: "text", text: `Unknown tool: ${params.name}` }],
123
+ isError: true,
124
+ };
125
+ }
126
+ try {
127
+ const result = await tool.handler(params.arguments, context);
128
+ const resultText = typeof result === "string" ? result : JSON.stringify(result);
129
+ return {
130
+ content: [{ type: "text", text: resultText }],
131
+ };
132
+ }
133
+ catch (error) {
134
+ const message = error instanceof Error ? error.message : String(error);
135
+ return {
136
+ content: [{ type: "text", text: `Tool error: ${message}` }],
137
+ isError: true,
138
+ };
139
+ }
140
+ }
141
+ }
142
+ /**
143
+ * Create a new MCP server builder
144
+ */
145
+ export function mcpServer(name) {
146
+ return new McpServerBuilder(name);
147
+ }
148
+ //# sourceMappingURL=mcp-server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-server.js","sourceRoot":"","sources":["../src/mcp-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAapC;;GAEG;AACH,MAAM,OAAO,gBAAgB;IACnB,KAAK,CAAS;IACd,aAAa,CAAqB;IAClC,MAAM,GAAgC,IAAI,GAAG,EAAE,CAAC;IAExD,YAAY,IAAY;QACtB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED;;OAEG;IACH,YAAY,CAAC,IAAY;QACvB,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,IAAI,CACF,IAAY,EACZ,WAAmB,EACnB,WAAuB,EACvB,YAAwB,EACxB,OAA0B;QAE1B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE;YACpB,IAAI;YACJ,WAAW;YACX,WAAW;YACX,YAAY;YACZ,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AU,GACd,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;YAC/D,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;aAC9C,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACvE,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,eAAe,OAAO,EAAE,EAAE,CAAC;gBAC3D,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,IAAY;IACpC,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,112 @@
1
+ import type { McpServer } from "./mcp-server.js";
2
+ import type { McpOverAcpHandler } from "./mcp-over-acp-handler.js";
3
+ import type { SacpConnection } from "./connection.js";
4
+ /**
5
+ * Session message from the agent
6
+ */
7
+ export type SessionUpdate = {
8
+ type: "text";
9
+ content: string;
10
+ } | {
11
+ type: "tool_use";
12
+ id: string;
13
+ name: string;
14
+ input: unknown;
15
+ } | {
16
+ type: "stop";
17
+ reason: string;
18
+ };
19
+ /**
20
+ * Prompt message to send to the agent
21
+ */
22
+ export interface PromptMessage {
23
+ role: "user" | "assistant";
24
+ content: string;
25
+ }
26
+ /**
27
+ * Active session with an agent
28
+ */
29
+ export declare class ActiveSession {
30
+ readonly sessionId: string;
31
+ private readonly _connection;
32
+ private readonly _mcpHandler;
33
+ private _pendingUpdates;
34
+ private _updateResolvers;
35
+ private _closed;
36
+ constructor(sessionId: string, connection: SacpConnection, mcpHandler: McpOverAcpHandler);
37
+ /**
38
+ * Send a prompt to the agent
39
+ */
40
+ sendPrompt(content: string): Promise<void>;
41
+ /**
42
+ * Push an update received from the agent
43
+ */
44
+ pushUpdate(update: SessionUpdate): void;
45
+ /**
46
+ * Read the next update from the agent
47
+ */
48
+ readUpdate(): Promise<SessionUpdate>;
49
+ /**
50
+ * Read all updates until completion, returning concatenated text
51
+ */
52
+ readToString(): Promise<string>;
53
+ /**
54
+ * Mark the session as closed
55
+ */
56
+ close(): void;
57
+ }
58
+ /**
59
+ * Options for waiting for MCP readiness
60
+ */
61
+ export interface McpReadyOptions {
62
+ /**
63
+ * Whether to wait for the agent to call tools/list before proceeding.
64
+ * Default: true when MCP servers are attached
65
+ */
66
+ enabled?: boolean;
67
+ /**
68
+ * Maximum time to wait for tools discovery in milliseconds.
69
+ * Default: 2000ms
70
+ */
71
+ timeout?: number;
72
+ }
73
+ /**
74
+ * Builder for creating ACP sessions with MCP servers
75
+ */
76
+ export declare class SessionBuilder {
77
+ private readonly _connection;
78
+ private readonly _mcpHandler;
79
+ private _mcpServers;
80
+ private _cwd;
81
+ private _systemPrompt;
82
+ private _mcpReadyOptions;
83
+ constructor(connection: SacpConnection, mcpHandler: McpOverAcpHandler);
84
+ /**
85
+ * Attach an MCP server to this session
86
+ */
87
+ withMcpServer(server: McpServer): this;
88
+ /**
89
+ * Set the working directory for the session
90
+ */
91
+ cwd(path: string): this;
92
+ /**
93
+ * Set the system prompt for the session
94
+ */
95
+ systemPrompt(prompt: string): this;
96
+ /**
97
+ * Configure waiting for MCP tools discovery.
98
+ *
99
+ * By default, when MCP servers are attached, the session will wait for
100
+ * the agent to call tools/list before invoking the callback. This prevents
101
+ * a race condition where the prompt is sent before the agent knows about
102
+ * available tools.
103
+ *
104
+ * @param options - Options for MCP readiness waiting
105
+ */
106
+ waitForMcpReady(options?: McpReadyOptions): this;
107
+ /**
108
+ * Start the session and run a callback
109
+ */
110
+ run<T>(callback: (session: ActiveSession) => Promise<T>): Promise<T>;
111
+ }
112
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAC9D;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAErC;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,qBAAa,aAAa;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAiB;IAC7C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAChD,OAAO,CAAC,eAAe,CAAuB;IAC9C,OAAO,CAAC,gBAAgB,CAA8C;IACtE,OAAO,CAAC,OAAO,CAAkB;gBAG/B,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,cAAc,EAC1B,UAAU,EAAE,iBAAiB;IAQ/B;;OAEG;IACG,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQhD;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI;IASvC;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,aAAa,CAAC;IAc1C;;OAEG;IACG,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;IAiBrC;;OAEG;IACH,KAAK,IAAI,IAAI;CAQd;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAiB;IAC7C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAChD,OAAO,CAAC,WAAW,CAAmB;IACtC,OAAO,CAAC,IAAI,CAAqB;IACjC,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,gBAAgB,CAAuB;gBAEnC,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,iBAAiB;IAKrE;;OAEG;IACH,aAAa,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI;IAMtC;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAKvB;;OAEG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAKlC;;;;;;;;;OASG;IACH,eAAe,CAAC,OAAO,GAAE,eAAoB,GAAG,IAAI;IAKpD;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAqC3E"}
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Active session with an agent
3
+ */
4
+ export class ActiveSession {
5
+ sessionId;
6
+ _connection;
7
+ _mcpHandler;
8
+ _pendingUpdates = [];
9
+ _updateResolvers = [];
10
+ _closed = false;
11
+ constructor(sessionId, connection, mcpHandler) {
12
+ this.sessionId = sessionId;
13
+ this._connection = connection;
14
+ this._mcpHandler = mcpHandler;
15
+ this._mcpHandler.setSessionId(sessionId);
16
+ }
17
+ /**
18
+ * Send a prompt to the agent
19
+ */
20
+ async sendPrompt(content) {
21
+ const response = await this._connection.sendPrompt(this.sessionId, content);
22
+ // Push a stop update when the prompt completes
23
+ if (response.stopReason) {
24
+ this.pushUpdate({ type: "stop", reason: response.stopReason });
25
+ }
26
+ }
27
+ /**
28
+ * Push an update received from the agent
29
+ */
30
+ pushUpdate(update) {
31
+ if (this._updateResolvers.length > 0) {
32
+ const resolver = this._updateResolvers.shift();
33
+ resolver(update);
34
+ }
35
+ else {
36
+ this._pendingUpdates.push(update);
37
+ }
38
+ }
39
+ /**
40
+ * Read the next update from the agent
41
+ */
42
+ async readUpdate() {
43
+ if (this._pendingUpdates.length > 0) {
44
+ return this._pendingUpdates.shift();
45
+ }
46
+ if (this._closed) {
47
+ return { type: "stop", reason: "session_closed" };
48
+ }
49
+ return new Promise((resolve) => {
50
+ this._updateResolvers.push(resolve);
51
+ });
52
+ }
53
+ /**
54
+ * Read all updates until completion, returning concatenated text
55
+ */
56
+ async readToString() {
57
+ const parts = [];
58
+ while (true) {
59
+ const update = await this.readUpdate();
60
+ if (update.type === "text") {
61
+ parts.push(update.content);
62
+ }
63
+ else if (update.type === "stop") {
64
+ break;
65
+ }
66
+ // tool_use updates are handled by the connection layer
67
+ }
68
+ return parts.join("");
69
+ }
70
+ /**
71
+ * Mark the session as closed
72
+ */
73
+ close() {
74
+ this._closed = true;
75
+ // Resolve any pending readers
76
+ for (const resolver of this._updateResolvers) {
77
+ resolver({ type: "stop", reason: "session_closed" });
78
+ }
79
+ this._updateResolvers = [];
80
+ }
81
+ }
82
+ /**
83
+ * Builder for creating ACP sessions with MCP servers
84
+ */
85
+ export class SessionBuilder {
86
+ _connection;
87
+ _mcpHandler;
88
+ _mcpServers = [];
89
+ _cwd;
90
+ _systemPrompt;
91
+ _mcpReadyOptions = {};
92
+ constructor(connection, mcpHandler) {
93
+ this._connection = connection;
94
+ this._mcpHandler = mcpHandler;
95
+ }
96
+ /**
97
+ * Attach an MCP server to this session
98
+ */
99
+ withMcpServer(server) {
100
+ this._mcpServers.push(server);
101
+ this._mcpHandler.register(server);
102
+ return this;
103
+ }
104
+ /**
105
+ * Set the working directory for the session
106
+ */
107
+ cwd(path) {
108
+ this._cwd = path;
109
+ return this;
110
+ }
111
+ /**
112
+ * Set the system prompt for the session
113
+ */
114
+ systemPrompt(prompt) {
115
+ this._systemPrompt = prompt;
116
+ return this;
117
+ }
118
+ /**
119
+ * Configure waiting for MCP tools discovery.
120
+ *
121
+ * By default, when MCP servers are attached, the session will wait for
122
+ * the agent to call tools/list before invoking the callback. This prevents
123
+ * a race condition where the prompt is sent before the agent knows about
124
+ * available tools.
125
+ *
126
+ * @param options - Options for MCP readiness waiting
127
+ */
128
+ waitForMcpReady(options = {}) {
129
+ this._mcpReadyOptions = options;
130
+ return this;
131
+ }
132
+ /**
133
+ * Start the session and run a callback
134
+ */
135
+ async run(callback) {
136
+ // Create the session
137
+ const sessionId = await this._connection.createSession({
138
+ cwd: this._cwd,
139
+ systemPrompt: this._systemPrompt,
140
+ mcpServers: this._mcpServers.map((s) => s.toSessionConfig()),
141
+ });
142
+ const session = new ActiveSession(sessionId, this._connection, this._mcpHandler);
143
+ // Set up the message pump for this session
144
+ this._connection.setSessionHandler(sessionId, session);
145
+ try {
146
+ // Wait for MCP tools discovery if we have MCP servers attached
147
+ const shouldWait = this._mcpServers.length > 0 &&
148
+ (this._mcpReadyOptions.enabled !== false);
149
+ if (shouldWait) {
150
+ const timeout = this._mcpReadyOptions.timeout ?? 2000;
151
+ await this._mcpHandler.waitForToolsDiscovery(sessionId, timeout);
152
+ }
153
+ return await callback(session);
154
+ }
155
+ finally {
156
+ session.close();
157
+ this._connection.removeSessionHandler(sessionId);
158
+ // Unregister MCP servers
159
+ for (const server of this._mcpServers) {
160
+ this._mcpHandler.unregister(server);
161
+ }
162
+ }
163
+ }
164
+ }
165
+ //# sourceM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