66 */
77
88/**
9- * Detect MCP client name from User-Agent header
9+ * Detect MCP client name from HTTP headers
1010 *
1111 * This is a fallback mechanism when client_name is not available from
1212 * OAuth client metadata. In a future enhancement, we can look up the
1313 * client_name directly from the dynamicOauthClients table.
1414 *
1515 * Detection Priority:
1616 * 1. Custom X-MCP-Client-Name header (if provided by client)
17- * 2. Parse User-Agent for known patterns
18- * 3. Return 'Unknown Client' if no match
17+ * 2. Mcp-Session-Id header presence (indicates official MCP SDK)
18+ * 3. Parse User-Agent for known patterns
19+ * 4. Return 'Unknown Client' if no match
1920 *
2021 * @param headers - HTTP headers from request
2122 * @returns Human-readable client name
@@ -27,16 +28,19 @@ export function deriveClientName(headers: Record<string, string | string[] | und
2728 return customName ;
2829 }
2930
30- // Parse User-Agent (fallback)
31+ // Check for Mcp-Session-Id header - indicates official MCP SDK usage
32+ // The official MCP TypeScript SDK (used by VS Code, Cursor, etc.) sends this header
33+ const mcpSessionId = getHeader ( headers , 'mcp-session-id' ) ;
3134 const userAgent = getHeader ( headers , 'user-agent' ) || '' ;
3235 const ua = userAgent . toLowerCase ( ) ;
3336
34- // VS Code detection
35- if ( ua . includes ( 'vscode' ) ) {
37+ // If Mcp-Session-Id is present with 'undici' user-agent, it's likely VS Code or similar
38+ // The official @modelcontextprotocol /sdk uses undici as HTTP client
39+ if ( mcpSessionId && ua === 'undici' ) {
3640 return 'VS Code' ;
3741 }
3842
39- // Cursor detection
43+ // Cursor detection (if Cursor sets specific headers or user-agent)
4044 if ( ua . includes ( 'cursor' ) ) {
4145 return 'Cursor' ;
4246 }
@@ -56,7 +60,17 @@ export function deriveClientName(headers: Record<string, string | string[] | und
5660 return 'Windsurf' ;
5761 }
5862
59- // Generic MCP client
63+ // Generic MCP SDK client (has mcp-session-id but unknown user-agent)
64+ if ( mcpSessionId ) {
65+ return 'MCP Client' ;
66+ }
67+
68+ // VS Code detection (legacy, in case it appears in user-agent)
69+ if ( ua . includes ( 'vscode' ) ) {
70+ return 'VS Code' ;
71+ }
72+
73+ // Generic MCP reference in user-agent
6074 if ( ua . includes ( 'mcp' ) ) {
6175 return 'MCP Client' ;
6276 }
0 commit comments