Skip to main content

Model Context Protocol (MCP) To LangChain Tools Conversion Utility

Project description

MCP To LangChain Tools Conversion Utility License: MIT pypi version

This package is intended to simplify the use of Model Context Protocol (MCP) server tools with LangChain / Python.

Model Context Protocol (MCP), introduced by Anthropic, extends the capabilities of LLMs by enabling interaction with external tools and resources, such as web search and database access. Thanks to its open-source nature, MCP has gained significant traction in the developer community, with over 400 MCP servers already developed and shared:

In the MCP framework, external features are encapsulated in an MCP server that runs in a separate process. This clear decoupling allows for easy adoption and reuse of any of the significant collections of MCP servers listed above.

To make it easy for LangChain to take advantage of such a vast resource base, this package offers quick and seamless access from LangChain to MCP servers.

It contains a utility function convert_mcp_to_langchain_tools().
This async function handles parallel initialization of specified multiple MCP servers and converts their available tools into a list of LangChain-compatible tools.

A typescript equivalent of this utility is available here

Requirements

  • Python 3.11+

Installation

pip install langchain-mcp-tools

Quick Start

convert_mcp_to_langchain_tools() utility function accepts MCP server configurations that follow the same structure as Claude for Desktop, but only the contents of the mcpServers property, and is expressed as a dict, e.g.:

mcp_configs = {
    'filesystem': {
        'command': 'npx',
        'args': ['-y', '@modelcontextprotocol/server-filesystem', '.']
    },
    'fetch': {
        'command': 'uvx',
        'args': ['mcp-server-fetch']
    }
}

tools, cleanup = await convert_mcp_to_langchain_tools(
    mcp_configs
)

This utility function initializes all specified MCP servers in parallel, and returns LangChain Tools (tools: List[BaseTool]) by gathering available MCP tools from the servers, and by wrapping them into LangChain tools. It also returns an async callback function (cleanup: McpServerCleanupFn) to be invoked to close all MCP server sessions when finished.

The returned tools can be used with LangChain, e.g.:

# from langchain.chat_models import init_chat_model
llm = init_chat_model(
    model='claude-3-5-haiku-latest',
    model_provider='anthropic'
)

# from langgraph.prebuilt import create_react_agent
agent = create_react_agent(
    llm,
    tools
)

A simple and experimentable usage example can be found here

A more realistic usage example can be found here

Limitations

Currently, only text results of tool calls are supported.

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_mcp_tools-0.1.3.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

langchain_mcp_tools-0.1.3-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file langchain_mcp_tools-0.1.3.tar.gz.

File metadata

  • Download URL: langchain_mcp_tools-0.1.3.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for langchain_mcp_tools-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5c716d7fd1973073dcc96ff13d01e83bbd96caec7e520a25628f78cd5cba7e8c
MD5 277631417c4f530d34f5a46be72a0075
BLAKE2b-256 057f4fa4479a147d2c088ada1926d5bfc7331a701aadb7563d31d405cc20dc3a

See more details on using hashes here.

File details

Details for the file langchain_mcp_tools-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_mcp_tools-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a2d4c14241f18eb40eac26c2b8b564741d56791dd6bb08d4e5c2d80f5f7d4f50
MD5 e3dfe5774969a1fc926ce78183f94139
BLAKE2b-256 28bbe700191a2b491a8a0918b7630e949d4d1757819291110adef63d6842e068

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