Update agent with new Gemini SDK and MCP integration
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
# ---------- Build stage ----------
|
# ---------- Build stage ----------
|
||||||
FROM node:18-alpine AS builder
|
FROM node:20-alpine AS builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install
|
RUN npm install
|
||||||
@@ -7,7 +7,7 @@ COPY . .
|
|||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# ---------- Production stage ----------
|
# ---------- Production stage ----------
|
||||||
FROM node:18-alpine
|
FROM node:20-alpine
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
|
|||||||
886
conversation-layer-agent/package-lock.json
generated
886
conversation-layer-agent/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -10,7 +10,8 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"@google/generative-ai": "^0.5.0",
|
"@google/genai": "^1.4.0",
|
||||||
|
"@modelcontextprotocol/sdk": "^1.12.1",
|
||||||
"dotenv": "^16.5.0"
|
"dotenv": "^16.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
import { GoogleGenAI, mcpToTool } from '@google/genai';
|
||||||
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||||||
|
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
@@ -8,9 +10,20 @@ const PORT = Number(process.env.PORT) || 8001;
|
|||||||
const API_KEY = process.env.GEMINI_API_KEY || '';
|
const API_KEY = process.env.GEMINI_API_KEY || '';
|
||||||
console.log(`Using Gemini API key: ${API_KEY}`);
|
console.log(`Using Gemini API key: ${API_KEY}`);
|
||||||
|
|
||||||
const genAI = API_KEY ? new GoogleGenerativeAI(API_KEY) : null;
|
const genAI = API_KEY ? new GoogleGenAI({ apiKey: API_KEY }) : null;
|
||||||
const model = genAI ? genAI.getGenerativeModel({ model: 'gemini-pro' }) : null;
|
|
||||||
|
|
||||||
|
const MCP_URL = process.env.MCP_URL || 'http://planilla.interno.mcp';
|
||||||
|
let mcpClient: Client | undefined;
|
||||||
|
let mcpTransport: StreamableHTTPClientTransport | undefined;
|
||||||
|
|
||||||
|
async function getMcpClient(): Promise<Client> {
|
||||||
|
if (!mcpClient) {
|
||||||
|
mcpClient = new Client({ name: 'planilla-client', version: '1.0.0' });
|
||||||
|
mcpTransport = new StreamableHTTPClientTransport(new URL(MCP_URL));
|
||||||
|
await mcpClient.connect(mcpTransport);
|
||||||
|
}
|
||||||
|
return mcpClient;
|
||||||
|
}
|
||||||
const repoInfo = `This repository contains a WhatsApp router, a simple chat UI and now a conversation-layer-agent service.
|
const repoInfo = `This repository contains a WhatsApp router, a simple chat UI and now a conversation-layer-agent service.
|
||||||
- whatsapp-router: Forwards WhatsApp messages to configured agents.
|
- whatsapp-router: Forwards WhatsApp messages to configured agents.
|
||||||
- chat-ui: Minimal web interface that also talks to an agent.
|
- chat-ui: Minimal web interface that also talks to an agent.
|
||||||
@@ -24,14 +37,23 @@ app.post('/', async (req, res) => {
|
|||||||
const message = req.body?.message as string | undefined;
|
const message = req.body?.message as string | undefined;
|
||||||
if (!message) return res.status(400).json({ error: 'Missing message' });
|
if (!message) return res.status(400).json({ error: 'Missing message' });
|
||||||
|
|
||||||
if (!model) {
|
if (!genAI) {
|
||||||
return res.json({ reply: repoInfo });
|
return res.json({ reply: repoInfo });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const prompt = `Repo information: ${repoInfo}\nUser message: ${message}`;
|
const contents = `Repo information: ${repoInfo}\nUser message: ${message}`;
|
||||||
const result = await model.generateContent(prompt);
|
const config: any = {};
|
||||||
const reply = result.response.text().trim();
|
if (message.toLowerCase().includes('/planilla')) {
|
||||||
|
const client = await getMcpClient();
|
||||||
|
config.tools = [mcpToTool(client)];
|
||||||
|
}
|
||||||
|
const result = await genAI.models.generateContent({
|
||||||
|
model: 'gemini-2.0-flash',
|
||||||
|
contents,
|
||||||
|
config,
|
||||||
|
});
|
||||||
|
const reply = (result.text || '').trim();
|
||||||
res.json({ reply });
|
res.json({ reply });
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('Gemini error', err.message);
|
console.error('Gemini error', err.message);
|
||||||
|
|||||||
Reference in New Issue
Block a user