@@ -65,8 +65,11 @@ export class McpServer {
65
65
private _registeredPrompts : { [ name : string ] : RegisteredPrompt } = { } ;
66
66
67
67
private _onCapabilityChange = createEventNotifier < CapabilityEvent > ( ) ;
68
+ /** Counter for unique resource invocation indexes, used to correlate resource invocation events */
68
69
private _resourceInvocationIndex = 0 ;
70
+ /** Counter for unique tool invocation indexes, used to correlate tool invocation events */
69
71
private _toolInvocationIndex = 0 ;
72
+ /** Counter for unique prompt invocation indexes, used to correlate prompt invocation events */
70
73
private _promptInvocationIndex = 0 ;
71
74
72
75
constructor ( serverInfo : Implementation , options ?: ServerOptions ) {
@@ -94,9 +97,18 @@ export class McpServer {
94
97
* Event notifier for capability changes. Listeners will be notified when capabilities are added, updated, removed,
95
98
* enabled, disabled, invoked, completed, or when errors occur.
96
99
*
100
+ * This provides a way to monitor and respond to all capability-related activities in the server,
101
+ * including both lifecycle changes and invocation events. Each capability type (resource, tool, prompt)
102
+ * maintains its own sequence of invocation indexes, which can be used to correlate invocations
103
+ * with their completions or errors.
104
+ *
97
105
* @example
98
106
* const subscription = server.onCapabilityChange((event) => {
99
- * console.log(`${event.capabilityType} ${event.capabilityName} ${event.action}`);
107
+ * if (event.action === "invoked") {
108
+ * console.log(`${event.capabilityType} ${event.capabilityName} invoked with index ${event.invocationIndex}`);
109
+ * } else if (event.action === "completed" || event.action === "error") {
110
+ * console.log(`${event.capabilityType} operation completed in ${event.durationMs}ms`);
111
+ * }
100
112
* });
101
113
*
102
114
* // Later, to stop listening:
@@ -1799,29 +1811,71 @@ const EMPTY_COMPLETION_RESULT: CompleteResult = {
1799
1811
} ,
1800
1812
} ;
1801
1813
1814
+ /**
1815
+ * Represents events emitted when capabilities (tools, resources, prompts) change state or are invoked.
1816
+ *
1817
+ * These events allow tracking the lifecycle and usage of all capabilities registered with an McpServer.
1818
+ * Events include capability registration, updates, invocation, completion, and errors.
1819
+ *
1820
+ * Each capability type (tool, resource, prompt) maintains its own sequence of invocation indexes.
1821
+ * The invocationIndex can be used to correlate "invoked" events with their corresponding
1822
+ * "completed" or "error" events for the same capability type.
1823
+ */
1802
1824
export type CapabilityEvent = {
1825
+ /** Information about the server that generated this event */
1803
1826
readonly serverInfo : { readonly name : string ; readonly version : string } ;
1827
+ /** The type of capability this event relates to */
1804
1828
readonly capabilityType : "resource" | "tool" | "prompt" ;
1829
+ /** The name (or URI for resources) of the specific capability */
1805
1830
readonly capabilityName : string ;
1806
1831
} & (
1807
1832
| {
1833
+ /**
1834
+ * Lifecycle events for capability registration and status changes.
1835
+ * - "added": The capability was registered
1836
+ * - "updated": The capability was modified
1837
+ * - "removed": The capability was unregistered
1838
+ * - "enabled": The capability was enabled
1839
+ * - "disabled": The capability was disabled
1840
+ */
1808
1841
readonly action : "added" | "updated" | "removed" | "enabled" | "disabled" ;
1809
1842
}
1810
1843
| {
1844
+ /** Emitted when a capability is invoked */
1811
1845
readonly action : "invoked" ;
1846
+ /**
1847
+ * Monotonically increasing index for each invocation, per capability type.
1848
+ * This index can be used to correlate this "invoked" event with a later
1849
+ * "completed" or "error" event with the same capabilityType and invocationIndex.
1850
+ */
1812
1851
readonly invocationIndex : number ;
1852
+ /** The arguments passed to the capability, if any */
1813
1853
readonly arguments ?: unknown ;
1814
1854
}
1815
1855
| {
1856
+ /** Emitted when a capability invocation completes successfully */
1816
1857
readonly action : "completed" ;
1858
+ /**
1859
+ * The invocationIndex from the corresponding "invoked" event.
1860
+ * This allows correlating the completion with its invocation.
1861
+ */
1817
1862
readonly invocationIndex : number ;
1863
+ /** The result returned by the capability, if any */
1818
1864
readonly result ?: unknown ;
1865
+ /** The duration of the operation in milliseconds, measured from invocation to completion */
1819
1866
readonly durationMs ?: number ;
1820
1867
}
1821
1868
| {
1869
+ /** Emitted when a capability invocation fails with an error */
1822
1870
readonly action : "error" ;
1871
+ /**
1872
+ * The invocationIndex from the corresponding "invoked" event.
1873
+ * This allows correlating the error with its invocation.
1874
+ */
1823
1875
readonly invocationIndex : number ;
1876
+ /** The error that occurred during capability execution */
1824
1877
readonly error : unknown ;
1878
+ /** The duration from invocation to error in milliseconds */
1825
1879
readonly durationMs ?: number ;
1826
1880
}
1827
1881
) ;
0 commit comments