Skip to content

Commit 6690009

Browse files
committed
fix: use SDK's oninitialized callback for proper protocol negotiation
Instead of overriding the initialize handler (which bypassed the SDK's protocol version negotiation), use the oninitialized callback to capture MCP client info after the SDK handles initialization. This ensures: - Protocol version negotiation works correctly with all MCP clients - Client info is still captured for User-Agent tracking - No hardcoded protocol version that could become stale
1 parent e806b63 commit 6690009

1 file changed

Lines changed: 5 additions & 41 deletions

File tree

src/clients/mcp-server.ts

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
3636
import {
3737
CallToolRequestSchema,
3838
ListToolsRequestSchema,
39-
InitializeRequestSchema,
4039
} from "@modelcontextprotocol/sdk/types.js";
4140
import type { IndexStoreReader } from "../stores/types.js";
4241
import { MultiIndexRunner } from "./multi-index-runner.js";
@@ -47,7 +46,6 @@ import {
4746
READ_FILE_DESCRIPTION,
4847
withIndexList,
4948
} from "./tool-descriptions.js";
50-
5149
/**
5250
* Configuration for the MCP server.
5351
*/
@@ -74,7 +72,6 @@ export interface MCPServerConfig {
7472
*/
7573
version?: string;
7674
}
77-
7875
/**
7976
* Create an MCP server instance.
8077
*
@@ -107,13 +104,10 @@ export async function createMCPServer(
107104
searchOnly: config.searchOnly,
108105
clientUserAgent,
109106
});
110-
111107
const { indexNames, indexes } = runner;
112108
const searchOnly = !runner.hasFileOperations();
113-
114109
// Format index list for tool descriptions
115110
const indexListStr = runner.getIndexListString();
116-
117111
// Create MCP server
118112
const server = new Server(
119113
{
@@ -126,36 +120,19 @@ export async function createMCPServer(
126120
},
127121
}
128122
);
129-
130-
// Set up a custom initialize handler to capture MCP client info
131-
// We intercept the initialize request to update the User-Agent with client info
132-
server.setRequestHandler(InitializeRequestSchema, async (request) => {
133-
// Extract client info from the initialize request
134-
const clientInfo = request.params.clientInfo;
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();
135127
if (clientInfo) {
136128
const mcpClientInfo: MCPClientInfo = {
137129
name: clientInfo.name,
138130
version: clientInfo.version,
139131
};
140-
// Update the runner's User-Agent with MCP client info
141132
const updatedUserAgent = buildClientUserAgent("mcp", mcpClientInfo);
142133
runner.updateClientUserAgent(updatedUserAgent);
143134
}
144-
145-
// Return the standard initialize response
146-
// The Server class will merge our capabilities with the standard response
147-
return {
148-
protocolVersion: "2024-11-05",
149-
serverInfo: {
150-
name: config.serverName ?? "context-connectors",
151-
version: config.version ?? "0.1.0",
152-
},
153-
capabilities: {
154-
tools: {},
155-
},
156-
};
157-
});
158-
135+
};
159136
// Define tool type for type safety
160137
type Tool = {
161138
name: string;
@@ -169,12 +146,10 @@ export async function createMCPServer(
169146
required?: string[];
170147
};
171148
};
172-
173149
// Tool descriptions with available indexes (from shared module)
174150
const searchDescription = withIndexList(SEARCH_DESCRIPTION, indexListStr);
175151
const listFilesDescription = withIndexList(LIST_FILES_DESCRIPTION, indexListStr);
176152
const readFileDescription = withIndexList(READ_FILE_DESCRIPTION, indexListStr);
177-
178153
// List available tools
179154
server.setRequestHandler(ListToolsRequestSchema, async () => {
180155
const tools: Tool[] = [
@@ -202,7 +177,6 @@ export async function createMCPServer(
202177
},
203178
},
204179
];
205-
206180
// Only advertise file tools if not in search-only mode
207181
if (!searchOnly) {
208182
tools.push(
@@ -282,18 +256,14 @@ export async function createMCPServer(
282256
}
283257
);
284258
}
285-
286259
return { tools };
287260
});
288-
289261
// Handle tool calls
290262
server.setRequestHandler(CallToolRequestSchema, async (request) => {
291263
const { name, arguments: args } = request.params;
292-
293264
try {
294265
const indexName = args?.index_name as string;
295266
const client = await runner.getClient(indexName);
296-
297267
switch (name) {
298268
case "search": {
299269
const result = await client.search(args?.query as string, {
@@ -305,7 +275,6 @@ export async function createMCPServer(
305275
],
306276
};
307277
}
308-
309278
case "list_files": {
310279
if (searchOnly) {
311280
return {
@@ -326,7 +295,6 @@ export async function createMCPServer(
326295
content: [{ type: "text", text }],
327296
};
328297
}
329-
330298
case "read_file": {
331299
if (searchOnly) {
332300
return {
@@ -356,7 +324,6 @@ export async function createMCPServer(
356324
content: [{ type: "text", text: result.contents ?? "" }],
357325
};
358326
}
359-
360327
default:
361328
return {
362329
content: [{ type: "text", text: `Unknown tool: ${name}` }],
@@ -370,10 +337,8 @@ export async function createMCPServer(
370337
};
371338
}
372339
});
373-
374340
return server;
375341
}
376-
377342
/**
378343
* Run an MCP server with stdio transport.
379344
*
@@ -404,4 +369,3 @@ export async function runMCPServer(config: MCPServerConfig): Promise<void> {
404369
const transport = new StdioServerTransport();
405370
await server.connect(transport);
406371
}
407-

0 commit comments

Comments
 (0)