Skip to main content
Glama

instagram_search_accounts

Find Instagram accounts by username or name query to retrieve profile information for matching results.

Instructions

Search for Instagram accounts by username query. Returns a list of matching accounts with their profile information.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe search query (username or name to search for)
limitNoMaximum number of results to return (default: 20, max: 50)

Implementation Reference

  • The 'execute' method contains the core handler logic for the 'instagram_search_accounts' tool. It validates input, checks login status, performs the user search via igpapiClient, limits and formats the results, and returns a structured ToolResult.
    async execute(args: { query: string; limit?: number }): Promise<ToolResult> {
      const { query, limit = 20 } = args;
    
      // Validate inputs
      if (!query || query.trim().length === 0) {
        throw new Error("Search query is required and cannot be empty");
      }
    
      const searchLimit = Math.min(Math.max(1, limit || 20), 50);
    
      // Check if logged in
      if (!igpapiClient.isLoggedIn()) {
        throw new Error("Not logged in. Please use the instagram_login tool to authenticate first.");
      }
    
      // Ensure client is initialized
      if (!igpapiClient.isInitialized()) {
        await igpapiClient.initialize();
      }
    
      const client = igpapiClient.getClient();
    
      try {
        // Perform account search
        const searchResponse = await client.user.search(query) as UserRepositorySearchResponseRootObject;
    
        // Extract users array from response
        // The response structure contains a 'users' property with the array of results
        const users = searchResponse.users || [];
        const totalResults = users.length;
    
        // Limit results
        const limitedResults = users.slice(0, searchLimit);
    
        // Format results
        if (limitedResults.length === 0) {
          return {
            content: [
              {
                type: "text",
                text: `No accounts found matching "${query}"`,
              },
            ],
          };
        }
    
        const formattedResults = limitedResults.map((user, index: number) => {
          const profile = {
            rank: index + 1,
            username: user.username || "N/A",
            fullName: user.full_name || "N/A",
            userId: user.pk || "N/A",
            isVerified: user.is_verified || false,
            isPrivate: user.is_private || false,
            followerCount: user.follower_count || 0,
          };
    
          return `\n${profile.rank}. @${profile.username}${profile.isVerified ? " βœ“" : ""}
     Full Name: ${profile.fullName}
     User ID: ${profile.userId}
     ${profile.isPrivate ? "πŸ”’ Private Account" : "🌐 Public Account"}
     Followers: ${profile.followerCount.toLocaleString()}`;
        }).join("\n");
    
        const summary = `Found ${limitedResults.length} account${limitedResults.length === 1 ? "" : "s"} matching "${query}"${totalResults > searchLimit ? ` (showing first ${searchLimit} of ${totalResults} total)` : ""}:`;
    
        return {
          content: [
            {
              type: "text",
              text: `${summary}${formattedResults}`,
            },
          ],
        };
      } catch (error) {
        // Re-throw to be handled by base error handling
        throw error;
      }
    }
  • The 'getDefinition' method provides the tool schema, including name 'instagram_search_accounts', description, and input schema requiring a 'query' string with optional 'limit'.
    getDefinition(): ToolDefinition {
      return {
        name: "instagram_search_accounts",
        description: "Search for Instagram accounts by username query. Returns a list of matching accounts with their profile information.",
        inputSchema: {
          type: "object",
          properties: {
            query: {
              type: "string",
              description: "The search query (username or name to search for)",
            },
            limit: {
              type: "number",
              description: "Maximum number of results to return (default: 20, max: 50)",
              default: 20,
            },
          },
          required: ["query"],
        },
      };
  • The tool is imported and instantiated as 'new SearchAccountsTool()' in the central tools registry array, which is used by getAllToolDefinitions() for MCP registration and executeTool() for invocation.
    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 { LikeCommentTool } from "./like_comment.js";
    import { CommentOnPostTool } from "./comment_on_post.js";
    import { GetPostCommentsTool } from "./get_post_comments.js";
    import { GetPostDetailsTool } from "./get_post_details.js";
    import { GetUserStoriesTool } from "./get_user_stories.js";
    import { GetTimelineFeedTool } from "./get_timeline_feed.js";
    import { FollowUserTool } from "./follow_user.js";
    import type { BaseTool } from "./base.js";
    
    // Import all tools
    const tools: BaseTool[] = [
      new LoginTool(),
      new Complete2FATool(),
      new SearchAccountsTool(),

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