react-mcp-chatbot 0.0.2 → 0.0.4
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 +2 -2
- package/dist/index.d.ts +13 -6
- package/dist/index.js +127 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
<li>🌗 Dark & Light themes</li>
|
|
73
73
|
<li>📐 Highly customizable</li>
|
|
74
74
|
<li>📝 Markdown responses (tables, lists, code)</li>
|
|
75
|
-
<li>⚡ Ollama / local LLM support</li>
|
|
75
|
+
<li>⚡ Ollama / Groq / local LLM support</li>
|
|
76
76
|
<li>🛠 Optional & validated tool parameters</li>
|
|
77
77
|
</ul>
|
|
78
78
|
|
|
@@ -109,7 +109,7 @@ Markdown-formatted response
|
|
|
109
109
|
<pre><code>import { MCPAgent } from "react-mcp-chatbot";
|
|
110
110
|
|
|
111
111
|
<MCPAgent
|
|
112
|
-
provider="ollama"
|
|
112
|
+
provider="ollama" // or "groq"
|
|
113
113
|
providerURL="http://localhost:11434"
|
|
114
114
|
model="llama3"
|
|
115
115
|
tools={tools}
|
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
|
|
27
|
-
providerURL: string;
|
|
28
|
-
provider: "ollama";
|
|
25
|
+
type BaseAgentProps = {
|
|
29
26
|
model: string;
|
|
30
27
|
tools: MCPTool[];
|
|
31
28
|
};
|
|
32
|
-
type
|
|
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(
|
|
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
|
@@ -112,7 +112,8 @@ Do NOT mention tools or APIs explicitly.
|
|
|
112
112
|
model: this.model,
|
|
113
113
|
system: MARKDOWN_SYSTEM_PROMPT,
|
|
114
114
|
prompt: finalPrompt,
|
|
115
|
-
stream: false
|
|
115
|
+
stream: false,
|
|
116
|
+
temperature: 0
|
|
116
117
|
})
|
|
117
118
|
});
|
|
118
119
|
const data = await res.json();
|
|
@@ -198,6 +199,94 @@ function buildTokens(accent, dark) {
|
|
|
198
199
|
};
|
|
199
200
|
}
|
|
200
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
|
+
|
|
201
290
|
// src/MCPAgent.tsx
|
|
202
291
|
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
203
292
|
var MARKDOWN_SYSTEM_PROMPT = `
|
|
@@ -211,22 +300,24 @@ Formatting rules:
|
|
|
211
300
|
- Use \`inline code\` for variables or keys
|
|
212
301
|
- Use code blocks only when necessary
|
|
213
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."
|
|
214
308
|
`;
|
|
215
|
-
function MCPAgent({
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
placeholder = "Message\u2026",
|
|
228
|
-
borderRadius = 20
|
|
229
|
-
}) {
|
|
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;
|
|
230
321
|
const isDark = theme === "dark";
|
|
231
322
|
const t = buildTokens(primaryColor, isDark);
|
|
232
323
|
const [open, setOpen] = useState(false);
|
|
@@ -250,11 +341,26 @@ function MCPAgent({
|
|
|
250
341
|
}
|
|
251
342
|
}, [open]);
|
|
252
343
|
const agent = useMemo(() => {
|
|
253
|
-
if (provider === "ollama") {
|
|
254
|
-
return new OllamaAgent(
|
|
344
|
+
if (props.provider === "ollama") {
|
|
345
|
+
return new OllamaAgent(
|
|
346
|
+
props.model,
|
|
347
|
+
props.providerURL,
|
|
348
|
+
props.tools
|
|
349
|
+
);
|
|
255
350
|
}
|
|
256
|
-
return new
|
|
257
|
-
|
|
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
|
+
]);
|
|
258
364
|
async function send() {
|
|
259
365
|
if (!input.trim() || loading) return;
|
|
260
366
|
const userMessage = input.trim();
|
|
@@ -263,11 +369,7 @@ function MCPAgent({
|
|
|
263
369
|
setMessages((prev) => [...prev, { role: "user", content: userMessage }]);
|
|
264
370
|
try {
|
|
265
371
|
const response = await agent.ask(userMessage);
|
|
266
|
-
|
|
267
|
-
setMessages((prev) => [
|
|
268
|
-
...prev,
|
|
269
|
-
{ role: "ai", content: response }
|
|
270
|
-
]);
|
|
372
|
+
setMessages((prev) => [...prev, { role: "ai", content: response }]);
|
|
271
373
|
} catch (err) {
|
|
272
374
|
console.log(err);
|
|
273
375
|
setMessages((prev) => [
|