Skip to content

Conversation

@EdouardCourty
Copy link

Add Server Lifecycle Events

This PR introduces a comprehensive PSR-14 compatible event system for the MCP PHP SDK, allowing developers to hook into the server's lifecycle for tools, prompts, resources, ping, and initialize operations.

Motivation and Context

Currently, there's no standardized way to observe or react to server operations like tool calls, prompt retrievals, or resource reads.

How Has This Been Tested?

  • Unit tests added for all new event classes
  • Tests for event dispatching in all handlers (CallToolHandler, GetPromptHandler, ReadResourceHandler, InitializeHandler, PingHandler)
  • Tests cover request events, result events, and exception events
  • All existing tests continue to pass

Breaking Changes

None. This is a purely additive change. The event dispatcher is optional - servers without an event dispatcher configured will continue to work as before.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

New Event Classes

Server Events:

  • InitializeRequestEvent - Fired when client sends initialize request
  • PingRequestEvent - Fired when client sends ping request

Tool Events:

  • CallToolRequestEvent - Before tool execution
  • CallToolResultEvent - After successful execution
  • CallToolExceptionEvent - On uncaught exception

Prompt Events:

  • GetPromptRequestEvent - Before prompt execution
  • GetPromptResultEvent - After successful execution
  • GetPromptExceptionEvent - On uncaught exception

Resource Events:

  • ReadResourceRequestEvent - Before resource read
  • ReadResourceResultEvent - After successful read
  • ReadResourceExceptionEvent - On uncaught exception

Full documentation available in docs/events.md.

@chr-hertel
Copy link
Member

Nice one, thanks - before going into detailed review we should verify if we want that granularity with the events or if it should rather be one level higher like RequestEvent, NotificationEvent and ResponseEvent and ErrorEvent
that would centralize the dispatching a bit more.

you could still check for the method there, like

public function onRequestEvent(RequestEvent $event): void
{
    if ('tools/list' !== $event->getMethod()) {
        return;
    }
}

or similar.

not super sure about it myself, but i just think of change scenarios and number of events - cc @CodeWithKyrian @Nyholm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants