npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mcp-order-service

v1.0.0

Published

MCP service for order query with mock data - provides query_order and list_orders tools

Downloads

11

Readme

AI 电商对话系统

一个基于 MCP + LangChain 的 AI 电商客服对话系统,支持:

  • IP + UUID Cookie 会话隔离
  • MongoDB 按日期存储聊天记录
  • MCP 工具调用(订单查询)
  • 阿里云百炼(通义千问)LLM

功能

MCP 订单查询工具

| 工具名 | 功能 | 参数 | |-------|------|------| | query_order | 查询单个订单详情 | orderId (必填) | | list_orders | 查询订单列表 | status, customerId, limit (可选) |

对话 API

| 路由 | 方法 | 功能 | |------|------|------| | /api/chat | POST | 发送消息,获取 AI 回复 | | /api/chat/history | GET | 获取会话历史记录 | | /api/chat/session | GET | 获取/创建会话 | | /api/chat/status | GET | Agent 状态 | | /health | GET | 健康检查 |

模拟数据

包含 6 个模拟订单:

  • ORD-2024-001 - 张三 - MacBook Pro + Magic Mouse (已送达)
  • ORD-2024-002 - 李四 - iPhone 15 Pro Max + AirPods (已发货)
  • ORD-2024-003 - 张三 - iPad Air + Apple Pencil (处理中)
  • ORD-2024-004 - 王五 - Apple Watch Ultra (待处理)
  • ORD-2024-005 - 李四 - HomePod Mini + Apple TV (已取消)
  • ORD-2024-006 - 赵六 - Mac Studio + Studio Display (已送达)

安装与运行

1. 安装依赖

npm install

2. 配置环境变量

设置 API Key 到本地环境变量:

# Windows (CMD)
set api_key=your_dashscope_api_key

# Windows (PowerShell)
$env:api_key="your_dashscope_api_key"

# Linux/macOS
export api_key=your_dashscope_api_key

可选:创建 .env 文件

cp .env.example .env

关键配置:

  • api_key: 阿里云百炼 API Key(从环境变量读取,必填)
  • DASHSCOPE_MODEL: 模型名称,默认 qwen3.6-plus
  • MONGO_URL: MongoDB 连接地址(已配置默认值)

3. 编译项目

npm run build

4. 启动服务

# 启动对话服务(会自动启动 MCP 服务)
npm run chat

# 或单独启动 MCP 服务
npm run mcp

服务启动后访问:http://localhost:3000

API 使用示例

发送消息

curl -X POST http://localhost:3000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "查询订单 ORD-2024-001"}'

响应:

{
  "success": true,
  "sessionId": "sess_xxx",
  "reply": "您的订单 ORD-2024-001 详情如下:...",
  "toolCalls": [{"name": "query_order", "args": {"orderId": "ORD-2024-001"}}]
}

获取历史记录

curl "http://localhost:3000/api/chat/history?sessionId=sess_xxx"

获取/创建会话

curl http://localhost:3000/api/chat/session

项目结构

src/
├── index.ts                 # MCP 服务入口
├── data/
│   └── orders.ts            # 订单模拟数据
└── chat/
    ├── server.ts            # Express 服务入口
    ├── routes/
    │   └── chat.ts          # 对话 API 路由
    ├── services/
    │   ├── langchain.ts     # LangChain Agent 配置
    │   ├── mcp-client.ts    # MCP Client 连接
    │   └── session.ts       # 会话管理
    └── db/
        └── mongodb.ts       # MongoDB 存储

技术栈

  • 后端框架: Express
  • AI 框架: LangChain.js
  • LLM: 阿里云百炼 qwen3.6-plus
  • MCP SDK: @modelcontextprotocol/sdk
  • 数据库: MongoDB
  • 语言: TypeScript

会话隔离机制

系统采用 IP + UUID Cookie 方式实现会话隔离:

  1. 用户首次访问,系统生成 UUID SessionId
  2. SessionId 通过 Cookie 返回给客户端
  3. 后续请求携带 Cookie,系统验证 IP 绑定
  4. Session 24 小时过期

MongoDB 数据结构

聊天记录按日期存储,每日一个 Collection:

数据库: ecommerce_chat
Collections: chat_YYYY-MM-DD

Document:
{
  sessionId: string,
  ip: string,
  role: "user" | "assistant",
  content: string,
  toolCalls: [...],
  timestamp: Date
}

配置到 Claude Code

~/.claude/claude_desktop_config.json 中添加:

{
  "mcpServers": {
    "order-service": {
      "command": "node",
      "args": ["D:\\download\\mcp_demo\\dist\\index.js"]
    }
  }
}

作为 npm 包使用

安装

npm install mcp-order-service

配置 MCP 客户端

{
  "mcpServers": {
    "order-query": {
      "command": "npx",
      "args": ["mcp-order-service"]
    }
  }
}

发布到 npm

# 1. 登录 npm
npm login

# 2. 发布
npm publish

常见问题

1. MongoDB 连接失败

确保 MongoDB 服务运行中,检查 MONGO_URL 配置。

2. LLM 调用失败

检查环境变量 api_key 是否正确设置,确保有余额。

# 验证环境变量
echo $api_key  # Linux/macOS
echo %api_key% # Windows CMD

3. MCP 工具调用失败

MCP 服务会在对话服务启动时自动启动,检查日志输出。