@lee2026/hello-world-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.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js");
5
+ const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
6
+ const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
7
+ const http_client_js_1 = require("./utils/http-client.js");
8
+ const hello_js_1 = require("./tools/hello.js");
9
+ const server = new index_js_1.Server({ name: 'hello-world-mcp', version: '1.0.0' }, { capabilities: { tools: {} } });
10
+ const tools = new hello_js_1.HelloTools((0, http_client_js_1.createHttpClient)());
11
+ // 注册工具列表
12
+ server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => ({
13
+ tools: [
14
+ {
15
+ name: 'hello_world',
16
+ description: 'Hello World 接口,根据用户ID返回问候信息',
17
+ inputSchema: {
18
+ type: 'object',
19
+ properties: {
20
+ userId: { type: 'string', description: '用户ID' },
21
+ },
22
+ required: ['userId'],
23
+ },
24
+ },
25
+ ],
26
+ }));
27
+ // 注册工具调用
28
+ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (req) => {
29
+ const { name, arguments: args } = req.params;
30
+ let result;
31
+ switch (name) {
32
+ case 'hello_world':
33
+ result = await tools.helloWorld(args || {});
34
+ break;
35
+ default:
36
+ throw new types_js_1.McpError(types_js_1.ErrorCode.MethodNotFound, `未知工具: ${name}`);
37
+ }
38
+ return { content: [{ type: 'text', text: result }] };
39
+ });
40
+ // 启动服务
41
+ async function main() {
42
+ await server.connect(new stdio_js_1.StdioServerTransport());
43
+ console.error('Hello World MCP Server 已启动');
44
+ }
45
+ main().catch((e) => {
46
+ console.error('启动失败:', e);
47
+ process.exit(1);
48
+ });
@@ -0,0 +1,6 @@
1
+ import { AxiosInstance } from 'axios';
2
+ export declare class HelloTools {
3
+ private http;
4
+ constructor(http: AxiosInstance);
5
+ helloWorld(args: Record<string, unknown>): Promise<string>;
6
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HelloTools = void 0;
4
+ const validator_js_1 = require("../utils/validator.js");
5
+ class HelloTools {
6
+ http;
7
+ constructor(http) {
8
+ this.http = http;
9
+ }
10
+ async helloWorld(args) {
11
+ try {
12
+ const userId = (0, validator_js_1.validateString)(args.userId, '用户ID');
13
+ const res = await this.http.get('/api/hello', { params: { userId } });
14
+ return JSON.stringify({ success: true, data: res.data });
15
+ }
16
+ catch (e) {
17
+ return JSON.stringify({ success: false, error: e instanceof Error ? e.message : '未知错误' });
18
+ }
19
+ }
20
+ }
21
+ exports.HelloTools = HelloTools;
@@ -0,0 +1,12 @@
1
+ export interface HelloWorldRequest {
2
+ userId: string;
3
+ }
4
+ export interface ApiResponse<T = unknown> {
5
+ code: number;
6
+ message: string;
7
+ data: T;
8
+ }
9
+ export interface HelloWorldData {
10
+ greeting: string;
11
+ userId: string;
12
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import { AxiosInstance } from 'axios';
2
+ export declare function createHttpClient(): AxiosInstance;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createHttpClient = createHttpClient;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ function createHttpClient() {
9
+ const baseURL = process.env.API_BASE;
10
+ const apiKey = process.env.API_KEY;
11
+ if (!baseURL)
12
+ throw new Error('环境变量 API_BASE 未配置');
13
+ if (!apiKey)
14
+ throw new Error('环境变量 API_KEY 未配置');
15
+ return axios_1.default.create({
16
+ baseURL,
17
+ timeout: 30000,
18
+ headers: {
19
+ 'Content-Type': 'application/json',
20
+ 'X-API-Key': apiKey,
21
+ },
22
+ });
23
+ }
@@ -0,0 +1,5 @@
1
+ export declare class ValidationError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare function validateString(value: unknown, field: string): string;
5
+ export declare function validateNumber(value: unknown, field: string, min?: number, max?: number): number;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidationError = void 0;
4
+ exports.validateString = validateString;
5
+ exports.validateNumber = validateNumber;
6
+ class ValidationError extends Error {
7
+ constructor(message) {
8
+ super(message);
9
+ this.name = 'ValidationError';
10
+ }
11
+ }
12
+ exports.ValidationError = ValidationError;
13
+ function validateString(value, field) {
14
+ if (!value || typeof value !== 'string') {
15
+ throw new ValidationError(`${field} 不能为空`);
16
+ }
17
+ return value.trim();
18
+ }
19
+ function validateNumber(value, field, min, max) {
20
+ const num = typeof value === 'string' ? parseFloat(value) : value;
21
+ if (typeof num !== 'number' || isNaN(num)) {
22
+ throw new ValidationError(`${field} 必须是数字`);
23
+ }
24
+ if (min !== undefined && num < min)
25
+ throw new ValidationError(`${field} 不能小于 ${min}`);
26
+ if (max !== undefined && num > max)
27
+ throw new ValidationError(`${field} 不能大于 ${max}`);
28
+ return num;
29
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@lee2026/hello-world-mcp",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "bin": { "hello-world-mcp": "./dist/index.js" },
6
+ "scripts": {
7
+ "build": "tsc",
8
+ "start": "node dist/index.js"
9
+ },
10
+ "engines": { "node": ">=18.0.0" },
11
+ "files": ["dist/**/*", "README.md"],
12
+ "dependencies": {
13
+ "@modelcontextprotocol/sdk": "^1.0.0",
14
+ "axios": "^1.6.0"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node": "^20.0.0",
18
+ "typescript": "^5.3.0"
19
+ }
20
+ }