Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion tools/modelcontextprotocol/src/userAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,33 @@ export function extractClientName(message: {
return undefined;
}

const MAX_CLIENT_NAME_LENGTH = 128;
const CLIENT_NAME_ALLOWED = /[^a-zA-Z0-9 ._/-]/g;

/**
* Build the User-Agent string, appending the MCP client name if available.
*/
function sanitizeClientName(clientName: string): string | undefined {
const sanitized = clientName
.replace(/[\r\n]/g, '')
.replace(/[^\x20-\x7E]/g, '')
.replace(CLIENT_NAME_ALLOWED, '')
.trim()
.slice(0, MAX_CLIENT_NAME_LENGTH);

if (!sanitized) {
return undefined;
}

return sanitized;
}

export function buildUserAgent(clientName?: string): string {
if (clientName) {
return `${BASE_USER_AGENT} (${clientName})`;
const sanitizedClientName = sanitizeClientName(clientName);
if (sanitizedClientName) {
return `${BASE_USER_AGENT} (${sanitizedClientName})`;
}
}
return BASE_USER_AGENT;
}
Loading