@@ -14,6 +14,7 @@ import {
1414 ListToolsRequestSchema ,
1515 McpError ,
1616 ServerNotificationSchema ,
17+ SetLevelRequestSchema ,
1718} from '@modelcontextprotocol/sdk/types.js' ;
1819import type { ValidateFunction } from 'ajv' ;
1920import { type ActorCallOptions , ApifyApiError } from 'apify-client' ;
@@ -31,7 +32,7 @@ import type { ActorMcpTool, ActorTool, HelperTool, ToolEntry } from '../types.js
3132import { createProgressTracker } from '../utils/progress.js' ;
3233import { getToolPublicFieldOnly } from '../utils/tools.js' ;
3334import { connectMCPClient } from './client.js' ;
34- import { EXTERNAL_TOOL_CALL_TIMEOUT_MSEC } from './const.js' ;
35+ import { EXTERNAL_TOOL_CALL_TIMEOUT_MSEC , LOG_LEVEL_MAP } from './const.js' ;
3536import { processParamsGetTools } from './utils.js' ;
3637
3738type ToolsChangedHandler = ( toolNames : string [ ] ) => void ;
@@ -44,6 +45,7 @@ export class ActorsMcpServer {
4445 public readonly tools : Map < string , ToolEntry > ;
4546 private toolsChangedHandler : ToolsChangedHandler | undefined ;
4647 private sigintHandler : ( ( ) => Promise < void > ) | undefined ;
48+ private currentLogLevel = 'info' ;
4749
4850 constructor ( setupSigintHandler = true ) {
4951 this . server = new Server (
@@ -59,8 +61,10 @@ export class ActorsMcpServer {
5961 } ,
6062 } ,
6163 ) ;
64+ this . setupLoggingProxy ( ) ;
6265 this . tools = new Map ( ) ;
6366 this . setupErrorHandling ( setupSigintHandler ) ;
67+ this . setupLoggingHandlers ( ) ;
6468 this . setupToolHandlers ( ) ;
6569 this . setupPromptHandlers ( ) ;
6670 }
@@ -264,6 +268,31 @@ export class ActorsMcpServer {
264268 }
265269 }
266270
271+ private setupLoggingProxy ( ) : void {
272+ // Store original sendLoggingMessage
273+ const originalSendLoggingMessage = this . server . sendLoggingMessage . bind ( this . server ) ;
274+
275+ // Proxy sendLoggingMessage to filter logs
276+ this . server . sendLoggingMessage = async ( params : { level : string ; data ?: unknown ; [ key : string ] : unknown } ) => {
277+ const messageLevelValue = LOG_LEVEL_MAP [ params . level ] ?? - 1 ; // Unknown levels get -1, discard
278+ const currentLevelValue = LOG_LEVEL_MAP [ this . currentLogLevel ] ?? LOG_LEVEL_MAP . info ; // Default to info if invalid
279+ if ( messageLevelValue >= currentLevelValue ) {
280+ await originalSendLoggingMessage ( params as Parameters < typeof originalSendLoggingMessage > [ 0 ] ) ;
281+ }
282+ } ;
283+ }
284+
285+ private setupLoggingHandlers ( ) : void {
286+ this . server . setRequestHandler ( SetLevelRequestSchema , ( request ) => {
287+ const { level } = request . params ;
288+ if ( LOG_LEVEL_MAP [ level ] !== undefined ) {
289+ this . currentLogLevel = level ;
290+ }
291+ // Sending empty result based on MCP spec
292+ return { } ;
293+ } ) ;
294+ }
295+
267296 /**
268297 * Sets up MCP request handlers for prompts.
269298 */
0 commit comments