UNPKG

@chinchillaenterprises/mcp-amplify

Version:

AWS Amplify MCP server with intelligent deployment automation, specialized logging suite, and recursive resource discovery

235 lines 8.16 kB
import { v4 as uuidv4 } from 'uuid'; import { CredentialManager } from '../utils/credential-manager.js'; import { createAWSClients } from '../utils/aws-clients.js'; // Initialize state let credentialManager; let accountState = { activeAccountId: null, defaultAccountId: null }; let accounts = new Map(); let awsClients = null; export function initializeHandlers() { credentialManager = new CredentialManager(); } export function getAccountState() { return { ...accountState, accounts: Array.from(accounts.values()) }; } export function getCurrentClients() { if (!awsClients || !accountState.activeAccountId) { throw new Error('No active AWS account. Please add and switch to an account first.'); } return awsClients; } export async function initializeAccounts() { credentialManager = new CredentialManager(); try { const savedAccounts = await credentialManager.loadAllAccounts(); for (const account of savedAccounts) { accounts.set(account.id, account); } console.error(`[Account Manager] Loaded ${accounts.size} accounts`); const defaultAccountId = await credentialManager.getDefaultAccount(); if (defaultAccountId && accounts.has(defaultAccountId)) { accountState.defaultAccountId = defaultAccountId; accountState.activeAccountId = defaultAccountId; const defaultAccount = accounts.get(defaultAccountId); awsClients = createAWSClients(defaultAccount); console.error(`[Account Manager] Set default account: ${defaultAccount.name} (${defaultAccountId})`); } else if (accounts.size === 1) { const firstAccount = Array.from(accounts.values())[0]; accountState.activeAccountId = firstAccount.id; awsClients = createAWSClients(firstAccount); console.error(`[Account Manager] Auto-selected single account: ${firstAccount.name}`); } else if (accounts.size > 0) { console.error('[Account Manager] Multiple accounts available. Use amplify_switch_account to select one.'); } } catch (error) { console.error('[Account Manager] Error loading accounts:', error); } } export async function handleListAccounts() { const accountList = Array.from(accounts.values()).map(acc => ({ id: acc.id, name: acc.name, region: acc.region, isActive: acc.id === accountState.activeAccountId, isDefault: acc.id === accountState.defaultAccountId, hasGitHub: !!acc.githubToken })); return { accounts: accountList, activeAccountId: accountState.activeAccountId, defaultAccountId: accountState.defaultAccountId, totalAccounts: accountList.length }; } export async function handleSwitchAccount(accountId) { if (!accountId) { throw new Error('account_id is required'); } const account = accounts.get(accountId); if (!account) { throw new Error(`Account ${accountId} not found`); } accountState.activeAccountId = accountId; awsClients = createAWSClients(account); return { success: true, activeAccount: { id: account.id, name: account.name, region: account.region } }; } export async function handleAddAccount(args) { const { name, access_key_id, secret_access_key, region, session_token, profile, github_username, github_token } = args; if (!name || !access_key_id || !secret_access_key || !region) { throw new Error('name, access_key_id, secret_access_key, and region are required'); } const accountId = uuidv4(); const account = { id: accountId, name, accessKeyId: access_key_id, secretAccessKey: secret_access_key, region, sessionToken: session_token, profile, githubUsername: github_username, githubToken: github_token }; await credentialManager.saveAccount(account); accounts.set(accountId, account); if (accounts.size === 1) { accountState.activeAccountId = accountId; accountState.defaultAccountId = accountId; await credentialManager.setDefaultAccount(accountId); awsClients = createAWSClients(account); } return { success: true, account: { id: accountId, name: account.name, region: account.region, isActive: accountId === accountState.activeAccountId, isDefault: accountId === accountState.defaultAccountId } }; } export async function handleRemoveAccount(accountId) { if (!accountId) { throw new Error('account_id is required'); } const account = accounts.get(accountId); if (!account) { throw new Error(`Account ${accountId} not found`); } await credentialManager.deleteAccount(accountId); accounts.delete(accountId); if (accountState.activeAccountId === accountId) { accountState.activeAccountId = null; awsClients = null; if (accounts.size > 0) { const firstAccount = Array.from(accounts.values())[0]; accountState.activeAccountId = firstAccount.id; awsClients = createAWSClients(firstAccount); } } if (accountState.defaultAccountId === accountId) { accountState.defaultAccountId = null; } return { success: true, removedAccount: account.name, remainingAccounts: accounts.size }; } export async function handleGetActiveAccount() { if (!accountState.activeAccountId) { return { activeAccount: null, message: 'No active account. Use amplify_add_account to add one.' }; } const account = accounts.get(accountState.activeAccountId); if (!account) { return { activeAccount: null, message: 'Active account not found in memory' }; } return { activeAccount: { id: account.id, name: account.name, region: account.region, isDefault: account.id === accountState.defaultAccountId, hasGitHub: !!account.githubToken } }; } export async function handleSetDefaultAccount(accountId) { if (!accountId) { throw new Error('account_id is required'); } const account = accounts.get(accountId); if (!account) { throw new Error(`Account ${accountId} not found`); } accountState.defaultAccountId = accountId; await credentialManager.setDefaultAccount(accountId); return { success: true, defaultAccount: { id: account.id, name: account.name, region: account.region } }; } export async function handleUpdateAccount(args) { const { account_id, access_key_id, secret_access_key, session_token, github_username, github_token } = args; if (!account_id) { throw new Error('account_id is required'); } const account = accounts.get(account_id); if (!account) { throw new Error(`Account ${account_id} not found`); } // Update fields if provided if (access_key_id) account.accessKeyId = access_key_id; if (secret_access_key) account.secretAccessKey = secret_access_key; if (session_token !== undefined) account.sessionToken = session_token; if (github_username !== undefined) account.githubUsername = github_username; if (github_token !== undefined) account.githubToken = github_token; // Save updated account await credentialManager.saveAccount(account); // Update AWS clients if this is the active account if (account_id === accountState.activeAccountId) { awsClients = createAWSClients(account); } return { success: true, updatedAccount: { id: account.id, name: account.name, region: account.region, hasGitHub: !!account.githubToken } }; } //# sourceMappingURL=account-handlers.js.map