Skip to content

Commit e517837

Browse files
committed
use mcp request handler extra to send notifications
1 parent c25ef30 commit e517837

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/mcp/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ export class ActorsMcpServer {
344344
* @param {object} request - The request object containing tool name and arguments.
345345
* @throws {McpError} - based on the McpServer class code from the typescript MCP SDK
346346
*/
347-
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
347+
this.server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
348348
// eslint-disable-next-line prefer-const
349349
let { name, arguments: args } = request.params;
350350
const apifyToken = (request.params.apifyToken || process.env.APIFY_TOKEN) as string;
@@ -412,6 +412,7 @@ export class ActorsMcpServer {
412412
const internalTool = tool.tool as HelperTool;
413413
const res = await internalTool.call({
414414
args,
415+
extra,
415416
apifyMcpServer: this,
416417
mcpServer: this.server,
417418
apifyToken,

src/tools/helpers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const addTool: ToolEntry = {
7878
ajvValidate: ajv.compile(zodToJsonSchema(addToolArgsSchema)),
7979
// TODO: I don't like that we are passing apifyMcpServer and mcpServer to the tool
8080
call: async (toolArgs) => {
81-
const { apifyMcpServer, mcpServer, apifyToken, args } = toolArgs;
81+
const { apifyMcpServer, apifyToken, args, extra } = toolArgs;
8282
const parsed = addToolArgsSchema.parse(args);
8383
if (apifyMcpServer.listAllToolNames().includes(parsed.actorName)) {
8484
return {
@@ -90,7 +90,7 @@ export const addTool: ToolEntry = {
9090
}
9191
const tools = await getActorsAsTools([parsed.actorName], apifyToken);
9292
const toolsAdded = apifyMcpServer.upsertTools(tools, true);
93-
await mcpServer.notification({ method: 'notifications/tools/list_changed' });
93+
await extra.sendNotification({ method: 'notifications/tools/list_changed' });
9494

9595
return {
9696
content: [{
@@ -121,13 +121,13 @@ export const removeTool: ToolEntry = {
121121
ajvValidate: ajv.compile(zodToJsonSchema(removeToolArgsSchema)),
122122
// TODO: I don't like that we are passing apifyMcpServer and mcpServer to the tool
123123
call: async (toolArgs) => {
124-
const { apifyMcpServer, mcpServer, args } = toolArgs;
124+
const { apifyMcpServer, args, extra } = toolArgs;
125125
const parsed = removeToolArgsSchema.parse(args);
126126
// Check if tool exists before attempting removal
127127
if (!apifyMcpServer.tools.has(parsed.toolName)) {
128128
// Send notification so client can update its tool list
129129
// just in case the client tool list is out of sync
130-
await mcpServer.notification({ method: 'notifications/tools/list_changed' });
130+
await extra.sendNotification({ method: 'notifications/tools/list_changed' });
131131
return {
132132
content: [{
133133
type: 'text',
@@ -136,7 +136,7 @@ export const removeTool: ToolEntry = {
136136
};
137137
}
138138
const removedTools = apifyMcpServer.removeToolsByName([parsed.toolName], true);
139-
await mcpServer.notification({ method: 'notifications/tools/list_changed' });
139+
await extra.sendNotification({ method: 'notifications/tools/list_changed' });
140140
return { content: [{ type: 'text', text: `Tools removed: ${removedTools.join(', ')}` }] };
141141
},
142142
} as InternalTool,

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
2+
import type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
3+
import type { Notification, Request } from '@modelcontextprotocol/sdk/types.js';
24
import type { ValidateFunction } from 'ajv';
35
import type { ActorDefaultRunOptions, ActorDefinition } from 'apify-client';
46

@@ -80,6 +82,10 @@ export interface ActorTool extends ToolBase {
8082
export type InternalToolArgs = {
8183
/** Arguments passed to the tool */
8284
args: Record<string, unknown>;
85+
/** MCP server extra args.
86+
* Can be used to send notifications from the server to the client.
87+
*/
88+
extra: RequestHandlerExtra<Request, Notification>;
8389
/** Reference to the Apify MCP server instance */
8490
apifyMcpServer: ActorsMcpServer;
8591
/** Reference to the MCP server instance */

0 commit comments

Comments
 (0)