LMCP Tool Builder - 简化 LangChain 工具加载和集成
Project description
LMCP Tool Builder
LMCP Tool Builder 是一个用于简化 LangChain 工具加载和集成的 Python 库。它提供了与 LMCP (Language Model Control Platform) 服务器无缝集成的能力,支持工具发现、构建和加载。
主要特性
- 🚀 一键式工具加载: 从 LMCP 服务器自动发现和加载工具
- 🔄 智能缓存: 支持本地工具缓存,提高加载速度
- 🛠️ LangChain 集成: 直接生成 LangChain 兼容的工具列表
- 📡 服务器回退: 当服务器不可用时自动回退到本地工具
- 🔧 调试模式: 详细的调试输出,便于问题排查
- 📦 模块化设计: 易于扩展和定制
安装
从 PyPI 安装
pip install lmcp-tool-builder
从源码安装
git clone https://github.com/lmcp-dev/lmcp-tool-builder.git
cd lmcp-tool-builder
pip install -e .
快速开始
基本使用
from lmcp_tool_builder import LMCPToolBuilder
# 创建工具构建器
builder = LMCPToolBuilder(
server_url="http://localhost:8000",
api_key="your-api-key-here",
local_tools_file="bot_tools.py", # 可选:本地工具缓存文件
debug=False # 可选:调试模式
)
# 构建并加载工具
tools = builder.build_and_load_tools()
print(f"✅ 加载了 {len(tools)} 个工具")
与 LangChain 集成
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
from lmcp_tool_builder import LMCPToolBuilder
# 创建工具构建器
builder = LMCPToolBuilder(
server_url="http://localhost:8000",
api_key="your-api-key-here"
)
# 构建并加载工具
tools = builder.build_and_load_tools()
# 创建 LangChain agent
agent = create_agent(
model=ChatOpenAI(
model="gpt-3.5-turbo",
api_key="your-openai-api-key"
),
tools=tools,
system_prompt="You are a helpful assistant",
)
# 使用 agent
response = agent.invoke(
{"messages": [{"role": "user", "content": "北京现在的天气怎么样?"}]}
)
print(response)
高级用法
启用调试模式
builder = LMCPToolBuilder(
server_url="http://localhost:8000",
api_key="your-api-key-here",
debug=True # 启用调试输出
)
# 现在会显示详细的调试信息
tools = builder.build_and_load_tools()
自定义本地工具文件
builder = LMCPToolBuilder(
server_url="http://localhost:8000",
api_key="your-api-key-here",
local_tools_file="my_tools.py" # 自定义工具文件路径
)
# 工具将保存到 my_tools.py
tools = builder.build_and_load_tools()
手动构建工具模块
builder = LMCPToolBuilder(
server_url="http://localhost:8000",
api_key="your-api-key-here"
)
# 1. 发现工具
server_tools = builder.discover_tools()
# 2. 构建工具模块
module_code = builder.build_tools_module(server_tools)
# 3. 保存模块
builder.save_tools_module(module_code)
# 4. 加载工具
tools = builder.load_tools_from_module()
API 参考
LMCPToolBuilder 类
构造函数
LMCPToolBuilder(
server_url: str = "http://localhost:8000",
api_key: str = "",
local_tools_file: str = "bot_tools.py",
debug: bool = False
)
参数:
server_url: LMCP 服务器地址api_key: 应用 API Keylocal_tools_file: 本地工具缓存文件路径(可选)debug: 是否启用调试模式
主要方法
discover_tools() -> List[str]: 从服务器发现工具,失败时回退到本地工具build_tools_module(server_tools: List[str]) -> str: 构建工具模块代码save_tools_module(module_code: str): 保存工具模块到文件load_tools_from_module() -> List: 从模块中加载工具函数build_and_load_tools() -> List: 构建并加载工具(推荐使用)test_tools(tools: List): 测试工具函数
命令行工具
安装后可以使用命令行工具:
# 显示帮助信息
lmcp-tool-builder --help
# 构建并加载工具
lmcp-tool-builder build --server-url http://localhost:8000 --api-key your-key
# 测试工具
lmcp-tool-builder test --tools-file bot_tools.py
配置 LMCP 服务器
在使用 LMCP Tool Builder 之前,需要先设置 LMCP 服务器:
-
安装 LMCP 服务器:
git clone https://github.com/lmcp-dev/lmcp-server.git cd lmcp-server pip install -r requirements.txt python server.py
-
创建应用并获取 API Key:
- 访问
http://localhost:8000 - 注册用户并登录
- 在应用管理页面创建应用
- 复制应用的 API Key
- 访问
-
创建工具:
- 在工具管理页面创建工具
- 工具必须是有效的 Python 函数,包含文档字符串
开发
设置开发环境
# 克隆仓库
git clone https://github.com/lmcp-dev/lmcp-tool-builder.git
cd lmcp-tool-builder
# 创建虚拟环境
python -m venv venv
source venv/bin/activate # Linux/Mac
# 或
venv\Scripts\activate # Windows
# 安装开发依赖
pip install -e ".[dev]"
运行测试
# 运行所有测试
pytest
# 运行测试并生成覆盖率报告
pytest --cov=lmcp_tool_builder tests/
# 运行特定测试文件
pytest tests/test_builder.py
代码质量检查
# 代码格式化
black lmcp_tool_builder tests
# 代码检查
flake8 lmcp_tool_builder tests
# 类型检查
mypy lmcp_tool_builder
贡献
欢迎贡献!请查看 CONTRIBUTING.md 了解如何参与项目开发。
许可证
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
支持
- 📖 文档: https://lmcp.dev/docs/tool-builder
- 🐛 问题跟踪: GitHub Issues
- 💬 讨论: GitHub Discussions
相关项目
- LMCP Server - LMCP 服务器端
- LMCP Client - LMCP 客户端库
- LMCP Examples - 使用示例
LMCP Tool Builder - 让 LangChain 工具管理变得更简单! 🚀
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
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 lmcp_tool_builder-0.1.0.tar.gz.
File metadata
- Download URL: lmcp_tool_builder-0.1.0.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc4bc4ae666b77a5d5ad03ab035a2870bacfa27ce37dcb5f89353e21fdff5d0c
|
|
| MD5 |
3a127278bb7e92511a315211a467ee6d
|
|
| BLAKE2b-256 |
5cbb835efb796c81a9497efca89badc302915ce0a5b2ac4a0bef2f9c57b4149f
|
File details
Details for the file lmcp_tool_builder-0.1.0-py3-none-any.whl.
File metadata
- Download URL: lmcp_tool_builder-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0647c8924c188343441233b8824539d7244469bb506973b2bdfde64d7a7e55b1
|
|
| MD5 |
83b7b3a6cbbe30d72869d0088ea6db42
|
|
| BLAKE2b-256 |
c2b7a47a04baa06d9172d3fd458d5ba94874d1ed4b04d46b29ddf6c552f253b3
|