react-mcp-chatbot 0.0.1 → 0.0.3

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 ADDED
@@ -0,0 +1,193 @@
1
+ <p align="center">
2
+ <a href="https://www.npmjs.com/package/react-mcp-chatbot">
3
+ <img src="https://img.shields.io/npm/v/react-mcp-chatbot" alt="npm version" />
4
+ </a>
5
+ <a href="https://www.npmjs.com/package/react-mcp-chatbot">
6
+ <img src="https://img.shields.io/npm/dm/react-mcp-chatbot" alt="npm downloads" />
7
+ </a>
8
+ <a href="https://github.com/TigranZakharyan/react-mcp-chatbot/blob/main/LICENSE">
9
+ <img src="https://img.shields.io/npm/l/react-mcp-chatbot" alt="license" />
10
+ </a>
11
+ <a href="https://github.com/TigranZakharyan/react-mcp-chatbot/stargazers">
12
+ <img
13
+ src="https://img.shields.io/github/stars/TigranZakharyan/react-mcp-chatbot?style=flat"
14
+ alt="GitHub stars"
15
+ />
16
+ </a>
17
+ </p>
18
+
19
+ <h1 align="center">🤖 React MCP Chatbot</h1>
20
+
21
+ <p align="center">
22
+ <strong>AI-powered React chatbot with intelligent API tool calling</strong>
23
+ </p>
24
+
25
+ <p align="center">
26
+ A production-ready chat widget that understands user intent, selects the correct API,
27
+ executes it, and responds with clean Markdown-formatted answers.
28
+ </p>
29
+
30
+ <hr />
31
+
32
+ <h2>✨ What is React MCP Chatbot?</h2>
33
+
34
+ <p>
35
+ <strong>React MCP Chatbot</strong> is a customizable AI chatbot component that connects
36
+ <strong>natural language user requests</strong> to <strong>real API calls</strong>
37
+ using an LLM-powered agent.
38
+ </p>
39
+
40
+ <p>
41
+ Instead of writing complex conditional logic, you simply define
42
+ <strong>tools (API functions)</strong> —
43
+ the AI automatically decides <strong>when and how</strong> to call them.
44
+ </p>
45
+
46
+ <p>
47
+ This makes it ideal for <strong>real-world, data-driven AI applications</strong>.
48
+ </p>
49
+
50
+ <hr />
51
+
52
+ <h2>🎥 Demo</h2>
53
+
54
+ <p>
55
+ Example: user asks a question → AI decides → API is called → formatted response
56
+ </p>
57
+
58
+ <p align="center">
59
+ <img src="./assets/demo.gif" alt="React MCP Chatbot Demo" width="800" />
60
+ </p>
61
+
62
+ <hr />
63
+
64
+ <h2>🚀 Features</h2>
65
+
66
+ <ul>
67
+ <li>🤖 AI-driven tool selection</li>
68
+ <li>🔌 Pluggable API tools</li>
69
+ <li>🧠 LLM-powered intent detection</li>
70
+ <li>🧩 Fully typed (TypeScript)</li>
71
+ <li>🎨 Modern floating chat UI</li>
72
+ <li>🌗 Dark & Light themes</li>
73
+ <li>📐 Highly customizable</li>
74
+ <li>📝 Markdown responses (tables, lists, code)</li>
75
+ <li>⚡ Ollama / local LLM support</li>
76
+ <li>🛠 Optional & validated tool parameters</li>
77
+ </ul>
78
+
79
+ <hr />
80
+
81
+ <h2>🧠 How It Works</h2>
82
+
83
+ <pre><code>
84
+ User Message
85
+ ↓
86
+ LLM analyzes intent
87
+ ↓
88
+ Tool decision (or no tool)
89
+ ↓
90
+ API function executed
91
+ ↓
92
+ Markdown-formatted response
93
+ </code></pre>
94
+
95
+ <hr />
96
+
97
+ <h2>📦 Installation</h2>
98
+
99
+ <pre><code>npm install react-mcp-chatbot</code></pre>
100
+
101
+ <p>or</p>
102
+
103
+ <pre><code>yarn add react-mcp-chatbot</code></pre>
104
+
105
+ <hr />
106
+
107
+ <h2>⚡ Quick Start</h2>
108
+
109
+ <pre><code>import { MCPAgent } from "react-mcp-chatbot";
110
+
111
+ &lt;MCPAgent
112
+ provider="ollama"
113
+ providerURL="http://localhost:11434"
114
+ model="llama3"
115
+ tools={tools}
116
+ /&gt;
117
+ </code></pre>
118
+
119
+ <hr />
120
+
121
+ <h2>🛠 Defining API Tools</h2>
122
+
123
+ <pre><code>import type { MCPTool } from "react-mcp-chatbot";
124
+
125
+ const weatherTool: MCPTool = {
126
+ name: "get_weather",
127
+ description: "Get current weather for a city",
128
+ parameters: {
129
+ city: {
130
+ type: "string",
131
+ description: "City name",
132
+ },
133
+ units: {
134
+ type: "string",
135
+ optional: true,
136
+ description: "metric or imperial",
137
+ },
138
+ },
139
+ call: async ({ city, units }) =&gt; {
140
+ const res = await fetch(
141
+ `https://api.example.com/weather?q=${city}&units=${units ?? "metric"}`
142
+ );
143
+ return res.json();
144
+ },
145
+ };
146
+ </code></pre>
147
+
148
+ <hr />
149
+
150
+ <h2>🎨 UI Customization</h2>
151
+
152
+ <pre><code>&lt;MCPAgent
153
+ title="Support Assistant"
154
+ align="right"
155
+ theme="dark"
156
+ width={420}
157
+ height={640}
158
+ primaryColor="#6366f1"
159
+ borderRadius={20}
160
+ placeholder="Ask me anything…"
161
+ /&gt;
162
+ </code></pre>
163
+
164
+ <hr />
165
+
166
+ <h2>🧭 Use Cases</h2>
167
+
168
+ <ul>
169
+ <li>AI customer support bots</li>
170
+ <li>Internal admin dashboards</li>
171
+ <li>API copilots</li>
172
+ <li>SaaS onboarding assistants</li>
173
+ <li>Analytics chatbots</li>
174
+ <li>Developer tools</li>
175
+ <li>Portfolio projects</li>
176
+ </ul>
177
+
178
+ <hr />
179
+
180
+ <h2>📜 License</h2>
181
+
182
+ <p>
183
+ MIT © <a href="https://github.com/TigranZakharyan">Tigran Zakharyan</a>
184
+ </p>
185
+
186
+ <hr />
187
+
188
+ <h2>⭐ Star the Project</h2>
189
+
190
+ <p>
191
+ If this project helped you or inspired you, please consider giving it a star ⭐<br />
192
+ It helps a lot and motivates further development.
193
+ </p>
package/dist/index.d.ts CHANGED
@@ -21,16 +21,23 @@ type BaseUIProps = {
21
21
  buttonLabel?: string;
22
22
  placeholder?: string;
23
23
  borderRadius?: number;
24
- providerURL: string;
25
24
  };
26
- type AgentProps = {
27
- providerURL: string;
28
- provider: "ollama";
25
+ type BaseAgentProps = {
29
26
  model: string;
30
27
  tools: MCPTool[];
31
28
  };
32
- type MCPAgentProps = BaseUIProps & AgentProps;
29
+ type OllamaAgentProps = BaseAgentProps & BaseUIProps & {
30
+ provider: "ollama";
31
+ providerURL: string;
32
+ apiKey?: never;
33
+ };
34
+ type GroqAgentProps = BaseAgentProps & BaseUIProps & {
35
+ provider: "groq";
36
+ apiKey: string;
37
+ providerURL?: never;
38
+ };
39
+ type MCPAgentProps = OllamaAgentProps | GroqAgentProps;
33
40
 
34
- declare function MCPAgent({ provider, providerURL, model, tools, title, align, primaryColor, width, height, theme, buttonLabel, placeholder, borderRadius, }: MCPAgentProps): react_jsx_runtime.JSX.Element;
41
+ declare function MCPAgent(props: MCPAgentProps): react_jsx_runtime.JSX.Element;
35
42
 
36
43
  export { MCPAgent, type MCPAgentProps, type MCPTool };
package/dist/index.js CHANGED
@@ -17,29 +17,40 @@ var Agent = class {
17
17
 
18
18
  // src/helpers/prompts.ts
19
19
  var buildToolDecisionPrompt = (tools, message) => `
20
- You are an API query planner.
20
+ You are a tool-selection engine.
21
+
22
+ Your job is to decide whether a tool should be called and extract arguments.
21
23
 
22
24
  Available tools:
23
- ${tools.map((t) => `
24
- Tool: ${t.name}
25
+ ${tools.map(
26
+ (t) => `
27
+ Tool name: ${t.name}
25
28
  Description: ${t.description}
26
29
  Parameters:
27
- ${Object.entries(t.parameters ?? []).map(([k, v]) => {
28
- const optional = typeof v.optional === "boolean" ? v.optional : true;
29
- return `- ${k} (${v.type})${optional ? " optional" : ""}`;
30
- }).join("\n")}
31
- `).join("\n")}
30
+ ${Object.entries(t.parameters ?? {}).map(([k, v]) => {
31
+ const optional = typeof v.optional === "boolean" ? v.optional : true;
32
+ return `- ${k}: ${v.type}${optional ? " (optional)" : " (required)"}`;
33
+ }).join("\n")}
34
+ `
35
+ ).join("\n")}
32
36
 
33
37
  User request:
34
38
  ${message}
35
39
 
36
- Rules:
37
- - Decide the best tool to use
38
- - Extract correct parameter values from the user message
39
- - Only include parameters relevant to the request
40
- - If no tool is needed, return { "tool": null }
40
+ STRICT OUTPUT RULES:
41
+ - Respond with ONLY a single valid JSON object
42
+ - The object MUST have exactly two top-level properties:
43
+ - "tool": string | null
44
+ - "arguments": object
45
+ - Do NOT include any other keys
46
+ - Do NOT include explanations, comments, or markdown
47
+ - If no tool applies, return:
48
+ { "tool": null, "arguments": {} }
49
+ - If a tool applies:
50
+ - "tool" must be the exact tool name
51
+ - "arguments" must include ONLY parameters required by the request
41
52
 
42
- Respond ONLY with valid JSON.
53
+ Return ONLY valid JSON.
43
54
  `;
44
55
 
45
56
  // src/agents/ollamaAgent.ts
@@ -72,6 +83,7 @@ var OllamaAgent = class extends Agent {
72
83
  const decision = await this.decideTool(message);
73
84
  if (decision.tool) {
74
85
  const tool = this.tools.find((t) => t.name === decision.tool);
86
+ console.log(decision);
75
87
  if (tool) {
76
88
  const result = await tool.call(decision.arguments);
77
89
  toolContext = `
@@ -100,7 +112,8 @@ Do NOT mention tools or APIs explicitly.
100
112
  model: this.model,
101
113
  system: MARKDOWN_SYSTEM_PROMPT,
102
114
  prompt: finalPrompt,
103
- stream: false
115
+ stream: false,
116
+ temperature: 0
104
117
  })
105
118
  });
106
119
  const data = await res.json();
@@ -186,6 +199,94 @@ function buildTokens(accent, dark) {
186
199
  };
187
200
  }
188
201
 
202
+ // src/agents/groqAgent.ts
203
+ var GroqAgent = class extends Agent {
204
+ apiKey;
205
+ constructor(apiKey, model = "llama3-70b-8192", providerURL = "https://api.groq.com/openai/v1", tools = []) {
206
+ super(model, providerURL, tools);
207
+ this.apiKey = apiKey;
208
+ }
209
+ async decideTool(message) {
210
+ if (this.tools.length === 0) return { tool: null };
211
+ const res = await fetch(`${this.url}/chat/completions`, {
212
+ method: "POST",
213
+ headers: {
214
+ "Content-Type": "application/json",
215
+ Authorization: `Bearer ${this.apiKey}`
216
+ },
217
+ body: JSON.stringify({
218
+ model: this.model,
219
+ temperature: 0,
220
+ messages: [
221
+ {
222
+ role: "system",
223
+ content: "You select tools and arguments. Respond ONLY with valid JSON."
224
+ },
225
+ {
226
+ role: "user",
227
+ content: buildToolDecisionPrompt(this.tools, message)
228
+ }
229
+ ]
230
+ })
231
+ });
232
+ const data = await res.json();
233
+ const content = data.choices?.[0]?.message?.content;
234
+ try {
235
+ return JSON.parse(content);
236
+ } catch {
237
+ return { tool: null };
238
+ }
239
+ }
240
+ async ask(message) {
241
+ let toolContext = "";
242
+ const decision = await this.decideTool(message);
243
+ if (decision.tool) {
244
+ const tool = this.tools.find((t) => t.name === decision.tool);
245
+ if (tool) {
246
+ const result = await tool.call(decision.arguments);
247
+ toolContext = `
248
+ Tool used: ${tool.name}
249
+ Result (partial):
250
+ \`\`\`json
251
+ ${JSON.stringify(result, null, 2).slice(0, 4e3)}
252
+ \`\`\`
253
+ `;
254
+ }
255
+ }
256
+ const finalPrompt = `
257
+ User request:
258
+ ${message}
259
+
260
+ ${toolContext}
261
+
262
+ Respond in clean, well-formatted Markdown.
263
+ Use lists, tables, and headings where appropriate.
264
+ Do NOT mention tools or APIs explicitly.
265
+ Context rules:
266
+ - Respond ONLY using the information provided in the context.
267
+ - Do NOT generate or assume any information not present in the context.
268
+ - If the answer cannot be found in the context, respond exactly: "No relevant information was found."
269
+ `;
270
+ const res = await fetch(`${this.url}/chat/completions`, {
271
+ method: "POST",
272
+ headers: {
273
+ "Content-Type": "application/json",
274
+ Authorization: `Bearer ${this.apiKey}`
275
+ },
276
+ body: JSON.stringify({
277
+ model: this.model,
278
+ messages: [
279
+ { role: "system", content: MARKDOWN_SYSTEM_PROMPT },
280
+ { role: "user", content: finalPrompt }
281
+ ],
282
+ temperature: 0
283
+ })
284
+ });
285
+ const data = await res.json();
286
+ return data.choices?.[0]?.message?.content ?? "";
287
+ }
288
+ };
289
+
189
290
  // src/MCPAgent.tsx
190
291
  import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
191
292
  var MARKDOWN_SYSTEM_PROMPT = `
@@ -199,22 +300,24 @@ Formatting rules:
199
300
  - Use \`inline code\` for variables or keys
200
301
  - Use code blocks only when necessary
201
302
  - Keep responses clean and readable
303
+
304
+ Context rules:
305
+ - Respond ONLY using the information provided in the context.
306
+ - Do NOT generate or assume any information not present in the context.
307
+ - If the answer cannot be found in the context, respond exactly: "No relevant information was found."
202
308
  `;
203
- function MCPAgent({
204
- provider,
205
- providerURL,
206
- model,
207
- tools = [],
208
- title = "AI Assistant",
209
- align = "right",
210
- primaryColor = "#6366f1",
211
- width = 600,
212
- height = 900,
213
- theme = "dark",
214
- buttonLabel,
215
- placeholder = "Message\u2026",
216
- borderRadius = 20
217
- }) {
309
+ function MCPAgent(props) {
310
+ const {
311
+ title = "AI Assistant",
312
+ align = "right",
313
+ primaryColor = "#6366f1",
314
+ width = 600,
315
+ height = 900,
316
+ theme = "dark",
317
+ buttonLabel,
318
+ placeholder = "Message\u2026",
319
+ borderRadius = 20
320
+ } = props;
218
321
  const isDark = theme === "dark";
219
322
  const t = buildTokens(primaryColor, isDark);
220
323
  const [open, setOpen] = useState(false);
@@ -238,11 +341,26 @@ function MCPAgent({
238
341
  }
239
342
  }, [open]);
240
343
  const agent = useMemo(() => {
241
- if (provider === "ollama") {
242
- return new OllamaAgent(model, providerURL, tools);
344
+ if (props.provider === "ollama") {
345
+ return new OllamaAgent(
346
+ props.model,
347
+ props.providerURL,
348
+ props.tools
349
+ );
243
350
  }
244
- return new OllamaAgent(model, providerURL, tools);
245
- }, [provider, model, providerURL, tools]);
351
+ return new GroqAgent(
352
+ props.apiKey,
353
+ props.model,
354
+ void 0,
355
+ props.tools
356
+ );
357
+ }, [
358
+ props.provider,
359
+ props.model,
360
+ props.providerURL,
361
+ props.apiKey,
362
+ props.tools
363
+ ]);
246
364
  async function send() {
247
365
  if (!input.trim() || loading) return;
248
366
  const userMessage = input.trim();
@@ -251,11 +369,9 @@ function MCPAgent({
251
369
  setMessages((prev) => [...prev, { role: "user", content: userMessage }]);
252
370
  try {
253
371
  const response = await agent.ask(userMessage);
254
- setMessages((prev) => [
255
- ...prev,
256
- { role: "ai", content: response }
257
- ]);
372
+ setMessages((prev) => [...prev, { role: "ai", content: response }]);
258
373
  } catch (err) {
374
+ console.log(err);
259
375
  setMessages((prev) => [
260
376
  ...prev,
261
377
  { role: "ai", content: "\u26A0\uFE0F **Error:** Unable to fetch response." }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-mcp-chatbot",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "A React MCP-style AI chatbot component with tool calling and backend integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -37,12 +37,20 @@
37
37
  },
38
38
  "license": "MIT",
39
39
  "keywords": [
40
- "react",
41
40
  "ai",
42
41
  "chatbot",
43
- "mcp",
42
+ "react",
43
+ "llm",
44
+ "ollama",
45
+ "ai-agent",
44
46
  "tool-calling",
45
- "react-component"
47
+ "api-chatbot",
48
+ "react-chatbot",
49
+ "ai-tools",
50
+ "assistant",
51
+ "ai-ui",
52
+ "copilot",
53
+ "llm-agent"
46
54
  ],
47
55
  "repository": {
48
56
  "type": "git",