44 *--------------------------------------------------------------------------------------------*/
55
66import type { DisposableLike } from '@microsoft/vscode-processutils' ;
7- import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' ;
87import type { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js' ;
98import * as crypto from 'crypto' ;
109import type * as express from 'express' ;
@@ -13,17 +12,17 @@ import * as os from 'os';
1312import * as path from 'path' ;
1413import * as vscode from 'vscode' ;
1514import { Lazy } from '../utils/Lazy' ;
15+ import type { McpProviderOptions } from './McpProviderOptions' ;
1616
1717const transports : Record < string , StreamableHTTPServerTransport > = { } ;
1818
1919/**
2020 * Starts a new MCP HTTP server instance on a random named pipe (Windows) or Unix socket (Unix).
21- * @param getNewMcpServer Function that returns a new MCP server instance. A new server must be created
22- * for each MCP session, so this function should not return the same instance each time.
21+ * @param mcpOptions Options for the MCP server
2322 * @returns An object containing the disposable to stop and clean up the server, the server URI, and headers
2423 * that should be attached to all requests
2524 */
26- export async function startInProcHttpServer ( getNewMcpServer : ( ) => McpServer | Promise < McpServer > ) : Promise < { disposable : DisposableLike , serverUri : vscode . Uri , headers : Record < string , string > } > {
25+ export async function startInProcHttpServer ( mcpOptions : McpProviderOptions ) : Promise < { disposable : DisposableLike , serverUri : vscode . Uri , headers : Record < string , string > } > {
2726 let socketPath : string | undefined ;
2827
2928 try {
@@ -37,7 +36,7 @@ export async function startInProcHttpServer(getNewMcpServer: () => McpServer | P
3736 app . use ( express . default . json ( ) ) ;
3837 app . use ( ( req , res , next ) => authMiddleware ( nonce , req , res , next ) ) ;
3938
40- app . post ( '/mcp' , ( req , res ) => handlePost ( getNewMcpServer , req , res ) ) ;
39+ app . post ( '/mcp' , ( req , res ) => handlePost ( mcpOptions , req , res ) ) ;
4140 app . get ( '/mcp' , handleGetDelete ) ;
4241 app . delete ( '/mcp' , handleGetDelete ) ;
4342
@@ -86,7 +85,7 @@ function authMiddleware(nonce: string, req: express.Request, res: express.Respon
8685 next ( ) ;
8786}
8887
89- async function handlePost ( getNewMcpServer : ( ) => McpServer | Promise < McpServer > , req : express . Request , res : express . Response ) : Promise < void > {
88+ async function handlePost ( mcpOptions : McpProviderOptions , req : express . Request , res : express . Response ) : Promise < void > {
9089 const sessionId = req . headers [ 'mcp-session-id' ] as string | undefined ;
9190
9291 const isInitializeRequest = await isInitializeRequestLazy . value ;
@@ -112,7 +111,17 @@ async function handlePost(getNewMcpServer: () => McpServer | Promise<McpServer>,
112111 allowedHosts : [ 'localhost' ] ,
113112 } ) ;
114113
115- const server = await Promise . resolve ( getNewMcpServer ( ) ) ;
114+ const { McpServer } = await mcpServerLazy . value ;
115+ const server = new McpServer (
116+ {
117+ name : mcpOptions . id ,
118+ title : mcpOptions . serverLabel ,
119+ version : mcpOptions . serverVersion ,
120+ }
121+ ) ;
122+
123+ await Promise . resolve ( mcpOptions . registerTools ( server ) ) ;
124+
116125 await server . connect ( transport ) ;
117126 } else {
118127 // Invalid request
@@ -182,6 +191,7 @@ function tryCleanupSocket(socketPath: string | undefined): void {
182191// Lazily load some modules that are only needed when an MCP server is actually started
183192const expressLazy = new Lazy ( async ( ) => await import ( 'express' ) ) ;
184193const streamableHttpLazy = new Lazy ( async ( ) => await import ( '@modelcontextprotocol/sdk/server/streamableHttp.js' ) ) ;
194+ const mcpServerLazy = new Lazy ( async ( ) => await import ( '@modelcontextprotocol/sdk/server/mcp.js' ) ) ;
185195const isInitializeRequestLazy = new Lazy ( async ( ) => {
186196 const { isInitializeRequest } = await import ( '@modelcontextprotocol/sdk/types.js' ) ;
187197 return isInitializeRequest ;
0 commit comments