@aashari/mcp-server-aws-sso
Version:
Node.js/TypeScript MCP server for AWS Single Sign-On (SSO). Enables AI systems (LLMs) with tools to initiate SSO login (device auth flow), list accounts/roles, and securely execute AWS CLI commands using temporary credentials. Streamlines AI interaction w
249 lines (248 loc) • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatLoginSuccess = formatLoginSuccess;
exports.formatAlreadyLoggedIn = formatAlreadyLoggedIn;
exports.formatLoginWithBrowserLaunch = formatLoginWithBrowserLaunch;
exports.formatLoginManual = formatLoginManual;
exports.formatCredentials = formatCredentials;
exports.formatAuthRequired = formatAuthRequired;
const formatter_util_js_1 = require("../utils/formatter.util.js");
/**
* Calculate the approximate duration from now until the expiration time
* @param expirationDate The date when the session expires
* @returns Formatted duration string like "approximately 12 hours"
*/
function calculateDuration(expirationDate) {
try {
const now = new Date();
const diffMs = expirationDate.getTime() - now.getTime();
// Convert to hours
const diffHours = Math.round(diffMs / (1000 * 60 * 60));
if (diffHours < 1) {
return 'less than an hour';
}
else if (diffHours === 1) {
return 'approximately 1 hour';
}
else {
return `approximately ${diffHours} hours`;
}
}
catch {
return 'unknown duration';
}
}
/**
* Format login success message
* @param expiresDate Formatted expiration date
* @returns Formatted markdown content
*/
function formatLoginSuccess(expiresDate) {
// Parse the expiration date to calculate the duration
let durationText = 'unknown duration';
try {
const expirationDate = new Date(expiresDate);
durationText = calculateDuration(expirationDate);
}
catch {
// Keep the default text if parsing fails
}
const lines = [
(0, formatter_util_js_1.formatHeading)('AWS SSO: Authentication Successful', 1),
'',
'You have successfully authenticated with AWS SSO.',
'',
(0, formatter_util_js_1.formatHeading)('Session Details', 2),
(0, formatter_util_js_1.formatBulletList)({
Expiration: expiresDate,
Duration: `Valid for ${durationText}`,
}),
'',
(0, formatter_util_js_1.formatHeading)('Next Steps', 2),
'To explore your AWS accounts and roles, run:',
(0, formatter_util_js_1.formatCodeBlock)('mcp-aws-sso ls-accounts', 'bash'),
'',
'To execute an AWS CLI command, use:',
(0, formatter_util_js_1.formatCodeBlock)('mcp-aws-sso exec-command --account-id <ACCOUNT_ID> --role-name <ROLE_NAME> --command "aws s3 ls"', 'bash'),
'',
(0, formatter_util_js_1.formatSeparator)(),
`*Information retrieved at: ${(0, formatter_util_js_1.formatDate)(new Date())}*`,
];
return lines.join('\n');
}
/**
* Format already logged in message
* @param expiresDate Formatted expiration date
* @returns Formatted markdown content
*/
function formatAlreadyLoggedIn(expiresDate) {
// Parse the expiration date to calculate the duration
let durationText = 'unknown duration';
try {
const expirationDate = new Date(expiresDate);
durationText = calculateDuration(expirationDate);
}
catch {
// Keep the default text if parsing fails
}
const lines = [
(0, formatter_util_js_1.formatHeading)('AWS SSO: Session Active', 1),
'',
'You are already authenticated with AWS SSO.',
'',
(0, formatter_util_js_1.formatHeading)('Session Details', 2),
(0, formatter_util_js_1.formatBulletList)({
Expiration: expiresDate,
Duration: `Valid for ${durationText}`,
}),
'',
(0, formatter_util_js_1.formatHeading)('Available Actions', 2),
'To explore your AWS accounts and roles, run:',
(0, formatter_util_js_1.formatCodeBlock)('mcp-aws-sso ls-accounts', 'bash'),
'',
'To execute an AWS CLI command, use:',
(0, formatter_util_js_1.formatCodeBlock)('mcp-aws-sso exec-command --account-id <ACCOUNT_ID> --role-name <ROLE_NAME> --command "aws s3 ls"', 'bash'),
'',
'**Note**: If you want to force a new login session, you need to clear your AWS SSO token cache first.',
'',
(0, formatter_util_js_1.formatSeparator)(),
`*Information retrieved at: ${(0, formatter_util_js_1.formatDate)(new Date())}*`,
];
return lines.join('\n');
}
/**
* Format login with browser launch message
* @param verificationUri Verification URI
* @param userCode User code
* @returns Formatted markdown content
*/
function formatLoginWithBrowserLaunch(verificationUri, userCode) {
const lines = [
(0, formatter_util_js_1.formatHeading)('AWS SSO: Authentication Started', 1),
'',
'A browser window should have opened automatically to complete authentication.',
'',
(0, formatter_util_js_1.formatHeading)('Important for AI Assistants', 2),
'**Please check the following:**',
'1. **First**, ask the user if a browser window opened automatically',
'2. **If browser opened**: Guide them to complete the login in that window',
'3. **If no browser opened**: Instruct them to manually open the URL below',
'4. **Always provide**: The verification code and URL as backup',
'',
(0, formatter_util_js_1.formatHeading)('Browser Authentication Steps', 2),
'1. Complete the login process in the browser window',
`2. Enter the verification code: **${userCode}** (if not pre-filled)`,
'3. Approve the requested permissions',
'',
(0, formatter_util_js_1.formatHeading)('Manual Browser URL (if needed)', 2),
(0, formatter_util_js_1.formatCodeBlock)(verificationUri, ''),
'',
(0, formatter_util_js_1.formatSeparator)(),
`*Information retrieved at: ${(0, formatter_util_js_1.formatDate)(new Date())}*`,
];
return lines.join('\n');
}
/**
* Format manual login message
* @param verificationUri Verification URI
* @param userCode User code
* @returns Formatted markdown content
*/
function formatLoginManual(verificationUri, userCode) {
const lines = [
(0, formatter_util_js_1.formatHeading)('AWS SSO: Manual Authentication Required', 1),
'',
"If the browser didn't open automatically, please follow these steps:",
'',
(0, formatter_util_js_1.formatHeading)('Authentication Steps', 2),
'1. Open this URL in your browser:',
(0, formatter_util_js_1.formatCodeBlock)(verificationUri, ''),
`2. Enter this verification code when prompted: **${userCode}**`,
'3. Complete the AWS SSO login process',
'4. Return here after authentication is complete',
'',
(0, formatter_util_js_1.formatSeparator)(),
`*Information retrieved at: ${(0, formatter_util_js_1.formatDate)(new Date())}*`,
(0, formatter_util_js_1.formatHeading)('Authentication Details', 2),
(0, formatter_util_js_1.formatBulletList)({
'Verification Code': `**${userCode}**`,
'Browser Not Launched': 'No',
'Verification URL': verificationUri,
'Code Expires In': '10 minutes',
'Background Polling': '**Active** (credentials will be collected automatically)',
}),
'',
"Complete the authentication in your browser. Use 'aws_sso_status' to check completion status, or proceed with other AWS commands once authenticated.",
];
return lines.join('\n');
}
/**
* Format credentials message
* @param fromCache Whether credentials were from cache
* @param accountId AWS account ID
* @param roleName IAM role name
* @param credentials AWS credentials
* @returns Formatted markdown content
*/
function formatCredentials(fromCache, accountId, roleName, credentials) {
// Format expiration timestamp
let expirationFormatted = 'Unknown';
let durationText = 'unknown duration';
try {
if (credentials.expiration) {
const expirationDate = new Date(credentials.expiration * 1000);
expirationFormatted = (0, formatter_util_js_1.formatDate)(expirationDate);
durationText = calculateDuration(expirationDate);
}
}
catch {
// Keep the default
}
// Build the response
const sourceText = fromCache ? 'Retrieved from cache' : 'Freshly obtained';
const lines = [
(0, formatter_util_js_1.formatHeading)('AWS SSO: Credentials', 1),
'',
`Temporary credentials have been ${sourceText.toLowerCase()} for:`,
`- **Account**: ${accountId}`,
`- **Role**: ${roleName}`,
'',
(0, formatter_util_js_1.formatHeading)('Credential Details', 2),
(0, formatter_util_js_1.formatBulletList)({
Source: sourceText,
Expiration: expirationFormatted,
'Valid for': durationText,
}),
'',
(0, formatter_util_js_1.formatHeading)('Usage Example', 2),
'To use these credentials for an AWS CLI command:',
(0, formatter_util_js_1.formatCodeBlock)(`mcp-aws-sso exec-command --account-id ${accountId} --role-name ${roleName} --command "aws s3 ls"`, 'bash'),
'',
'**Note**: For security reasons, the actual credential values are not displayed.',
'',
(0, formatter_util_js_1.formatSeparator)(),
`*Information retrieved at: ${(0, formatter_util_js_1.formatDate)(new Date())}*`,
];
return lines.join('\n');
}
/**
* Format auth required message
* @returns Formatted markdown content
*/
function formatAuthRequired() {
const lines = [
(0, formatter_util_js_1.formatHeading)('AWS SSO: Authentication Required', 1),
'',
'You need to authenticate with AWS SSO before using this command.',
'',
(0, formatter_util_js_1.formatHeading)('How to Authenticate', 2),
'Run the following command to start the login process:',
(0, formatter_util_js_1.formatCodeBlock)('mcp-aws-sso login', 'bash'),
'',
'This will open a browser window for AWS SSO authentication. Follow the prompts to complete the process.',
'',
(0, formatter_util_js_1.formatSeparator)(),
`*Information retrieved at: ${(0, formatter_util_js_1.formatDate)(new Date())}*`,
];
return lines.join('\n');
}