> ## Documentation Index
> Fetch the complete documentation index at: https://composio-preview.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain

> Build an agent with LangChain or LangGraph and enable it to use 1000+ tools with Composio.

The LangChain provider formats Composio tools for [LangChain](https://python.langchain.com/) and [LangGraph](https://langchain-ai.github.io/langgraph/) agents. Pick the tab that matches your setup.

To use LangChain's `MCPAdapter`, follow the [Python MCP research agent example](https://composio-preview.mintlify.site/examples/python-mcp-agents.md). It connects to a hosted session endpoint and includes setup, execution, and cleanup.

> [tab] LangChain
>
> The LangChain provider transforms each Composio tool into a LangChain [`DynamicStructuredTool`](https://js.langchain.com/docs/concepts/tools/) with built-in execution. You can hand the tools to `create_agent` in Python or wire them into a graph node in TypeScript, and the framework runs the tool loop for you.
>
> **1. Install**
>
> **Install**
>
> > [tab] Python
> >
> > ```bash uv theme={null}
> > uv add composio composio_langchain langchain langchain_openai
> > ```
> >
> > ```bash pip theme={null}
> > pip install composio composio_langchain langchain langchain_openai
> > ```
>
> > [tab] TypeScript
> >
> > ```bash npm theme={null}
> > npm install @composio/core @composio/langchain @langchain/openai @langchain/langgraph @langchain/core
> > ```
> >
> > ```bash pnpm theme={null}
> > pnpm add @composio/core @composio/langchain @langchain/openai @langchain/langgraph @langchain/core
> > ```
> >
> > ```bash yarn theme={null}
> > yarn add @composio/core @composio/langchain @langchain/openai @langchain/langgraph @langchain/core
> > ```
> >
> > ```bash bun theme={null}
> > bun add @composio/core @composio/langchain @langchain/openai @langchain/langgraph @langchain/core
> > ```
>
> **2. Configure API Keys**
>
> **Configure API Keys**
>
> > [!NOTE]
> >
> > Set `COMPOSIO_API_KEY` with your API key from [Settings](https://dashboard.composio.dev/~/project/settings/api-keys?utm_source=docs\&utm_medium=content\&utm_campaign=docs-providers-langchain) and `OPENAI_API_KEY` with your [OpenAI API key](https://platform.openai.com/api-keys).
>
> ```txt .env theme={null}
> COMPOSIO_API_KEY=xxxxxxxxx
> OPENAI_API_KEY=xxxxxxxxx
> ```
>
> **3. Create session and run**
>
> **Create session and run**
>
> > [tab] Python
> >
> > ```python theme={null}
> > from composio import Composio
> > from composio_langchain import LangchainProvider
> > from langchain.agents import create_agent
> > from langchain_openai import ChatOpenAI
> >
> > composio = Composio(provider=LangchainProvider())
> > llm = ChatOpenAI(model="gpt-5.2")
> >
> > # Create a session for your user
> > session = composio.create(user_id="user_123")
> > tools = session.tools()
> >
> > agent = create_agent(tools=tools, model=llm)
> > result = agent.invoke({"messages": [("user", "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'")]})
> >
> > print(result["messages"][-1].content)
> > ```
>
> > [tab] TypeScript
> >
> > ```typescript theme={null}
> > import { ChatOpenAI } from '@langchain/openai';
> > import { HumanMessage, AIMessage } from '@langchain/core/messages';
> > import { ToolNode } from '@langchain/langgraph/prebuilt';
> > import { StateGraph, MessagesAnnotation } from '@langchain/langgraph';
> > import { Composio } from '@composio/core';
> > import { LangchainProvider } from '@composio/langchain';
> >
> > const composio = new Composio({
> >   provider: new LangchainProvider(),
> > });
> >
> > // Create a session for your user
> > const session = await composio.create("user_123");
> > const tools = await session.tools();
> >
> > const toolNode = new ToolNode(tools);
> >
> > const model = new ChatOpenAI({
> >   model: 'gpt-5.2',
> >   temperature: 0,
> > }).bindTools(tools);
> >
> > function shouldContinue({ messages }: typeof MessagesAnnotation.State) {
> >   const lastMessage = messages[messages.length - 1] as AIMessage;
> >   if (lastMessage.tool_calls?.length) {
> >     return 'tools';
> >   }
> >   return '__end__';
> > }
> >
> > async function callModel(state: typeof MessagesAnnotation.State) {
> >   const response = await model.invoke(state.messages);
> >   return { messages: [response] };
> > }
> >
> > const workflow = new StateGraph(MessagesAnnotation)
> >   .addNode('agent', callModel)
> >   .addEdge('__start__', 'agent')
> >   .addNode('tools', toolNode)
> >   .addEdge('tools', 'agent')
> >   .addConditionalEdges('agent', shouldContinue);
> >
> > const app = workflow.compile();
> >
> > const finalState = await app.invoke({
> >   messages: [new HumanMessage("Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'")],
> > });
> > console.log(finalState.messages[finalState.messages.length - 1].content);
> > ```

> [tab] LangGraph
>
> The LangGraph provider transforms Composio tools into the same LangChain `DynamicStructuredTool` format, ready to use with LangGraph agents. LangGraph integration is Python-only.
>
> **1. Install**
>
> **Install**
>
> ```bash uv theme={null}
> uv add composio composio_langgraph langgraph langchain langchain_openai
> ```
>
> ```bash pip theme={null}
> pip install composio composio_langgraph langgraph langchain langchain_openai
> ```
>
> **2. Configure API Keys**
>
> **Configure API Keys**
>
> > [!NOTE]
> >
> > Set `COMPOSIO_API_KEY` with your API key from [Settings](https://dashboard.composio.dev/~/project/settings/api-keys?utm_source=docs\&utm_medium=content\&utm_campaign=docs-providers-langchain) and `OPENAI_API_KEY` with your [OpenAI API key](https://platform.openai.com/api-keys).
>
> ```txt .env theme={null}
> COMPOSIO_API_KEY=xxxxxxxxx
> OPENAI_API_KEY=xxxxxxxxx
> ```
>
> **3. Create session and run**
>
> **Create session and run**
>
> ```python theme={null}
> from composio import Composio
> from composio_langgraph import LanggraphProvider
> from langchain.agents import create_agent
> from langchain_openai import ChatOpenAI
>
> composio = Composio(provider=LanggraphProvider())
> llm = ChatOpenAI(model="gpt-5.2")
>
> # Create a session for your user
> session = composio.create(user_id="user_123")
> tools = session.tools()
>
> agent = create_agent(tools=tools, model=llm)
> result = agent.invoke({"messages": [("user", "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'")]})
>
> print(result["messages"][-1].content)
> ```

## Python argument validation

The Python LangChain and LangGraph providers build a Pydantic argument model from each tool's JSON Schema. Object arguments that declare no properties accept and preserve arbitrary nested keys. For objects that declare named properties, undeclared keys are rejected unless the schema allows them with `additionalProperties`.

Schemas that use `patternProperties` or schema-valued `additionalProperties` validate dynamic keys before execution. The providers also preserve whether an optional argument was omitted or explicitly set to `None`. By default, declared defaults are included, while optional arguments without defaults stay absent. Set `schema_config={"skip_defaults": True}` when creating the provider to leave declared defaults absent too.

When validation fails, the tool does not run. LangChain surfaces the failure as a tool error observation so the agent can correct its arguments and retry.

## Next

> [card] [What is a session?](https://composio-preview.mintlify.site/docs/how-composio-works.md)
>
> How sessions scope users, tools, and auth, and how to reuse them across requests.
