react-mcp-chatbot 0.0.3 → 0.0.5
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 +11 -1
- package/dist/index.js +204 -0
- 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 /
|
|
75
|
+
<li>⚡ Ollama / Groq / OpenAI / Gemini</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
|
@@ -36,7 +36,17 @@ type GroqAgentProps = BaseAgentProps & BaseUIProps & {
|
|
|
36
36
|
apiKey: string;
|
|
37
37
|
providerURL?: never;
|
|
38
38
|
};
|
|
39
|
-
type
|
|
39
|
+
type OpenaiAgentProps = BaseAgentProps & BaseUIProps & {
|
|
40
|
+
provider: "openai";
|
|
41
|
+
apiKey: string;
|
|
42
|
+
providerURL?: never;
|
|
43
|
+
};
|
|
44
|
+
type GeminiAgentProps = BaseAgentProps & BaseUIProps & {
|
|
45
|
+
provider: "gemini";
|
|
46
|
+
apiKey: string;
|
|
47
|
+
providerURL?: never;
|
|
48
|
+
};
|
|
49
|
+
type MCPAgentProps = OllamaAgentProps | GroqAgentProps | OpenaiAgentProps | GeminiAgentProps;
|
|
40
50
|
|
|
41
51
|
declare function MCPAgent(props: MCPAgentProps): react_jsx_runtime.JSX.Element;
|
|
42
52
|
|
package/dist/index.js
CHANGED
|
@@ -287,6 +287,196 @@ Context rules:
|
|
|
287
287
|
}
|
|
288
288
|
};
|
|
289
289
|
|
|
290
|
+
// src/agents/openai.ts
|
|
291
|
+
var OpenAIAgent = class extends Agent {
|
|
292
|
+
apiKey;
|
|
293
|
+
constructor(apiKey, model = "gpt-4o-mini", providerURL = "https://api.openai.com/v1", tools = []) {
|
|
294
|
+
super(model, providerURL, tools);
|
|
295
|
+
this.apiKey = apiKey;
|
|
296
|
+
}
|
|
297
|
+
async decideTool(message) {
|
|
298
|
+
if (this.tools.length === 0) return { tool: null };
|
|
299
|
+
const res = await fetch(`${this.url}/chat/completions`, {
|
|
300
|
+
method: "POST",
|
|
301
|
+
headers: {
|
|
302
|
+
"Content-Type": "application/json",
|
|
303
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
304
|
+
},
|
|
305
|
+
body: JSON.stringify({
|
|
306
|
+
model: this.model,
|
|
307
|
+
temperature: 0,
|
|
308
|
+
messages: [
|
|
309
|
+
{
|
|
310
|
+
role: "system",
|
|
311
|
+
content: "You select tools and arguments. Respond ONLY with valid JSON."
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
role: "user",
|
|
315
|
+
content: buildToolDecisionPrompt(this.tools, message)
|
|
316
|
+
}
|
|
317
|
+
]
|
|
318
|
+
})
|
|
319
|
+
});
|
|
320
|
+
const data = await res.json();
|
|
321
|
+
const content = data.choices?.[0]?.message?.content;
|
|
322
|
+
try {
|
|
323
|
+
return JSON.parse(content);
|
|
324
|
+
} catch {
|
|
325
|
+
return { tool: null };
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
async ask(message) {
|
|
329
|
+
let toolContext = "";
|
|
330
|
+
const decision = await this.decideTool(message);
|
|
331
|
+
if (decision.tool) {
|
|
332
|
+
const tool = this.tools.find((t) => t.name === decision.tool);
|
|
333
|
+
if (tool) {
|
|
334
|
+
const result = await tool.call(decision.arguments);
|
|
335
|
+
toolContext = `
|
|
336
|
+
Tool used: ${tool.name}
|
|
337
|
+
Result (partial):
|
|
338
|
+
\`\`\`json
|
|
339
|
+
${JSON.stringify(result, null, 2).slice(0, 4e3)}
|
|
340
|
+
\`\`\`
|
|
341
|
+
`;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const finalPrompt = `
|
|
345
|
+
User request:
|
|
346
|
+
${message}
|
|
347
|
+
|
|
348
|
+
${toolContext}
|
|
349
|
+
|
|
350
|
+
Respond in clean, well-formatted Markdown.
|
|
351
|
+
Use lists, tables, and headings where appropriate.
|
|
352
|
+
Do NOT mention tools or APIs explicitly.
|
|
353
|
+
Context rules:
|
|
354
|
+
- Respond ONLY using the information provided in the context.
|
|
355
|
+
- Do NOT generate or assume any information not present in the context.
|
|
356
|
+
- If the answer cannot be found in the context, respond exactly: "No relevant information was found."
|
|
357
|
+
`;
|
|
358
|
+
const res = await fetch(`${this.url}/chat/completions`, {
|
|
359
|
+
method: "POST",
|
|
360
|
+
headers: {
|
|
361
|
+
"Content-Type": "application/json",
|
|
362
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
363
|
+
},
|
|
364
|
+
body: JSON.stringify({
|
|
365
|
+
model: this.model,
|
|
366
|
+
temperature: 0,
|
|
367
|
+
messages: [
|
|
368
|
+
{ role: "system", content: MARKDOWN_SYSTEM_PROMPT },
|
|
369
|
+
{ role: "user", content: finalPrompt }
|
|
370
|
+
]
|
|
371
|
+
})
|
|
372
|
+
});
|
|
373
|
+
const data = await res.json();
|
|
374
|
+
return data.choices?.[0]?.message?.content ?? "";
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// src/agents/geminiAgent.ts
|
|
379
|
+
var GeminiAgent = class extends Agent {
|
|
380
|
+
apiKey;
|
|
381
|
+
constructor(apiKey, model = "models/gemini-1.5-pro", providerURL = "https://generativelanguage.googleapis.com/v1beta", tools = []) {
|
|
382
|
+
super(model, providerURL, tools);
|
|
383
|
+
this.apiKey = apiKey;
|
|
384
|
+
}
|
|
385
|
+
async decideTool(message) {
|
|
386
|
+
if (this.tools.length === 0) return { tool: null };
|
|
387
|
+
const res = await fetch(
|
|
388
|
+
`${this.url}/${this.model}:generateContent?key=${this.apiKey}`,
|
|
389
|
+
{
|
|
390
|
+
method: "POST",
|
|
391
|
+
headers: {
|
|
392
|
+
"Content-Type": "application/json"
|
|
393
|
+
},
|
|
394
|
+
body: JSON.stringify({
|
|
395
|
+
contents: [
|
|
396
|
+
{
|
|
397
|
+
role: "user",
|
|
398
|
+
parts: [
|
|
399
|
+
{
|
|
400
|
+
text: "You select tools and arguments. Respond ONLY with valid JSON.\n\n" + buildToolDecisionPrompt(this.tools, message)
|
|
401
|
+
}
|
|
402
|
+
]
|
|
403
|
+
}
|
|
404
|
+
],
|
|
405
|
+
generationConfig: {
|
|
406
|
+
temperature: 0
|
|
407
|
+
}
|
|
408
|
+
})
|
|
409
|
+
}
|
|
410
|
+
);
|
|
411
|
+
const data = await res.json();
|
|
412
|
+
const content = data.candidates?.[0]?.content?.parts?.[0]?.text ?? "";
|
|
413
|
+
try {
|
|
414
|
+
return JSON.parse(content);
|
|
415
|
+
} catch {
|
|
416
|
+
return { tool: null };
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
async ask(message) {
|
|
420
|
+
let toolContext = "";
|
|
421
|
+
const decision = await this.decideTool(message);
|
|
422
|
+
if (decision.tool) {
|
|
423
|
+
const tool = this.tools.find((t) => t.name === decision.tool);
|
|
424
|
+
if (tool) {
|
|
425
|
+
const result = await tool.call(decision.arguments);
|
|
426
|
+
toolContext = `
|
|
427
|
+
Tool used: ${tool.name}
|
|
428
|
+
Result (partial):
|
|
429
|
+
\`\`\`json
|
|
430
|
+
${JSON.stringify(result, null, 2).slice(0, 4e3)}
|
|
431
|
+
\`\`\`
|
|
432
|
+
`;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const finalPrompt = `
|
|
436
|
+
User request:
|
|
437
|
+
${message}
|
|
438
|
+
|
|
439
|
+
${toolContext}
|
|
440
|
+
|
|
441
|
+
Respond in clean, well-formatted Markdown.
|
|
442
|
+
Use lists, tables, and headings where appropriate.
|
|
443
|
+
Do NOT mention tools or APIs explicitly.
|
|
444
|
+
|
|
445
|
+
Context rules:
|
|
446
|
+
- Respond ONLY using the information provided in the context.
|
|
447
|
+
- Do NOT generate or assume any information not present in the context.
|
|
448
|
+
- If the answer cannot be found in the context, respond exactly:
|
|
449
|
+
"No relevant information was found."
|
|
450
|
+
`;
|
|
451
|
+
const res = await fetch(
|
|
452
|
+
`${this.url}/${this.model}:generateContent?key=${this.apiKey}`,
|
|
453
|
+
{
|
|
454
|
+
method: "POST",
|
|
455
|
+
headers: {
|
|
456
|
+
"Content-Type": "application/json"
|
|
457
|
+
},
|
|
458
|
+
body: JSON.stringify({
|
|
459
|
+
contents: [
|
|
460
|
+
{
|
|
461
|
+
role: "user",
|
|
462
|
+
parts: [{ text: MARKDOWN_SYSTEM_PROMPT }]
|
|
463
|
+
},
|
|
464
|
+
{
|
|
465
|
+
role: "user",
|
|
466
|
+
parts: [{ text: finalPrompt }]
|
|
467
|
+
}
|
|
468
|
+
],
|
|
469
|
+
generationConfig: {
|
|
470
|
+
temperature: 0
|
|
471
|
+
}
|
|
472
|
+
})
|
|
473
|
+
}
|
|
474
|
+
);
|
|
475
|
+
const data = await res.json();
|
|
476
|
+
return data.candidates?.[0]?.content?.parts?.[0]?.text ?? "";
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
|
|
290
480
|
// src/MCPAgent.tsx
|
|
291
481
|
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
292
482
|
var MARKDOWN_SYSTEM_PROMPT = `
|
|
@@ -347,6 +537,20 @@ function MCPAgent(props) {
|
|
|
347
537
|
props.providerURL,
|
|
348
538
|
props.tools
|
|
349
539
|
);
|
|
540
|
+
} else if (props.provider === "openai") {
|
|
541
|
+
return new OpenAIAgent(
|
|
542
|
+
props.apiKey,
|
|
543
|
+
props.model,
|
|
544
|
+
void 0,
|
|
545
|
+
props.tools
|
|
546
|
+
);
|
|
547
|
+
} else if (props.provider === "gemini") {
|
|
548
|
+
return new GeminiAgent(
|
|
549
|
+
props.apiKey,
|
|
550
|
+
props.model,
|
|
551
|
+
void 0,
|
|
552
|
+
props.tools
|
|
553
|
+
);
|
|
350
554
|
}
|
|
351
555
|
return new GroqAgent(
|
|
352
556
|
props.apiKey,
|