@@ -6,22 +6,43 @@ import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
66import pkgJson from "../package.json" with { type : "json" } ;
77import { registerStoryUrlsTool } from "./tools/get-story-urls" ;
88import { registerUIBuildingTool } from "./tools/get-ui-building-instructions" ;
9- import type { Options } from "storybook/internal/types" ;
9+ import type { Options , CoreConfig } from "storybook/internal/types" ;
1010import type { IncomingMessage , ServerResponse } from "node:http" ;
11+ import {
12+ collectTelemetry ,
13+ mcpSessionIdToClientMap ,
14+ setDisableTelemetry ,
15+ } from "./telemetry" ;
1116
12- function createMcpServer ( options : Options ) {
17+ async function createMcpServer ( options : Options , client : string ) {
1318 // New initialization request
1419 const transport = new StreamableHTTPServerTransport ( {
1520 sessionIdGenerator : ( ) => randomUUID ( ) ,
16- onsessioninitialized : ( sessionId ) => {
17- // Store the transport by session ID
21+ onsessioninitialized : async ( sessionId ) => {
1822 transports [ sessionId ] = transport ;
23+
24+ const { disableTelemetry } = await options . presets . apply < CoreConfig > (
25+ "core" ,
26+ { } ,
27+ ) ;
28+ setDisableTelemetry ( disableTelemetry ) ;
29+ mcpSessionIdToClientMap [ sessionId ] = client ;
30+
31+ await collectTelemetry ( {
32+ event : "session:initialized" ,
33+ mcpSessionId : sessionId ,
34+ } ) ;
1935 } ,
2036 } ) ;
2137
2238 transport . onclose = ( ) => {
23- if ( transport . sessionId ) {
24- delete transports [ transport . sessionId ] ;
39+ if ( ! transport . sessionId ) {
40+ return ;
41+ }
42+
43+ delete transports [ transport . sessionId ] ;
44+ if ( mcpSessionIdToClientMap [ transport . sessionId ] ) {
45+ delete mcpSessionIdToClientMap [ transport . sessionId ] ;
2546 }
2647 } ;
2748
@@ -61,7 +82,7 @@ const handlePostRequest = async (
6182 // Reuse existing transport
6283 transport = transports [ sessionId ] ;
6384 } else if ( ! sessionId && isInitializeRequest ( body ) ) {
64- transport = await createMcpServer ( options ) ;
85+ transport = await createMcpServer ( options , body . params . clientInfo . name ) ;
6586 } else {
6687 // Invalid request
6788 res . statusCode = 400 ;
0 commit comments