@h1deya/langchain-mcp-tools 0.1.11 → 0.1.13

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.
package/README.md CHANGED
@@ -5,35 +5,32 @@ This package is intended to simplify the use of
5
5
  server tools with LangChain / TypeScript.
6
6
 
7
7
  [Model Context Protocol (MCP)](https://modelcontextprotocol.io/),
8
- introduced by
9
- [Anthropic](https://www.anthropic.com/news/model-context-protocol),
10
- extends the capabilities of LLMs by enabling interaction with external tools and resources,
11
- such as web search and database access.
12
- Thanks to its open-source nature, MCP has gained significant traction in the developer community,
13
- with over 400 MCP servers already developed and shared:
8
+ an open source technology
9
+ [announced by Anthropic](https://www.anthropic.com/news/model-context-protocol),
10
+ dramatically expands LLM’s scope
11
+ by enabling external tool and resource integration, including
12
+ Google Drive, Slack, Notion, Spotify, Docker, PostgreSQL, and more…
13
+
14
+ Over 450 functional components available as MCP servers:
14
15
 
15
16
  - [Glama’s list of Open-Source MCP servers](https://glama.ai/mcp/servers)
16
17
  - [Smithery: MCP Server Registry](https://smithery.ai/)
17
18
  - [awesome-mcp-servers](https://github.com/hideya/awesome-mcp-servers#Server-Implementations)
18
19
  - [MCP Get Started/Example Servers](https://modelcontextprotocol.io/examples)
19
20
 
20
- In the MCP framework, external features are encapsulated in an MCP server,
21
- which typically runs in a separate process and communicates
22
- via `stdio` using the open standard protocol.
23
- This clean decoupling makes it easy to adopt and reuse any of
24
- the significant collections of MCP servers listed above.
25
-
26
- To make it easy for LangChain users to take advantage of such a vast resource base,
27
- this package offers quick and seamless access from LangChain to MCP servers.
21
+ The goal of this utility is to make these 450+ MCP servers readily accessible from LangChain.
28
22
 
29
23
  It contains a utility function `convertMcpToLangchainTools()`.
30
24
  This async function handles parallel initialization of specified multiple MCP servers
31
25
  and converts their available tools into an array of LangChain-compatible tools.
32
26
 
27
+ For detailed information on how to use this library, please refer to the following document:
28
+ - ["Supercharging LangChain: Integrating 450+ MCP with ReAct"](https://medium.com/@h1deya/supercharging-langchain-integrating-450-mcp-with-react-d4e467cbf41a)
29
+
33
30
  A python equivalent of this utility is available
34
31
  [here](https://pypi.org/project/langchain-mcp-tools)
35
32
 
36
- ## Requirements
33
+ ## Prerequisites
37
34
 
38
35
  - Node.js 16+
39
36
 
@@ -78,7 +75,7 @@ The returned tools can be used with LangChain, e.g.:
78
75
 
79
76
  ```ts
80
77
  // import { ChatAnthropic } from '@langchain/anthropic';
81
- const llm = new ChatAnthropic({ model: 'claude-3-5-haiku-latest' });
78
+ const llm = new ChatAnthropic({ model: 'claude-3-5-sonnet-latest' });
82
79
 
83
80
  // import { createReactAgent } from '@langchain/langgraph/prebuilt';
84
81
  const agent = createReactAgent({
@@ -86,11 +83,15 @@ const agent = createReactAgent({
86
83
  tools
87
84
  });
88
85
  ```
89
- A simple and experimentable usage example can be found
86
+
87
+ Find complete, minimal working usage examples
90
88
  [here](https://github.com/hideya/langchain-mcp-tools-ts-usage/blob/main/src/index.ts)
91
89
 
92
- A more realistic usage example can be found
93
- [here](https://github.com/hideya/langchain-mcp-client-ts)
90
+ For hands-on experimentation with MCP server integration,
91
+ try [this LangChain application built with the utility](https://github.com/hideya/mcp-client-langchain-ts)
92
+
93
+ For detailed information on how to use this library, please refer to the following document:
94
+ ["Supercharging LangChain: Integrating 450+ MCP with ReAct"](https://medium.com/@h1deya/supercharging-langchain-integrating-450-mcp-with-react-d4e467cbf41a)
94
95
 
95
96
  ## Limitations
96
97
 
@@ -121,26 +121,40 @@ async function convertSingleMcpToLangchainTools(serverName, config, logger) {
121
121
  // FIXME
122
122
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
123
123
  schema: jsonSchemaToZod(tool.inputSchema),
124
- func: async (input) => {
124
+ func: async function (input) {
125
125
  logger.info(`MCP tool "${serverName}"/"${tool.name}" received input:`, input);
126
- // Execute tool call
127
- const result = await client?.request({
128
- method: "tools/call",
129
- params: {
130
- name: tool.name,
131
- arguments: input,
132
- },
133
- }, CallToolResultSchema);
134
- const resultStringfied = JSON.stringify(result?.content);
135
- const roughLength = resultStringfied.length;
136
- logger.info(`MCP tool "${serverName}"/"${tool.name}" received result (length: ${roughLength})`);
137
- logger.debug('result:', result?.content);
138
- return resultStringfied;
139
- // const filteredResult = result?.content
140
- // .filter(content => content.type === 'text')
141
- // .map(content => content.text)
142
- // .join('\n\n');
143
- // return filteredResult;
126
+ try {
127
+ // Execute tool call
128
+ const result = await client?.request({
129
+ method: "tools/call",
130
+ params: {
131
+ name: tool.name,
132
+ arguments: input,
133
+ },
134
+ }, CallToolResultSchema);
135
+ // Handles null/undefined cases gracefully
136
+ if (!result?.content) {
137
+ logger.info(`MCP tool "${serverName}"/"${tool.name}" received null/undefined result`);
138
+ return '';
139
+ }
140
+ const textContent = result.content
141
+ .filter(content => content.type === 'text')
142
+ .map(content => content.text)
143
+ .join('\n\n');
144
+ // const textItems = result.content
145
+ // .filter(content => content.type === 'text')
146
+ // .map(content => content.text)
147
+ // const textContent = JSON.stringify(textItems);
148
+ // Log rough result size for monitoring
149
+ const size = new TextEncoder().encode(textContent).length;
150
+ logger.info(`MCP tool "${serverName}"/"${tool.name}" received result (size: ${size})`);
151
+ // If no text content, return a clear message describing the situation
152
+ return textContent || 'No text content available in response';
153
+ }
154
+ catch (error) {
155
+ logger.warn(`MCP tool "${serverName}"/"${tool.name}" caused error: ${error}`);
156
+ return `Error executing MCP tool: ${error}`;
157
+ }
144
158
  },
145
159
  })));
146
160
  logger.info(`MCP server "${serverName}": ${tools.length} tool(s) available:`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h1deya/langchain-mcp-tools",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "MCP To LangChain Tools Conversion Utility",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -33,11 +33,14 @@
33
33
  "build": "tsc",
34
34
  "prepare": "npm run build",
35
35
  "watch": "tsc --watch",
36
+ "example": "tsx examples/example.ts",
36
37
  "lint": "eslint src",
37
38
  "test": "vitest run",
38
39
  "test:watch": "vitest",
39
40
  "test:coverage": "vitest run --coverage",
40
- "clean": "git clean -fdxn -e .env && read -p 'OK?' && git clean -fdx -e .env"
41
+ "clean": "git clean -fdxn -e .env && read -p 'OK?' && git clean -fdx -e .env",
42
+ "do-publish": "npm run clean && npm install && npm publish --access=public",
43
+ "publish-dry-run": "npm run clean && npm install && npm publish --access=public --dry-run"
41
44
  },
42
45
  "dependencies": {
43
46
  "@langchain/core": "^0.3.27",
@@ -47,11 +50,16 @@
47
50
  },
48
51
  "devDependencies": {
49
52
  "@eslint/js": "^9.17.0",
53
+ "@langchain/anthropic": "^0.3.11",
54
+ "@langchain/langgraph": "^0.2.36",
55
+ "@langchain/openai": "^0.3.16",
50
56
  "@types/node": "^22.10.5",
51
57
  "@typescript-eslint/eslint-plugin": "^8.19.0",
52
58
  "@typescript-eslint/parser": "^8.19.0",
53
59
  "@vitest/coverage-v8": "^2.1.8",
60
+ "dotenv": "^16.4.7",
54
61
  "eslint": "^9.17.0",
62
+ "tsx": "^4.19.3",
55
63
  "typescript": "^5.7.2",
56
64
  "typescript-eslint": "^8.19.0",
57
65
  "vitest": "^2.1.8"