@h1deya/langchain-mcp-tools 0.1.3 → 0.1.5
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 +7 -5
- package/dist/langchain-mcp-tools.js +7 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
# MCP To LangChain Tools Conversion Utility [](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/LICENSE) [](https://www.npmjs.com/package/@h1deya/langchain-mcp-tools)
|
|
2
2
|
|
|
3
|
-
This package is intended to simplify the use of
|
|
3
|
+
This package is intended to simplify the use of
|
|
4
|
+
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/)
|
|
5
|
+
server tools with LangChain.
|
|
4
6
|
|
|
5
7
|
It contains a utility function `convertMcpToLangchainTools()`
|
|
6
8
|
that initializes specified MCP servers,
|
|
7
9
|
and returns [LangChain Tools](https://js.langchain.com/docs/how_to/tool_calling/)
|
|
8
|
-
that wrap all the tools found in
|
|
10
|
+
that wrap all the tools found in the MCP servers.
|
|
9
11
|
|
|
10
12
|
## Installation
|
|
11
13
|
|
|
@@ -15,7 +17,7 @@ npm i @h1deya/langchain-mcp-tools
|
|
|
15
17
|
|
|
16
18
|
## Quick Start
|
|
17
19
|
|
|
18
|
-
`convertMcpToLangchainTools()` utility function accepts MCP server
|
|
20
|
+
`convertMcpToLangchainTools()` utility function accepts MCP server configurations
|
|
19
21
|
that follows the same structure as
|
|
20
22
|
[Claude for Desktop](https://modelcontextprotocol.io/quickstart/user),
|
|
21
23
|
but only the contents of the `mcpServers` property,
|
|
@@ -44,10 +46,10 @@ const { tools, cleanup } = await convertMcpToLangchainTools(mcpServers);
|
|
|
44
46
|
|
|
45
47
|
The utility function initializes all specified MCP servers in parallel,
|
|
46
48
|
and returns LangChain Tools (`tools: DynamicStructuredTool[]`)
|
|
47
|
-
by gathering all
|
|
49
|
+
by gathering all available MCP server tools,
|
|
48
50
|
and by wrapping them into [LangChain Tools](https://js.langchain.com/docs/how_to/tool_calling/).
|
|
49
51
|
It also returns `cleanup` callback function
|
|
50
|
-
which is used to close all
|
|
52
|
+
which is used to close all connections to the MCP servers when finished.
|
|
51
53
|
|
|
52
54
|
The returned tools can be used with LangChain, e.g.:
|
|
53
55
|
|
|
@@ -87,7 +87,7 @@ async function convertSingleMcpToLangchainTools(serverName, config, logger) {
|
|
|
87
87
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
88
88
|
schema: jsonSchemaToZod(tool.inputSchema),
|
|
89
89
|
func: async (input) => {
|
|
90
|
-
logger.info(`MCP
|
|
90
|
+
logger.info(`MCP tool "${serverName}"/"${tool.name}" received input:`, input);
|
|
91
91
|
// Execute tool call
|
|
92
92
|
const result = await client?.request({
|
|
93
93
|
method: "tools/call",
|
|
@@ -96,15 +96,11 @@ async function convertSingleMcpToLangchainTools(serverName, config, logger) {
|
|
|
96
96
|
arguments: input,
|
|
97
97
|
},
|
|
98
98
|
}, CallToolResultSchema);
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
logger.
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
.map(content => content.text)
|
|
105
|
-
.join('\n\n');
|
|
106
|
-
return filteredResult;
|
|
107
|
-
// return JSON.stringify(result.content);
|
|
99
|
+
const resultStringfied = JSON.stringify(result?.content);
|
|
100
|
+
const roughLength = resultStringfied.length;
|
|
101
|
+
logger.info(`MCP tool "${serverName}"/"${tool.name}" received result (length: ${roughLength})`);
|
|
102
|
+
logger.debug('result:', result?.content);
|
|
103
|
+
return resultStringfied;
|
|
108
104
|
},
|
|
109
105
|
})));
|
|
110
106
|
logger.info(`MCP server "${serverName}": ${tools.length} tool(s) available:`);
|
|
@@ -112,7 +108,7 @@ async function convertSingleMcpToLangchainTools(serverName, config, logger) {
|
|
|
112
108
|
async function cleanup() {
|
|
113
109
|
if (transport) {
|
|
114
110
|
await transport.close();
|
|
115
|
-
logger.info(`MCP server "${serverName}":
|
|
111
|
+
logger.info(`MCP server "${serverName}": session closed`);
|
|
116
112
|
}
|
|
117
113
|
}
|
|
118
114
|
return { tools, cleanup };
|