Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

16.5 KB
Raw Blame
import {
  AgentVaultCredentialType,
  AgentVaultMcpTransport,
  AgentVaultServiceType
} from "@app/hooks/api/agentVault";

// Copied from the proxied-services catalog rather than imported: that module stays independently
// deletable, and Agent Vault's grammar rejects the paths several of its entries carry.
export enum AgentVaultTemplateCategory {
  Llm = "LLM Providers",
  DeveloperTools = "Developer Tools",
  Communication = "Communication",
  Monitoring = "Monitoring",
  Productivity = "Productivity",
  Payments = "Payments",
  Commerce = "Commerce"
}

export type AgentVaultTemplateCredential =
  | { type: AgentVaultCredentialType.Bearer; headerName?: string; headerPrefix?: string }
  | { type: AgentVaultCredentialType.Basic; username?: string }
  | { type: AgentVaultCredentialType.Passthrough };

export type AgentVaultHttpTemplate = {
  serviceType: AgentVaultServiceType.Http;
  key: string;
  name: string;
  image: string;
  category: AgentVaultTemplateCategory;
  description: string;
  hostPattern: string;
  aliases?: string[];
  credential: AgentVaultTemplateCredential;
};

export type AgentVaultMcpTemplate = {
  serviceType: AgentVaultServiceType.Mcp;
  key: string;
  name: string;
  image: string;
  description: string;
  serverUrl: string;
  transport: AgentVaultMcpTransport;
  aliases?: string[];
  credential:
    | { type: AgentVaultCredentialType.Bearer; headerName?: string; headerPrefix?: string }
    | { type: AgentVaultCredentialType.OAuth };
};

export type AgentVaultTemplate = AgentVaultHttpTemplate | AgentVaultMcpTemplate;

const HTTP_TEMPLATES: Omit<AgentVaultHttpTemplate, "serviceType">[] = [
  {
    key: "openai",
    name: "OpenAI",
    image: "OpenAIWhite.png",
    category: AgentVaultTemplateCategory.Llm,
    description: "Chat and models from OpenAI.",
    hostPattern: "api.openai.com",
    aliases: ["gpt", "chatgpt"],
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "anthropic",
    name: "Anthropic",
    image: "Anthropic.png",
    category: AgentVaultTemplateCategory.Llm,
    description: "Claude models from Anthropic.",
    hostPattern: "api.anthropic.com",
    aliases: ["claude"],
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "x-api-key",
      headerPrefix: ""
    }
  },
  {
    key: "gemini",
    name: "Google Gemini",
    image: "Gemini.svg",
    category: AgentVaultTemplateCategory.Llm,
    description: "Google's Gemini models.",
    hostPattern: "generativelanguage.googleapis.com",
    aliases: ["google", "bard", "vertex"],
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "x-goog-api-key",
      headerPrefix: ""
    }
  },
  {
    key: "mistral",
    name: "Mistral AI",
    image: "Mistral.svg",
    category: AgentVaultTemplateCategory.Llm,
    description: "Chat and embedding models from Mistral.",
    hostPattern: "api.mistral.ai",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "cohere",
    name: "Cohere",
    image: "Cohere.svg",
    category: AgentVaultTemplateCategory.Llm,
    description: "Cohere's language models.",
    hostPattern: "api.cohere.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "groq",
    name: "Groq",
    image: "Groq.svg",
    category: AgentVaultTemplateCategory.Llm,
    description: "Fast model inference from Groq.",
    hostPattern: "api.groq.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "perplexity",
    name: "Perplexity",
    image: "Perplexity.svg",
    category: AgentVaultTemplateCategory.Llm,
    description: "Perplexity's answer engine.",
    hostPattern: "api.perplexity.ai",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "openrouter",
    name: "OpenRouter",
    image: "OpenRouter.png",
    category: AgentVaultTemplateCategory.Llm,
    description: "One key for many AI models.",
    hostPattern: "openrouter.ai",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "together",
    name: "Together AI",
    image: "Together.svg",
    category: AgentVaultTemplateCategory.Llm,
    description: "Open models on Together AI.",
    hostPattern: "api.together.ai, api.together.xyz",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "deepseek",
    name: "DeepSeek",
    image: "DeepSeek.svg",
    category: AgentVaultTemplateCategory.Llm,
    description: "DeepSeek's chat and reasoning models.",
    hostPattern: "api.deepseek.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "xai",
    name: "xAI (Grok)",
    image: "xAI.svg",
    category: AgentVaultTemplateCategory.Llm,
    description: "Grok models from xAI.",
    hostPattern: "api.x.ai",
    aliases: ["grok"],
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "fireworks",
    name: "Fireworks AI",
    image: "Fireworks.png",
    category: AgentVaultTemplateCategory.Llm,
    description: "Fast open-model inference.",
    hostPattern: "api.fireworks.ai",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "stripe",
    name: "Stripe",
    image: "Stripe.svg",
    category: AgentVaultTemplateCategory.Payments,
    description: "Payments and billing.",
    hostPattern: "api.stripe.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "twilio",
    name: "Twilio",
    image: "Twilio.svg",
    category: AgentVaultTemplateCategory.Communication,
    description: "SMS, voice, and messaging.",
    hostPattern: "api.twilio.com",
    credential: { type: AgentVaultCredentialType.Basic }
  },
  {
    key: "sendgrid",
    name: "SendGrid",
    image: "SendGrid.png",
    category: AgentVaultTemplateCategory.Communication,
    description: "Send transactional email.",
    hostPattern: "api.sendgrid.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "resend",
    name: "Resend",
    image: "Resend.svg",
    category: AgentVaultTemplateCategory.Communication,
    description: "Email built for developers.",
    hostPattern: "api.resend.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "slack",
    name: "Slack",
    image: "Slack.svg",
    category: AgentVaultTemplateCategory.Communication,
    description: "Post messages and build Slack apps.",
    hostPattern: "slack.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "discord",
    name: "Discord",
    image: "Discord.svg",
    category: AgentVaultTemplateCategory.Communication,
    description: "Build and run Discord bots.",
    hostPattern: "discord.com",
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "Authorization",
      headerPrefix: "Bot"
    }
  },
  {
    key: "github",
    name: "GitHub",
    image: "GitHub.png",
    category: AgentVaultTemplateCategory.DeveloperTools,
    description: "Repos, issues, and more on GitHub.",
    // Git over HTTPS only accepts the token as a Basic password, and the API takes Basic too, so one
    // credential covers both.
    hostPattern: "api.github.com, github.com, uploads.github.com, raw.githubusercontent.com",
    credential: { type: AgentVaultCredentialType.Basic, username: "x-access-token" }
  },
  {
    key: "gitlab",
    name: "GitLab",
    image: "GitLab.png",
    category: AgentVaultTemplateCategory.DeveloperTools,
    description: "Repos and pipelines on GitLab.",
    hostPattern: "gitlab.com",
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "PRIVATE-TOKEN",
      headerPrefix: ""
    }
  },
  {
    key: "vercel",
    name: "Vercel",
    image: "Vercel.png",
    category: AgentVaultTemplateCategory.DeveloperTools,
    description: "Deploys and the Vercel API.",
    hostPattern: "api.vercel.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "cloudflare",
    name: "Cloudflare",
    image: "Cloudflare.png",
    category: AgentVaultTemplateCategory.DeveloperTools,
    description: "DNS, CDN, and the Cloudflare API.",
    hostPattern: "api.cloudflare.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "supabase",
    name: "Supabase",
    image: "Supabase.png",
    category: AgentVaultTemplateCategory.DeveloperTools,
    description: "Postgres, auth, and storage.",
    hostPattern: "<your-project>.supabase.co",
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "apikey",
      headerPrefix: ""
    }
  },
  {
    key: "npm",
    name: "npm",
    image: "NPM.svg",
    category: AgentVaultTemplateCategory.DeveloperTools,
    description: "The npm package registry.",
    hostPattern: "registry.npmjs.org",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "github-npm",
    name: "GitHub Packages",
    image: "GitHub.png",
    category: AgentVaultTemplateCategory.DeveloperTools,
    description: "GitHub's npm package registry.",
    hostPattern: "npm.pkg.github.com",
    aliases: ["npm", "packages"],
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "datadog",
    name: "Datadog",
    image: "Datadog.png",
    category: AgentVaultTemplateCategory.Monitoring,
    description: "Monitoring and analytics.",
    hostPattern: "api.datadoghq.com",
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "DD-API-KEY",
      headerPrefix: ""
    }
  },
  {
    key: "sentry",
    name: "Sentry",
    image: "Sentry.svg",
    category: AgentVaultTemplateCategory.Monitoring,
    description: "Error and performance monitoring.",
    hostPattern: "sentry.io",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "pagerduty",
    name: "PagerDuty",
    image: "PagerDuty.svg",
    category: AgentVaultTemplateCategory.Monitoring,
    description: "On-call and incident management.",
    hostPattern: "api.pagerduty.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "linear",
    name: "Linear",
    image: "Linear.svg",
    category: AgentVaultTemplateCategory.Productivity,
    description: "Issue tracking and project management.",
    hostPattern: "api.linear.app",
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "Authorization",
      headerPrefix: ""
    }
  },
  {
    key: "notion",
    name: "Notion",
    image: "Notion.svg",
    category: AgentVaultTemplateCategory.Productivity,
    description: "Notion workspace and docs.",
    hostPattern: "api.notion.com",
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "jira",
    name: "Jira",
    image: "Jira.svg",
    category: AgentVaultTemplateCategory.Productivity,
    description: "Atlassian Jira issue tracking.",
    hostPattern: "<your-tenant>.atlassian.net",
    credential: { type: AgentVaultCredentialType.Basic }
  },
  {
    key: "google-workspace",
    name: "Google Workspace",
    image: "Google Workspace.svg",
    category: AgentVaultTemplateCategory.Productivity,
    description: "Gmail, Calendar, Sheets, and Drive.",
    hostPattern: "gmail.googleapis.com, sheets.googleapis.com, www.googleapis.com",
    aliases: ["workspace", "gsuite", "gmail", "calendar", "sheets", "drive", "gdrive"],
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    key: "postmark",
    name: "Postmark",
    image: "Postmark.png",
    category: AgentVaultTemplateCategory.Communication,
    description: "Transactional email.",
    hostPattern: "api.postmarkapp.com",
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "X-Postmark-Server-Token",
      headerPrefix: ""
    }
  },
  {
    key: "shopify",
    name: "Shopify",
    image: "Shopify.svg",
    category: AgentVaultTemplateCategory.Commerce,
    description: "Shopify e-commerce API.",
    hostPattern: "<your-store>.myshopify.com",
    credential: {
      type: AgentVaultCredentialType.Bearer,
      headerName: "X-Shopify-Access-Token",
      headerPrefix: ""
    }
  }
];

export const AGENT_VAULT_TEMPLATES: AgentVaultHttpTemplate[] = HTTP_TEMPLATES.map((template) => ({
  ...template,
  serviceType: AgentVaultServiceType.Http
}));

export const AGENT_VAULT_MCP_TEMPLATES: AgentVaultMcpTemplate[] = [
  {
    serviceType: AgentVaultServiceType.Mcp,
    key: "github-mcp",
    name: "GitHub MCP",
    image: "GitHub.png",
    description: "Repos, issues, and pull requests as MCP tools.",
    serverUrl: "https://api.githubcopilot.com/mcp/",
    transport: AgentVaultMcpTransport.StreamableHttp,
    aliases: ["github", "mcp"],
    credential: { type: AgentVaultCredentialType.Bearer }
  },
  {
    serviceType: AgentVaultServiceType.Mcp,
    key: "linear-mcp",
    name: "Linear MCP",
    image: "Linear.svg",
    description: "Issues and projects as MCP tools.",
    serverUrl: "https://mcp.linear.app/mcp",
    transport: AgentVaultMcpTransport.StreamableHttp,
    aliases: ["linear", "mcp"],
    credential: { type: AgentVaultCredentialType.OAuth }
  },
  {
    serviceType: AgentVaultServiceType.Mcp,
    key: "notion-mcp",
    name: "Notion MCP",
    image: "Notion.svg",
    description: "Pages and databases as MCP tools.",
    serverUrl: "https://mcp.notion.com/mcp",
    transport: AgentVaultMcpTransport.StreamableHttp,
    aliases: ["notion", "mcp"],
    credential: { type: AgentVaultCredentialType.OAuth }
  },
  {
    serviceType: AgentVaultServiceType.Mcp,
    key: "sentry-mcp",
    name: "Sentry MCP",
    image: "Sentry.svg",
    description: "Issues and errors as MCP tools.",
    serverUrl: "https://mcp.sentry.dev/mcp",
    transport: AgentVaultMcpTransport.StreamableHttp,
    aliases: ["sentry", "mcp"],
    credential: { type: AgentVaultCredentialType.OAuth }
  }
];

export const POPULAR_AGENT_VAULT_TEMPLATES = [
  "openai",
  "anthropic",
  "github",
  "slack",
  "google-workspace"
];

const stripPort = (pattern: string) => pattern.split(":")[0];

// A template whose host starts with a placeholder label, such as <your-tenant>.atlassian.net, is
// saved with that label replaced. Treat it exactly like a "*." wildcard, or the icon is lost the
// moment someone fills their tenant in.
const ONE_LABEL_PREFIX_RE = /^(?:\*|<[^>]+>)\./;

const hostMatchesTemplateHost = (host: string, templateHost: string) => {
  if (!ONE_LABEL_PREFIX_RE.test(templateHost)) return host === templateHost;
  const suffix = templateHost.replace(ONE_LABEL_PREFIX_RE, "");
  if (!host.endsWith(`.${suffix}`)) return false;
  return !host.slice(0, host.length - suffix.length - 1).includes(".");
};

const hostOfUrl = (url: string) => {
  try {
    return new URL(url).hostname;
  } catch {
    return "";
  }
};

const templateHosts = (template: AgentVaultTemplate) =>
  template.serviceType === AgentVaultServiceType.Mcp
    ? [hostOfUrl(template.serverUrl)]
    : template.hostPattern.split(",").map((entry) => stripPort(entry.trim()));

const hostsOfPattern = (hostPattern: string) =>
  hostPattern
    .split(",")
    .map((entry) => stripPort(entry.trim()))
    .filter(Boolean);

export const findTemplateForHostPattern = (
  hostPattern: string
): AgentVaultHttpTemplate | undefined => {
  const hosts = hostsOfPattern(hostPattern);

  return AGENT_VAULT_TEMPLATES.find((template) =>
    templateHosts(template).some((templateHost) =>
      hosts.some((host) => hostMatchesTemplateHost(host, templateHost))
    )
  );
};

export const findMcpTemplateForHostPattern = (
  hostPattern: string
): AgentVaultMcpTemplate | undefined => {
  const hosts = hostsOfPattern(hostPattern);
  return AGENT_VAULT_MCP_TEMPLATES.find((template) =>
    templateHosts(template).some((templateHost) => hosts.includes(templateHost))
  );
};

/** For icons: an MCP service's host pattern is derived from its server URL, so the MCP templates match it too. */
export const findAnyTemplateForHostPattern = (
  hostPattern: string
): AgentVaultTemplate | undefined =>
  findTemplateForHostPattern(hostPattern) ?? findMcpTemplateForHostPattern(hostPattern);

const MCP_PATH_SEGMENTS = new Set(["mcp", "sse"]);

export const lastPathSegment = (path: string) =>
  path.split("?")[0].split("/").filter(Boolean).at(-1)?.toLowerCase() ?? "";

/**
 * Which kind of service a request from a session log most likely went to, for prefilling the service sheet: a known
 * MCP server, else a host whose first label is `mcp` or a path ending in `/mcp` or `/sse`, else an HTTP API.
 */
export const guessServiceTypeForRequest = (
  hostPattern: string,
  path = ""
): AgentVaultServiceType => {
  const [firstLabel] = stripPort(hostPattern).split(".");
  const isMcp =
    Boolean(findMcpTemplateForHostPattern(hostPattern)) ||
    firstLabel.toLowerCase() === "mcp" ||
    MCP_PATH_SEGMENTS.has(lastPathSegment(path));
  return isMcp ? AgentVaultServiceType.Mcp : AgentVaultServiceType.Http;
};