get_user_profile
Retrieve authenticated user profile information from Kroger, including personal and account details, for seamless integration with AI assistants and shopping features.
Instructions
Get the authenticated user's Kroger profile information.
Returns:
Dictionary containing user profile data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The handler function implementing the get_user_profile tool. It uses the authenticated Kroger client to fetch the user's profile ID and returns a success response with the ID or an error.@mcp.tool() async def get_user_profile(ctx: Context = None) -> Dict[str, Any]: """ Get the authenticated user's Kroger profile information. Returns: Dictionary containing user profile data """ if ctx: await ctx.info("Getting user profile information") try: client = get_authenticated_client() profile = client.identity.get_profile() if profile and "data" in profile: profile_id = profile["data"].get("id", "N/A") if ctx: await ctx.info(f"Retrieved profile for user ID: {profile_id}") return { "success": True, "profile_id": profile_id, "message": "User profile retrieved successfully", "note": "The Kroger Identity API only provides the profile ID for privacy reasons." } else: return { "success": False, "message": "Failed to retrieve user profile" } except Exception as e: if ctx: await ctx.error(f"Error getting user profile: {str(e)}") return { "success": False, "error": str(e) }
- src/kroger_mcp/server.py:76-76 (registration)Registration of the profile_tools module (containing get_user_profile) by calling its register_tools function on the MCP server instance.profile_tools.register_tools(mcp)
- Helper function get_authenticated_client() that retrieves or initializes the authenticated KrogerAPI client instance, used by the get_user_profile handler.def get_authenticated_client() -> KrogerAPI: """Get or create a user-authenticated client for cart operations This function attempts to load an existing token or prompts for authentication. In an MCP context, the user needs to explicitly call start_authentication and complete_authentication tools to authenticate. Returns: KrogerAPI: Authenticated client Raises: Exception: If no valid token is available and authentication is required """ global _authenticated_client if _authenticated_client is not None and _authenticated_client.test_current_token(): # Client exists and token is still valid return _authenticated_client # Clear the reference if token is invalid _authenticated_client = None try: load_and_validate_env(["KROGER_CLIENT_ID", "KROGER_CLIENT_SECRET", "KROGER_REDIRECT_URI"]) # Try to load existing user token first token_file = ".kroger_token_user.json" token_info = load_token(token_file) if token_info: # Create a new client with the loaded token _authenticated_client = KrogerAPI() _authenticated_client.client.token_info = token_info _authenticated_client.client.token_file = token_file if _authenticated_client.test_current_token(): # Token is valid, use it return _authenticated_client # Token is invalid, try to refresh it if "refresh_token" in token_info: try: _authenticated_client.authorization.refresh_token(token_info["refresh_token"]) # If refresh was successful, return the client if _authenticated_client.test_current_token(): return _authenticated_client except Exception: # Refresh failed, need to re-authenticate _authenticated_client = None # No valid token available, need user-initiated authentication raise Exception( "Authentication required. Please use the start_authentication tool to begin the OAuth flow, " "then complete it with the complete_authentication tool." ) except Exception as e: if "Authentication required" in str(e): # This is an expected error when authentication is needed raise else: # Other unexpected errors raise Exception(f"Authentication failed: {str(e)}")