@yskmfff/tianqi-mcp-server 0.0.1
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/dist/index.js +144 -0
- package/package.json +34 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 第一部分代码:声明导入的依赖内容等
|
|
3
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
6
|
+
import fetch from "node-fetch";
|
|
7
|
+
// 第二部分:定义工具数组和工具的定义
|
|
8
|
+
const GET_TIANQI = {
|
|
9
|
+
name: "get_tianqi",
|
|
10
|
+
description: "获取指定城市的天气信息",
|
|
11
|
+
inputSchema: {
|
|
12
|
+
type: "object",
|
|
13
|
+
properties: {
|
|
14
|
+
city: {
|
|
15
|
+
type: "string",
|
|
16
|
+
description: "要查询的城市名称",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
required: ["city"],
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const GET_TRAIN_TICKET = {
|
|
23
|
+
name: "get_train_ticket",
|
|
24
|
+
description: "获取火车票信息",
|
|
25
|
+
inputSchema: {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: {
|
|
28
|
+
departure: {
|
|
29
|
+
type: "string",
|
|
30
|
+
description: "出发地",
|
|
31
|
+
},
|
|
32
|
+
arrival: {
|
|
33
|
+
type: "string",
|
|
34
|
+
description: "目的地",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
required: ["departure", "arrival"],
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const SUPPORT_TOOLS = [
|
|
41
|
+
GET_TIANQI,
|
|
42
|
+
GET_TRAIN_TICKET,
|
|
43
|
+
];
|
|
44
|
+
// 辅助函数:构建响应
|
|
45
|
+
function buildSuccessResponse(text) {
|
|
46
|
+
return {
|
|
47
|
+
content: [{ type: "text", text }],
|
|
48
|
+
isError: false
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function buildErrorResponse(text) {
|
|
52
|
+
return {
|
|
53
|
+
content: [{ type: "text", text }],
|
|
54
|
+
isError: true
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async function getTrainTicket(departure, arrival) {
|
|
58
|
+
//1.参数校验
|
|
59
|
+
if (!departure || !arrival || typeof departure !== "string" || typeof arrival !== "string") {
|
|
60
|
+
return buildErrorResponse("Invalid departure/arrival/type parameter");
|
|
61
|
+
}
|
|
62
|
+
const url = `https://api.lolimi.cn/API/hc/api?departure=${departure}&arrival=${arrival}&type=json`;
|
|
63
|
+
const response1 = await fetch(url, {
|
|
64
|
+
method: "GET",
|
|
65
|
+
});
|
|
66
|
+
if (!response1.ok) {
|
|
67
|
+
return buildErrorResponse(`Failed to fetch train ticket: ${response1.statusText}`);
|
|
68
|
+
}
|
|
69
|
+
const respBody = await response1.json();
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
if (respBody.code === 200) {
|
|
72
|
+
return buildSuccessResponse(JSON.stringify(respBody));
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
return buildErrorResponse(`Failed to fetch train ticket: ${respBody.msg}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function getTianqi(city) {
|
|
80
|
+
//1.参数校验
|
|
81
|
+
if (!city || typeof city !== "string") {
|
|
82
|
+
return buildErrorResponse("Invalid city parameter");
|
|
83
|
+
}
|
|
84
|
+
const url = `https://v.api.aa1.cn/api/api-tianqi-3/index.php?msg=${city}&type=1`;
|
|
85
|
+
const response1 = await fetch(url, {
|
|
86
|
+
method: "GET",
|
|
87
|
+
});
|
|
88
|
+
const respBody = await response1.json();
|
|
89
|
+
return buildSuccessResponse(JSON.stringify(respBody));
|
|
90
|
+
}
|
|
91
|
+
// 第三部分:服务器的初始化
|
|
92
|
+
const server = new Server({
|
|
93
|
+
name: "tianqi-mcp-server",
|
|
94
|
+
version: "0.0.1",
|
|
95
|
+
}, {
|
|
96
|
+
capabilities: {
|
|
97
|
+
tools: {},
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
// Set up request handlers
|
|
101
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
102
|
+
tools: SUPPORT_TOOLS,
|
|
103
|
+
}));
|
|
104
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
105
|
+
try {
|
|
106
|
+
switch (request.params.name) {
|
|
107
|
+
case "get_tianqi": {
|
|
108
|
+
const { city } = request.params.arguments;
|
|
109
|
+
return await getTianqi(city);
|
|
110
|
+
}
|
|
111
|
+
case "get_train_ticket": {
|
|
112
|
+
const { departure, arrival } = request.params.arguments;
|
|
113
|
+
return await getTrainTicket(departure, arrival);
|
|
114
|
+
}
|
|
115
|
+
default:
|
|
116
|
+
return {
|
|
117
|
+
content: [{
|
|
118
|
+
type: "text",
|
|
119
|
+
text: `Unknown tool: ${request.params.name}`
|
|
120
|
+
}],
|
|
121
|
+
isError: true
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
return {
|
|
127
|
+
content: [{
|
|
128
|
+
type: "text",
|
|
129
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`
|
|
130
|
+
}],
|
|
131
|
+
isError: true
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
// 第四部分,服务器启动
|
|
136
|
+
async function runServer() {
|
|
137
|
+
const transport = new StdioServerTransport(); //使用标准输入输出作为通信方式
|
|
138
|
+
await server.connect(transport); //连接服务器和传输层
|
|
139
|
+
console.error("Tianqi MCP Server running on stdio");
|
|
140
|
+
}
|
|
141
|
+
runServer().catch((error) => {
|
|
142
|
+
console.error("Fatal error running server:", error);
|
|
143
|
+
process.exit(1);
|
|
144
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yskmfff/tianqi-mcp-server",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "基于天气的MCP Server",
|
|
5
|
+
"author": "yskmfff",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "bin/index.js",
|
|
9
|
+
"bin": {
|
|
10
|
+
"tianqi-mcp-server": "dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc && shx chmod +x dist/*.js",
|
|
17
|
+
"prepare": "npm run build",
|
|
18
|
+
"watch": "tsc --watch"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@modelcontextprotocol/sdk": "1.10.2",
|
|
22
|
+
"@types/node-fetch": "^2.6.12",
|
|
23
|
+
"node-fetch": "^3.3.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^22.15.3",
|
|
27
|
+
"shx": "^0.3.4",
|
|
28
|
+
"typescript": "^5.8.3"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"registry": "https://registry.npmjs.org"
|
|
33
|
+
}
|
|
34
|
+
}
|