@@ -40,14 +40,14 @@ import {
4040import type { IndexStoreReader , IndexStore } from "../stores/types.js" ;
4141import type { Source } from "../sources/types.js" ;
4242import { MultiIndexRunner } from "./multi-index-runner.js" ;
43+ import { buildClientUserAgent , type MCPClientInfo } from "../core/utils.js" ;
4344import {
4445 SEARCH_DESCRIPTION ,
4546 LIST_FILES_DESCRIPTION ,
4647 READ_FILE_DESCRIPTION ,
4748 withListIndexesReference ,
4849 withIndexList ,
4950} from "./tool-descriptions.js" ;
50-
5151/**
5252 * Configuration for the MCP server.
5353 */
@@ -81,7 +81,6 @@ export interface MCPServerConfig {
8181 */
8282 discovery ?: boolean ;
8383}
84-
8584/**
8685 * Create an MCP server instance.
8786 *
@@ -105,14 +104,20 @@ export async function createMCPServer(
105104 config : MCPServerConfig
106105) : Promise < Server > {
107106 // Create shared runner for multi-index operations
107+ // Build User-Agent for analytics tracking
108+ const clientUserAgent = buildClientUserAgent ( "mcp" ) ;
109+
108110 const runner = await MultiIndexRunner . create ( {
109111 store : config . store ,
110112 indexNames : config . indexNames ,
111113 searchOnly : config . searchOnly ,
114+ clientUserAgent,
112115 } ) ;
113116
117+ const { indexNames, indexes } = runner ;
114118 const searchOnly = ! runner . hasFileOperations ( ) ;
115-
119+ // Format index list for tool descriptions
120+ const indexListStr = runner . getIndexListString ( ) ;
116121 // Create MCP server
117122 const server = new Server (
118123 {
@@ -125,7 +130,19 @@ export async function createMCPServer(
125130 } ,
126131 }
127132 ) ;
128-
133+ // Use the SDK's oninitialized callback to capture MCP client info
134+ // This preserves the SDK's protocol version negotiation
135+ server . oninitialized = ( ) => {
136+ const clientInfo = server . getClientVersion ( ) ;
137+ if ( clientInfo ) {
138+ const mcpClientInfo : MCPClientInfo = {
139+ name : clientInfo . name ,
140+ version : clientInfo . version ,
141+ } ;
142+ const updatedUserAgent = buildClientUserAgent ( "mcp" , mcpClientInfo ) ;
143+ runner . updateClientUserAgent ( updatedUserAgent ) ;
144+ }
145+ } ;
129146 // Define tool type for type safety
130147 type Tool = {
131148 name : string ;
@@ -152,12 +169,10 @@ export async function createMCPServer(
152169 readFileDescription = withListIndexesReference ( READ_FILE_DESCRIPTION ) ;
153170 } else {
154171 // Fixed mode: include enum with index list
155- const indexListStr = runner . getIndexListString ( ) ;
156172 searchDescription = withIndexList ( SEARCH_DESCRIPTION , indexListStr ) ;
157173 listFilesDescription = withIndexList ( LIST_FILES_DESCRIPTION , indexListStr ) ;
158174 readFileDescription = withIndexList ( READ_FILE_DESCRIPTION , indexListStr ) ;
159175 }
160-
161176 // List available tools
162177 server . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
163178 const tools : Tool [ ] = [
@@ -194,7 +209,6 @@ export async function createMCPServer(
194209 } ,
195210 } ,
196211 ] ;
197-
198212 // Only advertise file tools if not in search-only mode
199213 if ( ! searchOnly ) {
200214 tools . push (
@@ -274,10 +288,8 @@ export async function createMCPServer(
274288 }
275289 ) ;
276290 }
277-
278291 return { tools } ;
279292 } ) ;
280-
281293 // Handle tool calls
282294 server . setRequestHandler ( CallToolRequestSchema , async ( request ) => {
283295 const { name, arguments : args } = request . params ;
@@ -302,7 +314,6 @@ export async function createMCPServer(
302314 try {
303315 const indexName = args ?. index_name as string ;
304316 const client = await runner . getClient ( indexName ) ;
305-
306317 switch ( name ) {
307318 case "search" : {
308319 const result = await client . search ( args ?. query as string , {
@@ -314,7 +325,6 @@ export async function createMCPServer(
314325 ] ,
315326 } ;
316327 }
317-
318328 case "list_files" : {
319329 if ( searchOnly ) {
320330 return {
@@ -335,7 +345,6 @@ export async function createMCPServer(
335345 content : [ { type : "text" , text } ] ,
336346 } ;
337347 }
338-
339348 case "read_file" : {
340349 if ( searchOnly ) {
341350 return {
@@ -365,7 +374,6 @@ export async function createMCPServer(
365374 content : [ { type : "text" , text : result . contents ?? "" } ] ,
366375 } ;
367376 }
368-
369377 default :
370378 return {
371379 content : [ { type : "text" , text : `Unknown tool: ${ name } ` } ] ,
@@ -379,10 +387,8 @@ export async function createMCPServer(
379387 } ;
380388 }
381389 } ) ;
382-
383390 return server ;
384391}
385-
386392/**
387393 * Run an MCP server with stdio transport.
388394 *
@@ -413,4 +419,3 @@ export async function runMCPServer(config: MCPServerConfig): Promise<void> {
413419 const transport = new StdioServerTransport ( ) ;
414420 await server . connect ( transport ) ;
415421}
416-
0 commit comments