Skip to main content

LMCP Tool Builder

A Python library for discovering, building and loading tools from LMCP (Language Model Context Protocol) servers.

Features

  • Tool Discovery: Automatically discover tools from LMCP servers
  • Local Caching: Cache tools locally for offline use
  • Auto Update: Configurable auto-update behavior
  • Tool Testing: Built-in testing functionality for discovered tools
  • Easy Integration: Simple API for integrating with your applications

Installation

pip install lmcp-tool-builder

Quick Start

from lmcp_tool_builder import LMCPToolBuilder
import os

# Create a tool builder instance
builder = LMCPToolBuilder(
    server_url="http://your-lmcp-server:8000",
    api_key=os.getenv("LMCP_API_KEY"),
    local_tools_file="bot_tools.py",
    debug=True,
    auto_update=True
)

# Build and load tools
tools = builder.build_and_load_tools()

# Use the tools
for tool in tools:
    print(f"Tool: {tool.__name__}")

Configuration

Environment Variables

Create a .env file in your project:

LMCP_API_KEY=your_api_key_here
LMCP_LOCAL_TOOLS_FILE=bot_tools.py

LMCPToolBuilder Parameters

  • server_url: URL of the LMCP server (default: "http://localhost:8000")
  • api_key: API key for authentication (default: "")
  • local_tools_file: Path to local tools cache file (default: "bot_tools.py")
  • debug: Enable debug output (default: False)
  • auto_update: Auto-update tools from server (default: False)

API Reference

LMCPToolBuilder Class

Methods

  • discover_tools(): Discover tools from LMCP server or local cache
  • build_tools_module(server_tools): Build a Python module from server tools
  • save_tools_module(module_code): Save module to local file
  • load_tools_from_module(): Load tools from local module
  • build_and_load_tools(): Main method to build and load tools
  • test_tools(tools): Test loaded tools with sample inputs

Example Usage

from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
from lmcp_tool_builder import LMCPToolBuilder # lmcp_tool_builder
import asyncio

import os
from dotenv import load_dotenv

load_dotenv()

# 到lmcp平台注册创建应用、绑定工具、获得api_key(略)
print(f'[LMCP] 开始更新工具...')

# 创建工具构建器
builder = LMCPToolBuilder(
    server_url="http://aicity.wang:8000",
    api_key=os.getenv("aca8942ed1b43ab16f796eb223db10dc12edaa0c3fad7d41b85215bcb6aceefb"),
    local_tools_file=os.getenv("LMCP_LOCAL_TOOLS_FILE", "bot_tools.py"),
    debug=True,
    auto_update=False
)

# 构建并加载工具
tools = builder.build_and_load_tools()

# 构建agent
agent = create_agent(
    model=ChatOpenAI(
        model="deepseek-chat",
        api_key="your api key",
        base_url="https://api.deepseek.com/v1"
    ),
    tools=tools
)


# 异步流式输出
async def async_stream_agent():
    async for message, metadata in agent.astream(
        {"messages": [("user", "你必须使用工具,异步计算2+8=?告诉我你使用哪工具")]}, 
        stream_mode="messages"
    ):
        # 实时打印 Token
        if message.content:
            print(message.content, end="", flush=True)

# 同步流式输出
def stream_agent():
    for token, metadata in agent.stream(
        {"messages": [{"role": "user", "content": "1+2=?"}]},
        stream_mode="messages" #update
    ):
        if token.content:
            print(token.content, end="", flush=True)


if __name__ == "__main__":
    stream_agent()
    asyncio.run(async_stream_agent())

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/example/lmcp-tool-builder.git
cd lmcp-tool-builder

# Install in development mode
pip install -e .

Running Tests

python -m pytest tests/

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Metadata

Release files for lmcp-tool-builder 0.1.4

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

Source distribution (sdist)

Source distribution for lmcp-tool-builder 0.1.4
File Size Uploaded Provenance
lmcp_tool_builder-0.1.4.tar.gz 17.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for lmcp-tool-builder 0.1.4
File Interpreter ABI Platform
lmcp_tool_builder-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 39.5 kB

Release files / lmcp_tool_builder-0.1.4.tar.gz

Download URL lmcp_tool_builder-0.1.4.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b81926fdad134f20c357c833d9140eb9592d7ac1c4b23093f13cec0142052ef9
BLAKE2b-256 checksum
How to use checksums
ddce18f857201bf30285a01bf2d63acc4167744cdcdb0ae91b51db31ae56da38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release files / lmcp_tool_builder-0.1.4-py3-none-any.whl

Download URL lmcp_tool_builder-0.1.4-py3-none-any.whl
Size 21.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e28cb0d5ebfba55299a1d782cbb2fdab57332f96460660aa2ea798e8eb31f27f
BLAKE2b-256 checksum
How to use checksums
870b686ae2c5ea15557771bcaa06effcc76db7f5239948816c1a761947836fb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Security for lmcp-tool-builder 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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