yhh-first-mcp 1.0.0
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.d.ts +2 -0
- package/dist/index.js +112 -0
- package/package.json +29 -0
- package/src/index.ts +134 -0
- package/tsconfig.json +17 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
3
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
5
|
+
// 创建MCP服务器实例
|
|
6
|
+
const server = new Server({
|
|
7
|
+
name: "my-first-mcp",
|
|
8
|
+
version: "1.0.0",
|
|
9
|
+
}, {
|
|
10
|
+
capabilities: {
|
|
11
|
+
tools: {},
|
|
12
|
+
resources: {}, // 添加这行
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
// 定义工具列表
|
|
16
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
17
|
+
return {
|
|
18
|
+
tools: [
|
|
19
|
+
{
|
|
20
|
+
name: "hello_world",
|
|
21
|
+
description: "一个简单的Hello World工具,返回问候语",
|
|
22
|
+
inputSchema: {
|
|
23
|
+
type: "object",
|
|
24
|
+
properties: {
|
|
25
|
+
name: {
|
|
26
|
+
type: "string",
|
|
27
|
+
description: "要问候的名字",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
required: ["name"],
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
// 在 ListToolsRequestSchema 处理器中添加
|
|
34
|
+
{
|
|
35
|
+
name: "get_current_time",
|
|
36
|
+
description: "获取当前时间",
|
|
37
|
+
inputSchema: {
|
|
38
|
+
type: "object",
|
|
39
|
+
properties: {
|
|
40
|
+
timezone: {
|
|
41
|
+
type: "string",
|
|
42
|
+
description: "时区(如 'Asia/Shanghai')",
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
// 处理工具调用
|
|
51
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
52
|
+
const { name, arguments: args } = request.params;
|
|
53
|
+
if (name === "hello_world") {
|
|
54
|
+
const userName = args?.name || "World";
|
|
55
|
+
return {
|
|
56
|
+
content: [
|
|
57
|
+
{
|
|
58
|
+
type: "text",
|
|
59
|
+
text: `Hello, ${userName}! 这是来自my-first-mcp的问候!`,
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (name === "get_current_time") {
|
|
65
|
+
const timezone = args?.timezone || "Asia/Shanghai";
|
|
66
|
+
const now = new Date().toLocaleString("zh-CN", { timeZone: timezone });
|
|
67
|
+
return {
|
|
68
|
+
content: [
|
|
69
|
+
{
|
|
70
|
+
type: "text",
|
|
71
|
+
text: `当前时间(${timezone}):${now}`,
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`未知工具: ${name}`);
|
|
77
|
+
});
|
|
78
|
+
// 定义资源列表
|
|
79
|
+
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
80
|
+
return {
|
|
81
|
+
resources: [
|
|
82
|
+
{
|
|
83
|
+
uri: "config://app-settings",
|
|
84
|
+
name: "应用配置",
|
|
85
|
+
mimeType: "application/json",
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
// 处理资源读取
|
|
91
|
+
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
92
|
+
const { uri } = request.params;
|
|
93
|
+
if (uri === "config://app-settings") {
|
|
94
|
+
return {
|
|
95
|
+
contents: [
|
|
96
|
+
{
|
|
97
|
+
uri,
|
|
98
|
+
mimeType: "application/json",
|
|
99
|
+
text: JSON.stringify({ version: "1.0.0", debug: false }, null, 2),
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
throw new Error(`未知资源: ${uri}`);
|
|
105
|
+
});
|
|
106
|
+
// 启动服务器
|
|
107
|
+
async function main() {
|
|
108
|
+
const transport = new StdioServerTransport();
|
|
109
|
+
await server.connect(transport);
|
|
110
|
+
console.error("my-first-mcp 服务器已启动"); // 日志输出到stderr
|
|
111
|
+
}
|
|
112
|
+
main().catch(console.error);
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "yhh-first-mcp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "我的第一个MCP服务器",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"yhh-first-mcp": "dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"dev": "ts-node --esm src/index.ts",
|
|
14
|
+
"start": "node dist/index.js",
|
|
15
|
+
"watch": "tsc --watch"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"description": "",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@modelcontextprotocol/sdk": "^1.29.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^26.1.0",
|
|
26
|
+
"ts-node": "^10.9.2",
|
|
27
|
+
"typescript": "^6.0.3"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
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 {
|
|
6
|
+
CallToolRequestSchema,
|
|
7
|
+
ListToolsRequestSchema,
|
|
8
|
+
ListResourcesRequestSchema,
|
|
9
|
+
ReadResourceRequestSchema,
|
|
10
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
// 创建MCP服务器实例
|
|
14
|
+
const server = new Server(
|
|
15
|
+
{
|
|
16
|
+
name: "my-first-mcp",
|
|
17
|
+
version: "1.0.0",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
capabilities: {
|
|
21
|
+
tools: {},
|
|
22
|
+
resources: {}, // 添加这行
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
// 定义工具列表
|
|
28
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
29
|
+
return {
|
|
30
|
+
tools: [
|
|
31
|
+
{
|
|
32
|
+
name: "hello_world",
|
|
33
|
+
description: "一个简单的Hello World工具,返回问候语",
|
|
34
|
+
inputSchema: {
|
|
35
|
+
type: "object",
|
|
36
|
+
properties: {
|
|
37
|
+
name: {
|
|
38
|
+
type: "string",
|
|
39
|
+
description: "要问候的名字",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
required: ["name"],
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
// 在 ListToolsRequestSchema 处理器中添加
|
|
46
|
+
{
|
|
47
|
+
name: "get_current_time",
|
|
48
|
+
description: "获取当前时间",
|
|
49
|
+
inputSchema: {
|
|
50
|
+
type: "object",
|
|
51
|
+
properties: {
|
|
52
|
+
timezone: {
|
|
53
|
+
type: "string",
|
|
54
|
+
description: "时区(如 'Asia/Shanghai')",
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// 处理工具调用
|
|
64
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
65
|
+
const { name, arguments: args } = request.params;
|
|
66
|
+
|
|
67
|
+
if (name === "hello_world") {
|
|
68
|
+
const userName = args?.name as string || "World";
|
|
69
|
+
return {
|
|
70
|
+
content: [
|
|
71
|
+
{
|
|
72
|
+
type: "text",
|
|
73
|
+
text: `Hello, ${userName}! 这是来自my-first-mcp的问候!`,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (name === "get_current_time") {
|
|
79
|
+
const timezone = args?.timezone as string || "Asia/Shanghai";
|
|
80
|
+
const now = new Date().toLocaleString("zh-CN", { timeZone: timezone });
|
|
81
|
+
return {
|
|
82
|
+
content: [
|
|
83
|
+
{
|
|
84
|
+
type: "text",
|
|
85
|
+
text: `当前时间(${timezone}):${now}`,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
throw new Error(`未知工具: ${name}`);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// 定义资源列表
|
|
95
|
+
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
96
|
+
return {
|
|
97
|
+
resources: [
|
|
98
|
+
{
|
|
99
|
+
uri: "config://app-settings",
|
|
100
|
+
name: "应用配置",
|
|
101
|
+
mimeType: "application/json",
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// 处理资源读取
|
|
108
|
+
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
109
|
+
const { uri } = request.params;
|
|
110
|
+
|
|
111
|
+
if (uri === "config://app-settings") {
|
|
112
|
+
return {
|
|
113
|
+
contents: [
|
|
114
|
+
{
|
|
115
|
+
uri,
|
|
116
|
+
mimeType: "application/json",
|
|
117
|
+
text: JSON.stringify({ version: "1.0.0", debug: false }, null, 2),
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
throw new Error(`未知资源: ${uri}`);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
// 启动服务器
|
|
128
|
+
async function main() {
|
|
129
|
+
const transport = new StdioServerTransport();
|
|
130
|
+
await server.connect(transport);
|
|
131
|
+
console.error("my-first-mcp 服务器已启动"); // 日志输出到stderr
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
main().catch(console.error);
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "Node16",
|
|
5
|
+
"moduleResolution": "Node16",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"declaration": true
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*"],
|
|
16
|
+
"exclude": ["node_modules", "dist"]
|
|
17
|
+
}
|