Skip to content

feat: improve AI agent detection for MCP server#10389

Open
joehan wants to merge 3 commits intomainfrom
feat/mcp-agent-detection
Open

feat: improve AI agent detection for MCP server#10389
joehan wants to merge 3 commits intomainfrom
feat/mcp-agent-detection

Conversation

@joehan
Copy link
Copy Markdown
Member

@joehan joehan commented Apr 20, 2026

Description

Improve the reliability of AI agent detection when running as an MCP server by using Client Info instead of environment variables.

### 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
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/env.ts
Comment thread src/mcp/index.ts Outdated
Comment thread src/mcp/index.ts
Comment thread src/env.ts Outdated
Comment thread src/env.spec.ts Outdated
@joehan
Copy link
Copy Markdown
Member Author

joehan commented Apr 22, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/env.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants