campus-security-mcp 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -39,10 +39,11 @@ npx campus-security-mcp
39
39
 
40
40
  服务器启动后,会监听标准输入,处理 JSON 格式的请求,并返回响应。
41
41
 
42
- #### 示例请求
42
+ #### 示例请求与响应
43
43
 
44
44
  1. **区域查询**
45
45
 
46
+ **请求**:
46
47
  ```json
47
48
  {
48
49
  "id": "1",
@@ -54,8 +55,20 @@ npx campus-security-mcp
54
55
  }
55
56
  ```
56
57
 
58
+ **响应**:
59
+ ```json
60
+ {
61
+ "result": "学校区域列表:东、西、南、北",
62
+ "structuredContent": {
63
+ "areas": ["东", "西", "南", "北"]
64
+ },
65
+ "requestId": "1"
66
+ }
67
+ ```
68
+
57
69
  2. **街道查询**
58
70
 
71
+ **请求**:
59
72
  ```json
60
73
  {
61
74
  "id": "2",
@@ -67,8 +80,20 @@ npx campus-security-mcp
67
80
  }
68
81
  ```
69
82
 
83
+ **响应**:
84
+ ```json
85
+ {
86
+ "result": "学校街道列表:春晖路、洋房路、经一路、经二路、经三路、经四路、八里庄街道、紫竹院街道、北新桥街道",
87
+ "structuredContent": {
88
+ "streets": ["春晖路", "洋房路", "经一路", "经二路", "经三路", "经四路", "八里庄街道", "紫竹院街道", "北新桥街道"]
89
+ },
90
+ "requestId": "2"
91
+ }
92
+ ```
93
+
70
94
  3. **警告数量查询**
71
95
 
96
+ **请求**:
72
97
  ```json
73
98
  {
74
99
  "id": "3",
@@ -85,6 +110,27 @@ npx campus-security-mcp
85
110
  }
86
111
  ```
87
112
 
113
+ **响应**:
114
+ ```json
115
+ {
116
+ "result": "警告查询结果:\n总警告数:14\n\n按日期统计:\n2026-04-07: 3个\n2026-04-08: 1个\n2026-04-09: 5个\n2026-04-10: 1个\n2026-04-11: 1个\n2026-04-12: 3个\n\n按区域统计:\n东区: 14个\n\n按街道统计:\n春晖路: 14个\n",
117
+ "structuredContent": {
118
+ "totalCount": 14,
119
+ "dailyData": [
120
+ {"date": "2026-04-07", "count": 3},
121
+ {"date": "2026-04-08", "count": 1},
122
+ {"date": "2026-04-09", "count": 5},
123
+ {"date": "2026-04-10", "count": 1},
124
+ {"date": "2026-04-11", "count": 1},
125
+ {"date": "2026-04-12", "count": 3}
126
+ ],
127
+ "areaBreakdown": [{"area": "东", "count": 14}],
128
+ "streetBreakdown": [{"street": "春晖路", "count": 14}]
129
+ },
130
+ "requestId": "3"
131
+ }
132
+ ```
133
+
88
134
  ## 项目结构
89
135
 
90
136
  ```
package/bin/index.js CHANGED
@@ -25,21 +25,38 @@ server.start().then(() => {
25
25
  });
26
26
 
27
27
  // 处理输入请求
28
- rl.on('line', (line) => {
28
+ rl.on('line', async (line) => {
29
29
  try {
30
30
  const request = JSON.parse(line);
31
31
  console.log('收到请求:', request);
32
32
 
33
- // 简单响应示例 - 实际实现需要根据 MCP 协议处理
33
+ // 检查请求格式
34
+ if (!request.toolcall) {
35
+ throw new Error('Invalid request format: missing toolcall');
36
+ }
37
+
38
+ const { name: toolName, params } = request.toolcall;
39
+
40
+ if (!toolName) {
41
+ throw new Error('Invalid request format: missing tool name');
42
+ }
43
+
44
+ // 调用工具
45
+ const toolResult = await server.callTool(toolName, params || {});
46
+
47
+ // 构建响应
34
48
  const response = {
35
- result: 'Hello, MCP from npm!',
49
+ result: toolResult.content,
50
+ structuredContent: toolResult.structuredContent,
36
51
  requestId: request.id
37
52
  };
38
53
 
39
54
  console.log(JSON.stringify(response));
40
55
  } catch (error) {
56
+ const errorMessage = error instanceof Error ? error.message : String(error);
41
57
  const response = {
42
- error: error.toString()
58
+ error: errorMessage,
59
+ requestId: request ? request.id : undefined
43
60
  };
44
61
  console.log(JSON.stringify(response));
45
62
  }
package/dist/mcp/index.js CHANGED
@@ -36,4 +36,21 @@ MCP.Server = class {
36
36
  getTools() {
37
37
  return this.tools;
38
38
  }
39
+ // 调用工具
40
+ async callTool(toolName, params) {
41
+ // 查找工具
42
+ const tool = this.tools.find(t => t.name === toolName);
43
+ if (!tool) {
44
+ throw new Error(`Tool not found: ${toolName}`);
45
+ }
46
+ try {
47
+ // 执行工具
48
+ const result = await tool.handler(params);
49
+ return result;
50
+ }
51
+ catch (error) {
52
+ const errorMessage = error instanceof Error ? error.message : String(error);
53
+ throw new Error(`Tool execution failed: ${errorMessage}`);
54
+ }
55
+ }
39
56
  };
package/dist/server.js CHANGED
@@ -5,6 +5,6 @@ const mcp_1 = require("./mcp");
5
5
  const server = new mcp_1.MCP.Server({
6
6
  name: 'campus-security-assistant',
7
7
  description: '校园安防助手 MCP 服务器',
8
- version: '1.0.0'
8
+ version: '1.0.6'
9
9
  });
10
10
  exports.default = server;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "campus-security-mcp",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "校园安防助手 MCP 服务器",
5
5
  "main": "./bin/index.js",
6
6
  "bin": {
package/src/mcp/index.ts CHANGED
@@ -68,5 +68,26 @@ export class MCP {
68
68
  getTools(): ToolDefinition<any, any>[] {
69
69
  return this.tools;
70
70
  }
71
+
72
+ // 调用工具
73
+ async callTool(toolName: string, params: any): Promise<{
74
+ content: string;
75
+ structuredContent: any;
76
+ }> {
77
+ // 查找工具
78
+ const tool = this.tools.find(t => t.name === toolName);
79
+ if (!tool) {
80
+ throw new Error(`Tool not found: ${toolName}`);
81
+ }
82
+
83
+ try {
84
+ // 执行工具
85
+ const result = await tool.handler(params);
86
+ return result;
87
+ } catch (error) {
88
+ const errorMessage = error instanceof Error ? error.message : String(error);
89
+ throw new Error(`Tool execution failed: ${errorMessage}`);
90
+ }
91
+ }
71
92
  };
72
93
  }
package/src/server.ts CHANGED
@@ -4,7 +4,7 @@ import { MCP } from './mcp';
4
4
  const server = new MCP.Server({
5
5
  name: 'campus-security-assistant',
6
6
  description: '校园安防助手 MCP 服务器',
7
- version: '1.0.0'
7
+ version: '1.0.6'
8
8
  });
9
9
 
10
10
  export default server;
package/test-tools.js ADDED
@@ -0,0 +1,81 @@
1
+ // test-tools.js - 测试 MCP 服务器工具调用
2
+ const { spawn } = require('child_process');
3
+
4
+ // 启动 MCP 服务器
5
+ const server = spawn('node', ['bin/index.js']);
6
+
7
+ // 存储服务器输出
8
+ let serverOutput = '';
9
+
10
+ // 监听服务器输出
11
+ server.stdout.on('data', (data) => {
12
+ const output = data.toString();
13
+ serverOutput += output;
14
+ console.log('服务器输出:', output);
15
+ });
16
+
17
+ // 监听服务器错误
18
+ server.stderr.on('data', (data) => {
19
+ console.error('服务器错误:', data.toString());
20
+ });
21
+
22
+ // 监听服务器退出
23
+ server.on('close', (code) => {
24
+ console.log(`服务器退出,代码: ${code}`);
25
+ });
26
+
27
+ // 等待服务器启动
28
+ setTimeout(() => {
29
+ console.log('\n=== 开始测试工具调用 ===\n');
30
+
31
+ // 测试 1: 区域查询
32
+ console.log('测试 1: 区域查询');
33
+ const areaRequest = {
34
+ id: '1',
35
+ toolcall: {
36
+ thought: '查询学校区域',
37
+ name: 'campus_security_get_areas',
38
+ params: {}
39
+ }
40
+ };
41
+ server.stdin.write(JSON.stringify(areaRequest) + '\n');
42
+
43
+ // 测试 2: 街道查询
44
+ setTimeout(() => {
45
+ console.log('\n测试 2: 街道查询');
46
+ const streetRequest = {
47
+ id: '2',
48
+ toolcall: {
49
+ thought: '查询学校街道',
50
+ name: 'campus_security_get_streets',
51
+ params: {}
52
+ }
53
+ };
54
+ server.stdin.write(JSON.stringify(streetRequest) + '\n');
55
+
56
+ // 测试 3: 警告数量查询
57
+ setTimeout(() => {
58
+ console.log('\n测试 3: 警告数量查询');
59
+ const warningRequest = {
60
+ id: '3',
61
+ toolcall: {
62
+ thought: '查询警告数量',
63
+ name: 'campus_security_get_warnings',
64
+ params: {
65
+ startDate: '2026-04-06',
66
+ endDate: '2026-04-12',
67
+ area: '东',
68
+ street: '春晖路'
69
+ }
70
+ }
71
+ };
72
+ server.stdin.write(JSON.stringify(warningRequest) + '\n');
73
+
74
+ // 等待测试完成后关闭服务器
75
+ setTimeout(() => {
76
+ console.log('\n=== 测试完成 ===\n');
77
+ server.stdin.end();
78
+ }, 2000);
79
+ }, 2000);
80
+ }, 2000);
81
+ }, 3000);