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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/plugins/mcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Most MCP servers are built to run as spawned processes on the same machine using
- **`serverWebsocketUrl`: The URL of a remote server to connect to using the WebSocket MCP transport.
- **`transport`**: An existing MCP transport object for connecting to the server.
- **`rawToolResponses`**: (optional) A boolean flag. If `true`, tool responses are returned in their raw MCP format; otherwise, they are processed for Genkit compatibility.
- **`sendGenkitContext`**: (optional) A boolean flag. If `true`, tool calls include context data from the current execution.

### Using MCP Actions

Expand Down
30 changes: 23 additions & 7 deletions js/plugins/mcp/src/client/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import type { Client } from '@modelcontextprotocol/sdk/client/index.js' with { 'resolution-mode': 'import' };
import type {
CallToolRequest,
CallToolResult,
Tool,
} from '@modelcontextprotocol/sdk/types.js' with { 'resolution-mode': 'import' };
Expand Down Expand Up @@ -59,15 +60,30 @@ function registerTool(
inputJsonSchema: tool.inputSchema as JSONSchema7,
outputSchema: z.any(),
},
async (args) => {
logger.debug(
`[@genkit-ai/mcp] Calling MCP tool ${params.name}/${tool.name} with arguments`,
JSON.stringify(args)
);
const result = await client.callTool({
async (args, { context }) => {
let callToolOptions: CallToolRequest['params'] = {
name: tool.name,
arguments: args,
});
};
if (params.sendGenkitContext) {
callToolOptions = {
...callToolOptions,
_meta: {
context,
},
};
logger.debug(
`[@genkit-ai/mcp] Calling MCP tool ${params.name}/${tool.name} with arguments and Genkit context`,
JSON.stringify({ args, context })
);
} else {
logger.debug(
`[@genkit-ai/mcp] Calling MCP tool ${params.name}/${tool.name} with arguments`,
JSON.stringify(args)
);
}

const result = await client.callTool(callToolOptions);
logger.debug(
`MCP tool ${tool.name} result:`,
JSON.stringify(result, null, 2)
Expand Down
2 changes: 2 additions & 0 deletions js/plugins/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface McpClientOptions {
serverWebsocketUrl?: string | URL;
/** Return tool responses in raw MCP form instead of processing them for Genkit compatibility. */
rawToolResponses?: boolean;
/** Send the Genkit context to the server. */
sendGenkitContext?: boolean;
}

async function transportFrom(params: McpClientOptions): Promise<Transport> {
Expand Down
14 changes: 13 additions & 1 deletion js/plugins/mcp/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,19 @@ export class GenkitMcpServer {
status: 'NOT_FOUND',
message: `Tried to call tool '${req.params.name}' but it could not be found.`,
});
const result = await tool(req.params.arguments);

const clientContext = req.params._meta?.context;

let result: any;

if (clientContext) {
const currentContext = this.ai.currentContext() || {};
result = await tool(req.params.arguments, {
context: { ...currentContext, ...clientContext },
});
} else {
result = await tool(req.params.arguments);
}
return { content: [{ type: 'text', text: JSON.stringify(result) }] };
}

Expand Down