Skip to content

DRIVERS-3529 Add env.agent to handshake metadata for agentic client identification - #1967

Open
aclark4life wants to merge 11 commits into
mongodb:masterfrom
aclark4life:DRIVERS-3529
Open

aclark4life wants to merge 11 commits into
mongodb:masterfrom
aclark4life:DRIVERS-3529

Conversation

@aclark4life

@aclark4life aclark4life commented Jul 22, 2026 •

Copy link
Copy Markdown

Summary

Adds client.env.agent to the MongoDB Handshake metadata, so agent-mediated usage of MongoDB (Claude Code, Cursor, Gemini CLI, …) can be distinguished from direct human usage. See DRIVERS-3529.

Detection matches what mongosh already ships, except for AI_AGENT normalization. mongosh alignment: MONGOSH-3696.

Changes

  • Add env.agent to the client document, with an ordered environment-variable table for detection.
  • Evaluate the generic AI_AGENT last, so a versioned value such as claude-code_2-1-238_agent cannot mask a known agent. Normalize it: trim, lowercase, truncate to the largest valid UTF-8 prefix of 64 bytes, and map 1/true to ai_agent.
  • Omit env.agent before env.name in the size-limit cascade.
  • Append Test 3 to the Client Metadata prose tests, leaving Test 1 and Test 2 numbering unchanged.
  • Record the three AI_AGENT normalization divergences from mongosh, tracked by MONGOSH-3696.

Notes

v1 targets Node and Python. Other language tickets are split from DRIVERS-3529 and blocked on this spec change. Implementations: PYTHON-5929, NODE-7666.


Please complete the following before merging:

  • Is the relevant DRIVERS ticket in the PR title?
  • Update changelog.
  • Test changes in at least one language driver.
  • Test these changes against all server versions and topologies (including standalone, replica set, and sharded clusters).

@aclark4life
aclark4life marked this pull request as ready for review July 22, 2026 19:05
@aclark4life
aclark4life requested a review from a team as a code owner July 22, 2026 19:05
@aclark4life
aclark4life requested a review from vbabanin July 22, 2026 19:05
@codeowners-service-app

Copy link
Copy Markdown

Assigned blink1073 for team dbx-spec-maintainers-connection-establishment because vbabanin is out of office.

| `KUBERNETES_SERVICE_HOST` | `1` |

### Test 2: Test that the driver accepts an arbitrary auth mechanism
### Test 2: Test that agent metadata is properly captured

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this should be added as Test 3, to prevent drivers from having to change existing test code

@aclark4life aclark4life Sep 22, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 196c6b05.

Comment thread source/mongodb-handshake/handshake.md Outdated

##### Agent

Many AI coding assistants and agentic clients ("agents", e.g. Claude Code, Cursor, Gemini CLI) set an environment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The word wrapping here feels unnatural. I suggest either a soft-fixed length or sentence per line

@aclark4life aclark4life Sep 22, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 196c6b05.

This comment was marked as outdated.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

source/mongodb-handshake/tests/README.md:135

  • This table is intended to represent “no environment variables set”, but an all-empty row renders oddly and can be confusing (or trigger table-formatting checks). Consider replacing the table with a short sentence stating that no variables are set.
    | Environment Variable | Value |
    | -------------------- | ----- |
    |                      |       |

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

source/mongodb-handshake/tests/README.md:88

  • Subject–verb agreement: “a connection and hello command succeeds” should be plural (“…command succeed”).
Drivers that capture values for `client.env` should test that a connection and hello command succeeds in the presence of

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

source/mongodb-handshake/tests/README.md:129

  • In Test 3 case 6, the table shows the AI_AGENT value as "" (two quote characters), which is not an empty string. This could cause implementers to set the variable to a non-empty value and accidentally treat it as populated, defeating the intent of the test case (empty value treated as unset). Consider leaving the table value cell blank (or otherwise unambiguously indicating an empty value) while keeping the bullet text as the normative requirement.
    | Environment Variable | Value               |
    | -------------------- | ------------------- |
    | `AI_AGENT`           | `""` (empty string) |

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

source/mongodb-handshake/tests/README.md:143

  • The new Agent prose test cases cover generic-vs-known precedence, but they don’t include a case asserting the precedence between the two generic variables themselves (AI_AGENT must win over AGENT, since it is earlier in the ordered table in the spec). Without this, a driver could accidentally flip the generic precedence and still pass these tests.
7. No agent variables. None of the environment variables in the `client.env.agent` table are set. `client.env.agent`
    MUST be omitted.

8. Agent alongside FaaS. This test MUST verify that both the AWS Lambda metadata and `client.env.agent` (equal to
    `claude-code`) are present in `client.env`.

    | Environment Variable | Value              |
    | -------------------- | ------------------ |
    | `AWS_EXECUTION_ENV`  | `AWS_Lambda_java8` |
    | `AWS_REGION`         | `us-east-2`        |
    | `CLAUDECODE`         | `1`                |

Comment thread source/mongodb-handshake/handshake.md Outdated
Comment thread source/mongodb-handshake/handshake.md Outdated
Comment thread source/mongodb-handshake/handshake.md Outdated
…tion

Address review feedback from PavelSafronov and johnmtll:

- Evaluate known agent variables before the generic AI_AGENT, so a
  versioned AI_AGENT value cannot mask a known agent.
- Adopt the environment variables and snake_case agent names that
  mongosh already uses, so drivers and the shell agree.
- Drop the generic AGENT variable, which is common outside agents.
- Specify normalization: trim, lowercase, map 1/true to ai_agent, and
  truncate to 64 characters.
- Treat a whitespace-only value as unset.
- Omit env.agent before env.name in the size-limit cascade.
Entries 1 through 11 report a fixed name from the table, but a value
derived from AI_AGENT is reported by the agent itself. Say so, so that
the two value spaces of client.env.agent are distinguishable.
Comment thread source/mongodb-handshake/handshake.md Outdated
Comment thread source/mongodb-handshake/tests/README.md Outdated
Comment thread source/mongodb-handshake/handshake.md Outdated
Comment thread source/mongodb-handshake/handshake.md Outdated
Truncate to the largest valid UTF-8 prefix of 64 bytes, never splitting a
character or substituting U+FFFD. State why the cap exists. Scope the mongosh
parity claim to the variable list and names, and record the three normalization
divergences. Add prose tests for a boolean value with whitespace and for
truncation on a character boundary.
Comment thread source/mongodb-handshake/handshake.md Outdated
3. Omit the `env` document entirely.
4. Truncate `platform`.
1. Omit fields from `env` except `env.name` and `env.agent`.
2. Omit `env.agent`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On an agent-only host (no FaaS, no container), steps 1+2 result in an empty env, and if this now fits, env: {} gets sent, which contradicts line 295

If no fields of client.env would be populated, client.env MUST be entirely omitted.

Suggest step 2 be changed to:

2. Omit `env.agent`; if `env` then has no remaining fields, omit `env` entirely.”

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 8958aa12.

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.

5 participants