| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- const API = require("../../lib/API")
- const MCP = require("../../lib/Lepao/Mcp.js").MCP
- class McpRpc extends API {
- constructor() {
- super()
- this.noEncrypt()
- this.setPath('/mcp')
- this.setMethod('POST')
- }
- async onRequest(req, res) {
- const { method, params = {}, id: req_id } = req.body
- try {
- let result
- if (method === 'initialize') {
- result = {
- protocolVersion: "2024-11-05",
- capabilities: { "tools": {} },
- serverInfo: { name: "runforge", version: "1.0" }
- }
- } else if (method === 'tools/list') {
- result = {
- "tools": [
- {
- "name": "bind_account",
- "description": "Bind a user account when the user provides a bind code",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- },
- "bind_code": {
- "type": "string",
- "description": "Binding code provided by the user"
- }
- },
- "required": ["sender", "bind_code"]
- }
- },
- {
- "name": "unbind_account",
- "description": "Unbind the currently linked user account",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- }
- },
- "required": ["sender"]
- }
- },
- {
- "name": "get_account_info",
- "description": "Retrieve user account information such as name, student ID, account status, and running statistics",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- }
- },
- "required": ["sender"]
- }
- },
- {
- "name": "set_notification",
- "description": "Set notification preference when the user wants to enable or disable notifications",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- },
- "mode": {
- "type": "string",
- "enum": ["bot", "email", "none"],
- "description": "Notification mode: bot (chat bot), email (email notification), none (disable notifications)"
- }
- },
- "required": ["sender", "mode"]
- }
- },
- {
- "name": "change_email",
- "description": "Update the user's notification email address",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- },
- "email": {
- "type": "string",
- "format": "email",
- "description": "New email address for notifications"
- }
- },
- "required": ["sender", "email"]
- }
- },
- {
- "name": "create_work_order",
- "description": "Create a customer support ticket when the user reports a problem or suggestion",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- },
- "email": {
- "type": "string",
- "format": "email",
- "description": "User contact email"
- },
- "title": {
- "type": "string",
- "maxLength": 20,
- "description": "Short title of the issue (max 20 characters)"
- },
- "content": {
- "type": "string",
- "maxLength": 100,
- "description": "Detailed description of the issue (max 100 characters)"
- }
- },
- "required": ["sender", "email", "title", "content"]
- }
- },
- {
- "name": "change_area",
- "description": "Set the running area for the user. If the user requests a random area, use '随机分配'",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- },
- "area": {
- "type": "string",
- "enum": [
- "兰花湖校区跑区",
- "主校区北跑区",
- "主校区南跑区",
- "重庆工商大学茶园校区",
- "随机分配"
- ],
- "description": "Running area. Must be one of the predefined areas, or '随机分配' for random assignment"
- }
- },
- "required": ["sender", "area"]
- }
- },
- {
- "name": "change_auto_time",
- "description": "Set the automatic running start hour. Use -1 if the user wants a random time assigned",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- },
- "auto_time": {
- "type": "integer",
- "minimum": -1,
- "maximum": 23,
- "description": "Hour of day to start running (7-23), or -1 to assign a random time"
- }
- },
- "required": ["sender", "auto_time"]
- }
- },
- {
- "name": "change_auto_day",
- "description": "Set the days of the week for automatic running",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": {
- "type": "string",
- "description": "Unique user identifier from the chat platform"
- },
- "auto_day": {
- "type": "array",
- "items": {
- "type": "integer",
- "minimum": 0,
- "maximum": 6
- },
- "description": "Days of week to run (0=Sunday, 6=Saturday)"
- }
- },
- "required": ["sender", "auto_day"]
- }
- }
- ]
- }
- } else if (method === 'tools/call') {
- const { name, arguments: args = {} } = params
- let output
- switch (name) {
- case "bind_account":
- output = await MCP.bind_account(args)
- break
- case "get_account_info":
- output = await MCP.get_account_info(args)
- break
- case "unbind_account":
- output = await MCP.unbind_account(args)
- break
- case "set_notification":
- output = await MCP.set_notification(args)
- break
- case "change_email":
- output = await MCP.change_email(args)
- break
- case "create_work_order":
- output = await MCP.create_work_order(args)
- break
- case "change_area":
- output = await MCP.change_area(args)
- break
- case "change_auto_time":
- output = await MCP.change_auto_time(args)
- break
- case "change_auto_day":
- output = await MCP.change_auto_day(args)
- break
- default:
- output = "未知工具"
- }
- result = { content: [{ type: "text", text: output }] }
- } else {
- result = { error: `未知方法: ${method}` }
- }
- // 返回标准 JSON-RPC 响应
- return res.json({
- jsonrpc: "2.0",
- id: req_id ?? null,
- result
- })
- } catch (e) {
- return res.json({
- jsonrpc: "2.0",
- id: req_id ?? null,
- error: { code: -32000, message: e.message }
- })
- }
- }
- }
- module.exports.McpRpc = McpRpc
|