@h1deya/langchain-mcp-tools 0.1.5 → 0.1.7

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
@@ -1,13 +1,20 @@
1
- # MCP To LangChain Tools Conversion Utility [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/@h1deya/langchain-mcp-tools.svg)](https://www.npmjs.com/package/@h1deya/langchain-mcp-tools)
1
+ # MCP To LangChain Tools Conversion Utility / TypeScript [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/@h1deya/langchain-mcp-tools.svg)](https://www.npmjs.com/package/@h1deya/langchain-mcp-tools)
2
2
 
3
3
  This package is intended to simplify the use of
4
4
  [Model Context Protocol (MCP)](https://modelcontextprotocol.io/)
5
- server tools with LangChain.
5
+ server tools with LangChain / TypeScript.
6
6
 
7
- It contains a utility function `convertMcpToLangchainTools()`
8
- that initializes specified MCP servers,
9
- and returns [LangChain Tools](https://js.langchain.com/docs/how_to/tool_calling/)
10
- that wrap all the tools found in the MCP servers.
7
+ It contains a utility function `convertMcpToLangchainTools()`.
8
+ This function handles parallel initialization of specified multiple MCP servers
9
+ and converts their available tools into an array of
10
+ [LangChain-compatible tools](https://js.langchain.com/docs/how_to/tool_calling/).
11
+
12
+ A python version of this utility library is available
13
+ [here](https://pypi.org/project/langchain-mcp-tools)
14
+
15
+ ## Requirements
16
+
17
+ - Node.js 16+
11
18
 
12
19
  ## Installation
13
20
 
@@ -18,7 +25,7 @@ npm i @h1deya/langchain-mcp-tools
18
25
  ## Quick Start
19
26
 
20
27
  `convertMcpToLangchainTools()` utility function accepts MCP server configurations
21
- that follows the same structure as
28
+ that follow the same structure as
22
29
  [Claude for Desktop](https://modelcontextprotocol.io/quickstart/user),
23
30
  but only the contents of the `mcpServers` property,
24
31
  and is expressed as a JS Object, e.g.:
@@ -27,17 +34,11 @@ and is expressed as a JS Object, e.g.:
27
34
  const mcpServers: McpServersConfig = {
28
35
  filesystem: {
29
36
  command: 'npx',
30
- args: [
31
- '-y',
32
- '@modelcontextprotocol/server-filesystem',
33
- '/Users/username/Desktop' // path to a directory to allow access to
34
- ]
37
+ args: ['-y', '@modelcontextprotocol/server-filesystem', '.']
35
38
  },
36
39
  fetch: {
37
40
  command: 'uvx',
38
- args: [
39
- 'mcp-server-fetch'
40
- ]
41
+ args: ['mcp-server-fetch']
41
42
  }
42
43
  };
43
44
 
@@ -49,7 +50,7 @@ and returns LangChain Tools (`tools: DynamicStructuredTool[]`)
49
50
  by gathering all available MCP server tools,
50
51
  and by wrapping them into [LangChain Tools](https://js.langchain.com/docs/how_to/tool_calling/).
51
52
  It also returns `cleanup` callback function
52
- which is used to close all connections to the MCP servers when finished.
53
+ to be invoked to close all MCP server sessions when finished.
53
54
 
54
55
  The returned tools can be used with LangChain, e.g.:
55
56
 
@@ -10,11 +10,30 @@ export interface McpServersConfig {
10
10
  interface LogOptions {
11
11
  logLevel?: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
12
12
  }
13
- export interface McpServerCleanupFunction {
13
+ export interface McpServerCleanupFn {
14
14
  (): Promise<void>;
15
15
  }
16
+ /**
17
+ * Initializes multiple MCP (Model Context Protocol) servers and converts them into LangChain tools.
18
+ * This function concurrently sets up all specified servers and aggregates their tools.
19
+ *
20
+ * @param configs - A mapping of server names to their respective configurations
21
+ * @param options - Optional logging configuration
22
+ *
23
+ * @returns A promise that resolves to:
24
+ * - tools: Array of DynamicStructuredTool instances ready for use with LangChain
25
+ * - cleanup: Function to properly terminate all server connections
26
+ *
27
+ * @throws McpInitializationError if any server fails to initialize
28
+ *
29
+ * @example
30
+ * const { tools, cleanup } = await convertMcpToLangchainTools({
31
+ * filesystem: { command: 'npx', args: ['-y', '@modelcontextprotocol/server-filesystem', '.'] },
32
+ * fetch: { command: 'uvx', args: ['mcp-server-fetch'] }
33
+ * });
34
+ */
16
35
  export declare function convertMcpToLangchainTools(configs: McpServersConfig, options?: LogOptions): Promise<{
17
36
  tools: DynamicStructuredTool[];
18
- cleanup: McpServerCleanupFunction;
37
+ cleanup: McpServerCleanupFn;
19
38
  }>;
20
39
  export {};
@@ -5,17 +5,35 @@ import { DynamicStructuredTool } from '@langchain/core/tools';
5
5
  import { jsonSchemaToZod } from '@n8n/json-schema-to-zod';
6
6
  import { Logger } from './logger.js';
7
7
  // Custom error type for MCP server initialization failures
8
- class MCPInitializationError extends Error {
8
+ class McpInitializationError extends Error {
9
9
  serverName;
10
10
  details;
11
11
  constructor(serverName, message, details) {
12
12
  super(message);
13
13
  this.serverName = serverName;
14
14
  this.details = details;
15
- this.name = 'MCPInitializationError';
15
+ this.name = 'McpInitializationError';
16
16
  }
17
17
  }
18
- // Primary function to convert multiple MCP servers to LangChain tools
18
+ /**
19
+ * Initializes multiple MCP (Model Context Protocol) servers and converts them into LangChain tools.
20
+ * This function concurrently sets up all specified servers and aggregates their tools.
21
+ *
22
+ * @param configs - A mapping of server names to their respective configurations
23
+ * @param options - Optional logging configuration
24
+ *
25
+ * @returns A promise that resolves to:
26
+ * - tools: Array of DynamicStructuredTool instances ready for use with LangChain
27
+ * - cleanup: Function to properly terminate all server connections
28
+ *
29
+ * @throws McpInitializationError if any server fails to initialize
30
+ *
31
+ * @example
32
+ * const { tools, cleanup } = await convertMcpToLangchainTools({
33
+ * filesystem: { command: 'npx', args: ['-y', '@modelcontextprotocol/server-filesystem', '.'] },
34
+ * fetch: { command: 'uvx', args: ['mcp-server-fetch'] }
35
+ * });
36
+ */
19
37
  export async function convertMcpToLangchainTools(configs, options) {
20
38
  const allTools = [];
21
39
  const cleanupCallbacks = [];
@@ -53,7 +71,24 @@ export async function convertMcpToLangchainTools(configs, options) {
53
71
  allTools.forEach((tool) => logger.debug(`- ${tool.name}`));
54
72
  return { tools: allTools, cleanup };
55
73
  }
56
- // Convert a single MCP server into LangChain tools
74
+ /**
75
+ * Initializes a single MCP server and converts its capabilities into LangChain tools.
76
+ * Sets up a connection to the server, retrieves available tools, and creates corresponding
77
+ * LangChain tool instances.
78
+ *
79
+ * @param serverName - Unique identifier for the server instance
80
+ * @param config - Server configuration including command, arguments, and environment variables
81
+ * @param logger - Logger instance for recording operation details
82
+ *
83
+ * @returns A promise that resolves to:
84
+ * - tools: Array of DynamicStructuredTool instances from this server
85
+ * - cleanup: Function to properly terminate the server connection
86
+ *
87
+ * @throws McpInitializationError if server initialization fails
88
+ * (includes connection errors, tool listing failures)
89
+ *
90
+ * @internal This function is meant to be called by convertMcpToLangchainTools
91
+ */
57
92
  async function convertSingleMcpToLangchainTools(serverName, config, logger) {
58
93
  let transport = null;
59
94
  let client = null;
@@ -101,6 +136,11 @@ async function convertSingleMcpToLangchainTools(serverName, config, logger) {
101
136
  logger.info(`MCP tool "${serverName}"/"${tool.name}" received result (length: ${roughLength})`);
102
137
  logger.debug('result:', result?.content);
103
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;
104
144
  },
105
145
  })));
106
146
  logger.info(`MCP server "${serverName}": ${tools.length} tool(s) available:`);
@@ -124,6 +164,6 @@ async function convertSingleMcpToLangchainTools(serverName, config, logger) {
124
164
  logger.error(`Failed to cleanup during initialization error: ${cleanupError}`);
125
165
  }
126
166
  }
127
- throw new MCPInitializationError(serverName, `Failed to initialize MCP server: ${error instanceof Error ? error.message : String(error)}`, error);
167
+ throw new McpInitializationError(serverName, `Failed to initialize MCP server: ${error instanceof Error ? error.message : String(error)}`, error);
128
168
  }
129
169
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h1deya/langchain-mcp-tools",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "MCP To LangChain Tools Conversion Utility",
5
5
  "license": "MIT",
6
6
  "keywords": [