Last updated

LangChain - Frontegg AI for TypeScript

The Frontegg AI TypeScript SDK - @frontegg/ai-sdk - helps you integrate Frontegg-powered tools and identity into your LangChain agents. With built-in support for user authentication, context-aware prompts, and dynamic tool injection, it enables secure and personalized agent experiences.



Prerequisites

  • Node.js v18.16.0+
  • TypeScript v5.7.2+
  • npm or yarn
  • A working LangChain agent project
  • A Frontegg account
  • Your Frontegg Client ID, Client Secret, and Agent ID

Install the SDK

npm install @frontegg/ai-sdk

Configure the SDK

Create a frontegg.config.ts file and initialize the client using environment variables:

// frontegg.config.ts
import { Environment, FronteggAiClient } from '@frontegg/ai-sdk';

export const FronteggAiClient = await FronteggAiClient.getInstance({
  agentId: process.env.FRONTEGG_AGENT_ID!,
  clientId: process.env.FRONTEGG_CLIENT_ID!,
  clientSecret: process.env.FRONTEGG_CLIENT_SECRET!,
  environment: Environment.EU,
});

Ensure your .env file includes:

FRONTEGG_CLIENT_ID=your_client_id
FRONTEGG_CLIENT_SECRET=your_client_secret
FRONTEGG_AGENT_ID=your_agent_id

  • Client ID / Secret: Settings → Keys & Domains
  • Base URL: Settings → Domains tab (e.g., https://app-xxxx.frontegg.com)
  • Agent ID: AI Agents → Agents → [Your Agent]

Create a LangChain agent with Frontegg tools

Fetch user-specific tools from Frontegg and dynamically construct a LangChain agent:

public async processRequest(request: string, userJwt: string): Promise<any> {
  if (!this.fronteggAiClient) {
    throw new Error('Frontegg client not initialized');
  }

  await this.fronteggAiClient.setUserContextByJWT(userJwt);
  const tools = await this.fronteggAiClient.getToolsAsLangchainTools();

  const messages = [
    {
      role: 'system',
      content: this.fronteggAiClient.addUserContextToSystemPrompt(this.systemMessage),
    },
    ...this.conversationHistory,
    new MessagesPlaceholder('agent_scratchpad'),
  ];

  const prompt = ChatPromptTemplate.fromMessages(messages);

  const openAIFunctionsAgent = await createOpenAIFunctionsAgent({
    llm: this.model as any,
    tools: tools as any,
    prompt: prompt as any,
  });

  this.agent = new AgentExecutor({
    agent: openAIFunctionsAgent as any,
    tools: tools as any,
    verbose: true,
  });

  const result = await this.agent.invoke({ input: request });

  return result;
}