docs/content/cookbooks/chat-app.mdx
Give your AI agent access to Gmail, GitHub, Slack, Notion, and 1000+ other apps. This tutorial builds a Next.js chat app that handles tool discovery, user authentication, and action execution in about 30 lines of code.
Stack: Next.js, Vercel AI SDK, OpenAI, Composio
Create a new Next.js app and install the dependencies:
bunx create-next-app composio-chat --yes
cd composio-chat
bun add @composio/core @composio/vercel @ai-sdk/openai ai @ai-sdk/react
Add your API keys to a .env.local file in the project root:
COMPOSIO_API_KEY=your_composio_api_key
OPENAI_API_KEY=your_openai_api_key
Start the dev server:
bun dev
A session scopes tools and credentials to a specific user. Create app/api/chat/route.ts:
<include meta='title="app/api/chat/route.ts"'>../../examples/chat-app/route.ts</include>
That's the entire backend. composio.create("user_123") creates a session that lets the agent search for tools, authenticate users, and execute actions. This ID scopes all connections and credentials to this user. In production, use the user ID from your auth system.
Replace app/page.tsx:
<include meta='title="app/page.tsx"'>../../examples/chat-app/page.tsx</include>
Go to localhost:3000. You should see the chat interface.
Send this message:
Star the composio repo on GitHub
Here's what happens:
GITHUB_STAR_REPO, and checks your connection status.GITHUB_STAR_REPO with your credentials.Discovery, authentication, and execution all happened automatically. You didn't write any tool-specific code.
<Callout> This in-chat authentication flow works out of the box. For production apps, you can [authenticate users ahead of time](/docs/authenticating-users/manually-authenticating) during onboarding. </Callout>Tool calls happen invisibly by default. Add a component that shows the tool name and status so you can see what the agent is doing.
Create components/ToolCallDisplay.tsx:
<include meta='title="components/ToolCallDisplay.tsx"'>../../examples/chat-app/ToolCallDisplay.tsx</include>
Update app/page.tsx to render tool calls:
<include meta='title="app/page.tsx"'>../../examples/chat-app/page-with-tools.tsx</include>
Tool calls now show inline with a spinner while running and a checkmark when complete. Click to expand and see the raw output.
The session handles tool discovery and execution at runtime. Instead of loading thousands of tool definitions into your agent's context, the agent searches for what it needs, authenticates the user if necessary, and executes the action. Token refresh and credential management are handled automatically.
Try a few more requests: