campus-security-mcp 1.0.3 → 1.0.5

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/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/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "campus-security-mcp",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "校园安防助手 MCP 服务器",
5
- "main": "bin/index.js",
5
+ "main": "./bin/index.js",
6
6
  "bin": {
7
- "campus-security-mcp": "bin/index.js"
7
+ "campus-security-mcp": "./bin/index.js"
8
8
  },
9
9
  "scripts": {
10
10
  "build": "tsc",
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/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);