react-mcp-chatbot 0.0.4 → 0.0.6
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 +1 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +301 -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 / Groq /
|
|
75
|
+
<li>⚡ Ollama / Groq / OpenAI / Gemini / Deepseek</li>
|
|
76
76
|
<li>🛠 Optional & validated tool parameters</li>
|
|
77
77
|
</ul>
|
|
78
78
|
|
package/dist/index.d.ts
CHANGED
|
@@ -36,7 +36,22 @@ 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 DeepSeekAgentProps = BaseAgentProps & BaseUIProps & {
|
|
50
|
+
provider: "deepseek";
|
|
51
|
+
apiKey: string;
|
|
52
|
+
providerURL?: never;
|
|
53
|
+
};
|
|
54
|
+
type MCPAgentProps = OllamaAgentProps | GroqAgentProps | OpenaiAgentProps | GeminiAgentProps | DeepSeekAgentProps;
|
|
40
55
|
|
|
41
56
|
declare function MCPAgent(props: MCPAgentProps): react_jsx_runtime.JSX.Element;
|
|
42
57
|
|
package/dist/index.js
CHANGED
|
@@ -287,6 +287,286 @@ 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
|
+
|
|
480
|
+
// src/agents/deepseekAgent.ts
|
|
481
|
+
var DeepSeekAgent = class extends Agent {
|
|
482
|
+
apiKey;
|
|
483
|
+
constructor(apiKey, model = "deepseek-chat", providerURL = "https://api.deepseek.com/v1", tools = []) {
|
|
484
|
+
super(model, providerURL, tools);
|
|
485
|
+
this.apiKey = apiKey;
|
|
486
|
+
}
|
|
487
|
+
async decideTool(message) {
|
|
488
|
+
if (this.tools.length === 0) return { tool: null };
|
|
489
|
+
const res = await fetch(`${this.url}/chat/completions`, {
|
|
490
|
+
method: "POST",
|
|
491
|
+
headers: {
|
|
492
|
+
"Content-Type": "application/json",
|
|
493
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
494
|
+
},
|
|
495
|
+
body: JSON.stringify({
|
|
496
|
+
model: this.model,
|
|
497
|
+
temperature: 0,
|
|
498
|
+
messages: [
|
|
499
|
+
{
|
|
500
|
+
role: "system",
|
|
501
|
+
content: "You select tools and arguments. Respond ONLY with valid JSON."
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
role: "user",
|
|
505
|
+
content: buildToolDecisionPrompt(this.tools, message)
|
|
506
|
+
}
|
|
507
|
+
]
|
|
508
|
+
})
|
|
509
|
+
});
|
|
510
|
+
const data = await res.json();
|
|
511
|
+
const content = data.choices?.[0]?.message?.content;
|
|
512
|
+
try {
|
|
513
|
+
return JSON.parse(content);
|
|
514
|
+
} catch {
|
|
515
|
+
return { tool: null };
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
async ask(message) {
|
|
519
|
+
let toolContext = "";
|
|
520
|
+
const decision = await this.decideTool(message);
|
|
521
|
+
if (decision.tool) {
|
|
522
|
+
const tool = this.tools.find((t) => t.name === decision.tool);
|
|
523
|
+
if (tool) {
|
|
524
|
+
const result = await tool.call(decision.arguments);
|
|
525
|
+
toolContext = `
|
|
526
|
+
Tool used: ${tool.name}
|
|
527
|
+
Result (partial):
|
|
528
|
+
\`\`\`json
|
|
529
|
+
${JSON.stringify(result, null, 2).slice(0, 4e3)}
|
|
530
|
+
\`\`\`
|
|
531
|
+
`;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
const finalPrompt = `
|
|
535
|
+
User request:
|
|
536
|
+
${message}
|
|
537
|
+
|
|
538
|
+
${toolContext}
|
|
539
|
+
|
|
540
|
+
Respond in clean, well-formatted Markdown.
|
|
541
|
+
Use lists, tables, and headings where appropriate.
|
|
542
|
+
Do NOT mention tools or APIs explicitly.
|
|
543
|
+
|
|
544
|
+
Context rules:
|
|
545
|
+
- Respond ONLY using the information provided in the context.
|
|
546
|
+
- Do NOT generate or assume any information not present in the context.
|
|
547
|
+
- If the answer cannot be found in the context, respond exactly:
|
|
548
|
+
"No relevant information was found."
|
|
549
|
+
`;
|
|
550
|
+
const res = await fetch(`${this.url}/chat/completions`, {
|
|
551
|
+
method: "POST",
|
|
552
|
+
headers: {
|
|
553
|
+
"Content-Type": "application/json",
|
|
554
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
555
|
+
},
|
|
556
|
+
body: JSON.stringify({
|
|
557
|
+
model: this.model,
|
|
558
|
+
temperature: 0,
|
|
559
|
+
messages: [
|
|
560
|
+
{ role: "system", content: MARKDOWN_SYSTEM_PROMPT },
|
|
561
|
+
{ role: "user", content: finalPrompt }
|
|
562
|
+
]
|
|
563
|
+
})
|
|
564
|
+
});
|
|
565
|
+
const data = await res.json();
|
|
566
|
+
return data.choices?.[0]?.message?.content ?? "";
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
|
|
290
570
|
// src/MCPAgent.tsx
|
|
291
571
|
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
292
572
|
var MARKDOWN_SYSTEM_PROMPT = `
|
|
@@ -347,6 +627,27 @@ function MCPAgent(props) {
|
|
|
347
627
|
props.providerURL,
|
|
348
628
|
props.tools
|
|
349
629
|
);
|
|
630
|
+
} else if (props.provider === "openai") {
|
|
631
|
+
return new OpenAIAgent(
|
|
632
|
+
props.apiKey,
|
|
633
|
+
props.model,
|
|
634
|
+
void 0,
|
|
635
|
+
props.tools
|
|
636
|
+
);
|
|
637
|
+
} else if (props.provider === "gemini") {
|
|
638
|
+
return new GeminiAgent(
|
|
639
|
+
props.apiKey,
|
|
640
|
+
props.model,
|
|
641
|
+
void 0,
|
|
642
|
+
props.tools
|
|
643
|
+
);
|
|
644
|
+
} else if (props.provider === "deepseek") {
|
|
645
|
+
return new DeepSeekAgent(
|
|
646
|
+
props.apiKey,
|
|
647
|
+
props.model,
|
|
648
|
+
void 0,
|
|
649
|
+
props.tools
|
|
650
|
+
);
|
|
350
651
|
}
|
|
351
652
|
return new GroqAgent(
|
|
352
653
|
props.apiKey,
|