@@ -62,6 +62,7 @@ In that case, the user should check the MCP client documentation to see if the c
6262
6363export const addToolArgsSchema = z . object ( {
6464 actorName : z . string ( )
65+ . min ( 1 )
6566 . describe ( 'Add a tool, Actor or MCP-Server to available tools by Actor ID or tool full name.'
6667 + 'Tool name is always composed from `username/name`' ) ,
6768} ) ;
@@ -79,6 +80,14 @@ export const addTool: ToolEntry = {
7980 call : async ( toolArgs ) => {
8081 const { apifyMcpServer, mcpServer, apifyToken, args } = toolArgs ;
8182 const parsed = addToolArgsSchema . parse ( args ) ;
83+ if ( apifyMcpServer . listAllToolNames ( ) . includes ( parsed . actorName ) ) {
84+ return {
85+ content : [ {
86+ type : 'text' ,
87+ text : `Actor ${ parsed . actorName } is already available. No new tools were added.` ,
88+ } ] ,
89+ } ;
90+ }
8291 const tools = await getActorsAsTools ( [ parsed . actorName ] , apifyToken ) ;
8392 const toolsAdded = apifyMcpServer . upsertTools ( tools , true ) ;
8493 await mcpServer . notification ( { method : 'notifications/tools/list_changed' } ) ;
@@ -98,6 +107,7 @@ export const addTool: ToolEntry = {
98107} ;
99108export const removeToolArgsSchema = z . object ( {
100109 toolName : z . string ( )
110+ . min ( 1 )
101111 . describe ( 'Tool name to remove from available tools.' )
102112 . transform ( ( val ) => actorNameToToolName ( val ) ) ,
103113} ) ;
@@ -112,8 +122,19 @@ export const removeTool: ToolEntry = {
112122 // TODO: I don't like that we are passing apifyMcpServer and mcpServer to the tool
113123 call : async ( toolArgs ) => {
114124 const { apifyMcpServer, mcpServer, args } = toolArgs ;
115-
116125 const parsed = removeToolArgsSchema . parse ( args ) ;
126+ // Check if tool exists before attempting removal
127+ if ( ! apifyMcpServer . tools . has ( parsed . toolName ) ) {
128+ // Send notification so client can update its tool list
129+ // just in case the client tool list is out of sync
130+ await mcpServer . notification ( { method : 'notifications/tools/list_changed' } ) ;
131+ return {
132+ content : [ {
133+ type : 'text' ,
134+ text : `Tool '${ parsed . toolName } ' not found. No tools were removed.` ,
135+ } ] ,
136+ } ;
137+ }
117138 const removedTools = apifyMcpServer . removeToolsByName ( [ parsed . toolName ] , true ) ;
118139 await mcpServer . notification ( { method : 'notifications/tools/list_changed' } ) ;
119140 return { content : [ { type : 'text' , text : `Tools removed: ${ removedTools . join ( ', ' ) } ` } ] } ;
0 commit comments