2ly python module providing helpers to quickly connect 2ly to your agents in Python.
Project description
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
- examples/list_tools.py: list tools available for a given 2ly agent
- examples/langgraph.py: connect 2ly to a LangGraph agent
- examples/langgraph_without_adapter.py: another LangGraph example but without Langchain adapter
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file twoly-0.0.3.tar.gz.
File metadata
- Download URL: twoly-0.0.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bb6b007434b8e9f7f0b3f954aa287a62b1e899a5534be2fec881fd197b49f25
|
|
| MD5 |
194d1ecc319681203c2a0983b2ac2dde
|
|
| BLAKE2b-256 |
7635b7b1663b6809145e3c720cc27fcd4fe27a469aa809ab32cbaaa1eed9e63c
|
File details
Details for the file twoly-0.0.3-py3-none-any.whl.
File metadata
- Download URL: twoly-0.0.3-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db5e29330d10dceba30a828a850d2395cdc8325b4310eefc8ab3266d80fe01ee
|
|
| MD5 |
05f7cced5e4e70fa98b1b161f0fce2c7
|
|
| BLAKE2b-256 |
e3b59b60508ccaf5cbd576a3b4708ff31ea65c4342f81bebb92d29c29bba15d2
|