getCurrentUser
Retrieves the currently authenticated user's information from AWS Cognito for authentication verification and user management.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- index.ts:398-460 (handler)The handler function that executes the 'getCurrentUser' tool. It retrieves the current Cognito user from the user pool, checks if signed in, fetches the session and user attributes asynchronously, and returns a formatted content response with attributes and session validity.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 the 'getCurrentUser' tool, which requires no parameters (empty object).{ },
- index.ts:394-461 (registration)Registration of the 'getCurrentUser' tool using server.tool(), including name, schema, and handler reference.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'}`, } ] }); }); }); }); } )