Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.7 KB
Raw Blame
import { createAction, Property } from "@activepieces/pieces-framework";
import { makeRequest } from "../common/client";
import { AgentXAuth } from "../common/auth";
import { HttpMethod } from "@activepieces/pieces-common";

export const searchAgents = createAction({
  auth: AgentXAuth,
  name: "search_agents",
  classification: 'SEARCH',
  displayName: "Search Agents",
  description: "Find agents by name or ID using search filters.",
  audience: 'both',
  aiMetadata: { description: 'Lists AgentX agents on the account, optionally narrowing the results by partial name match and/or exact agent ID; leave both filters blank to return all agents. Use this to discover available agents or resolve an agent ID before starting a conversation. Read-only and idempotent.', idempotent: true },
  props: {
    name: Property.ShortText({
      displayName: "Agent Name",
      description: "Search by agent name (partial matches allowed).",
      required: false,
    }),
    id: Property.ShortText({
      displayName: "Agent ID",
      description: "Search by exact Agent ID.",
      required: false,
    }),
  },

  async run({ auth, propsValue }) {
    const { name, id } = propsValue;

    const agents = await makeRequest(auth.secret_text, HttpMethod.GET, "/agents");

    if (!Array.isArray(agents)) {
      throw new Error("Unexpected response from AgentX API: expected an array of agents.");
    }

    const filtered = agents.filter((agent: any) => {
      let matches = true;

      if (id) {
        matches = matches && agent._id === id;
      }

      if (name && agent.name) {
        matches = matches && agent.name.toLowerCase().includes(name.toLowerCase());
      }

      return matches;
    });

    return filtered;
  },
});