Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
in chat auth
Sushmithamalleshb4e8ff4d85e9
diff --git a/fern/pages/src/tool-router/using-in-chat-authentication.mdx b/fern/pages/src/tool-router/using-in-chat-authentication.mdxindex 275c45e78..a8bc5218f 100644--- a/fern/pages/src/tool-router/using-in-chat-authentication.mdx+++ b/fern/pages/src/tool-router/using-in-chat-authentication.mdx@@ -1,7 +1,84 @@----title: Using In-chat Authentication+title: Using in-chat authenticationimage: "https://og.composio.dev/api/og?title=Using%20In-chat%20Authentication"-description: "Let your agent authenticate users via MANAGE_CONNECTIONS_TOOL"-keywords: "tool router"+description: "Let your agent prompt users to connect accounts during conversation"+keywords: "tool router, authentication, in-chat, MANAGE_CONNECTIONS"---+In-chat authentication lets your agent prompt users to connect accounts during chat. When a tool requires authentication, the agent returns a Connect Link URL. The user authenticates, confirms in chat, and the agent retries.++## How it works++1. User asks agent to do something (e.g., "star my repos")+2. Agent tries to execute but user hasn't connected GitHub+3. `COMPOSIO_MANAGE_CONNECTIONS` returns a Connect Link URL+4. Agent shares the URL with the user+5. User clicks, authenticates, returns to chat+6. User confirms ("done") and agent retries successfully++## Configuration++In-chat auth is enabled by default. The `COMPOSIO_MANAGE_CONNECTIONS` meta-tool is included automatically.++```typescript+const session = await composio.createSession({+ user: "pg-user-550e8400-e29b-41d4",+ toolkits: ["github", "gmail"],+});+```++### Custom callback URL++Redirect users to a specific page after authentication:++```typescript+const session = await composio.createSession({+ user: "pg-user-550e8400-e29b-41d4",+ toolkits: ["github", "gmail"],+ manageConnections: {+ callbackUrl: "https://yourapp.com/chat",+ },+});+```++### Disabling in-chat auth++To manage authentication outside of chat, disable the `COMPOSIO_MANAGE_CONNECTIONS` tool:++```typescript+const session = await composio.createSession({+ user: "pg-user-550e8400-e29b-41d4",+ toolkits: ["github", "gmail"],+ manageConnections: false,+});+```++See [Manually authenticating users](/tool-router/manually-authenticating-users) for the alternative approach.++## Examples++<CodeGroup>+<CodeGroupItem title="Vercel AI SDK (TypeScript)">+<SnippetCode src="fern/snippets/tool-router/typescript/in-chat-auth.ts" />+</CodeGroupItem>+<CodeGroupItem title="OpenAI Agents SDK (Python)">+<SnippetCode src="fern/snippets/tool-router/python/in-chat-auth.py" />+</CodeGroupItem>+</CodeGroup>++### Example conversation++```+You: Star the composio repo on GitHub++Agent: I'll help you star the Composio repo. Let me search for the right tools.++I found the tool to star repositories, but I need you to connect your GitHub+account first. Please click the link below to authorize:+https://connect.composio.dev/link/ln_abc123++You: Done++Agent: Done! I've starred ComposioHQ/composio.+```+diff --git a/fern/snippets/tool-router/python/in-chat-auth.py b/fern/snippets/tool-router/python/in-chat-auth.pynew file mode 100644index 000000000..33fe07447--- /dev/null+++ b/fern/snippets/tool-router/python/in-chat-auth.py@@ -0,0 +1,41 @@+import asyncio+from composio import Composio+from agents import Agent, Runner, HostedMCPTool++composio = Composio()++async def chat():+ session = await composio.create_session(+ user="pg-user-550e8400-e29b-41d4",+ )++ agent = Agent(+ name="GitHub Assistant",+ instructions="Help users with their GitHub repositories.",+ tools=[+ HostedMCPTool(+ tool_config={+ "type": "mcp",+ "server_label": "composio",+ "server_url": session.mcp.url,+ "require_approval": "never",+ }+ )+ ],+ )++ print("Chat with your agent. Type 'exit' to quit.\n")++ while True:+ user_input = input("You: ")+ if user_input.lower() == "exit":+ break++ result = await Runner.run(+ agent,+ user_input,+ )++ print(f"Agent: {result.final_output}\n")++asyncio.run(chat())diff --git a/fern/snippets/tool-router/typescript/in-chat-auth.ts b/fern/snippets/tool-router/typescript/in-chat-auth.tsnew file mode 100644index 000000000..13b1112d3--- /dev/null+++ b/fern/snippets/tool-router/typescript/in-chat-auth.ts@@ -0,0 +1,52 @@+import { Composio } from "@composio/core";+import { VercelProvider } from "@composio/vercel";+import { openai } from "@ai-sdk/openai";+import { generateText } from "ai";+import * as readline from "readline";++const composio = new Composio({+ provider: new VercelProvider(),+});++async function chat() {+ const session = await composio.createSession({+ user: "pg-user-550e8400-e29b-41d4",+ });++ const tools = await session.tools();+ const messages: { role: "user" | "assistant"; content: string }[] = [];++ const rl = readline.createInterface({+ input: process.stdin,+ output: process.stdout,+ });++ console.log("Chat with your agent. Type 'exit' to quit.\n");++ const prompt = () => {+ rl.question("You: ", async (input) => {+ if (input.toLowerCase() === "exit") {+ rl.close();+ return;+ }++ messages.push({ role: "user", content: input });++ const { text } = await generateText({+ model: openai("gpt-4o"),+ tools,+ messages,+ maxSteps: 5,+ });++ console.log(`Agent: ${text}\n`);+ messages.push({ role: "assistant", content: text });++ prompt();+ });+ };++ prompt();+}++chat();