react-mcp-chatbot 0.0.1 β 0.0.2
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 +193 -0
- package/dist/index.js +28 -14
- package/package.json +12 -4
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
|
+
<MCPAgent
|
|
112
|
+
provider="ollama"
|
|
113
|
+
providerURL="http://localhost:11434"
|
|
114
|
+
model="llama3"
|
|
115
|
+
tools={tools}
|
|
116
|
+
/>
|
|
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 }) => {
|
|
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><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
|
+
/>
|
|
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.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
|
|
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(
|
|
24
|
-
|
|
25
|
+
${tools.map(
|
|
26
|
+
(t) => `
|
|
27
|
+
Tool name: ${t.name}
|
|
25
28
|
Description: ${t.description}
|
|
26
29
|
Parameters:
|
|
27
|
-
${Object.entries(t.parameters ??
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}).join("\n")}
|
|
31
|
-
`
|
|
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
|
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
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
|
-
|
|
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 = `
|
|
@@ -251,11 +263,13 @@ function MCPAgent({
|
|
|
251
263
|
setMessages((prev) => [...prev, { role: "user", content: userMessage }]);
|
|
252
264
|
try {
|
|
253
265
|
const response = await agent.ask(userMessage);
|
|
266
|
+
console.log(response);
|
|
254
267
|
setMessages((prev) => [
|
|
255
268
|
...prev,
|
|
256
269
|
{ role: "ai", content: response }
|
|
257
270
|
]);
|
|
258
271
|
} catch (err) {
|
|
272
|
+
console.log(err);
|
|
259
273
|
setMessages((prev) => [
|
|
260
274
|
...prev,
|
|
261
275
|
{ 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.
|
|
3
|
+
"version": "0.0.2",
|
|
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
|
-
"
|
|
42
|
+
"react",
|
|
43
|
+
"llm",
|
|
44
|
+
"ollama",
|
|
45
|
+
"ai-agent",
|
|
44
46
|
"tool-calling",
|
|
45
|
-
"
|
|
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",
|