Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gadgrandez
Copy link

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:

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

Copy link

google-cla bot commented Apr 5, 2025

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.

@github-actions github-actions bot added docs Improvements or additions to documentation js labels Apr 5, 2025
@gadgrandez gadgrandez force-pushed the gadgrandez/mcp-context branch from 9fed18b to 080f9fb Compare April 5, 2025 21:12
@pavelgj pavelgj requested a review from mbleigh April 7, 2025 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation js
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

1 participant