getCurrentUser
Retrieve the currently authenticated user's profile and session information from AWS Cognito for identity verification and user context management.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- index.ts:398-460 (handler)The handler function for the 'getCurrentUser' tool. It fetches the current Cognito user using userPool.getCurrentUser(), retrieves the session and user attributes, and returns the attributes along with session validity, or appropriate error messages if no user is signed in or errors occur.async ({ }) => { return new Promise((resolve, reject) => { const cognitoUser = userPool.getCurrentUser(); if (!cognitoUser) { resolve({ content: [ { type: "text" as const, text: "No user currently signed in", } ] }); return; } cognitoUser.getSession((err: Error | null, _session: CognitoUserSession) => { if (err) { reject({ content: [ { type: "text" as const, text: `Error getting session: ${err.message}`, } ] }); return; } cognitoUser.getUserAttributes((err, attributes) => { if (err) { reject({ content: [ { type: "text" as const, text: `Error getting user attributes: ${err.message}`, } ] }); return; } const attributeItems = attributes || []; resolve({ content: [ { type: "text" as const, text: "User attributes:", }, ...attributeItems.map((attr) => ({ type: "text" as const, text: `${attr.getName()}: ${attr.getValue()}`, })), { type: "text" as const, text: `Session validity: ${_session.isValid() ? 'Valid' : 'Invalid'}`, } ] }); }); }); }); }
- index.ts:394-461 (registration)Registration of the 'getCurrentUser' MCP tool using server.tool(). Input schema is empty object (no parameters). The handler is the inline async function.server.tool( "getCurrentUser", { }, async ({ }) => { return new Promise((resolve, reject) => { const cognitoUser = userPool.getCurrentUser(); if (!cognitoUser) { resolve({ content: [ { type: "text" as const, text: "No user currently signed in", } ] }); return; } cognitoUser.getSession((err: Error | null, _session: CognitoUserSession) => { if (err) { reject({ content: [ { type: "text" as const, text: `Error getting session: ${err.message}`, } ] }); return; } cognitoUser.getUserAttributes((err, attributes) => { if (err) { reject({ content: [ { type: "text" as const, text: `Error getting user attributes: ${err.message}`, } ] }); return; } const attributeItems = attributes || []; resolve({ content: [ { type: "text" as const, text: "User attributes:", }, ...attributeItems.map((attr) => ({ type: "text" as const, text: `${attr.getName()}: ${attr.getValue()}`, })), { type: "text" as const, text: `Session validity: ${_session.isValid() ? 'Valid' : 'Invalid'}`, } ] }); }); }); }); } )
- index.ts:396-397 (schema)Input schema for 'getCurrentUser' tool: empty object, indicating no input parameters required.{ },