@salesforce/sdk-chat 1.60.2 → 1.61.1

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/dist/index.d.ts CHANGED
@@ -1,15 +1,21 @@
1
- import type { ChatSDK, SDKOptions } from "@salesforce/sdk-core";
1
+ import type { ChatSDK, SDKOptions, McpAppsSessionOptions } from "@salesforce/sdk-core";
2
2
  /**
3
3
  * Options for creating a ChatSDK instance.
4
4
  */
5
5
  export interface ChatSDKOptions extends SDKOptions {
6
+ /** MCP Apps session options (app identity, handshake timeout) */
7
+ mcpApps?: McpAppsSessionOptions;
6
8
  }
7
9
  /**
8
10
  * Create and initialize a ChatSDK instance based on the detected surface
9
11
  *
12
+ * For MCP Apps surfaces, this performs the SEP-1865 handshake (ui/initialize)
13
+ * via the shared {@link McpAppsSession} singleton on the first call.
14
+ *
10
15
  * @param options - Optional configuration including surface override
11
16
  * @returns Promise resolving to an initialized ChatSDK instance
12
17
  */
13
18
  export declare function createChatSDK(options?: ChatSDKOptions): Promise<ChatSDK>;
19
+ export { MCPAppsChatSDK } from "./mcpapps.js";
14
20
  export type { ChatMessage, ChatSDK, DisplayMode, ResourceRequest, SDKOptions, ToolCall, ToolState, } from "@salesforce/sdk-core";
15
21
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAKhE;;GAEG;AAEH,MAAM,WAAW,cAAe,SAAQ,UAAU;CAAG;AAErD;;;;;GAKG;AACH,wBAAsB,aAAa,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAe9E;AAED,YAAY,EACX,WAAW,EACX,OAAO,EACP,WAAW,EACX,eAAe,EACf,UAAU,EACV,QAAQ,EACR,SAAS,GACT,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAKvF;;GAEG;AACH,MAAM,WAAW,cAAe,SAAQ,UAAU;IACjD,iEAAiE;IACjE,OAAO,CAAC,EAAE,qBAAqB,CAAC;CAChC;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAyB9E;AAED,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,YAAY,EACX,WAAW,EACX,OAAO,EACP,WAAW,EACX,eAAe,EACf,UAAU,EACV,QAAQ,EACR,SAAS,GACT,MAAM,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -3,13 +3,16 @@
3
3
  * All rights reserved.
4
4
  * For full license text, see the LICENSE.txt file
5
5
  */
6
- import { getSurface, Surface } from "@salesforce/sdk-core";
6
+ import { getSurface, Surface, McpAppsSession } from "@salesforce/sdk-core";
7
7
  import { MCPAppsChatSDK } from "./mcpapps.js";
8
8
  import { OpenAIChatSDK } from "./openai.js";
9
9
  import { SalesforceACCChatSDK } from "./salesforce-acc.js";
10
10
  /**
11
11
  * Create and initialize a ChatSDK instance based on the detected surface
12
12
  *
13
+ * For MCP Apps surfaces, this performs the SEP-1865 handshake (ui/initialize)
14
+ * via the shared {@link McpAppsSession} singleton on the first call.
15
+ *
13
16
  * @param options - Optional configuration including surface override
14
17
  * @returns Promise resolving to an initialized ChatSDK instance
15
18
  */
@@ -23,9 +26,20 @@ export async function createChatSDK(options) {
23
26
  return {};
24
27
  case Surface.SalesforceACC:
25
28
  return new SalesforceACCChatSDK();
26
- case Surface.MCPApps:
27
- return new MCPAppsChatSDK();
29
+ case Surface.MCPApps: {
30
+ const session = await McpAppsSession.getInstance(options?.mcpApps);
31
+ if (session.handshakeSucceeded) {
32
+ return new MCPAppsChatSDK(session);
33
+ }
34
+ // Handshake timed out — not a real MCP Apps host.
35
+ // Fall back to OpenAI if available, otherwise empty.
36
+ if (window.openai) {
37
+ return new OpenAIChatSDK();
38
+ }
39
+ return {};
40
+ }
28
41
  default:
29
42
  return {};
30
43
  }
31
44
  }
45
+ export { MCPAppsChatSDK } from "./mcpapps.js";
package/dist/mcpapps.d.ts CHANGED
@@ -4,19 +4,24 @@
4
4
  * For full license text, see the LICENSE.txt file
5
5
  */
6
6
  import type { ChatSDK, ChatMessage, ResourceRequest, ToolCall, ToolState, DisplayMode } from "@salesforce/sdk-core";
7
- import { JsonRpcClient } from "@salesforce/sdk-core";
7
+ import type { McpAppsSession } from "@salesforce/sdk-core";
8
8
  /**
9
- * Chat SDK implementation for Webapp and MicroFrontend surfaces using MCP Apps protocol
9
+ * Chat SDK implementation for MCP Apps surface using the shared McpAppsSession.
10
+ *
10
11
  * Supports: sendMessageToHost, callTool, readResource, accessToolInput, accessToolOutput,
11
12
  * accessToolMetadata (stub), onToolCanceled, subscribe
12
- * Extends JsonRpcClient for JSON-RPC 2.0 notification support
13
+ *
14
+ * Uses a shared {@link McpAppsSession} for JSON-RPC 2.0 transport instead of
15
+ * owning its own postMessage listener, avoiding duplicate listeners and ID
16
+ * counter collisions when used alongside MCPAppsViewSDK.
13
17
  */
14
- export declare class MCPAppsChatSDK extends JsonRpcClient implements ChatSDK {
18
+ export declare class MCPAppsChatSDK implements ChatSDK {
19
+ private session;
15
20
  private chatHostContext;
16
21
  private globals;
17
22
  private canceledCallbacks;
18
23
  private subscriptions;
19
- constructor();
24
+ constructor(session: McpAppsSession);
20
25
  /**
21
26
  * Handle host context change notifications from the host
22
27
  * @param params - Partial host context update from the host
@@ -53,5 +58,6 @@ export declare class MCPAppsChatSDK extends JsonRpcClient implements ChatSDK {
53
58
  accessToolMetadata<T>(): T | null;
54
59
  onToolCanceled(callback: (reason: string) => void): () => void;
55
60
  subscribe(callback: () => void): () => void;
61
+ private notifySubscribers;
56
62
  }
57
63
  //# sourceMappingURL=mcpapps.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mcpapps.d.ts","sourceRoot":"","sources":["../src/mcpapps.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EACX,OAAO,EACP,WAAW,EACX,eAAe,EACf,QAAQ,EACR,SAAS,EACT,WAAW,EACX,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAarD;;;;;GAKG;AACH,qBAAa,cAAe,SAAQ,aAAc,YAAW,OAAO;IACnE,OAAO,CAAC,eAAe,CAAuB;IAC9C,OAAO,CAAC,OAAO,CAGR;IACP,OAAO,CAAC,iBAAiB,CAAuC;IAChE,OAAO,CAAC,aAAa,CAAyB;;IA0B9C;;;OAGG;IACH,OAAO,CAAC,wBAAwB;IAchC;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IASxB;;;OAGG;IACH,OAAO,CAAC,eAAe;IAUvB;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAQ3B;;;;OAIG;IACG,cAAc,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,WAAW,CAAA;KAAE,CAAC;IAOjE,iBAAiB,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAUtD,QAAQ,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,CAAC,CAAC;IAQ3C,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,CAAC,CAAC;IAO3D,eAAe,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI;IAMzC,gBAAgB,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI;IAM1C,kBAAkB,CAAC,CAAC,KAAK,CAAC,GAAG,IAAI;IAKjC,cAAc,CAAC,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SAAS,CAAC,CAAC,CAAC,GAAG,IAAI;IAM1C,kBAAkB,CAAC,CAAC,KAAK,CAAC,GAAG,IAAI;IAKjC,cAAc,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI;IAS9D,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAQ3C,OAAO,CAAC,iBAAiB;CAGzB"}
package/dist/mcpapps.js CHANGED
@@ -1,25 +1,26 @@
1
- import { JsonRpcClient } from "@salesforce/sdk-core";
2
1
  /**
3
- * Chat SDK implementation for Webapp and MicroFrontend surfaces using MCP Apps protocol
2
+ * Chat SDK implementation for MCP Apps surface using the shared McpAppsSession.
3
+ *
4
4
  * Supports: sendMessageToHost, callTool, readResource, accessToolInput, accessToolOutput,
5
5
  * accessToolMetadata (stub), onToolCanceled, subscribe
6
- * Extends JsonRpcClient for JSON-RPC 2.0 notification support
6
+ *
7
+ * Uses a shared {@link McpAppsSession} for JSON-RPC 2.0 transport instead of
8
+ * owning its own postMessage listener, avoiding duplicate listeners and ID
9
+ * counter collisions when used alongside MCPAppsViewSDK.
7
10
  */
8
- export class MCPAppsChatSDK extends JsonRpcClient {
11
+ export class MCPAppsChatSDK {
12
+ session;
9
13
  chatHostContext = {};
10
14
  globals = {};
11
15
  canceledCallbacks = new Set();
12
16
  subscriptions = new Set();
13
- constructor() {
14
- super(); // Sets up JSON-RPC message listener
15
- // Register notification handler for display mode
16
- this.registerNotificationHandler("ui/notifications/host-context-changed", (params) => this.handleHostContextChanged(params));
17
- // Register notification handler for tool results
18
- this.registerNotificationHandler("ui/notifications/tool-result", (params) => this.handleToolResult(params));
19
- // Register notification handler for tool input
20
- this.registerNotificationHandler("ui/notifications/tool-input", (params) => this.handleToolInput(params));
21
- // Register notification handler for tool cancelled
22
- this.registerNotificationHandler("ui/notifications/tool-cancelled", (params) => this.handleToolCancelled(params));
17
+ constructor(session) {
18
+ this.session = session;
19
+ // Register notification handlers on the shared session
20
+ session.registerNotificationHandler("ui/notifications/host-context-changed", (params) => this.handleHostContextChanged(params));
21
+ session.registerNotificationHandler("ui/notifications/tool-result", (params) => this.handleToolResult(params));
22
+ session.registerNotificationHandler("ui/notifications/tool-input", (params) => this.handleToolInput(params));
23
+ session.registerNotificationHandler("ui/notifications/tool-cancelled", (params) => this.handleToolCancelled(params));
23
24
  }
24
25
  /**
25
26
  * Handle host context change notifications from the host
@@ -28,12 +29,12 @@ export class MCPAppsChatSDK extends JsonRpcClient {
28
29
  handleHostContextChanged(params) {
29
30
  const contextUpdate = params;
30
31
  if (contextUpdate.displayMode) {
31
- // Update stored host context
32
32
  this.chatHostContext = {
33
33
  ...this.chatHostContext,
34
34
  displayMode: contextUpdate.displayMode,
35
35
  };
36
36
  }
37
+ this.notifySubscribers();
37
38
  }
38
39
  /**
39
40
  * Handle tool result notifications from the host
@@ -42,8 +43,8 @@ export class MCPAppsChatSDK extends JsonRpcClient {
42
43
  handleToolResult(params) {
43
44
  const toolResult = params;
44
45
  if (toolResult.content !== undefined) {
45
- // Store tool output from notification
46
46
  this.globals.toolOutput = toolResult.content;
47
+ this.notifySubscribers();
47
48
  }
48
49
  }
49
50
  /**
@@ -54,8 +55,8 @@ export class MCPAppsChatSDK extends JsonRpcClient {
54
55
  const toolInput = params;
55
56
  // Only store non-empty arguments - treat empty object as "no input"
56
57
  if (toolInput.arguments !== undefined && Object.keys(toolInput.arguments).length > 0) {
57
- // Store tool input from notification
58
58
  this.globals.toolInput = toolInput.arguments;
59
+ this.notifySubscribers();
59
60
  }
60
61
  }
61
62
  /**
@@ -65,7 +66,6 @@ export class MCPAppsChatSDK extends JsonRpcClient {
65
66
  handleToolCancelled(params) {
66
67
  const cancellation = params;
67
68
  const reason = cancellation.reason ?? "";
68
- // Notify all registered cancellation callbacks
69
69
  this.canceledCallbacks.forEach((callback) => callback(reason));
70
70
  }
71
71
  /**
@@ -74,29 +74,31 @@ export class MCPAppsChatSDK extends JsonRpcClient {
74
74
  * @returns Promise with the new display mode (confirmed by host)
75
75
  */
76
76
  async setDisplayMode(mode) {
77
- const response = await this.request("ui/request-display-mode", {
77
+ const response = await this.session.request("ui/request-display-mode", {
78
78
  displayMode: mode,
79
79
  });
80
80
  return response;
81
81
  }
82
82
  async sendMessageToHost(message) {
83
- await this.request("ui/message", {
83
+ await this.session.request("ui/message", {
84
84
  role: "user",
85
- content: {
86
- type: "text",
87
- text: message.content,
88
- },
85
+ content: [
86
+ {
87
+ type: "text",
88
+ text: message.content,
89
+ },
90
+ ],
89
91
  });
90
92
  }
91
93
  async callTool(toolCall) {
92
- const result = await this.request("tools/call", {
94
+ const result = await this.session.request("tools/call", {
93
95
  name: toolCall.toolName,
94
96
  arguments: toolCall.params ?? {},
95
97
  });
96
98
  return result;
97
99
  }
98
100
  async readResource(request) {
99
- const result = await this.request("resources/read", {
101
+ const result = await this.session.request("resources/read", {
100
102
  uri: request.uri,
101
103
  });
102
104
  return result;
@@ -125,11 +127,12 @@ export class MCPAppsChatSDK extends JsonRpcClient {
125
127
  };
126
128
  }
127
129
  subscribe(callback) {
128
- // Add callback to subscriptions set
129
130
  this.subscriptions.add(callback);
130
- // Return cleanup function
131
131
  return () => {
132
132
  this.subscriptions.delete(callback);
133
133
  };
134
134
  }
135
+ notifySubscribers() {
136
+ this.subscriptions.forEach((callback) => callback());
137
+ }
135
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/sdk-chat",
3
- "version": "1.60.2",
3
+ "version": "1.61.1",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -26,7 +26,7 @@
26
26
  "test:coverage": "vitest run --coverage"
27
27
  },
28
28
  "dependencies": {
29
- "@salesforce/sdk-core": "^1.60.2"
29
+ "@salesforce/sdk-core": "^1.61.1"
30
30
  },
31
31
  "devDependencies": {
32
32
  "vitest": "^4.0.6"
@@ -34,5 +34,5 @@
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  },
37
- "gitHead": "abc41ea9a3be79240ce212be95208320d197d14a"
37
+ "gitHead": "74f2289f311e488569b6e9616a34604b2154802e"
38
38
  }