Skip to main content

Twoly

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

Usage

Install the module

pip install twoly

Connects your LangGraph agent:

# Import TwolyMCP
from twoly import TwolyMCP
mcp = TwolyMCP("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 Twoly without Langchain MCP Adapter).

Twoly lifecycle

Twoly connector 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 TwolyMCP with async and thus it will automatically stop itself when the agent is done.

async def main():
    async with TwolyMCP("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())

Twoly without Langchain MCP Adapter

Simply replace TwolyMCP with TwolyMCPWithoutAdapter. The API is identical.

```python
async def main():
    async with TwolyMCPWithoutAdapter("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 twoly import TwolyMCP, TwolyMCPWithoutAdapter

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 = TwolyMCP("my-runtime", options)
# or
mcp = TwolyMCPWithoutAdapter("my-runtime", options)

Development

Prepare your venv

cd packages/twoly
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
pip install dist/twoly-0.0.2-py3-none-any.whl --force-reinstall

Run the examples

python examples/list_tools.py
python examples/langgraph.py
python examples/langgraph_without_adapter.py

Metadata

Release files for twoly 0.0.3

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

Source distribution (sdist)

Source distribution for twoly 0.0.3
File Size Uploaded
twoly-0.0.3.tar.gz 12.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for twoly 0.0.3
File Interpreter ABI Platform
twoly-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / twoly-0.0.3.tar.gz

Download URL twoly-0.0.3.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1bb6b007434b8e9f7f0b3f954aa287a62b1e899a5534be2fec881fd197b49f25
BLAKE2b-256 checksum
How to use checksums
7635b7b1663b6809145e3c720cc27fcd4fe27a469aa809ab32cbaaa1eed9e63c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.13

Release files / twoly-0.0.3-py3-none-any.whl

Download URL twoly-0.0.3-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db5e29330d10dceba30a828a850d2395cdc8325b4310eefc8ab3266d80fe01ee
BLAKE2b-256 checksum
How to use checksums
e3b59b60508ccaf5cbd576a3b4708ff31ea65c4342f81bebb92d29c29bba15d2
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.3 This release

2 release files

0.0.2

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