2LY Logo

LangChain

Connect LangChain agents to 2LY

LangChain agents connect to 2LY through the MCPAdapter, providing access to your configured tools via the MCP protocol.

Overview

The langchain_2ly connector enables LangChain agents to access 2LY's tool ecosystem. This integration:

  • Connects LangChain/LangGraph agents to 2LY's MCP runtime
  • Provides tools as standard LangChain tools through get_langchain_tools()
  • Handles MCP communication and tool execution automatically
  • Auto-creates agents in 2LY if they don't exist

Quick Start

1. Install Connector

pip install langchain_2ly

The connector contains the MCPAdapter to quickly connect to 2LY.

2. Use Tools in LangGraph

from langchain_2ly import MCPAdapter
from langchain.agents import create_react_agent
from langchain_openai import ChatOpenAI

# Set NATS connection
nats = "nats://localhost:4222"
options = {
    "nats_servers": nats
}

# Instantiate MCPAdapter
async with MCPAdapter("My Agent", options) as mcp:
    # Retrieve tools
    tools = await mcp.get_langchain_tools()

    # Create your agent as usual
    llm = ChatOpenAI()
    agent = create_react_agent(llm, tools)
    agent_response = await agent.ainvoke({
        "input": "What tools are available?"
    })

When instantiating the MCPAdapter, give it the name of your agent. Agents are automatically created in 2LY if they don't yet exist.

Configuration

Connection Options

from langchain_2ly import MCPAdapter

options = {
    "nats_servers": "nats://localhost:4222",  # NATS connection URL
    "workspace_id": "my-workspace",           # Optional workspace ID
    "log_level": "info",                      # Runtime logging level
}

async with MCPAdapter("Agent Name", options) as mcp:
    tools = await mcp.get_langchain_tools()

Environment Variables

export NATS_SERVERS="nats://localhost:4222"
export WORKSPACE_ID="my-workspace"
export LOG_LEVEL="debug"

Example: List Available Tools

from langchain_2ly import MCPAdapter

async def list_agent_tools():
    options = {"nats_servers": "nats://localhost:4222"}

    async with MCPAdapter("My Agent", options) as mcp:
        tools = await mcp.get_langchain_tools()

        print(f"Agent has access to {len(tools)} tools:")
        for tool in tools:
            print(f"  - {tool.name}: {tool.description}")

Troubleshooting

NATS Connection Failed

# Verify 2LY services are running
docker-compose ps

# Check NATS connectivity
curl http://localhost:8222/varz

Tools Not Loading

  • Verify agent name matches in 2LY dashboard
  • Check tools are assigned to the agent
  • Ensure NATS URL is correct

Debug Mode

options = {
    "nats_servers": "nats://localhost:4222",
    "log_level": "debug"
}

async with MCPAdapter("Debug Agent", options) as mcp:
    tools = await mcp.get_langchain_tools()
    print(f"Loaded {len(tools)} tools")

Next Steps