Skip to content

Capability notification for the MCP Server #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

m-paternostro
Copy link

@m-paternostro m-paternostro commented Apr 14, 2025

Hello,

This PR adds capability notification to the MCPServer, enabling applications to track and monitor the lifecycle and execution of tools, resources, and prompts. It includes event notification infrastructure, comprehensive event tracking for all capabilities, and performance measurement.

Motivation and Context

Without a notification system, it's challenging to implement proper monitoring, debugging, and performance tracking for MCP capabilities. This change enables developers to build observability tools that can track capability usage, measure performance, identify errors, and correlate related operations. This is particularly valuable in complex applications where understanding the flow of capability invocations is crucial for debugging and optimization.

How Has This Been Tested?

I have been using a simpler implementation to provide telemetry information on the capability usage. This implementation basically decorates the capabilities callbacks to emit invocation, complete, and error events.

Breaking Changes

No

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

This PR includes:

  • A type-safe EventNotifier implementation (src/shared/eventNotifier.ts) providing consistent event subscription and handling.
  • Comprehensive capability notification in McpServer (src/server/mcp.ts), tracking lifecycle and invocation events.
  • A getVersion() method on Server (src/server/index.ts) exposing server name and version, mirroring the existing getClientVersion() method.
  • Tests for the notification system confirming correct event propagation and duration tracking.
  • Example usage showing how to subscribe to capability events and process them (src/examples/server/simpleStreamableHttp.ts).

Technical Details:

  • Each capability type (tool, resource, prompt) maintains its own monotonically increasing invocation index.
  • Events include context (server info, capability type, name) and action-specific data.
  • Duration is measured using performance.now() for high precision timing.
  • The implementation is non-intrusive and doesn't change existing APIs.
  • All events are properly documented with JSDoc comments.

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.

1 participant