Skip to content

Commit 080f9fb

Browse files
Gad Grandezggrandez
Gad Grandez
authored andcommitted
feat(js/plugins/mcp): allow passing context data to the server for tool execution
1 parent c40fe8a commit 080f9fb

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

js/plugins/mcp/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Most MCP servers are built to run as spawned processes on the same machine using
5454
- **`serverWebsocketUrl`: The URL of a remote server to connect to using the WebSocket MCP transport.
5555
- **`transport`**: An existing MCP transport object for connecting to the server.
5656
- **`rawToolResponses`**: (optional) A boolean flag. If `true`, tool responses are returned in their raw MCP format; otherwise, they are processed for Genkit compatibility.
57+
- **`sendGenkitContext`**: (optional) A boolean flag. If `true`, tool calls include context data from the current execution.
5758

5859
### Using MCP Actions
5960

js/plugins/mcp/src/client/tools.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import type { Client } from '@modelcontextprotocol/sdk/client/index.js' with { 'resolution-mode': 'import' };
1818
import type {
19+
CallToolRequest,
1920
CallToolResult,
2021
Tool,
2122
} from '@modelcontextprotocol/sdk/types.js' with { 'resolution-mode': 'import' };
@@ -59,15 +60,30 @@ function registerTool(
5960
inputJsonSchema: tool.inputSchema as JSONSchema7,
6061
outputSchema: z.any(),
6162
},
62-
async (args) => {
63-
logger.debug(
64-
`[@genkit-ai/mcp] Calling MCP tool ${params.name}/${tool.name} with arguments`,
65-
JSON.stringify(args)
66-
);
67-
const result = await client.callTool({
63+
async (args, { context }) => {
64+
let callToolOptions: CallToolRequest['params'] = {
6865
name: tool.name,
6966
arguments: args,
70-
});
67+
};
68+
if (params.sendGenkitContext) {
69+
callToolOptions = {
70+
...callToolOptions,
71+
_meta: {
72+
context,
73+
},
74+
};
75+
logger.debug(
76+
`[@genkit-ai/mcp] Calling MCP tool ${params.name}/${tool.name} with arguments and Genkit context`,
77+
JSON.stringify({ args, context })
78+
);
79+
} else {
80+
logger.debug(
81+
`[@genkit-ai/mcp] Calling MCP tool ${params.name}/${tool.name} with arguments`,
82+
JSON.stringify(args)
83+
);
84+
}
85+
86+
const result = await client.callTool(callToolOptions);
7187
logger.debug(
7288
`MCP tool ${tool.name} result:`,
7389
JSON.stringify(result, null, 2)

js/plugins/mcp/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export interface McpClientOptions {
3838
serverWebsocketUrl?: string | URL;
3939
/** Return tool responses in raw MCP form instead of processing them for Genkit compatibility. */
4040
rawToolResponses?: boolean;
41+
/** Send the Genkit context to the server. */
42+
sendGenkitContext?: boolean;
4143
}
4244

4345
async function transportFrom(params: McpClientOptions): Promise<Transport> {

js/plugins/mcp/src/server.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,19 @@ export class GenkitMcpServer {
134134
status: 'NOT_FOUND',
135135
message: `Tried to call tool '${req.params.name}' but it could not be found.`,
136136
});
137-
const result = await tool(req.params.arguments);
137+
138+
const clientContext = req.params._meta?.context;
139+
140+
let result: any;
141+
142+
if (clientContext) {
143+
const currentContext = this.ai.currentContext() || {};
144+
result = await tool(req.params.arguments, {
145+
context: { ...currentContext, ...clientContext },
146+
});
147+
} else {
148+
result = await tool(req.params.arguments);
149+
}
138150
return { content: [{ type: 'text', text: JSON.stringify(result) }] };
139151
}
140152

0 commit comments

Comments
 (0)