langchain-mcp-user-query-demo

langchain-mcp-user-query-demo

A demonstrative MCP server that exposes a query-user tool for retrieving user info by ID, integrated with LangChain for agent-driven model reasoning and tool calls.

Category
Visit Server

README

LangChain MCP User Query Demo

一个基于 Node.js 的 MCP Server 示例项目,演示如何:

  • 使用 @modelcontextprotocol/sdk 创建本地 MCP Server
  • 在 Cursor 等 MCP Client 中挂载本地工具
  • 通过 @langchain/mcp-adapters 让 LangChain 调用 MCP 工具
  • 使用 OpenAI 兼容接口完成“模型思考 -> 工具调用 -> 返回结果”的 Agent 流程

建议仓库名:langchain-mcp-user-query-demo

功能概览

当前示例内置了一个简单的用户数据源,并注册了以下 MCP 能力:

类型 名称 说明
Tool query-user 根据用户 ID 查询用户姓名、邮箱和角色
Resource docs://guide 提供 MCP Server 使用说明

LangChain 示例客户端会启动本地 MCP Server,读取可用工具,并让模型根据用户问题自动决定是否调用 query-user。

项目结构

.
├── assets/
│   └── cursor-mcp-setting.png
├── src/
│   ├── my-mcp-server.mjs       # MCP Server,注册工具和资源
│   └── langchain-mcp-test.mjs  # LangChain MCP Client 调用示例
├── package.json
├── pnpm-lock.yaml
└── README.md

环境要求

  • Node.js 18+
  • pnpm
  • Cursor 或其他支持 MCP 的客户端
  • OpenAI 兼容的模型服务

安装依赖

pnpm install

配置环境变量

在项目根目录创建 .env 文件:

MODEL_NAME=your-model-name
OPENAI_API_KEY=your-api-key
OPENAI_BASE_URL=https://your-openai-compatible-endpoint/v1

如果使用官方 OpenAI API,OPENAI_BASE_URL 可以按你的 SDK 配置习惯填写或省略。

在 Cursor 中使用 MCP Server

打开 Cursor 的 MCP 配置,添加本地 server:

{
  "mcpServers": {
    "my-mcp-server": {
      "command": "node",
      "args": [
        "D:/code/agent-node/mcp-server/src/my-mcp-server.mjs"
      ]
    }
  }
}

如果项目路径不同,请把 args 中的路径替换为你本机的 src/my-mcp-server.mjs 绝对路径。

配置示例:

Cursor MCP setting

配置完成后,Cursor 可以在对话中识别并调用 query-user 工具。

通过 LangChain 调用 MCP Server

运行示例客户端:

pnpm start

src/langchain-mcp-test.mjs 的主要流程:

  1. 从 .env 读取模型名称、API Key 和接口地址。
  2. 使用 MultiServerMCPClient 启动并连接本地 MCP Server。
  3. 调用 mcpClient.getTools() 获取 MCP 工具列表。
  4. 使用 model.bindTools(tools) 将工具绑定到模型。
  5. 进入 Agent 循环:模型产生工具调用时执行工具,并把结果追加回消息历史。

默认测试问题是:

查一下用户 002 信息

如果工具调用成功,模型会读取本地示例数据并返回用户信息。

示例数据

MCP Server 当前内置了 3 条用户数据:

用户 ID 姓名 邮箱 角色
001 张三 zhangsan@example.com admin
002 李四 lisi@example.com user
003 王五 wangwu@example.com user

你可以在 src/my-mcp-server.mjs 中替换 database,把它改造成真实数据库、HTTP API 或业务系统查询工具。

常见问题

Cursor 找不到 MCP Server

请检查:

  • command 是否能在终端中直接执行
  • args 是否使用了正确的绝对路径
  • 依赖是否已经通过 pnpm install 安装
  • Node.js 版本是否满足要求

LangChain 示例无法调用模型

请检查:

  • .env 中的 MODEL_NAME 是否可用
  • OPENAI_API_KEY 是否正确
  • OPENAI_BASE_URL 是否与所使用的模型服务匹配

修改项目路径后示例失效

README.md 和 src/langchain-mcp-test.mjs 中都使用了本地绝对路径示例。移动项目后,需要同步更新 MCP Server 的入口路径。

后续可扩展方向

  • 将示例数据替换为数据库查询
  • 增加更多 MCP tools,例如新增用户、搜索订单、查询知识库
  • 将 server 路径改为环境变量,避免硬编码本机路径
  • 为 MCP Server 添加单元测试和集成测试

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured