1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| const express = require('express'); const { createMcpServer } = require('@qwen/mcp-server'); const fs = require('fs'); const path = require('path');
const students = JSON.parse(fs.readFileSync(path.join(__dirname, 'data/students.json'), 'utf8')); const grades = JSON.parse(fs.readFileSync(path.join(__dirname, 'data/grades.json'), 'utf8')); const classes = JSON.parse(fs.readFileSync(path.join(__dirname, 'data/classes.json'), 'utf8'));
const app = express(); app.use(express.json());
const mcpServer = createMcpServer({ port: 8080, tools: { getStudent: { description: "根据学生ID获取学生详细信息", parameters: { type: "object", properties: { studentId: { type: "string", description: "学生ID,如S001" } }, required: ["studentId"] }, handler: async ({ studentId }) => { const student = students.find(s => s.id === studentId); return student || { error: "学生不存在" }; } }, getTotalScore: { description: "计算指定学生的总成绩", parameters: { type: "object", properties: { studentId: { type: "string", description: "学生ID" } }, required: ["studentId"] }, handler: async ({ studentId }) => { const studentGrades = grades.filter(g => g.studentId === studentId); const total = studentGrades.reduce((sum, g) => sum + g.score, 0); const subjects = studentGrades.map(g => g.subject); return { studentId, totalScore: total, subjectCount: studentGrades.length, subjects: subjects }; } }, getClassAverage: { description: "计算指定班级的平均成绩", parameters: { type: "object", properties: { classId: { type: "string", description: "班级ID,如C101" }, subject: { type: "string", description: "科目名称,可选" } }, required: ["classId"] }, handler: async ({ classId, subject }) => { const classInfo = classes.find(c => c.id === classId); if (!classInfo) return { error: "班级不存在" }; let filteredGrades = grades.filter(g => classInfo.studentIds.includes(g.studentId) ); if (subject) { filteredGrades = filteredGrades.filter(g => g.subject === subject); } if (filteredGrades.length === 0) { return { error: "没有找到相关成绩数据" }; } const average = filteredGrades.reduce((sum, g) => sum + g.score, 0) / filteredGrades.length; return { classId: classInfo.id, className: classInfo.name, subject: subject || "所有科目", averageScore: parseFloat(average.toFixed(2)), studentCount: classInfo.studentIds.length, gradeCount: filteredGrades.length }; } }, getYearlyGrades: { description: "获取指定学生的历年成绩", parameters: { type: "object", properties: { studentId: { type: "string", description: "学生ID" }, year: { type: "string", description: "学年,如2024-2025,可选" } }, required: ["studentId"] }, handler: async ({ studentId, year }) => { let studentGrades = grades.filter(g => g.studentId === studentId); if (year) { studentGrades = studentGrades.filter(g => g.semester.startsWith(year)); } const yearlyData = {}; studentGrades.forEach(grade => { const year = grade.semester.split('-')[0]; if (!yearlyData[year]) { yearlyData[year] = []; } yearlyData[year].push({ subject: grade.subject, score: grade.score, semester: grade.semester }); }); return { studentId, yearlyGrades: yearlyData, totalYears: Object.keys(yearlyData).length }; } } } });
mcpServer.start(); console.log('MCP服务器启动在 http://localhost:8080');
|