-
Notifications
You must be signed in to change notification settings - Fork 11
Dynamic user agent #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughAdds MCP_USER_AGENT to config. Introduces getDynamicUserAgent to choose a User-Agent based on runtime MCP context. MailtrapClient now uses this dynamic User-Agent in Axios headers. Minor import cleanup for SuppressionsBaseAPI. Changes
Sequence Diagram(s)sequenceDiagram
actor Caller
participant Client as MailtrapClient
participant Agent as getDynamicUserAgent
participant Config as CONFIG.CLIENT_SETTINGS
participant HTTP as Axios
Caller->>Client: sendRequest(...)
Client->>Agent: getDynamicUserAgent()
Agent->>Config: read USER_AGENT, MCP_USER_AGENT
Note over Agent,Config: Detect MCP via main module, CWD, call stack
alt MCP context detected
Agent-->>Client: MCP_USER_AGENT
else Not MCP
Agent-->>Client: USER_AGENT
end
Client->>HTTP: request(headers.User-Agent = value)
HTTP-->>Client: response
Client-->>Caller: response
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (5)
src/config/index.ts (1)
21-21
: Option: include package version in UA for better telemetry.Consider appending the library version (e.g., "mailtrap-mcp/1.2.3 (...)"). Implement in get-agent.ts to keep config static.
src/lib/get-agent.ts (4)
23-30
: Make cwd-based detection case-insensitive.Paths can differ in case across platforms; normalize before matching.
Apply this diff:
function isWorkingDirectoryMCP(): boolean { try { const cwd = process.cwd(); - return cwd.includes("mailtrap-mcp") && !cwd.includes("node_modules"); + const lower = cwd.toLowerCase(); + return lower.includes("mailtrap-mcp") && !lower.includes("node_modules"); } catch { return false; } }
36-44
: Broaden self-exclusion and normalize case in stack-based detection.Also exclude
node_modules/mailtrap-nodejs
to avoid false positives when this lib is on the stack.Apply this diff:
function isCallStackMCP(): boolean { - const { stack } = new Error(); - - return !!( - stack && - stack.includes("mailtrap-mcp") && - !stack.includes("node_modules/mailtrap") - ); + const { stack } = new Error(); + const lower = stack?.toLowerCase(); + return !!( + lower && + lower.includes("mailtrap-mcp") && + !/node_modules\/mailtrap(-nodejs)?/.test(lower) + ); }
59-61
: Allow env override and ensure robust fallback.Let users force UA selection and guard against unexpected undefineds.
Apply this diff:
-function getDynamicUserAgent(): string { - return isMailtrapMCPContext() ? MCP_USER_AGENT : USER_AGENT; -} +function getDynamicUserAgent(): string { + const override = process.env.MAILTRAP_UA?.toLowerCase(); + if (override === "mcp") return MCP_USER_AGENT; + if (override === "default") return USER_AGENT; + const ua = isMailtrapMCPContext() ? MCP_USER_AGENT : USER_AGENT; + return ua || "mailtrap-nodejs"; +}
59-61
: Optional: add version to UA string.If
resolveJsonModule
is enabled, append package version for traceability (example implementation):// at top-level (outside selected range): // import { version } from "../../package.json"; // inside getDynamicUserAgent(): // const base = isMailtrapMCPContext() ? MCP_USER_AGENT : USER_AGENT; // return `${base}/${version}`;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
src/config/index.ts
(1 hunks)src/lib/MailtrapClient.ts
(2 hunks)src/lib/get-agent.ts
(1 hunks)
🔇 Additional comments (4)
src/config/index.ts (1)
21-21
: LGTM: adds MCP-specific User-Agent.Field name and value look consistent with existing config keys.
src/lib/MailtrapClient.ts (2)
9-9
: LGTM: importing dynamic UA helper.
69-70
: LGTM: apply dynamic User-Agent header.Header is set once per client instance, which is appropriate.
src/lib/get-agent.ts (1)
1-63
: CommonJS-only packaging detected; no ESM support found.
package.json “type” is absent (defaults to CJS), tsconfig module=commonjs, and no ESM exports fields—no changes needed.
function isMainModuleMCP(): boolean { | ||
const mainFile = require?.main?.filename; | ||
|
||
return !!( | ||
mainFile && | ||
mainFile.includes("mailtrap-mcp") && | ||
!mainFile.includes("node_modules") | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix ESM ReferenceError risk from using require
directly.
require?.main
still throws in pure ESM because require
is an unbound identifier. Guard with typeof require !== 'undefined'
and fall back to process.argv[1]
.
Apply this diff:
function isMainModuleMCP(): boolean {
- const mainFile = require?.main?.filename;
-
- return !!(
- mainFile &&
- mainFile.includes("mailtrap-mcp") &&
- !mainFile.includes("node_modules")
- );
+ const mainFile =
+ (typeof require !== "undefined" ? require.main?.filename : undefined) ??
+ process.argv?.[1];
+ if (!mainFile) return false;
+ const lower = mainFile.toLowerCase();
+ return lower.includes("mailtrap-mcp") && !lower.includes("node_modules");
}
🤖 Prompt for AI Agents
In src/lib/get-agent.ts around lines 9 to 17, avoid referencing the unbound
require in pure ESM by first checking typeof require !== 'undefined' and using
require.main?.filename only when defined, otherwise fall back to
process.argv[1]; replace const mainFile = require?.main?.filename with a safe
resolution like: if typeof require !== 'undefined' and require.main use
require.main.filename else use process.argv[1], then keep the subsequent
includes checks the same to determine mailtrap-mcp presence and node_modules
exclusion.
Motivation
Changes
How to test
Images and GIFs
Summary by CodeRabbit
New Features
Refactor