@@ -39,13 +39,13 @@ import {
3939} from "@modelcontextprotocol/sdk/types.js" ;
4040import type { IndexStoreReader } from "../stores/types.js" ;
4141import { MultiIndexRunner } from "./multi-index-runner.js" ;
42+ import { buildClientUserAgent , type MCPClientInfo } from "../core/utils.js" ;
4243import {
4344 SEARCH_DESCRIPTION ,
4445 LIST_FILES_DESCRIPTION ,
4546 READ_FILE_DESCRIPTION ,
4647 withIndexList ,
4748} from "./tool-descriptions.js" ;
48-
4949/**
5050 * Configuration for the MCP server.
5151 */
@@ -72,7 +72,6 @@ export interface MCPServerConfig {
7272 */
7373 version ?: string ;
7474}
75-
7675/**
7776 * Create an MCP server instance.
7877 *
@@ -96,18 +95,19 @@ export async function createMCPServer(
9695 config : MCPServerConfig
9796) : Promise < Server > {
9897 // Create shared runner for multi-index operations
98+ // Build User-Agent for analytics tracking
99+ const clientUserAgent = buildClientUserAgent ( "mcp" ) ;
100+
99101 const runner = await MultiIndexRunner . create ( {
100102 store : config . store ,
101103 indexNames : config . indexNames ,
102104 searchOnly : config . searchOnly ,
105+ clientUserAgent,
103106 } ) ;
104-
105107 const { indexNames, indexes } = runner ;
106108 const searchOnly = ! runner . hasFileOperations ( ) ;
107-
108109 // Format index list for tool descriptions
109110 const indexListStr = runner . getIndexListString ( ) ;
110-
111111 // Create MCP server
112112 const server = new Server (
113113 {
@@ -120,7 +120,19 @@ export async function createMCPServer(
120120 } ,
121121 }
122122 ) ;
123-
123+ // Use the SDK's oninitialized callback to capture MCP client info
124+ // This preserves the SDK's protocol version negotiation
125+ server . oninitialized = ( ) => {
126+ const clientInfo = server . getClientVersion ( ) ;
127+ if ( clientInfo ) {
128+ const mcpClientInfo : MCPClientInfo = {
129+ name : clientInfo . name ,
130+ version : clientInfo . version ,
131+ } ;
132+ const updatedUserAgent = buildClientUserAgent ( "mcp" , mcpClientInfo ) ;
133+ runner . updateClientUserAgent ( updatedUserAgent ) ;
134+ }
135+ } ;
124136 // Define tool type for type safety
125137 type Tool = {
126138 name : string ;
@@ -134,12 +146,10 @@ export async function createMCPServer(
134146 required ?: string [ ] ;
135147 } ;
136148 } ;
137-
138149 // Tool descriptions with available indexes (from shared module)
139150 const searchDescription = withIndexList ( SEARCH_DESCRIPTION , indexListStr ) ;
140151 const listFilesDescription = withIndexList ( LIST_FILES_DESCRIPTION , indexListStr ) ;
141152 const readFileDescription = withIndexList ( READ_FILE_DESCRIPTION , indexListStr ) ;
142-
143153 // List available tools
144154 server . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
145155 const tools : Tool [ ] = [
@@ -167,7 +177,6 @@ export async function createMCPServer(
167177 } ,
168178 } ,
169179 ] ;
170-
171180 // Only advertise file tools if not in search-only mode
172181 if ( ! searchOnly ) {
173182 tools . push (
@@ -247,18 +256,14 @@ export async function createMCPServer(
247256 }
248257 ) ;
249258 }
250-
251259 return { tools } ;
252260 } ) ;
253-
254261 // Handle tool calls
255262 server . setRequestHandler ( CallToolRequestSchema , async ( request ) => {
256263 const { name, arguments : args } = request . params ;
257-
258264 try {
259265 const indexName = args ?. index_name as string ;
260266 const client = await runner . getClient ( indexName ) ;
261-
262267 switch ( name ) {
263268 case "search" : {
264269 const result = await client . search ( args ?. query as string , {
@@ -270,7 +275,6 @@ export async function createMCPServer(
270275 ] ,
271276 } ;
272277 }
273-
274278 case "list_files" : {
275279 if ( searchOnly ) {
276280 return {
@@ -291,7 +295,6 @@ export async function createMCPServer(
291295 content : [ { type : "text" , text } ] ,
292296 } ;
293297 }
294-
295298 case "read_file" : {
296299 if ( searchOnly ) {
297300 return {
@@ -321,7 +324,6 @@ export async function createMCPServer(
321324 content : [ { type : "text" , text : result . contents ?? "" } ] ,
322325 } ;
323326 }
324-
325327 default :
326328 return {
327329 content : [ { type : "text" , text : `Unknown tool: ${ name } ` } ] ,
@@ -335,10 +337,8 @@ export async function createMCPServer(
335337 } ;
336338 }
337339 } ) ;
338-
339340 return server ;
340341}
341-
342342/**
343343 * Run an MCP server with stdio transport.
344344 *
@@ -369,4 +369,3 @@ export async function runMCPServer(config: MCPServerConfig): Promise<void> {
369369 const transport = new StdioServerTransport ( ) ;
370370 await server . connect ( transport ) ;
371371}
372-
0 commit comments