Add Metorial tools to a LangChain or LangGraph agent
Connect with the metorialLangchain adapter in TypeScript or metorial_langchain in Python, then pass session.tools() to createAgent, create_agent or a ToolNode.
This guide takes you from an empty folder to a LangChain agent that searches the web through Metorial. Connect with the LangChain adapter (metorialLangchain() in TypeScript, metorial_langchain() in Python) and pass session.tools() to createAgent or create_agent. For a LangGraph graph, give the same tools to a ToolNode.
What you'll learn
- How to create an API key and a provider deployment from the CLI
- How to pass Metorial tools to a LangChain agent in TypeScript and Python
- How to use the same tools in a LangGraph
StateGraph
Before you start
- Install the CLI and sign in
- An Anthropic API key
- Node.js 20 or later, or Python 3.10 or later
Set up Metorial with the CLI
Find your instance
An instance is one environment of a project. List yours and note the ID of the one the agent should use:
Create an API key
Copy the secret from the output. It starts with metorial_sk_ and is shown only once.
Deploy a provider
Metorial Search is a built-in web search provider that needs no credentials.
Copy the deployment ID from the output. It starts with pde_.
Export the values
Build a LangChain agent
Create the project
Write the agent
Save this as agent.ts or agent.py.
Run it
Use the tools in a LangGraph graph
metorial_langgraph() returns the same LangChain tools. Bind them to the model and give them to a ToolNode. Save this as graph.py in the same project and run python graph.py.
Note
The Python tools are async only. Run the agent or graph with ainvoke or astream.
Check it worked
The agent prints the tool names, then its answer. Metorial prefixes each tool with the provider name and adds metorial_connection_status, a diagnostic tool, to every session. The graph prints every message, so you see the tool call and its result before the answer.
List the tool calls Metorial recorded for the deployment:
Use other apps and each user's own account
Find a provider and deploy it the same way, then add its deployment ID to providers. One session can hold several.
Apps such as HubSpot need a sign-in. Pass the user's provider auth config next to the deployment ID, as providerAuthConfigId in TypeScript and provider_auth_config_id in Python. Let each user connect their own account shows how to get one for each user.
Troubleshooting
StructuredTool does not support sync invocation.
You called the Python agent or graph with invoke or stream. Use ainvoke or astream.
Invalid API key status=401
The Python SDK raises this AuthenticationError when the key is wrong. In TypeScript the message is [METORIAL ERROR]: unauthorized - Invalid API key. Check that METORIAL_API_KEY holds the full secret and belongs to the instance where you created the deployment.
ImportError: langchain-core is required for LangChain integration. Install it with: pip install langchain-core
The adapter could not import LangChain. Install langchain in the same virtual environment as metorial.
Provider requires an auth config to be provided
A provider deployment in the session needs a sign-in and got no provider auth config. Add the user's provider_auth_config_id to that entry.
Next steps
- Let each user connect their own account: per-user OAuth for Slack, Gmail, GitHub and other apps
- Limit the tools an agent can use: serve only some of a provider's tools
- Use tools from several apps in one session: one agent, several providers
- The official examples:
metorial-admin example create metorial-langchain-typescript-langchainfor TypeScript,metorial-langchain-langchainandmetorial-langgraphfor Python