feat: improve AI agent detection for MCP server#10389
Conversation
### Description Improve the reliability of AI agent detection when running as an MCP server by utilizing the client information provided during the MCP `initialize` request. This replaces the reliance on environment variables which were proving to be unreliable in some environments. ### Scenarios Tested - Verified that `detectAIAgent` returns "unknown" by default. - Verified that `detectAIAgent` returns mapped name from env vars. - Verified that `detectAIAgent` returns normalized MCP client name when in MCP mode. - Verified that it falls back to env vars if no client name is set in MCP mode. - Verified that MCP client name is ignored if not in MCP mode. ### Sample Commands Ran unit tests: npx mocha src/env.spec.ts
There was a problem hiding this comment.
Code Review
This pull request introduces the ability to detect the specific AI agent being used when the CLI is invoked via the Model Context Protocol (MCP). It adds a new setMcpClientName function to the environment module and updates the MCP server to capture and store the client name during the handshake. However, several critical issues were identified: the detectAIAgent function is now stateful but its results are cached statically in src/track.ts, meaning analytics will not reflect the detected agent. Additionally, the isFirebaseMcp flag is not being set to true when the MCP server starts, which prevents the detection logic from executing. Suggestions were also provided to improve the API of setMcpClientName to better support state resetting in tests.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request enables the detection and tracking of specific AI agents when the CLI operates in MCP mode. Key changes include adding state management for the MCP client name in env.ts, updating detectAIAgent to utilize this information, and refactoring src/track.ts to evaluate user properties dynamically during telemetry. A new test suite was also added to verify these detection logic paths. Feedback was provided to make the MCP client name slugification more robust by handling multiple spaces and hyphens.
Description
Improve the reliability of AI agent detection when running as an MCP server by using Client Info instead of environment variables.