genkit icon indicating copy to clipboard operation
genkit copied to clipboard

feat(js/plugins/mcp): allow passing context data to the server for to…

Open gadgrandez opened this issue 8 months ago • 4 comments

feat(js/plugins/mcp): allow passing context data to the server for tool execution

This feature introduces the ability to send Genkit context data to the MCP server when executing tools. This can be useful in scenarios where the MCP server needs access to context information like userId from the Genkit execution environment to properly execute tools.

Description

This commit adds a new optional parameter sendGenkitContext to the McpClientOptions in the genkitx-mcp plugin. When set to true, this flag will include the Genkit context data in the _meta.context field of the CallToolRequest sent to the MCP server.

On the server side, the GenkitMcpServer has been updated to optionally receive and utilize this context data. If context data is present in the request, it will be merged with the server's current context and passed to the tool function.

Usage Example

To enable sending Genkit context, you can configure the McpClientOptions when initializing the MCP client:

import { genkit } from 'genkit';
import { mcpClient } from 'genkitx-mcp';

const myClient = mcpClient({
  // ... other options
  sendGenkitContext: true, // Enable sending Genkit context
});

const ai = genkit({
  plugins: [
    myClient /* ... other plugins such as model providers ...*/,
  ],
});

// ...

When sendGenkitContext is set to true, any tool calls made through this MCP client will include the Genkit context.

Checklist:

  • [x] PR title is following https://www.conventionalcommits.org/en/v1.0.0/
  • [x] Tested (manually, unit tested, etc.) - Manually tested.
  • [x] Docs updated (updated docs or a docs bug required) - README.md updated to reflect the new sendGenkitContext option.

gadgrandez avatar Apr 05 '25 21:04 gadgrandez

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Apr 05 '25 21:04 google-cla[bot]

Hello. I'd like to know if there's any problem with this change. It's been on hold for a month despite implementing a small but significant change to Genkit context communication between the MCP client and server. Thank you.

gadgrandez avatar May 08 '25 20:05 gadgrandez

Hi, honestly I think this just fell through the cracks - thanks for the PR! I think I'd take a slightly different approach to this. Instead of a sendGenkitContext option when initializing, I'd prefer perhaps looking for an mcp: {_meta} field in context. This makes it more explicit and flexible.

ai.generate({
  prompt: "do a thing",
  tools: [mcpTool],
  context: {mcp: {_meta: {userId: "foo"}}}
});

This may mean that some data in context is duplicated, but gives you full control over what and how MCP metadata is sent.

mbleigh avatar May 08 '25 20:05 mbleigh

Hey! Im waiting this feature to send context to my MCP server! Some update?

erxonxi avatar May 18 '25 16:05 erxonxi