This repository demonstrates different ways to integrate LangChain with TypeScript, including native tool calling, single MCP server, and multiple MCP servers.
git clone https://github.com/rehmat123/Langchain-typescript
cd langchain-typescriptpnpm installCreate a .env file in the root:
OPENAI_API_KEY=your-openai-keyedit the MCP configuration file at ~/.cursor/mcp.json:
{
"mcpServers": {
"weather": {
"command": "npx",
"args": ["-y", "@rehmatalisayany/weather-mcp-server"],
"transport": "stdio"
},
}
}The src/langchain/toolsCalling.ts file demonstrates how to use LangChain with native tool calling. This approach allows you to define custom tools directly in your code.
pnpm function-calling-langchainThe src/langchain/singleMcp.ts file shows how to integrate a single MCP server (weather service) with LangChain. This allows you to use external services as tools in your LangChain agents.
pnpm single-mcp-weather-langchainThe src/langchain/multiMcp.ts file demonstrates how to use multiple MCP servers (weather and math) with LangChain. This enables your agents to use different tools from various services.
pnpm run multi-mcp-weather-langchain- LangChain.js - Framework for building context-aware, language model-driven applications
- TypeScript - Strongly typed JavaScript for better developer experience
- OpenAI - Used as the underlying language model
- MCP Server - Model Context Protocol server for external services
.
├── src/
│ ├── langchain/
│ │ ├── toolsCalling.ts # Native tool calling example
│ │ ├── singleMcp.ts # Single MCP server example
│ │ └── multiMcp.ts # Multiple MCP servers example
│ └── index.ts # Main entry point
├── package.json
└── README.md
-
MCP Server Connection Timeout
- If you see errors like
Failed to connect to stdio server: McpError: MCP error -32001: Request timed out, try:- Reducing the
delayMsvalue in the server configuration - Ensuring the MCP server package is installed correctly
- Checking your network connection
- Reducing the
- If you see errors like
-
Tool Not Found
- If you see errors about tools not being found, ensure:
- The tool name in your prompt matches the actual tool name (including prefixes)
- The MCP server is properly configured and running
- If you see errors about tools not being found, ensure:
-
Template Variable Errors
- If you see errors like
Missing value for input variable, check:- Your prompt template for proper variable formatting
- That all required variables are provided in the
invokemethod
- If you see errors like
- Enable verbose output in the agent executor to see detailed logs
- Check the console for specific error messages
- Verify that all required environment variables are set
The project uses the following configuration for MCP servers:
{
"mcpServers": {
"weather": {
"command": "npx",
"args": ["-y", "@rehmatalisayany/weather-mcp-server"],
"transport": "stdio"
},
"math": {
"command": "npx",
"args": ["-y", "nm-mcp-math"],
"transport": "stdio"
}
}
}If you encounter issues:
- Make sure your OpenAI API key is correctly set in the
.envfile - Verify that all dependencies are installed
- Check that the MCP servers are running correctly
- For connection timeouts, try reducing the
delayMsvalue in the server configuration
MIT