mcpField
M
MCP Tools Usage From LangChain / Example in TypeScript

MCP Tools Usage From LangChain / Example in TypeScript

github.com/hideyaFrameworks & SDKs12Updated 31w ago

About MCP Tools Usage From LangChain / Example in TypeScript

This TypeScript example connects MCP server tools to a LangChain ReAct agent. It uses `convertMcpToLangchainTools()` to initialize multiple servers in parallel and convert their tools for LangChain.

Key features

  1. 01Parallel initialization of MCP servers
  2. 02Conversion to LangChain StructuredTools
  3. 03Provider-specific schema transformations
  4. 04OpenAI model example with alternatives

Use cases

  1. 01Use MCP server tools in a LangChain ReAct agent
  2. 02Initialize tools from multiple MCP servers
  3. 03Adapt tool schemas for supported LLM providers

MCP support

The example acts as an MCP client: it initializes specified MCP servers and makes their tools available to a LangChain ReAct agent.

Details

From the project README

MCP Tools Usage From LangChain / Example in TypeScript License: MIT

This simple Model Context Protocol (MCP) client demonstrates the use of MCP server tools by LangChain ReAct Agent.

It leverages a utility function convertMcpToLangchainTools() from @h1deya/langchain-mcp-tools.
This function handles parallel initialization of specified multiple MCP servers and converts their available tools into an array of LangChain-compatible tools (StructuredTool[]). It also performs LLM provider-specific schema transformations to prevent schema compatibility issues

OpenAI's gpt-5.4-mini is used as the LLM. For convenience, code for OpenAI's and Anthropic's LLMs are also included and commented out.

A bit more realistic (conversational) MCP Client is available here

A python equivalent of this app is available here

Prerequisites

  • Node.js 18+
  • npm 8+ (npx) to run Node.js-based MCP servers
  • [optional] npm 7+ (npx) to run Node.js package-based local MCP servers
  • [optional] uv (uvx) installed to run Python-based MCP servers
  • LLM API keys from OpenAI, Anthropic, Google GenAI, and/or xAI as needed

Usage

  1. Install dependencies:

    npm install
    
  2. Setup API key:

    cp .env.template .env
    
    • Update .env as needed.
    • .gitignore is configured to ignore .env to prevent accidental commits of the credentials.
  3. Run the app:

    npm start
    

License

MIT

Comments

Markdown supported

New to MCP?

From what the protocol is to installing your first server, these answers cover most first questions.

An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.