Use Pipedream MCP with Google Gemini
npm install @google/generativeai @modelcontextprotocol/sdk @pipedream/sdk
GOOGLE_API_KEY=your_google_api_key
import { GoogleGenerativeAI } from '@google/generativeai';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { PipedreamClient } from '@pipedream/sdk';
// Environment variables
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY!;
const PIPEDREAM_CLIENT_ID = process.env.PIPEDREAM_CLIENT_ID!;
const PIPEDREAM_CLIENT_SECRET = process.env.PIPEDREAM_CLIENT_SECRET!;
const PIPEDREAM_PROJECT_ID = process.env.PIPEDREAM_PROJECT_ID!;
const PIPEDREAM_ENVIRONMENT = process.env.PIPEDREAM_ENVIRONMENT as 'development' | 'production';
async function runGeminiWithMCP() {
// Initialize Pipedream SDK client
const pdClient = new PipedreamClient({
projectEnvironment: PIPEDREAM_ENVIRONMENT,
clientId: PIPEDREAM_CLIENT_ID,
clientSecret: PIPEDREAM_CLIENT_SECRET,
projectId: PIPEDREAM_PROJECT_ID
});
// Get access token for MCP authentication
const accessToken = await pdClient.rawAccessToken;
const externalUserId = 'user-123'; // Your user's unique ID
const appSlug = 'gmail,google_calendar'; // Apps to use
// Create MCP transport
const transport = new StreamableHTTPClientTransport(
new URL('https://remote.mcp.pipedream.net'),
{
requestInit: {
headers: {
'Authorization': `Bearer ${accessToken}`,
'x-pd-project-id': PIPEDREAM_PROJECT_ID,
'x-pd-environment': PIPEDREAM_ENVIRONMENT,
'x-pd-external-user-id': externalUserId,
'x-pd-app-slug': appSlug,
}
}
}
);
// Initialize MCP client
const mcpClient = new Client({
name: 'gemini-pipedream-client',
version: '1.0.0',
});
await mcpClient.connect(transport);
// Initialize Gemini client
const genAI = new GoogleGenerativeAI(GOOGLE_API_KEY);
try {
// Get available tools from MCP
const toolsResponse = await mcpClient.listTools();
const mcpTools = toolsResponse.tools || [];
// Convert MCP tools to Gemini function calling format
const geminiTools = mcpTools.map((tool: any) => ({
function_declarations: [{
name: tool.name,
description: tool.description,
parameters: tool.inputSchema,
}]
}));
// Create model with tools
const model = genAI.getGenerativeModel({
model: 'gemini-pro',
tools: geminiTools,
systemInstruction: `You are a helpful AI assistant with access to powerful tools.
The current date is ${new Date().toISOString().split('T')[0]}.
Use the available tools to help users accomplish their tasks effectively.
If you encounter any errors, explain what happened and suggest alternatives.`
});
// Start chat
const chat = model.startChat({
history: [],
});
const userMessage = "Check my recent emails and summarize the important ones";
console.log(`User: ${userMessage}`);
// Send message and handle tool calls
const result = await chat.sendMessage(userMessage);
const response = await result.response;
// Handle function calls if present
if (response.functionCalls?.length > 0) {
console.log('Executing tools...');
const functionResults = [];
for (const functionCall of response.functionCalls) {
try {
const result = await mcpClient.callTool({
name: functionCall.name,
arguments: functionCall.args,
});
functionResults.push({
name: functionCall.name,
response: result,
});
console.log(`✅ ${functionCall.name}: Success`);
} catch (error) {
console.error(`❌ ${functionCall.name}: ${error}`);
functionResults.push({
name: functionCall.name,
response: { error: String(error) },
});
}
}
// Send function results back to Gemini
const finalResult = await chat.sendMessage([{
functionResponse: {
name: functionResults[0].name,
response: functionResults[0].response,
}
}]);
console.log(`Gemini: ${finalResult.response.text()}`);
} else {
console.log(`Gemini: ${response.text()}`);
}
} finally {
// Clean up MCP client
await mcpClient.close();
}
}
// Run the example
runGeminiWithMCP().catch(console.error);
Was this page helpful?