@assemble-inc/chat-widget 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,81 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/server/index.ts
21
+ var server_exports = {};
22
+ __export(server_exports, {
23
+ createChatHandler: () => createChatHandler
24
+ });
25
+ module.exports = __toCommonJS(server_exports);
26
+ var import_anthropic = require("@ai-sdk/anthropic");
27
+ var import_ai = require("ai");
28
+ var import_streamableHttp = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
29
+ var import_client = require("@modelcontextprotocol/sdk/client/index.js");
30
+ var SYSTEM_PROMPT = `You are a helpful assistant. You must ONLY answer questions using information returned by the tools available to you \u2014 never draw on your own training knowledge. If the tools do not return information relevant to the user's question, respond with: "I don't have that information in the knowledge base." Do not guess or speculate.`;
31
+ function createChatHandler(config) {
32
+ const anthropicClient = (0, import_anthropic.createAnthropic)({ apiKey: config.apiKey });
33
+ const model = config.model ?? "claude-sonnet-4-5";
34
+ const transportOptions = config.mcpBearerToken ? { requestInit: { headers: { Authorization: `Bearer ${config.mcpBearerToken}` } } } : {};
35
+ return async (req, res) => {
36
+ const { messages: uiMessages } = req.body;
37
+ const messages = await (0, import_ai.convertToModelMessages)(uiMessages);
38
+ const mcpClient = new import_client.Client({ name: "@assemble-inc/chat-widget", version: "0.1.0" });
39
+ try {
40
+ await mcpClient.connect(
41
+ new import_streamableHttp.StreamableHTTPClientTransport(new URL(config.mcpServerUrl), transportOptions)
42
+ );
43
+ const { tools: mcpTools } = await mcpClient.listTools();
44
+ const tools = Object.fromEntries(
45
+ mcpTools.map((t) => [
46
+ t.name,
47
+ (0, import_ai.tool)({
48
+ description: t.description,
49
+ inputSchema: (0, import_ai.jsonSchema)(t.inputSchema),
50
+ execute: async (input) => {
51
+ const result2 = await mcpClient.callTool({
52
+ name: t.name,
53
+ arguments: input
54
+ });
55
+ return result2.content;
56
+ }
57
+ })
58
+ ])
59
+ );
60
+ const result = (0, import_ai.streamText)({
61
+ model: anthropicClient(model),
62
+ system: SYSTEM_PROMPT,
63
+ messages,
64
+ tools,
65
+ stopWhen: (0, import_ai.stepCountIs)(10)
66
+ });
67
+ result.pipeUIMessageStreamToResponse(res);
68
+ } catch (err) {
69
+ console.error("[chat handler] error:", err);
70
+ if (!res.headersSent) {
71
+ res.status(502).json({ error: "Failed to connect to knowledge base. Check MCP server credentials." });
72
+ }
73
+ } finally {
74
+ await mcpClient.close();
75
+ }
76
+ };
77
+ }
78
+ // Annotate the CommonJS export names for ESM import in node:
79
+ 0 && (module.exports = {
80
+ createChatHandler
81
+ });
@@ -0,0 +1,28 @@
1
+ import { Request, Response } from 'express';
2
+
3
+ interface ChatHandlerConfig {
4
+ /** Anthropic API key */
5
+ apiKey: string;
6
+ /** URL of the coherence-engine-mcp-server Streamable HTTP endpoint */
7
+ mcpServerUrl: string;
8
+ /** Bearer token for authenticating with the MCP server */
9
+ mcpBearerToken?: string;
10
+ /** Claude model to use. Defaults to 'claude-sonnet-4-5'. */
11
+ model?: string;
12
+ }
13
+ /**
14
+ * Returns an Express-compatible request handler that streams chat responses
15
+ * grounded exclusively in the coherence-engine-mcp-server.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { createChatHandler } from '@assemble-inc/chat-widget/server'
20
+ * app.post('/api/chat', createChatHandler({
21
+ * apiKey: process.env.ANTHROPIC_API_KEY!,
22
+ * mcpServerUrl: process.env.MCP_SERVER_URL!,
23
+ * }))
24
+ * ```
25
+ */
26
+ declare function createChatHandler(config: ChatHandlerConfig): (req: Request, res: Response) => Promise<void>;
27
+
28
+ export { type ChatHandlerConfig, createChatHandler };
@@ -0,0 +1,28 @@
1
+ import { Request, Response } from 'express';
2
+
3
+ interface ChatHandlerConfig {
4
+ /** Anthropic API key */
5
+ apiKey: string;
6
+ /** URL of the coherence-engine-mcp-server Streamable HTTP endpoint */
7
+ mcpServerUrl: string;
8
+ /** Bearer token for authenticating with the MCP server */
9
+ mcpBearerToken?: string;
10
+ /** Claude model to use. Defaults to 'claude-sonnet-4-5'. */
11
+ model?: string;
12
+ }
13
+ /**
14
+ * Returns an Express-compatible request handler that streams chat responses
15
+ * grounded exclusively in the coherence-engine-mcp-server.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { createChatHandler } from '@assemble-inc/chat-widget/server'
20
+ * app.post('/api/chat', createChatHandler({
21
+ * apiKey: process.env.ANTHROPIC_API_KEY!,
22
+ * mcpServerUrl: process.env.MCP_SERVER_URL!,
23
+ * }))
24
+ * ```
25
+ */
26
+ declare function createChatHandler(config: ChatHandlerConfig): (req: Request, res: Response) => Promise<void>;
27
+
28
+ export { type ChatHandlerConfig, createChatHandler };
package/dist/server.js ADDED
@@ -0,0 +1,56 @@
1
+ // src/server/index.ts
2
+ import { createAnthropic } from "@ai-sdk/anthropic";
3
+ import { streamText, tool, jsonSchema, stepCountIs, convertToModelMessages } from "ai";
4
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
5
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
6
+ var SYSTEM_PROMPT = `You are a helpful assistant. You must ONLY answer questions using information returned by the tools available to you \u2014 never draw on your own training knowledge. If the tools do not return information relevant to the user's question, respond with: "I don't have that information in the knowledge base." Do not guess or speculate.`;
7
+ function createChatHandler(config) {
8
+ const anthropicClient = createAnthropic({ apiKey: config.apiKey });
9
+ const model = config.model ?? "claude-sonnet-4-5";
10
+ const transportOptions = config.mcpBearerToken ? { requestInit: { headers: { Authorization: `Bearer ${config.mcpBearerToken}` } } } : {};
11
+ return async (req, res) => {
12
+ const { messages: uiMessages } = req.body;
13
+ const messages = await convertToModelMessages(uiMessages);
14
+ const mcpClient = new Client({ name: "@assemble-inc/chat-widget", version: "0.1.0" });
15
+ try {
16
+ await mcpClient.connect(
17
+ new StreamableHTTPClientTransport(new URL(config.mcpServerUrl), transportOptions)
18
+ );
19
+ const { tools: mcpTools } = await mcpClient.listTools();
20
+ const tools = Object.fromEntries(
21
+ mcpTools.map((t) => [
22
+ t.name,
23
+ tool({
24
+ description: t.description,
25
+ inputSchema: jsonSchema(t.inputSchema),
26
+ execute: async (input) => {
27
+ const result2 = await mcpClient.callTool({
28
+ name: t.name,
29
+ arguments: input
30
+ });
31
+ return result2.content;
32
+ }
33
+ })
34
+ ])
35
+ );
36
+ const result = streamText({
37
+ model: anthropicClient(model),
38
+ system: SYSTEM_PROMPT,
39
+ messages,
40
+ tools,
41
+ stopWhen: stepCountIs(10)
42
+ });
43
+ result.pipeUIMessageStreamToResponse(res);
44
+ } catch (err) {
45
+ console.error("[chat handler] error:", err);
46
+ if (!res.headersSent) {
47
+ res.status(502).json({ error: "Failed to connect to knowledge base. Check MCP server credentials." });
48
+ }
49
+ } finally {
50
+ await mcpClient.close();
51
+ }
52
+ };
53
+ }
54
+ export {
55
+ createChatHandler
56
+ };
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@assemble-inc/chat-widget",
3
+ "version": "0.1.0",
4
+ "description": "Embeddable AI chat widget powered by Anthropic and MCP",
5
+ "keywords": [
6
+ "chat",
7
+ "widget",
8
+ "ai",
9
+ "mcp",
10
+ "anthropic",
11
+ "react"
12
+ ],
13
+ "license": "MIT",
14
+ "type": "module",
15
+ "main": "./dist/index.js",
16
+ "module": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ },
23
+ "./server": {
24
+ "types": "./dist/server.d.ts",
25
+ "import": "./dist/server.js",
26
+ "require": "./dist/server.cjs"
27
+ },
28
+ "./style.css": "./dist/index.css"
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "scripts": {
37
+ "dev": "concurrently \"vite\" \"tsx watch server.ts\"",
38
+ "build": "vite build && tsup",
39
+ "preview": "vite preview",
40
+ "lint": "tsc --noEmit",
41
+ "auth": "tsx scripts/auth.ts"
42
+ },
43
+ "peerDependencies": {
44
+ "react": ">=18",
45
+ "react-dom": ">=18"
46
+ },
47
+ "dependencies": {
48
+ "@ai-sdk/anthropic": "^3.0.75",
49
+ "@ai-sdk/react": "^3.0.178",
50
+ "@modelcontextprotocol/sdk": "^1.12.0",
51
+ "ai": "^6.0.176",
52
+ "class-variance-authority": "^0.7.1",
53
+ "express": "^4.21.2"
54
+ },
55
+ "devDependencies": {
56
+ "@tailwindcss/postcss": "^4.2.4",
57
+ "@tailwindcss/vite": "^4.2.4",
58
+ "@types/express": "^5.0.1",
59
+ "@types/node": "22.15.20",
60
+ "@types/react": "19.1.4",
61
+ "@types/react-dom": "19.1.5",
62
+ "@vitejs/plugin-react": "^4.3.4",
63
+ "autoprefixer": "10.4.21",
64
+ "concurrently": "^9.1.2",
65
+ "dotenv": "^17.4.2",
66
+ "postcss": "8.5.3",
67
+ "react": "19.1.0",
68
+ "react-dom": "19.1.0",
69
+ "tailwindcss": "4.1.7",
70
+ "tsup": "^8.4.0",
71
+ "tsx": "^4.19.2",
72
+ "typescript": "5.8.3",
73
+ "vite": "^6.3.4",
74
+ "vite-plugin-dts": "^4.5.4"
75
+ }
76
+ }