stdio-node-demo 1.0.2

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/LICENSE ADDED
@@ -0,0 +1,197 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but not
32
+ limited to compiled object code, generated documentation, and
33
+ conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including the
49
+ original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of, publicly
70
+ display, publicly perform, sublicense, and distribute the Work and
71
+ such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works that
101
+ You distribute, all copyright, patent, trademark, and attribution
102
+ notices from the Source form of the Work, excluding those notices
103
+ that do not pertain to any part of the Derivative Works; and
104
+
105
+ (d) If the Work includes a "NOTICE" text file as part of its
106
+ distribution, then any Derivative Works that You distribute must
107
+ include a readable copy of the attribution notices contained
108
+ within such NOTICE file, excluding those notices that do not
109
+ pertain to any part of the Derivative Works, in at least one of
110
+ the following places: within a NOTICE text file distributed
111
+ as part of the Derivative Works; within the Source form or
112
+ documentation, if provided along with the Derivative Works; or,
113
+ within a display generated by the Derivative Works, if and
114
+ wherever such third-party notices normally appear. The contents
115
+ of the NOTICE file are for informational purposes only and do
116
+ not modify the License. You may add Your own attribution
117
+ notices within Derivative Works that You distribute, alongside
118
+ or as an addendum to the NOTICE text from the Work, provided
119
+ that such additional attribution notices cannot be construed
120
+ as modifying the License.
121
+
122
+ You may add Your own copyright statement to Your modifications and
123
+ may provide additional or different license terms and conditions
124
+ for use, reproduction, or distribution of Your modifications, or
125
+ for any such Derivative Works as a whole, provided Your use,
126
+ reproduction, and distribution of the Work otherwise complies with
127
+ the conditions stated in this License.
128
+
129
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
130
+ any Contribution intentionally submitted for inclusion in the Work
131
+ by You to the Licensor shall be under the terms and conditions of
132
+ this License, without any additional terms or conditions.
133
+ Notwithstanding the above, nothing herein shall supersede or modify
134
+ the terms of any separate license agreement you may have executed
135
+ with Licensor regarding such Contributions.
136
+
137
+ 6. Trademarks. This License does not grant permission to use the trade
138
+ names, trademarks, service marks, or product names of the Licensor,
139
+ except as required for reasonable and customary use in describing the
140
+ origin of the Work and reproducing the content of the NOTICE file.
141
+
142
+ 7. Disclaimer of Warranty. Unless required by applicable law or
143
+ agreed to in writing, Licensor provides the Work (and each
144
+ Contributor provides its Contributions) on an "AS IS" BASIS,
145
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
146
+ implied, including, without limitation, any warranties or conditions
147
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
148
+ PARTICULAR PURPOSE. You are solely responsible for determining the
149
+ appropriateness of using or redistributing the Work and assume any
150
+ risks associated with Your exercise of permissions under this License.
151
+
152
+ 8. Limitation of Liability. In no event and under no legal theory,
153
+ whether in tort (including negligence), contract, or otherwise,
154
+ unless required by applicable law (such as deliberate and grossly
155
+ negligent acts) or agreed to in writing, shall any Contributor be
156
+ liable to You for damages, including any direct, indirect, special,
157
+ incidental, or consequential damages of any character arising as a
158
+ result of this License or out of the use or inability to use the
159
+ Work (including but not limited to damages for loss of goodwill,
160
+ work stoppage, computer failure or malfunction, or any and all
161
+ other commercial damages or losses), even if such Contributor
162
+ has been advised of the possibility of such damages.
163
+
164
+ 9. Accepting Warranty or Additional Liability. While redistributing
165
+ the Work or Derivative Works thereof, You may choose to offer,
166
+ and charge a fee for, acceptance of support, warranty, indemnity,
167
+ or other liability obligations and/or rights consistent with this
168
+ License. However, in accepting such obligations, You may act only
169
+ on Your own behalf and on Your sole responsibility, not on behalf
170
+ of any other Contributor, and only if You agree to indemnify,
171
+ defend, and hold each Contributor harmless for any liability
172
+ incurred by, or claims asserted against, such Contributor by reason
173
+ of your accepting any such warranty or additional liability.
174
+
175
+ END OF TERMS AND CONDITIONS
176
+
177
+ APPENDIX: How to apply the Apache License to your work.
178
+
179
+ To apply the Apache License to your work, attach the following
180
+ boilerplate notice, with the fields enclosed by brackets "[]" replaced
181
+ with your own identifying information. (Don't include the brackets.)
182
+ The text should be placed in a file called "LICENSE" at the root of
183
+ your project, and optionally included in the headers of source files.
184
+
185
+ Copyright 2026 Dave Johnson
186
+
187
+ Licensed under the Apache License, Version 2.0 (the "License");
188
+ you may not use this file except in compliance with the License.
189
+ You may obtain a copy of the License at
190
+
191
+ http://www.apache.org/licenses/LICENSE-2.0
192
+
193
+ Unless required by applicable law or agreed to in writing, software
194
+ distributed under the License is distributed on an "AS IS" BASIS,
195
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
196
+ See the License for the specific language governing permissions and
197
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,272 @@
1
+ # MCP TypeScript Stdio 示例项目
2
+
3
+ 这是一个可分发的 Model Context Protocol (MCP) 服务端示例,采用了 `stdio` 协议进行通讯。
4
+
5
+ ## 目录结构
6
+
7
+ 项目采用了标准推荐的 `src` 布局:
8
+ - `src/`: 源代码目录
9
+ - `index.ts`: 主入口文件
10
+ - `resources/`: MCP 资源定义
11
+ - `tools/`: MCP 工具定义
12
+ - `dist/`: 编译后的 JavaScript 输出
13
+ - `package.json`: 包含打包元数据和入口点配置
14
+
15
+ ## 如何作为用户直接运行
16
+
17
+ 如果你将此项目发布到 npm,其他用户**无需手动下载代码**,可以直接通过 `npx` 运行:
18
+
19
+ ```bash
20
+ # 如果已发布到 npm
21
+ npx stdio-node-demo
22
+ ```
23
+
24
+ 或者从 GitHub 仓库直接运行(如果配置了 GitHub Actions 或其他分发方式):
25
+
26
+ ```bash
27
+ # 替换为你的 GitHub 仓库地址
28
+ npx github:YOUR_USERNAME/mcp-hello-world-ts
29
+ ```
30
+
31
+ ## 本地开发与运行
32
+
33
+ ### 1. 安装环境
34
+
35
+ ```bash
36
+ npm install
37
+ ```
38
+
39
+ ### 2. 构建项目
40
+
41
+ 编译 TypeScript 源代码到 JavaScript:
42
+
43
+ ```bash
44
+ npm run build
45
+ ```
46
+
47
+ ### 3. 运行服务端
48
+
49
+ 项目配置了 `stdio-node-demo` 入口点,可以直接运行:
50
+
51
+ ```bash
52
+ npm start
53
+ ```
54
+
55
+ 或者直接使用 node:
56
+
57
+ ```bash
58
+ node dist/index.js
59
+ ```
60
+
61
+ ### 4. 测试验证
62
+
63
+ 使用 MCP Inspector 进行测试:
64
+
65
+ ```bash
66
+ npx @modelcontextprotocol/inspector node dist/index.js
67
+ ```
68
+
69
+ 或者运行集成测试:
70
+
71
+ ```bash
72
+ npm test
73
+ ```
74
+
75
+ ## 分发到公共仓库 (npm) 的建议
76
+
77
+ 如果你想让用户直接通过 `npx stdio-node-demo` 运行:
78
+
79
+ 1. 注册 [npm](https://www.npmjs.com/) 账号。
80
+ 2. 在项目根目录运行 `npm run build` 构建项目。
81
+ 3. 使用 `npm publish` 将构建好的包发布。
82
+
83
+ 发布后,用户的运行体验将非常顺滑:
84
+
85
+ ```bash
86
+ npx stdio-node-demo
87
+ ```
88
+
89
+ 在 Claude Desktop 中的配置示例:
90
+
91
+ ```json
92
+ {
93
+ "mcpServers": {
94
+ "stdio-node-demo": {
95
+ "command": "npx",
96
+ "args": [
97
+ "stdio-node-demo"
98
+ ],
99
+ "env": {
100
+ "GREETING_PREFIX": "你好"
101
+ }
102
+ }
103
+ }
104
+ }
105
+ ```
106
+
107
+ 或者使用本地开发版本:
108
+
109
+ ```json
110
+ {
111
+ "mcpServers": {
112
+ "stdio-node-demo": {
113
+ "command": "node",
114
+ "args": [
115
+ "/path/to/mcp-hello-world-ts/dist/index.js"
116
+ ],
117
+ "env": {
118
+ "GREETING_PREFIX": "你好"
119
+ }
120
+ }
121
+ }
122
+ }
123
+ ```
124
+
125
+ ## 环境变量配置示例
126
+
127
+ 本项目演示了两种配置环境变量的方式:
128
+
129
+ ### 1. 使用 `.env` 文件(本地开发推荐)
130
+
131
+ 在项目根目录下创建 `.env` 文件,你可以配置自定义的问候语前缀:
132
+
133
+ ```env
134
+ GREETING_PREFIX="你好"
135
+ ```
136
+
137
+ 服务端在初始化时会调用 `dotenv` 加载 `.env` 文件,你可以通过 `say_greeting` 工具来验证配置是否生效。
138
+
139
+ ### 2. 在 Claude Desktop 中直接配置
140
+
141
+ 如果你在 Claude Desktop 中使用此项目,可以在配置文件的 `env` 字段中注入环境变量:
142
+
143
+ ```json
144
+ {
145
+ "mcpServers": {
146
+ "stdio-node-demo": {
147
+ "command": "node",
148
+ "args": [
149
+ "/path/to/mcp-hello-world-ts/dist/index.js"
150
+ ],
151
+ "env": {
152
+ "GREETING_PREFIX": "尊敬的"
153
+ }
154
+ }
155
+ }
156
+ }
157
+ ```
158
+
159
+ ### 3. 可用工具演示
160
+
161
+ - `helloTool(message?)`: 回显消息或返回默认问候语。如果提供了 message 参数,则回显该消息;否则返回 "Hello, World!"。
162
+ - `add_numbers(a, b)`: 基础数值加法,将两个数字相加并返回结果。
163
+ - `say_greeting(name)`: 演示从环境变量获取前缀并拼接姓名。从环境变量 `GREETING_PREFIX` 读取前缀(默认为 "Hello"),然后与提供的姓名拼接。
164
+
165
+ ### 4. 安全建议
166
+
167
+ - **不要提交 `.env`**:敏感信息(如 API Key)应仅存在于本地 `.env` 中,本项目已将其加入 `.gitignore`。
168
+ - **配置模板**:参考 `.env.example` 创建你自己的本地配置。
169
+
170
+ ## 使用 with Claude Desktop
171
+
172
+ 你可以配置 Claude Desktop 或任何 MCP 客户端来使用此服务器作为插件/工具,通过指定启动 MCP 服务器的命令。
173
+
174
+ ### 配置示例
175
+
176
+ #### 使用 npm 包(推荐)
177
+
178
+ 如果你通过 npm 安装了包,可以在 Claude Desktop 设置中添加以下配置:
179
+
180
+ **选项 1: 使用 npx(推荐 - 适用于所有环境)**
181
+
182
+ ```json
183
+ {
184
+ "mcpServers": {
185
+ "stdio-node-demo": {
186
+ "command": "npx",
187
+ "args": [
188
+ "stdio-node-demo"
189
+ ],
190
+ "env": {
191
+ "GREETING_PREFIX": "你好"
192
+ }
193
+ }
194
+ }
195
+ }
196
+ ```
197
+
198
+ **选项 2: 直接路径(需要完整路径)**
199
+
200
+ ```json
201
+ {
202
+ "mcpServers": {
203
+ "stdio-node-demo": {
204
+ "command": "node",
205
+ "args": [
206
+ "/usr/local/lib/node_modules/stdio-node-demo/dist/index.js"
207
+ ],
208
+ "env": {
209
+ "GREETING_PREFIX": "你好"
210
+ }
211
+ }
212
+ }
213
+ }
214
+ ```
215
+
216
+ **注意:** 根据你的系统调整路径:
217
+ - **macOS/Linux (全局安装):** `/usr/local/lib/node_modules/stdio-node-demo/dist/index.js`
218
+ - **Windows (全局安装):** `%APPDATA%\\npm\\node_modules\\stdio-node-demo\\dist\\index.js`
219
+ - **本地安装:** `./node_modules/stdio-node-demo/dist/index.js`
220
+
221
+ #### 使用源代码
222
+
223
+ 如果你克隆了仓库,可以在 Claude Desktop 设置中添加以下配置:
224
+
225
+ ```json
226
+ {
227
+ "mcpServers": {
228
+ "stdio-node-demo": {
229
+ "command": "node",
230
+ "args": [
231
+ "/path/to/mcp-hello-world-ts/dist/index.js"
232
+ ],
233
+ "env": {
234
+ "GREETING_PREFIX": "你好"
235
+ }
236
+ }
237
+ }
238
+ }
239
+ ```
240
+
241
+ - 将 `/path/to/mcp-hello-world-ts/` 替换为你克隆此仓库的完整路径。
242
+ - 确保在启动 Claude Desktop 之前已构建项目(`npm run build`)。
243
+
244
+ ## 项目结构
245
+
246
+ ```
247
+ .
248
+ ├── src/
249
+ │ ├── index.ts # 主 MCP 服务器入口点
250
+ │ ├── resources/
251
+ │ │ └── helloResource.ts # MCP 资源定义
252
+ │ └── tools/
253
+ │ └── helloTool.ts # MCP 工具定义
254
+ ├── dist/ # 编译后的 JS 输出
255
+ ├── package.json
256
+ ├── tsconfig.json
257
+ ├── README.md
258
+ └── ...
259
+ ```
260
+
261
+ ## 自定义
262
+
263
+ - 要添加新工具或资源,在 `src/resources/` 或 `src/tools/` 中创建新文件,并在 `src/index.ts` 中注册它们。
264
+ - 修改 `helloResource.ts` 或 `helloTool.ts` 来更改问候语或添加其他功能。
265
+
266
+ ## License
267
+
268
+ MIT License
269
+
270
+ ## Author
271
+
272
+ - [@snoopdave](https://github.com/snoopdave)
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env node
2
+ import { config } from "dotenv";
3
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
4
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
5
+ import { registerHelloResources } from "./resources/helloResource.js";
6
+ import { registerHelloTool } from "./tools/helloTool.js";
7
+ // 加载 .env 文件(如果存在)
8
+ config();
9
+ /**
10
+ * Main entry point for the Hello World MCP Server
11
+ */
12
+ async function main() {
13
+ // Create the MCP Server
14
+ const server = new McpServer({
15
+ name: "HelloWorldServer",
16
+ version: "1.0.0",
17
+ description: "A minimal MCP server for Hello World!"
18
+ });
19
+ console.error('Registering Hello World modules...');
20
+ registerHelloResources(server);
21
+ registerHelloTool(server);
22
+ // Create the stdio transport
23
+ const transport = new StdioServerTransport();
24
+ // Connect the server to the transport
25
+ console.error('Hello World MCP Server starting...');
26
+ try {
27
+ await server.connect(transport);
28
+ console.error('Hello World MCP Server connected');
29
+ }
30
+ catch (error) {
31
+ console.error('Error connecting MCP server:', error);
32
+ process.exit(1);
33
+ }
34
+ }
35
+ // Start the server
36
+ main().catch((error) => {
37
+ console.error('Fatal error:', error);
38
+ process.exit(1);
39
+ });
40
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAC,sBAAsB,EAAC,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAC,iBAAiB,EAAC,MAAM,sBAAsB,CAAC;AAEvD,mBAAmB;AACnB,MAAM,EAAE,CAAC;AAGT;;GAEG;AACH,KAAK,UAAU,IAAI;IACjB,wBAAwB;IACxB,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;QAC3B,IAAI,EAAE,kBAAkB;QACxB,OAAO,EAAE,OAAO;QAChB,WAAW,EAAE,uCAAuC;KACrD,CAAC,CAAC;IAEH,OAAO,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAC;IAEpD,sBAAsB,CAAC,MAAM,CAAC,CAAC;IAC/B,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAE1B,6BAA6B;IAC7B,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAE7C,sCAAsC;IACtC,OAAO,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACpD,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAChC,OAAO,CAAC,KAAK,CAAC,kCAAkC,CAAC,CAAC;IACpD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;QACrD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,mBAAmB;AACnB,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC9B,OAAO,CAAC,KAAK,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IACrC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,70 @@
1
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
+ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
3
+ import path from 'path';
4
+ import { beforeAll, expect, test } from '@jest/globals';
5
+ const projectRoot = process.cwd();
6
+ // Setup and teardown
7
+ let client;
8
+ let transport;
9
+ beforeAll(async () => {
10
+ // Calculate the correct path to the compiled server
11
+ const serverPath = path.join(projectRoot, "dist", "index.js");
12
+ console.log(`Looking for server at: ${serverPath}`);
13
+ // Create the client with the stdio transport to spawn the server
14
+ transport = new StdioClientTransport({
15
+ command: "node",
16
+ args: [serverPath],
17
+ });
18
+ client = new Client({
19
+ name: "HelloWorldTestClient",
20
+ version: "1.0.0"
21
+ });
22
+ try {
23
+ await client.connect(transport);
24
+ console.log("Client connected to Hello World MCP server!");
25
+ }
26
+ catch (err) {
27
+ console.error("Error connecting client to server:", err);
28
+ throw err;
29
+ }
30
+ });
31
+ // In your test, make sure you use the right method names
32
+ test('Call hello tool with no arguments', async () => {
33
+ const response = await client.callTool({
34
+ name: "helloTool",
35
+ arguments: {}
36
+ });
37
+ expect(response.content[0].text).toBe('Hello, World!');
38
+ });
39
+ test('Call hello tool with a message', async () => {
40
+ const response = await client.callTool({
41
+ name: "helloTool",
42
+ arguments: {
43
+ message: "Hello from the test suite!"
44
+ }
45
+ });
46
+ expect(response.content[0].text).toBe('You said: Hello from the test suite!');
47
+ });
48
+ test('Navigate to available resources', async () => {
49
+ // First list available resources
50
+ const listResponse = await client.readResource({
51
+ uri: 'hello://list'
52
+ });
53
+ // Check if resources exist
54
+ expect(listResponse.resources).toBeDefined();
55
+ // Extract resource URIs from the response
56
+ const resources = listResponse.resources?.map(r => r.uri) || [];
57
+ console.log("Available resources:", resources);
58
+ // Verify resources count
59
+ expect(resources.length).toBeGreaterThan(0);
60
+ // Verify expected resources are present
61
+ expect(resources).toContain('hello://greeting');
62
+ expect(resources).toContain('hello://info');
63
+ expect(resources).toContain('hello://list');
64
+ // Verify contents exist
65
+ expect(listResponse.contents).toBeDefined();
66
+ expect(listResponse.contents.length).toBeGreaterThan(0);
67
+ // Check that the text contains information about available resources
68
+ expect(listResponse.contents[0].text).toContain('Available resources');
69
+ });
70
+ //# sourceMappingURL=integration.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"integration.test.js","sourceRoot":"","sources":["../src/integration.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,eAAe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qE;IACrE,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,qBAAqB,CAAC,CAAC;AACzE,CAAC,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ /**
3
+ * Register Hello World resources
4
+ */
5
+ export declare function registerHelloResources(server: McpServer): void;
@@ -0,0 +1,72 @@
1
+ import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ /**
3
+ * Available resources in the Hello World server
4
+ */
5
+ const HELLO_RESOURCES = [
6
+ {
7
+ uri: 'hello://greeting',
8
+ name: 'Hello World Greeting',
9
+ description: 'A simple greeting message',
10
+ mimeType: 'text/plain',
11
+ },
12
+ {
13
+ uri: 'hello://info',
14
+ name: 'Hello World Info',
15
+ description: 'Information about the Hello World server',
16
+ mimeType: 'text/plain',
17
+ },
18
+ {
19
+ uri: 'hello://list',
20
+ name: 'Resource List',
21
+ description: 'List of all available resources',
22
+ mimeType: 'text/plain',
23
+ }
24
+ ];
25
+ /**
26
+ * Register Hello World resources
27
+ */
28
+ export function registerHelloResources(server) {
29
+ console.error('Registering Hello World resources...');
30
+ // Define a list function for the list resource
31
+ const listFunction = () => ({
32
+ resources: HELLO_RESOURCES
33
+ });
34
+ // Create a properly typed undefined for the callback
35
+ const emptyList = undefined;
36
+ // Register the list resource separately for clarity
37
+ server.resource('HelloWorldList', new ResourceTemplate('hello://list', { list: listFunction }), async () => {
38
+ console.error('MCP Server: List resource handler called');
39
+ return {
40
+ resources: HELLO_RESOURCES,
41
+ contents: [{
42
+ uri: 'hello://list',
43
+ text: 'Available resources: hello://greeting, hello://info, hello://list',
44
+ mimeType: 'text/plain',
45
+ }]
46
+ };
47
+ });
48
+ // Register the greeting resource
49
+ server.resource('HelloWorldGreeting', new ResourceTemplate('hello://greeting', { list: emptyList }), async () => {
50
+ console.error('MCP Server: Greeting resource handler called');
51
+ return {
52
+ contents: [{
53
+ uri: 'hello://greeting',
54
+ text: 'Hello, World!',
55
+ mimeType: 'text/plain',
56
+ }]
57
+ };
58
+ });
59
+ // Register the info resource
60
+ server.resource('HelloWorldInfo', new ResourceTemplate('hello://info', { list: emptyList }), async () => {
61
+ console.error('MCP Server: Info resource handler called');
62
+ return {
63
+ contents: [{
64
+ uri: 'hello://info',
65
+ text: 'This is a Hello World MCP Server for demonstration purposes.',
66
+ mimeType: 'text/plain',
67
+ }]
68
+ };
69
+ });
70
+ console.error('Hello World resources registered successfully');
71
+ }
72
+ //# sourceMappingURL=helloResource.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helloResource.js","sourceRoot":"","sources":["../../src/resources/helloResource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,gBAAgB,EAA8B,MAAM,yCAAyC,CAAC;AAGvH;;GAEG;AACH,MAAM,eAAe,GAAG;IACtB;QACE,GAAG,EAAE,kBAAkB;QACvB,IAAI,EAAE,sBAAsB;QAC5B,WAAW,EAAE,2BAA2B;QACxC,QAAQ,EAAE,YAAY;KACvB;IACD;QACE,GAAG,EAAE,cAAc;QACnB,IAAI,EAAE,kBAAkB;QACxB,WAAW,EAAE,0CAA0C;QACvD,QAAQ,EAAE,YAAY;KACvB;IACD;QACE,GAAG,EAAE,cAAc;QACnB,IAAI,EAAE,eAAe;QACrB,WAAW,EAAE,iCAAiC;QAC9C,QAAQ,EAAE,YAAY;KACvB;CACF,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAiB;IACtD,OAAO,CAAC,KAAK,CAAC,sCAAsC,CAAC,CAAC;IAEtD,+CAA+C;IAC/C,MAAM,YAAY,GAA0B,GAAG,EAAE,CAAC,CAAC;QACjD,SAAS,EAAE,eAAe;KAC3B,CAAC,CAAC;IAEH,qDAAqD;IACrD,MAAM,SAAS,GAAG,SAA6C,CAAC;IAEhE,oDAAoD;IACpD,MAAM,CAAC,QAAQ,CACb,gBAAgB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
5
+ export declare function registerHelloTool(server: McpServer): void;
@@ -0,0 +1,92 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * Register the Hello World tools
4
+ */
5
+ export function registerHelloTool(server) {
6
+ console.error('Registering Hello World tools...');
7
+ // Tool to echo a message or respond with "Hello, World!"
8
+ server.tool("helloTool", {
9
+ message: z.string().optional().describe("Message to echo back. If not provided, returns a default greeting.")
10
+ }, async ({ message }) => {
11
+ try {
12
+ const responseMessage = message ? `You said: ${message}` : "Hello, World!";
13
+ return {
14
+ content: [
15
+ {
16
+ type: "text",
17
+ text: responseMessage
18
+ }
19
+ ]
20
+ };
21
+ }
22
+ catch (error) {
23
+ return {
24
+ content: [
25
+ {
26
+ type: "text",
27
+ text: `Error in hello tool: ${error instanceof Error ? error.message : String(error)}`
28
+ }
29
+ ],
30
+ isError: true
31
+ };
32
+ }
33
+ });
34
+ // Tool to add two numbers
35
+ server.tool("add_numbers", {
36
+ a: z.number().describe("第一个加数"),
37
+ b: z.number().describe("第二个加数")
38
+ }, async ({ a, b }) => {
39
+ try {
40
+ const result = a + b;
41
+ return {
42
+ content: [
43
+ {
44
+ type: "text",
45
+ text: `计算结果是: ${result}`
46
+ }
47
+ ]
48
+ };
49
+ }
50
+ catch (error) {
51
+ return {
52
+ content: [
53
+ {
54
+ type: "text",
55
+ text: `Error in add_numbers tool: ${error instanceof Error ? error.message : String(error)}`
56
+ }
57
+ ],
58
+ isError: true
59
+ };
60
+ }
61
+ });
62
+ // Tool to say greeting with environment variable support
63
+ server.tool("say_greeting", {
64
+ name: z.string().describe("用户的姓名")
65
+ }, async ({ name }) => {
66
+ try {
67
+ // 从环境变量中读取前缀,如果没有则默认为 "Hello"
68
+ const prefix = process.env.GREETING_PREFIX || "Hello";
69
+ return {
70
+ content: [
71
+ {
72
+ type: "text",
73
+ text: `${prefix}, ${name}!`
74
+ }
75
+ ]
76
+ };
77
+ }
78
+ catch (error) {
79
+ return {
80
+ content: [
81
+ {
82
+ type: "text",
83
+ text: `Error in say_greeting tool: ${error instanceof Error ? error.message : String(error)}`
84
+ }
85
+ ],
86
+ isError: true
87
+ };
88
+ }
89
+ });
90
+ console.error('Hello World tools registered successfully');
91
+ }
92
+ //# sourceMappingURL=helloTool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helloTool.js","sourceRoot":"","sources":["../../src/tools/helloTool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAiB;IACjD,OAAO,CAAC,KAAK,CAAC,kCAAkC,CAAC,CAAC;IAElD,yDAAyD;IACzD,MAAM,CAAC,IAAI,CACT,WAAW,EACX;QACE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,oEAAoE,CAAC;KAC9G,EACD,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;QACpB,IAAI,CAAC;YACH,MAAM,eAAe,GAAG,OAAO,CAAC,CAAC,CAAC,aAAa,OAAO,EAAE,CAAC,CAAC,CAAC,eAAe,CAAC;YAE3E,OAAO;gBACL,OAAO,EAAE;oBACP;wBACE,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,eAAe;qBACtB;iBACF;aACF,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO;gBACL,OAAO,EAAE;oBACP;wBACE,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,wBAAwB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;qBACvF;iBACF;gBACD,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC,CACF,CAAC;IAEF,0BAA0B;IAC1B,MAAM,CAAC,IAAI,CACT,aAAa,EACb;QACE,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ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,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;qBAC9F;iBACF;gBACD,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC,CACF,CAAC;IAEF,OAAO,CAAC,KAAK,CAAC,2CAA2C,CAAC,CAAC;AAC7D,CAAC"}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "stdio-node-demo",
3
+ "version": "1.0.2",
4
+ "description": "Model Context Protocol server demo using stdio transport (Node.js)",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "stdio-node-demo": "dist/index.js"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "README.md",
14
+ "LICENSE"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsc",
18
+ "start": "node dist/index.js",
19
+ "test": "jest",
20
+ "clean": "rm -rf dist/*",
21
+ "prepublishOnly": "npm run build"
22
+ },
23
+ "keywords": [
24
+ "mcp",
25
+ "stdio",
26
+ "node",
27
+ "demo",
28
+ "llm",
29
+ "openctx",
30
+ "model-context-protocol",
31
+ "claude",
32
+ "mcp-server"
33
+ ],
34
+ "author": "Dave Johnson",
35
+ "license": "Apache-2.0",
36
+ "dependencies": {
37
+ "@modelcontextprotocol/sdk": "^1.9.0",
38
+ "dotenv": "^16.4.5",
39
+ "zod": "^3.24.3"
40
+ },
41
+ "devDependencies": {
42
+ "@types/jest": "^29.5.11",
43
+ "@types/node": "^20.10.5",
44
+ "@typescript-eslint/eslint-plugin": "^6.15.0",
45
+ "@typescript-eslint/parser": "^6.15.0",
46
+ "eslint": "^8.56.0",
47
+ "jest": "^29.7.0",
48
+ "prettier": "^3.1.1",
49
+ "ts-jest": "^29.3.4",
50
+ "ts-node": "^10.9.2",
51
+ "tsx": "^4.19.4",
52
+ "typescript": "^5.3.3"
53
+ }
54
+ }