Skip to main content

on

Learn how to register event listeners on the MCP server and which events are available.

The MCP server emits various events throughout its lifecycle that you can listen to. This allows you to react to important server events such as initialization, message sending, subscription changes, and logging level changes.

Basic API

You can register an event listener by invoking the on method on the server instance. The first argument is the event name and the second is a callback function that will be invoked whenever that event occurs.

		server.on('initialize', (event) => {
	console.log('Server initialized with:', event);
});
	

The callback receives event-specific data depending on which event you're listening to.

Available Events

initialize

Emitted when the server is initialized. Receives the initialization request parameters from the client.

		server.on('initialize', (initializeRequest) => {
	console.log('Client info:', initializeRequest.clientInfo);
	console.log('Client capabilities:', initializeRequest.clientCapabilities);
	console.log('Protocol version:', initializeRequest.protocolVersion);
});
	

This is useful for performing setup tasks based on client capabilities or storing client information for the session.

send

Emitted when the server sends a message to the client.

		server.on('send', (message) => {
	console.log('Sending message:', message.request);
});
	

This can be useful for logging, monitoring, or debugging communication between the server and client. It's also used by the transports to actually perform the communication (and it's what should be used if you want to build a custom transport)

broadcast

Emitted when the server broadcasts a message to all connected clients (in multi-client scenarios).

		server.on('broadcast', (message) => {
	console.log('Broadcasting message:', message.request);
});
	

This is particularly useful in scenarios where you have multiple clients connected to the same server instance and need to track what's being sent to everyone. It's also used by the transports to actually perform the communication (and it's what should be used if you want to build a custom transport).

subscription

Emitted when a client subscribes or unsubscribes from resource updates.

		server.on('subscription', (subscriptionRequest) => {
	console.log('Resource URI:', subscriptionRequest.uri);
	console.log('Action:', subscriptionRequest.action); // 'add' or 'remove'
});
	

This allows you to set up or tear down resource monitoring based on client subscriptions.

loglevelchange

Emitted when the logging level changes.

		server.on('loglevelchange', (change) => {
	console.log('New logging level:', change.level);
});
	

This is useful for adjusting your application's logging behavior dynamically without restarting the server.

Event Listener Options

The on method also accepts an optional third parameter for EventListener options:

		server.on(
	'initialize',
	(event) => {
		console.log('Server initialized');
	},
	{ once: true }, // This listener will only be called once
);
	

Common options include:

  • once: If true, the listener will be automatically removed after the first invocation
  • signal: An AbortSignal that can be used to remove the listener