Skip to main content
Glama

instagram_login

Authenticate with Instagram to establish a persistent session that remains active across server restarts, enabling access to Instagram features.

Instructions

Login to an Instagram account. The session will be saved and persist across server restarts.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
usernameYesInstagram username
passwordYesInstagram password

Implementation Reference

  • The execute method of the LoginTool class that handles Instagram login using credentials from config, checks existing session, initializes client, performs login with igpapiClient, handles success response, and prompts for 2FA if required.
    async execute(args: Record<string, unknown>): Promise<ToolResult> { // Get credentials from environment variables via config const config = getConfig(); const username = config.igpapi?.username; const password = config.igpapi?.password; // Validate that credentials are available if (!username || !password) { throw new Error( "Instagram credentials not found in environment variables. " + "Please set IG_USERNAME and IG_PASSWORD environment variables." ); } // Check if already logged in if (igpapiClient.isLoggedIn()) { try { const currentUser = await igpapiClient.getCurrentUser(); return { content: [ { type: "text", text: `Already logged in as: ${currentUser.username || username}\nSession is active and persistent.`, }, ], }; } catch { // Session might be invalid, continue with login } } // Initialize client if needed if (!igpapiClient.isInitialized()) { await igpapiClient.initialize(); } try { // Perform login await igpapiClient.login(username, password); // Get user info to confirm login const user = await igpapiClient.getCurrentUser(); return { content: [ { type: "text", text: `Successfully logged in to Instagram!\n\nAccount: ${user.username}\nFull Name: ${user.full_name || "N/A"}\nUser ID: ${user.pk}\n\nSession has been saved and will persist across server restarts.`, }, ], }; } catch (error) { // Check if this is a 2FA requirement if (error && typeof error === "object" && "verificationMethod" in error) { const twoFactorInfo = error as { username: string; verificationMethod: "0" | "1"; totpTwoFactorOn: boolean; }; const methodName = twoFactorInfo.verificationMethod === "1" ? "SMS" : "TOTP (Authenticator App)"; const codeSentMessage = twoFactorInfo.verificationMethod === "1" ? "A verification code has been sent to your phone via SMS." : "Please check your authenticator app for the verification code."; return { content: [ { type: "text", text: `Two-Factor Authentication (2FA) is required for this account.\n\n${codeSentMessage}\n\nVerification Method: ${methodName}\nUsername: ${twoFactorInfo.username}\n\nPlease use the 'instagram_complete_2fa' tool with the verification code to complete the login.`, }, ], isError: false, // Not an error, just requires additional step }; } // Re-throw other errors throw error; } }
  • Defines the tool schema including name, description, and empty input schema (no parameters required).
    getDefinition(): ToolDefinition { return { name: "instagram_login", description: "Login to an Instagram account using credentials from environment variables (IG_USERNAME and IG_PASSWORD). The session will be saved and persist across server restarts.", inputSchema: { type: "object", properties: {}, required: [], }, }; }
  • Imports LoginTool and instantiates it in the tools array for registration and use in getAllToolDefinitions() and executeTool().
    import { LoginTool } from "./login.js"; import { Complete2FATool } from "./complete_2fa.js"; import { SearchAccountsTool } from "./search_accounts.js"; import { LogoutTool } from "./logout.js"; import { GetUserProfileTool } from "./get_user_profile.js"; import { GetCurrentUserProfileTool } from "./get_current_user_profile.js"; import { GetUserPostsTool } from "./get_user_posts.js"; import { LikePostTool } from "./like_post.js"; import type { BaseTool } from "./base.js"; // Import all tools const tools: BaseTool[] = [ new LoginTool(), new Complete2FATool(), new SearchAccountsTool(), new LogoutTool(), new GetUserProfileTool(), new GetCurrentUserProfileTool(), new GetUserPostsTool(), new LikePostTool(), // Add more tools here as you create them ];

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/anand-kamble/mcp-instagram'

If you have feedback or need assistance with the MCP directory API, please join our Discord server