From e2fd8c3345065bbb48517265fad1c608e499baf3 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 2 Sep 2025 15:08:29 +0400 Subject: [PATCH 1/3] config: add MCP_USER_AGENT to configuration for Mailtrap MCP --- src/config/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config/index.ts b/src/config/index.ts index f1b5ad2..0859ce3 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -18,6 +18,7 @@ export default { GENERAL_ENDPOINT: "https://mailtrap.io", USER_AGENT: "mailtrap-nodejs (https://github.com/railsware/mailtrap-nodejs)", + MCP_USER_AGENT: "mailtrap-mcp (https://github.com/railsware/mailtrap-mcp)", MAX_REDIRECTS: 0, TIMEOUT: 10000, }, From 3bd652ada1f5ac642e04cd72477937b3bf2daf08 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 2 Sep 2025 15:08:43 +0400 Subject: [PATCH 2/3] lib: replace static USER_AGENT with dynamic user agent function in MailtrapClient --- src/lib/MailtrapClient.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/MailtrapClient.ts b/src/lib/MailtrapClient.ts index a41060e..9fa46d5 100644 --- a/src/lib/MailtrapClient.ts +++ b/src/lib/MailtrapClient.ts @@ -6,12 +6,14 @@ import axios, { AxiosInstance } from "axios"; import encodeMailBuffers from "./mail-buffer-encoder"; import handleSendingError from "./axios-logger"; import MailtrapError from "./MailtrapError"; +import getDynamicUserAgent from "./get-agent"; import GeneralAPI from "./api/General"; import TestingAPI from "./api/Testing"; import ContactsBaseAPI from "./api/Contacts"; import ContactListsBaseAPI from "./api/ContactLists"; import TemplatesBaseAPI from "./api/Templates"; +import SuppressionsBaseAPI from "./api/Suppressions"; import CONFIG from "../config"; @@ -22,13 +24,11 @@ import { BatchSendResponse, BatchSendRequest, } from "../types/mailtrap"; -import SuppressionsBaseAPI from "./api/Suppressions"; const { CLIENT_SETTINGS, ERRORS } = CONFIG; const { SENDING_ENDPOINT, MAX_REDIRECTS, - USER_AGENT, TIMEOUT, TESTING_ENDPOINT, BULK_ENDPOINT, @@ -66,7 +66,7 @@ export default class MailtrapClient { headers: { Authorization: `Bearer ${token}`, Connection: "keep-alive", - "User-Agent": USER_AGENT, + "User-Agent": getDynamicUserAgent(), }, maxRedirects: MAX_REDIRECTS, timeout: TIMEOUT, From 9da5776fad1f706e61b9f1be3b928ddf9b7eaedf Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 2 Sep 2025 15:09:06 +0400 Subject: [PATCH 3/3] lib: implement dynamic User-Agent detection for Mailtrap MCP context --- src/lib/get-agent.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/lib/get-agent.ts diff --git a/src/lib/get-agent.ts b/src/lib/get-agent.ts new file mode 100644 index 0000000..6a6943c --- /dev/null +++ b/src/lib/get-agent.ts @@ -0,0 +1,63 @@ +import CONFIG from "../config"; + +const { USER_AGENT, MCP_USER_AGENT } = CONFIG.CLIENT_SETTINGS; + +/** + * Checks if the main module filename indicates MCP context. + * @returns true if main module contains "mailtrap-mcp" and is not in node_modules + */ +function isMainModuleMCP(): boolean { + const mainFile = require?.main?.filename; + + return !!( + mainFile && + mainFile.includes("mailtrap-mcp") && + !mainFile.includes("node_modules") + ); +} + +/** + * Checks if the current working directory indicates MCP context. + * @returns true if cwd contains "mailtrap-mcp" and is not in node_modules + */ +function isWorkingDirectoryMCP(): boolean { + try { + const cwd = process.cwd(); + return cwd.includes("mailtrap-mcp") && !cwd.includes("node_modules"); + } catch { + return false; + } +} + +/** + * Checks if the call stack indicates MCP context. + * @returns true if stack contains "mailtrap-mcp" and is not from node_modules/mailtrap + */ +function isCallStackMCP(): boolean { + const { stack } = new Error(); + + return !!( + stack && + stack.includes("mailtrap-mcp") && + !stack.includes("node_modules/mailtrap") + ); +} + +/** + * Determines if the code is running in a Mailtrap MCP context. + * Uses multiple detection methods to ensure accurate context identification. + * @returns true if running in MCP context, false otherwise + */ +function isMailtrapMCPContext(): boolean { + return isMainModuleMCP() || isWorkingDirectoryMCP() || isCallStackMCP(); +} + +/** + * Gets the appropriate User-Agent string based on the current context. + * @returns The User-Agent string for the current context + */ +function getDynamicUserAgent(): string { + return isMailtrapMCPContext() ? MCP_USER_AGENT : USER_AGENT; +} + +export default getDynamicUserAgent;