Skip to main content

langchain_2ly

2ly python module providing helpers to quickly connect 2ly to your agents in Python.

Usage

Install the module

pip install langchain_2ly

Connects your LangGraph agent:

# Import MCPAdapter
from langchain_2ly import MCPAdapter
mcp = MCPAdapter("my-runtime")
tools = await mcp.get_langchain_tools()
agent = create_react_agent(llm, tools)
await mcp.stop()

Under the hood our connector leverages Langchain MCP adapters. You can also use our variant which is strictly based on the Official MCP Python SDK (see Using MCP without Langchain MCP Adapter).

MCPAdapter lifecycle

MCP Adapter will start an MCP Server lazily when you first call get_langchain_tools() or when the agent call a given tool. In practice the call to retrieve the list of tools must always come first.

Since this is an async task, we provide a way to instantiate MCPAdapter with async and thus it will automatically stop itself when the agent is done.

async def main():
    async with MCPAdapter("My agent name") as mcp:
        tools = await mcp.get_langchain_tools()
        agent = create_react_agent(llm, tools)
        agent_response = await agent.ainvoke() # as usual

if __name__ == "__main__":
    asyncio.run(main())

Using MCP without Langchain MCP Adapter

Simply replace MCPAdapter with MCPClient. The API is identical.

```python
async def main():
    async with MCPClient("My agent name") as mcp:
        tools = await mcp.get_langchain_tools()
        agent = create_react_agent(llm, tools)
        agent_response = await agent.ainvoke() # as usual

if __name__ == "__main__":
    asyncio.run(main())

Usage examples

Options

Both variants accept optional configuration to control how the MCP runtime is started via npx and how the client behaves:

from langchain_2ly import MCPAdapter, MCPClient

options = {
  "workspace": "my-workspace-id",           # forwarded to runtime as WORKSPACE_ID
  "nats_servers": "nats://localhost:4222",  # NATS URL used by runtime
  "version": "latest",                      # @2ly/runtime version for npx
  "startup_timeout_seconds": 20.0,            # wait for initialize()
  "log_level": "info",                       # forwarded to runtime as LOG_LEVEL
}

mcp = MCPAdapter("my-runtime", options)
# or
mcp = MCPClient("my-runtime", options)

Development

Prepare your venv

cd packages/langchain_2ly
python3.11 -m venv .venv # any version python3.10+ will do
source .venv/bin/activate
pip install --upgrade pip
pip install -e ".[all]"

Execute tests

pytest

Build locally

python -m build

Test local installation

# update the filename to the build version
# update the filename to the build version
pip install dist/langchain_2ly-0.0.4-py3-none-any.whl --force-reinstall

Run the examples

python examples/list_tools.py
python examples/langgraph_agent.py
python examples/langgraph_without_adapter.py

Metadata

Release files for langchain-2ly 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-2ly 0.0.1
File Size Uploaded
langchain_2ly-0.0.1.tar.gz 12.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-2ly 0.0.1
File Interpreter ABI Platform
langchain_2ly-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.5 kB

Release files / langchain_2ly-0.0.1.tar.gz

Download URL langchain_2ly-0.0.1.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4651b0edcd2043d4485ebd94cfd52b5416c94bb218a9abc0b25eb4cca55d076b
BLAKE2b-256 checksum
How to use checksums
9436a8f63f646141ec3fdf6062ae95daaf9ac3a907b2c52430e10fa0be5612b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.13

Release files / langchain_2ly-0.0.1-py3-none-any.whl

Download URL langchain_2ly-0.0.1-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17f5dad72e1d2a4630d6a6535df44cca6821c3217e520b1f856c41a0fbc83110
BLAKE2b-256 checksum
How to use checksums
38a67b55128c78cd49c300135bbbcd1d4f0f71b0747f92753d9e852004411a54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.13

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page