genkitx-mcp plugin with standalone MCP server
I have created standalone MCP server now for connecting the MCP server I am using genkit MCP-plugin. for transport layer I am using StreamableHTTPClientTransport and sucessfully connected with it but how to pass the headers at the runtime.
Code -
import { z, genkit } from 'genkit'; import { googleAI } from '@genkit-ai/googleai'; import { mcpClient } from 'genkitx-mcp'; import { vertexAI } from '@genkit-ai/vertexai'; import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
const express = require('express'); const cors = require('cors'); require('dotenv').config();
const app = express(); app.use(express.json());
const createMCPClient = (accessToken: string) => {
const customTransport = new StreamableHTTPClientTransport(
new URL('http://localhost:3001/mcp'), // MCP server endpoint
{
requestInit: {
headers: { Authorization: ${accessToken} },
},
}
);
return mcpClient({ name: 'google-slides', transport: customTransport, }); };
const ai = genkit({ plugins: [
googleAI({ apiKey: '' }),
createMCPClient('') ],
model: googleAI.model('gemini-2.0-flash'), });
const chat = async (req: any, res: any) => { try { let { message, userId, accessToken } = req.body;
const slideClient = createMCPClient(accessToken);
const contentResponse: any = await ai.generate({
prompt: `User Request: ${message}.`,
tools: ['google-slides/create_presentation'], // this is form MCP server
});
let result = contentResponse.message.content;
console.log(contentResponse.text);
// console.log(contentResponse.message.content);
return res.json({
success: true,
message: 'Presentation created successfully!! 🎉',
chatResponse: result,
userId,
timestamp: new Date().toISOString(),
});
} catch (error: any) { console.log('error', error); res.status(500).json(error.Error); } };
app.post('/chat', chat);
app.listen(3000, () => { console.log('Express APP running'); });
End goal - pass the access-token to the MCP server via http Temporary solution -
- Every API call I can create the genkit instance add pass the accesstoken in that, but it will create genkit instance every time and discovery the tool .
- passing the access token via prompt