Repository navigation
feat: detect AI coding agents in the user agent - #294
Merged
Merged
Conversation
Report the AI agent driving the client as its own User-Agent token, between the library and the requesting app: ts-octopusdeploy AIAgent/ClaudeCode GitHubActions (release;deploy;v3) Callers that know the agent from a protocol, such as an MCP server with the connected client's clientInfo, can pass it as `aiAgent` in the client configuration. That takes precedence over AI_AGENT, which takes precedence over vendor-specific environment variables. Only a normalised agent name is sent. The agent list is ported from go-octopusdeploy, which is the reference implementation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Merge the identifier prefix table into the agent list so each agent is one entry, keeping two lookups (prefix, then vendor variables) so a self-reported identity still wins. Replace the chained fallback with an explicit return. Behaviour is unchanged, matching review on the .NET port. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Match the Go and .NET AI_AGENT mapping cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Keep edge cases that guard real behaviour plus a happy path, and drop the per-agent table that restated the agent list. The same cases are used in the Go, .NET and TypeScript clients. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
egorpavlikhin
approved these changes
Nov 19, 2026
A self-reported identifier now only matches an agent's prefix when the prefix is followed by the end of the identifier or a separator (-, @, .) or digit. Previously a short prefix such as "amp" matched an unrelated identifier such as "amplitude-mcp", and because self-reported identifiers take precedence, that also hid a correct match from vendor variables. Applied the same way in the Go, .NET and TypeScript clients. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Report the agent as an RFC 9110 comment, (ai-agent=Name), appended at the very end of the User-Agent. Everything before it is byte-for-byte what the client sent before, so servers and tools that match on the start of the header keep working (Octopus Server identifies the local MCP server by the "ts-octopusdeploy octopus-mcp-server" prefix). A comment also avoids presenting the agent as a product/version pair. Applied the same way in the Go, .NET and TypeScript clients. Also ignore a caller-supplied aiAgent that isn't a string, and never fail a request if the environment can't be read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
AI coding agents increasingly drive tools built on this client, including the Octopus MCP server, but the User-Agent doesn't identify that traffic. Unlike the Go and .NET clients, this client also has no environment detection at all today.
This ports the agent detection added to go-octopusdeploy in OctopusDeploy/go-octopusdeploy#419, so the server sees the same agent names from every client.
Results
When an agent is detected, an
(ai-agent=<Name>)comment is appended to the very end of the User-Agent:ts-octopusdeploy octopus-mcp-serverprefix, both to enforce the "local MCP disabled" setting and for MCP usage telemetry. An RFC 9110 comment also describes the request rather than presenting the agent as a product/version pair.aiAgentconfiguration option. This takes a lesson from the MCP server, which learns the connected agent from the MCPinitializehandshake (clientInfo). That's a better signal than environment variables, so callers that know the agent can pass it in. It's mapped to a known name and takes precedence over environment detection; if it isn't recognised, detection falls back to the environment.AI_AGENT, the emerging cross-vendor convention, mapped to a known name by prefix (e.g.claude-code_2-1-322_agent→ClaudeCode). A prefix only matches when followed by the end of the value, a separator (-,@,.) or a digit, soampdoesn't matchamplitude-mcp.(ai-agent=Other), and raw values are never included.aiAgentis ignored, and if the environment can't be read no agent is added.Testing
aiAgent.test.tscovers the edge cases: value-matched rules, excluded variables, nested agents,AI_AGENTformats and precedence, the prefix boundary, the caller-supplied agent, and that raw values never appear.createUserAgent.test.tschecks that appending the agent leaves the existing User-Agent as an exact prefix, and that a non-stringaiAgentis ignored.tscand ESLint pass. I only ran the unit test suites, not the integration tests that need a live Octopus instance.How to review this PR
The logic is in
src/aiAgent.ts.createUserAgent.tsreplaces the inline string building inaxiosAdapter.ts. The agent list is ported from go-octopusdeploy, which is the reference copy; a comment at the top of the file points there.Caveats
clientInfo.nameasaiAgent, so its requests carry the normalised name too.Generated by Claude Code