langchain-mcp-tools 0.1.4__py3-none-any.whl → 0.1.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,7 @@ from typing import (
14
14
  Dict,
15
15
  List,
16
16
  NoReturn,
17
+ Optional,
17
18
  Tuple,
18
19
  Type,
19
20
  TypeAlias,
@@ -27,7 +28,7 @@ try:
27
28
  from mcp.client.stdio import stdio_client
28
29
  import mcp.types as mcp_types
29
30
  from pydantic import BaseModel
30
- from pympler import asizeof
31
+ # from pydantic_core import to_json
31
32
  except ImportError as e:
32
33
  print(f'\nError: Required package not found: {e}')
33
34
  print('Please ensure all required packages are installed\n')
@@ -143,6 +144,7 @@ async def get_mcp_server_tools(
143
144
  # Wrap MCP tools into LangChain tools
144
145
  langchain_tools: List[BaseTool] = []
145
146
  for tool in tools_response.tools:
147
+
146
148
  # Define adapter class to convert MCP tool to LangChain format
147
149
  class McpToLangChainAdapter(BaseTool):
148
150
  name: str = tool.name or 'NO NAME'
@@ -151,10 +153,11 @@ async def get_mcp_server_tools(
151
153
  args_schema: Type[BaseModel] = jsonschema_to_pydantic(
152
154
  tool.inputSchema
153
155
  )
156
+ session: Optional[ClientSession] = None
154
157
 
155
158
  def _run(self, **kwargs: Any) -> NoReturn:
156
159
  raise NotImplementedError(
157
- 'Only async operation is supported'
160
+ 'MCP tools only support async operations'
158
161
  )
159
162
 
160
163
  async def _arun(self, **kwargs: Any) -> Any:
@@ -162,17 +165,62 @@ async def get_mcp_server_tools(
162
165
  Asynchronously executes the tool with given arguments.
163
166
  Logs input/output and handles errors.
164
167
  """
165
- logger.info(f'MCP tool "{server_name}"/"{tool.name}"'
166
- f' received input: {kwargs}')
167
- result = await session.call_tool(self.name, kwargs)
168
- if result.isError:
169
- raise ToolException(result.content)
170
-
171
- # Log result size for monitoring
172
- size = asizeof.asizeof(result.content)
173
168
  logger.info(f'MCP tool "{server_name}"/"{tool.name}" '
174
- f'received result (size: {size})')
175
- return result.content
169
+ f'received input: {kwargs}')
170
+
171
+ try:
172
+ result = await session.call_tool(self.name, kwargs)
173
+
174
+ if hasattr(result, 'isError') and result.isError:
175
+ raise ToolException(
176
+ f'Tool execution failed: {result.content}'
177
+ )
178
+
179
+ if not hasattr(result, 'content'):
180
+ return str(result)
181
+
182
+ # The return type of `BaseTool`'s `arun` is `str`.
183
+ try:
184
+ result_content_text = '\n\n'.join(
185
+ item.text
186
+ for item in result.content
187
+ if isinstance(item, mcp_types.TextContent)
188
+ )
189
+ # text_items = [
190
+ # item
191
+ # for item in result.content
192
+ # if isinstance(item, mcp_types.TextContent)
193
+ # ]
194
+ # result_content_text =to_json(text_items).decode()
195
+
196
+ except KeyError as e:
197
+ result_content_text = (
198
+ f'Error in parsing result.content: {str(e)}; '
199
+ f'contents: {repr(result.content)}'
200
+ )
201
+
202
+ # Log rough result size for monitoring
203
+ size = len(result_content_text.encode())
204
+ logger.info(f'MCP tool "{server_name}"/"{tool.name}" '
205
+ f'received result (size: {size})')
206
+
207
+ # If no text content, return a clear message
208
+ # describing the situation.
209
+ result_content_text = (
210
+ result_content_text or
211
+ 'No text content available in response'
212
+ )
213
+
214
+ return result_content_text
215
+
216
+ except Exception as e:
217
+ logger.warn(
218
+ f'MCP tool "{server_name}"/"{tool.name}" '
219
+ f'caused error: {str(e)}'
220
+ )
221
+ if self.handle_tool_error:
222
+ return f'Error executing MCP tool: {str(e)}'
223
+ raise
176
224
 
177
225
  langchain_tools.append(McpToLangChainAdapter())
178
226
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: langchain-mcp-tools
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: Model Context Protocol (MCP) To LangChain Tools Conversion Utility
5
5
  Project-URL: Bug Tracker, https://github.com/hideya/langchain-mcp-tools-py/issues
6
6
  Project-URL: Source Code, https://github.com/hideya/langchain-mcp-tools-py
@@ -16,7 +16,6 @@ Requires-Dist: langchain-openai>=0.3.0
16
16
  Requires-Dist: langgraph>=0.2.62
17
17
  Requires-Dist: mcp>=1.2.0
18
18
  Requires-Dist: pyjson5>=1.6.8
19
- Requires-Dist: pympler>=1.1
20
19
  Requires-Dist: python-dotenv>=1.0.1
21
20
  Provides-Extra: dev
22
21
  Requires-Dist: pytest>=8.3.4; extra == "dev"
@@ -29,35 +28,32 @@ This package is intended to simplify the use of
29
28
  server tools with LangChain / Python.
30
29
 
31
30
  [Model Context Protocol (MCP)](https://modelcontextprotocol.io/),
32
- introduced by
33
- [Anthropic](https://www.anthropic.com/news/model-context-protocol),
34
- extends the capabilities of LLMs by enabling interaction with external tools and resources,
35
- such as web search and database access.
36
- Thanks to its open-source nature, MCP has gained significant traction in the developer community,
37
- with over 400 MCP servers already developed and shared:
31
+ an open source technology
32
+ [announced by Anthropic](https://www.anthropic.com/news/model-context-protocol),
33
+ dramatically expands LLM’s scope
34
+ by enabling external tool and resource integration, including
35
+ Google Drive, Slack, Notion, Spotify, Docker, PostgreSQL, and more…
36
+
37
+ Over 450 functional components available as MCP servers:
38
38
 
39
39
  - [Glama’s list of Open-Source MCP servers](https://glama.ai/mcp/servers)
40
40
  - [Smithery: MCP Server Registry](https://smithery.ai/)
41
41
  - [awesome-mcp-servers](https://github.com/hideya/awesome-mcp-servers#Server-Implementations)
42
42
  - [MCP Get Started/Example Servers](https://modelcontextprotocol.io/examples)
43
43
 
44
- In the MCP framework, external features are encapsulated in an MCP server,
45
- which typically runs in a separate process and communicates
46
- via `stdio` using the open standard protocol.
47
- This clean decoupling makes it easy to adopt and reuse any of
48
- the significant collections of MCP servers listed above.
49
-
50
- To make it easy for LangChain users to take advantage of such a vast resource base,
51
- this package offers quick and seamless access from LangChain to MCP servers.
44
+ The goal of this utility is to make these 450+ MCP servers readily accessible from LangChain.
52
45
 
53
46
  It contains a utility function `convert_mcp_to_langchain_tools()`.
54
47
  This async function handles parallel initialization of specified multiple MCP servers
55
48
  and converts their available tools into a list of LangChain-compatible tools.
56
49
 
50
+ For detailed information on how to use this library, please refer to the following document:
51
+ - ["Supercharging LangChain: Integrating 450+ MCP with ReAct"](https://medium.com/@h1deya/supercharging-langchain-integrating-450-mcp-with-react-d4e467cbf41a)
52
+
57
53
  A typescript equivalent of this utility is available
58
54
  [here](https://www.npmjs.com/package/@h1deya/langchain-mcp-tools)
59
55
 
60
- ## Requirements
56
+ ## Prerequisites
61
57
 
62
58
  - Python 3.11+
63
59
 
@@ -105,7 +101,7 @@ The returned tools can be used with LangChain, e.g.:
105
101
  ```python
106
102
  # from langchain.chat_models import init_chat_model
107
103
  llm = init_chat_model(
108
- model='claude-3-5-haiku-latest',
104
+ model='claude-3-5-sonnet-latest',
109
105
  model_provider='anthropic'
110
106
  )
111
107
 
@@ -115,12 +111,15 @@ agent = create_react_agent(
115
111
  tools
116
112
  )
117
113
  ```
118
- A simple and experimentable usage example can be found
114
+
115
+ Find complete, minimal working usage examples
119
116
  [here](https://github.com/hideya/langchain-mcp-tools-py-usage/blob/main/src/example.py)
120
117
 
121
- A more realistic usage example can be found
122
- [here](https://github.com/hideya/mcp-client-langchain-py)
118
+ For hands-on experimentation with MCP server integration,
119
+ try [this LangChain application built with the utility](https://github.com/hideya/mcp-client-langchain-py)
123
120
 
121
+ For detailed information on how to use this library, please refer to the following document:
122
+ ["Supercharging LangChain: Integrating 450+ MCP with ReAct"](https://medium.com/@h1deya/supercharging-langchain-integrating-450-mcp-with-react-d4e467cbf41a)
124
123
 
125
124
  ## Limitations
126
125
 
@@ -0,0 +1,8 @@
1
+ langchain_mcp_tools/__init__.py,sha256=Xtv2VphhrWB_KlxTIofHZqtCIGtNEl0MxugnrNXTERA,94
2
+ langchain_mcp_tools/langchain_mcp_tools.py,sha256=WY1ckLyIMbRIVhsECEFdAY6DnZB2Q3BLH-Nrb727M-o,11699
3
+ langchain_mcp_tools/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ langchain_mcp_tools-0.1.6.dist-info/LICENSE,sha256=CRC91e8v116gCpnp7h49oIa6_zjhxqnHFTREeoZFJwA,1072
5
+ langchain_mcp_tools-0.1.6.dist-info/METADATA,sha256=yUpYI9tc-JYxN15UzHGTpe9KeQbVT9NhRhUy6MJIqaY,4926
6
+ langchain_mcp_tools-0.1.6.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
+ langchain_mcp_tools-0.1.6.dist-info/top_level.txt,sha256=aR_9V2A1Yt-Bca60KmndmGLUWb2wiM5IOG-Gkaf1dxY,20
8
+ langchain_mcp_tools-0.1.6.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- langchain_mcp_tools/__init__.py,sha256=Xtv2VphhrWB_KlxTIofHZqtCIGtNEl0MxugnrNXTERA,94
2
- langchain_mcp_tools/langchain_mcp_tools.py,sha256=-kzxOhYUaieWngSSYk0kUZw6SeBNmxjnXm6Ed2rStz8,9627
3
- langchain_mcp_tools/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- langchain_mcp_tools-0.1.4.dist-info/LICENSE,sha256=CRC91e8v116gCpnp7h49oIa6_zjhxqnHFTREeoZFJwA,1072
5
- langchain_mcp_tools-0.1.4.dist-info/METADATA,sha256=-JPzwr4567fPMFq9caElQjQ2BorchdeXyf_TDje-bwo,4794
6
- langchain_mcp_tools-0.1.4.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
- langchain_mcp_tools-0.1.4.dist-info/top_level.txt,sha256=aR_9V2A1Yt-Bca60KmndmGLUWb2wiM5IOG-Gkaf1dxY,20
8
- langchain_mcp_tools-0.1.4.dist-info/RECORD,,