Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 06:00 UTC
| 1 | # @composio/langchain |
| 2 | |
| 3 | The LangChain provider turns Composio tools into LangChain `DynamicStructuredTool` objects with built-in execution, ready for LangChain and LangGraph agents. In TypeScript, LangGraph is served by this same package. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | npm install @composio/core @composio/langchain @langchain/core @langchain/openai @langchain/langgraph |
| 9 | ``` |
| 10 | |
| 11 | Set `COMPOSIO_API_KEY` with your API key from [the dashboard](https://dashboard.composio.dev/settings), and `OPENAI_API_KEY` (or your LLM provider's key). |
| 12 | |
| 13 | ## Quickstart |
| 14 | |
| 15 | Create a session for your user, fetch its tools, and wire them into a LangGraph agent: |
| 16 | |
| 17 | ```typescript |
| 18 | import { ChatOpenAI } from '@langchain/openai'; |
| 19 | import { HumanMessage, AIMessage } from '@langchain/core/messages'; |
| 20 | import { ToolNode } from '@langchain/langgraph/prebuilt'; |
| 21 | import { StateGraph, MessagesAnnotation } from '@langchain/langgraph'; |
| 22 | import { Composio } from '@composio/core'; |
| 23 | import { LangchainProvider } from '@composio/langchain'; |
| 24 | |
| 25 | const composio = new Composio({ |
| 26 | provider: new LangchainProvider(), |
| 27 | }); |
| 28 | |
| 29 | // Create a session for your user |
| 30 | const session = await composio.create('user_123'); |
| 31 | const tools = await session.tools(); |
| 32 | |
| 33 | const toolNode = new ToolNode(tools); |
| 34 | |
| 35 | const model = new ChatOpenAI({ |
| 36 | model: 'gpt-5.2', |
| 37 | temperature: 0, |
| 38 | }).bindTools(tools); |
| 39 | |
| 40 | function shouldContinue({ messages }: typeof MessagesAnnotation.State) { |
| 41 | const lastMessage = messages[messages.length - 1] as AIMessage; |
| 42 | if (lastMessage.tool_calls?.length) { |
| 43 | return 'tools'; |
| 44 | } |
| 45 | return '__end__'; |
| 46 | } |
| 47 | |
| 48 | async function callModel(state: typeof MessagesAnnotation.State) { |
| 49 | const response = await model.invoke(state.messages); |
| 50 | return { messages: [response] }; |
| 51 | } |
| 52 | |
| 53 | const workflow = new StateGraph(MessagesAnnotation) |
| 54 | .addNode('agent', callModel) |
| 55 | .addEdge('__start__', 'agent') |
| 56 | .addNode('tools', toolNode) |
| 57 | .addEdge('tools', 'agent') |
| 58 | .addConditionalEdges('agent', shouldContinue); |
| 59 | |
| 60 | const app = workflow.compile(); |
| 61 | |
| 62 | const finalState = await app.invoke({ |
| 63 | messages: [ |
| 64 | new HumanMessage( |
| 65 | "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'" |
| 66 | ), |
| 67 | ], |
| 68 | }); |
| 69 | console.log(finalState.messages[finalState.messages.length - 1].content); |
| 70 | ``` |
| 71 | |
| 72 | Each tool is a standard `DynamicStructuredTool`, so it also works anywhere LangChain accepts tools: chains, LCEL pipelines, and `bindTools` on any chat model. |
| 73 | |
| 74 | ## Links |
| 75 | |
| 76 | - [LangChain provider docs](https://docs.composio.dev/docs/providers/langchain) |
| 77 | - [Composio documentation](https://docs.composio.dev) |