发票管理系统

基于模型上下文协议(MCP)的客户和发票管理系统,支持自然语言处理和HTTP通信。

  • by lapth
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 lapth/mcp-test

使用说明

MCP发票系统

客户和发票管理系统使用具有客户端/服务器架构的模型上下文协议(MCP)。

建筑

┌──────────────────────────────────────────┐
│    Main Application (Client)             │
│  ┌────────────────────────────────────┐  │
│  │      OpenAI GPT-4 Turbo            │  │
│  │   - Natural language analysis      │  │
│  │   - Function calling for tools     │  │
│  │   - Friendly responses             │  │
│  └────────────────┬───────────────────┘  │
│                   │                       │
│  ┌────────────────▼───────────────────┐  │
│  │  MCP Client (HTTP Transport)       │  │
│  │   - HTTP connections to servers    │  │
│  │   - Execute tool calls             │  │
│  │   - Manage conversation history    │  │
│  └────────┬───────────┬────────────────┘  │
└───────────┼───────────┼───────────────────┘
            │           │
            │   HTTP    │   HTTP
            │           │
    ┌───────▼────┐  ┌───▼────────┐
    │ Customer   │  │ Invoice    │
    │ Server     │  │ Server     │
    │ Port: 3001 │  │ Port: 3002 │
    │ (Express)  │  │ (Express)  │
    └────────────┘  └────────────┘
    Independent      Independent

传输协议:

  • HTTP POST:MCP协议的简单请求/响应
  • 流式HTTPClientTransport:客户端MCP传输
  • 快速REST:健康检查和标准终点
  • MCP协议:客户端和服务器之间的通信

安装

  1. 初始化项目:
mkdir mcp-invoice-system
cd mcp-invoice-system
npm init -y
  1. 安装依赖项:
npm install @modelcontextprotocol/sdk openai dotenv express
npm install -D typescript @types/node @types/express
  1. 配置OpenAI API密钥: 创建 .env 根目录中的文件:
OPENAI_API_KEY=sk-your-api-key-here
OPENAI_MODEL=gpt-4-turbo-preview
CUSTOMER_SERVER_URL=http://localhost:3001
INVOICE_SERVER_URL=http://localhost:3002
CUSTOMER_PORT=3001
INVOICE_PORT=3002
  1. 创建目录结构:
mkdir src
  1. 复制文件:
  • src/customer-server.ts -客户MCP服务器
  • src/invoice-server.ts -发票MCP服务器
  • src/main-client.ts -主应用程序(使用OpenAI的客户端)
  • package.json -软件包配置
  • tsconfig.json -TypeScript配置
  • .env -环境变量(OPENAI_API_KEY)
  1. 构建项目:
npm run build

运行应用程序

方法1:分别运行每个服务(推荐)

终端1-客户服务器:

npm run start:customer
# Server runs at http://localhost:3001

终端2-发票服务器:

npm run start:invoice
# Server runs at http://localhost:3002

终端3-主应用程序(客户端):

npm run start:main

方法2:检查服务器运行状况

# Check Customer Server
curl http://localhost:3001/health

# Check Invoice Server
curl http://localhost:3002/health

方法3:自定义端口

在 .env 文件:

CUSTOMER_PORT=4001
INVOICE_PORT=4002
CUSTOMER_SERVER_URL=http://localhost:4001
INVOICE_SERVER_URL=http://localhost:4002

用法

运行主应用程序后,您可以与AI助手自然通信。OpenAI将自动分析并调用适当的工具。

自然语言命令示例:

1.创建客户

Create a new customer named John Doe, email john@example.com, phone 0909123456

或者简单地说:

Add a new customer: Jane Smith, jane@test.com

2.列出客户

Show me all customers

或者:

How many customers do we have?

3.创建发票

Create an invoice for customer CUST-1234567890

或者在创建客户之后:

Create an invoice for the customer I just created

4.列出发票

Show me all invoices

或者:

What invoices do we have?

5.退出

exit

工作流示例

# Terminal 1: Start Customer Server
npm run start:customer
✅ Customer MCP Server running on http://localhost:3001
   MCP endpoint: http://localhost:3001/mcp
   Health check: http://localhost:3001/health

# Terminal 2: Start Invoice Server
npm run start:invoice
✅ Invoice MCP Server running on http://localhost:3002
   MCP endpoint: http://localhost:3002/mcp
   Health check: http://localhost:3002/health

# Terminal 3: Start Main Client
npm run start:main
🔌 Connecting to MCP servers via HTTP...
✅ Connected to Customer Server: http://localhost:3001
✅ Connected to Invoice Server: http://localhost:3002
📋 Loaded 7 tools

🚀 Main Application - AI-Powered MCP Client
Enter your request (or 'exit' to quit):

# 1. Create customer with natural language
👉 Create a new customer named John Doe, email john@example.com, phone 0909123456

🤖 Analyzing request with OpenAI...

🔧 Executing actions...

📞 Calling: customer_create_customer
   Args: {
     "name": "John Doe",
     "email": "john@example.com",
     "phone": "0909123456"
   }
✅ Result: {...}

💬 Assistant: Successfully created customer John Doe with ID CUST-1700000000000. 
Email: john@example.com, Phone: 0909123456

────────────────────────────────────────────────────────────

# 2. Create invoice
👉 Create an invoice for customer CUST-1700000000000

🤖 Analyzing request with OpenAI...

🔧 Executing actions...

📞 Calling: invoice_create_invoice
   Args: {
     "customerId": "CUST-1700000000000",
     "items": [...]
   }
✅ Result: {...}

💬 Assistant: Created invoice INV-1700000000000 for the customer.
Total amount: $440.00 (including 10% tax)

────────────────────────────────────────────────────────────

MCP结构

客户服务器工具

  • create_customer -创建新客户
  • get_customer -按ID获取客户信息
  • list_customers -列出所有客户

发票服务器工具

  • create_invoice -为客户创建发票
  • get_invoice -按ID获取发票信息
  • list_invoices -列出发票(可按customerId筛选)
  • update_invoice_status -更新发票状态

延伸

添加新功能:

  1. 将工具添加到服务器:
// In ListToolsRequestSchema handler
{
  name: "your_new_tool",
  description: "Tool description",
  inputSchema: { ... }
}
  1. 处理工具调用:
// In CallToolRequestSchema handler
case "your_new_tool": {
  // Processing logic
  return { content: [...] };
}
  1. 主客户来电:
const result = await client.callTool({
  name: "your_new_tool",
  arguments: { ... }
});

备注

  • OpenAI API密钥:需要来自OpenAI的有效API密钥
  • 模型:默认为GPT-4 Turbo,可以切换到GPT-3.5 Turbo以节省成本
  • 独立服务器:客户和发票服务器独立运行,可以单独部署
  • HTTP传输:服务器使用简单的HTTP POST进行通信
  • 流式HTTPClientTransport:客户端使用基于HTTP的MCP传输
  • 数据存储:存储在内存中,服务器重启时会丢失(可以集成数据库)
  • 对话历史:在人工智能环境中保持会话状态
  • 自然语言:支持英语自然语言查询
  • 工具选择:OpenAI根据上下文自动选择合适的工具
  • 可扩展性:可以添加更多服务器,客户端将自动加载工具

这种架构的优点:

  • ✅ 微服务:每台服务器都是独立的,易于维护和扩展
  • ✅ 超文本传输协议:易于部署,可以在不同的机器上运行
  • ✅ 健康检查:易于监控服务器
  • ✅ 负载平衡:可以在服务器前添加负载平衡器
  • ✅ Docker就绪:每项服务都易于集装箱化
  • ✅ 简单运输:标准HTTP POST,无需复杂的SSE设置

成本优化:

# In .env file, switch to GPT-3.5-turbo
OPENAI_MODEL=gpt-3.5-turbo

参考