Skip to main content

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

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_2ly-0.0.1.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_2ly-0.0.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file langchain_2ly-0.0.1.tar.gz.

File metadata

  • Download URL: langchain_2ly-0.0.1.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for langchain_2ly-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4651b0edcd2043d4485ebd94cfd52b5416c94bb218a9abc0b25eb4cca55d076b
MD5 439c66255f0e5346bef695f7bd0cdd79
BLAKE2b-256 9436a8f63f646141ec3fdf6062ae95daaf9ac3a907b2c52430e10fa0be5612b7

See more details on using hashes here.

File details

Details for the file langchain_2ly-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: langchain_2ly-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for langchain_2ly-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17f5dad72e1d2a4630d6a6535df44cca6821c3217e520b1f856c41a0fbc83110
MD5 7698a813afd70e90d755f4af97891e0c
BLAKE2b-256 38a67b55128c78cd49c300135bbbcd1d4f0f71b0747f92753d9e852004411a54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page