#!/usr/bin/env node /** * TLBM MCP Server - 无人车运营 API MCP 封装 * * 基于 http://localhost:8088/v3/api-docs 的 OpenAPI 规范自动生成 * 线上服务地址: https://www.atomdancing.com/pm */ import { Server } from "@modelcontextprotocol/sdk/server/index.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js"; // API 配置 const API_BASE_URL = process.env.TLBM_API_URL || "https://www.atomdancing.com/pm"; const USER_TOKEN = process.env.TLBM_TOKEN || ""; // HTTP 请求函数 async function httpRequest(method: string, path: string, params?: Record, body?: any, headers?: Record): Promise { const url = new URL(`${API_BASE_URL}${path}`); // 添加查询参数 if (params && method === "GET") { Object.entries(params).forEach(([key, value]) => { if (value !== undefined && value !== null) { url.searchParams.append(key, String(value)); } }); } const fetchHeaders: Record = { "Content-Type": "application/json", ...headers, }; // 用户 Token 认证 if (USER_TOKEN) { fetchHeaders["Authorization"] = `Bearer ${USER_TOKEN}`; } const response = await fetch(url.toString(), { method, headers: fetchHeaders, body: body ? JSON.stringify(body) : undefined, }); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } return response.json(); } // 工具定义 const TOOLS = [ // === 车辆状态 === { name: "list_vehicle_status", description: "查询所有车辆最新状态,按电量由低到高排序(支持满满、自营)", inputSchema: { type: "object", properties: {} }, endpoint: { method: "GET", path: "/api/vehicle/status/list" }, }, { name: "get_vehicle_status", description: "根据车牌号查询车辆最新状态(支持满满、自营)", inputSchema: { type: "object", properties: { vinId: { type: "string", description: "车牌号,如:京A12345" }, }, required: ["vinId"], }, endpoint: { method: "GET", path: "/api/vehicle/status/getByVinId" }, }, { name: "list_vehicle_by_status", description: "根据状态码查询车辆列表(当前仅支持满满)", inputSchema: { type: "object", properties: { status: { type: "integer", description: "车辆状态码:0=关机,1=开机,2=任务中,3=故障,4=充电中,5=不可接单,6=OTA升级中,71=有任务装货开门,72=有任务卸货开门,8=非营运时间" }, }, required: ["status"], }, endpoint: { method: "GET", path: "/api/vehicle/status/listByStatus" }, }, { name: "get_vehicle_metrics", description: "获取指定车辆的工作时长等指标(工作时间窗口9:00-16:00,仅支持满满)", inputSchema: { type: "object", properties: { vinId: { type: "string", description: "车牌号" }, date: { type: "string", description: "日期 yyyy-MM-dd,默认当天" }, }, required: ["vinId"], }, endpoint: { method: "GET", path: "/api/vehicle/status/metrics" }, }, { name: "get_vehicle_unavailability", description: "统计车辆不可用时长(关机、故障、充电、低电量等)", inputSchema: { type: "object", properties: { vinId: { type: "string", description: "车牌号" }, date: { type: "string", description: "日期 yyyy-MM-dd,默认当天" }, }, required: ["vinId"], }, endpoint: { method: "GET", path: "/api/vehicle/status/unavailability" }, }, { name: "list_vehicle_unavailability", description: "统计所有车辆不可用时长,按不可用时长从小到大排序(当前仅支持满满)", inputSchema: { type: "object", properties: { date: { type: "string", description: "日期 yyyy-MM-dd,默认当天" }, }, }, endpoint: { method: "GET", path: "/api/vehicle/status/unavailability/list" }, }, { name: "get_vehicle_drive_mile", description: "统计车辆在任务中和非任务中的行驶距离(当前仅支持满满)", inputSchema: { type: "object", properties: { vinId: { type: "string", description: "车牌号(可选,不传则查询所有车辆)" }, date: { type: "string", description: "日期 yyyy-MM-dd,默认当天" }, }, }, endpoint: { method: "GET", path: "/api/vehicle/status/driveMile" }, }, // === 车辆操作 === { name: "get_vehicle_path_playback", description: "车辆运行轨迹回放,获取指定日期的轨迹和状态信息", inputSchema: { type: "object", properties: { vinId: { type: "string", description: "车牌号" }, date: { type: "string", description: "日期 yyyy-MM-dd" }, status: { type: "integer", description: "状态码(可选)" }, }, required: ["vinId", "date"], }, endpoint: { method: "GET", path: "/api/vehicle/path/playback" }, }, { name: "list_vehicle_heartbeat", description: "查询车辆心跳数据列表,支持分页", inputSchema: { type: "object", properties: { cityId: { type: "integer", description: "城市ID" }, operator: { type: "string", description: "操作员" }, page: { type: "integer", default: 1, description: "页码" }, pageSize: { type: "integer", default: 20, description: "每页数量" }, }, }, endpoint: { method: "GET", path: "/api/vehicle/heartbeat/list" }, }, // === 运单监控 === { name: "get_overdue_shipping", description: "查询超时未确认装卸货的运单段", inputSchema: { type: "object", properties: { minutes: { type: "integer", default: 10, description: "超时阈值(分钟)" }, }, }, endpoint: { method: "GET", path: "/api/shipping/monitor/overdue" }, }, // === 订单管理 === { name: "get_order_summary", description: "根据外部订单号查询运单汇总信息(当前仅支持满满)", inputSchema: { type: "object", properties: { outOrderNo: { type: "string", description: "外部订单号" }, }, required: ["outOrderNo"], }, endpoint: { method: "GET", path: "/api/order/summary/{outOrderNo}" }, }, { name: "get_today_orders", description: "查询当日运单汇总列表(当前仅支持满满)", inputSchema: { type: "object", properties: {} }, endpoint: { method: "GET", path: "/api/order/summary/today" }, }, { name: "get_orders_by_date", description: "查询指定日期的运单汇总列表(当前仅支持满满)", inputSchema: { type: "object", properties: { date: { type: "string", description: "日期 yyyy-MM-dd" }, }, required: ["date"], }, endpoint: { method: "GET", path: "/api/order/summary/date" }, }, { name: "get_unfinished_orders", description: "查询所有未完成的运单汇总列表(当前仅支持满满)", inputSchema: { type: "object", properties: {} }, endpoint: { method: "GET", path: "/api/order/summary/unfinished" }, }, { name: "query_orders", description: "查询指定日期范围订单列表数据,支持分页和时间筛选(仅支持自营)", inputSchema: { type: "object", properties: { cityId: { type: "integer", description: "城市ID" }, orderStatus: { type: "string", description: "订单状态:WAITING_DISPATCH-待派车, DISPATCHED-已派车, IN_OPERATION-装配中, COMPLETED-已完成, CANCELLED-已取消, ABNORMAL-异常" }, startDate: { type: "string", description: "开始日期 yyyy-MM-dd,默认当天" }, endDate: { type: "string", description: "结束日期 yyyy-MM-dd,默认当天" }, page: { type: "integer", default: 1, description: "页码" }, pageSize: { type: "integer", default: 20, description: "每页数量" }, }, }, endpoint: { method: "GET", path: "/api/order/rpc/query" }, }, // === 配置查询 === { name: "get_open_cities", description: "获取开通城市列表", inputSchema: { type: "object", properties: { adcode: { type: "string", description: "行政区划代码(可选),如 320200 表示无锡" }, }, }, endpoint: { method: "GET", path: "/api/config/open-cities" }, }, ]; // 创建 Server const server = new Server( { name: "tlbm-mcp", version: "1.0.0" }, { capabilities: { tools: {} } } ); // 注册 list_tools handler server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: TOOLS.map((tool) => ({ name: tool.name, description: tool.description, inputSchema: tool.inputSchema, })), }; }); // 注册 call_tool handler server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; const tool = TOOLS.find((t) => t.name === name); if (!tool) { throw new Error(`Unknown tool: ${name}`); } try { // 处理路径参数 let path = tool.endpoint.path; const params = args ? { ...args } : {}; if (path.includes("{vinId}") && params.vinId) { path = path.replace("{vinId}", String(params.vinId)); delete params.vinId; } if (path.includes("{outOrderNo}") && params.outOrderNo) { path = path.replace("{outOrderNo}", String(params.outOrderNo)); delete params.outOrderNo; } if (path.includes("{orderId}") && params.orderId) { path = path.replace("{orderId}", String(params.orderId)); delete params.orderId; } if (path.includes("{wxOpenId}") && params.wxOpenId) { path = path.replace("{wxOpenId}", String(params.wxOpenId)); delete params.wxOpenId; } // 发送请求 const result = await httpRequest( tool.endpoint.method, path, tool.endpoint.method === "GET" ? params : undefined, tool.endpoint.method === "POST" ? params : undefined ); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; } catch (error: any) { return { content: [{ type: "text", text: `错误: ${error.message}` }], }; } }); // 启动服务 async function main() { const transport = new StdioServerTransport(); await server.connect(transport); } main().catch(console.error);