@pero-mcp/core 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pero Games
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,41 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { Tool } from '@modelcontextprotocol/sdk/types.js';
3
+ /**
4
+ * 工具处理函数类型
5
+ */
6
+ export type ToolHandler = (args: Record<string, unknown>) => Promise<unknown>;
7
+ /**
8
+ * 基础 MCP Server 类
9
+ * 提供工具注册、stdio 传输等通用功能
10
+ */
11
+ export declare abstract class BaseMCPServer {
12
+ protected name: string;
13
+ protected version: string;
14
+ protected server: Server;
15
+ protected tools: Map<string, {
16
+ definition: Tool;
17
+ handler: ToolHandler;
18
+ }>;
19
+ constructor(name: string, version: string);
20
+ /**
21
+ * 注册工具
22
+ */
23
+ protected registerTool(definition: Tool, handler: ToolHandler): void;
24
+ /**
25
+ * 设置请求处理器
26
+ */
27
+ private setupHandlers;
28
+ /**
29
+ * 初始化服务器(子类实现,注册具体工具)
30
+ */
31
+ protected abstract initialize(): Promise<void>;
32
+ /**
33
+ * 启动 stdio 模式服务器
34
+ */
35
+ runStdio(): Promise<void>;
36
+ /**
37
+ * 获取 Server 实例(用于 HTTP 模式)
38
+ */
39
+ getServer(): Server;
40
+ }
41
+ //# sourceMappingURL=base-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-server.d.ts","sourceRoot":"","sources":["../src/base-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAEnE,OAAO,EAGL,IAAI,EACL,MAAM,oCAAoC,CAAC;AAG5C;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAE9E;;;GAGG;AACH,8BAAsB,aAAa;IAK/B,SAAS,CAAC,IAAI,EAAE,MAAM;IACtB,SAAS,CAAC,OAAO,EAAE,MAAM;IAL3B,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE;QAAE,UAAU,EAAE,IAAI,CAAC;QAAC,OAAO,EAAE,WAAW,CAAA;KAAE,CAAC,CAAa;gBAGzE,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM;IAiB3B;;OAEG;IACH,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,GAAG,IAAI;IAIpE;;OAEG;IACH,OAAO,CAAC,aAAa;IAiCrB;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAE9C;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAS/B;;OAEG;IACH,SAAS,IAAI,MAAM;CAGpB"}
@@ -0,0 +1,81 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3
+ import { CallToolRequestSchema, ListToolsRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
4
+ import { MCPError } from '@pero-mcp/shared';
5
+ /**
6
+ * 基础 MCP Server 类
7
+ * 提供工具注册、stdio 传输等通用功能
8
+ */
9
+ export class BaseMCPServer {
10
+ name;
11
+ version;
12
+ server;
13
+ tools = new Map();
14
+ constructor(name, version) {
15
+ this.name = name;
16
+ this.version = version;
17
+ this.server = new Server({
18
+ name: this.name,
19
+ version: this.version,
20
+ }, {
21
+ capabilities: {
22
+ tools: {},
23
+ },
24
+ });
25
+ this.setupHandlers();
26
+ }
27
+ /**
28
+ * 注册工具
29
+ */
30
+ registerTool(definition, handler) {
31
+ this.tools.set(definition.name, { definition, handler });
32
+ }
33
+ /**
34
+ * 设置请求处理器
35
+ */
36
+ setupHandlers() {
37
+ // 列出所有工具
38
+ this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
39
+ tools: Array.from(this.tools.values()).map((t) => t.definition),
40
+ }));
41
+ // 调用工具
42
+ this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
43
+ const toolName = request.params.name;
44
+ const tool = this.tools.get(toolName);
45
+ if (!tool) {
46
+ throw new MCPError(`Unknown tool: ${toolName}`, 'UNKNOWN_TOOL', 404);
47
+ }
48
+ try {
49
+ const result = await tool.handler(request.params.arguments || {});
50
+ return {
51
+ content: [
52
+ {
53
+ type: 'text',
54
+ text: JSON.stringify(result, null, 2),
55
+ },
56
+ ],
57
+ };
58
+ }
59
+ catch (error) {
60
+ const message = error instanceof Error ? error.message : String(error);
61
+ throw new MCPError(`Tool execution failed: ${message}`, 'TOOL_EXECUTION_ERROR', 500);
62
+ }
63
+ });
64
+ }
65
+ /**
66
+ * 启动 stdio 模式服务器
67
+ */
68
+ async runStdio() {
69
+ await this.initialize();
70
+ const transport = new StdioServerTransport();
71
+ await this.server.connect(transport);
72
+ console.error(`${this.name} v${this.version} running on stdio`);
73
+ }
74
+ /**
75
+ * 获取 Server 实例(用于 HTTP 模式)
76
+ */
77
+ getServer() {
78
+ return this.server;
79
+ }
80
+ }
81
+ //# sourceMappingURL=base-server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-server.js","sourceRoot":"","sources":["../src/base-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EACL,qBAAqB,EACrB,sBAAsB,GAEvB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAO5C;;;GAGG;AACH,MAAM,OAAgB,aAAa;IAKrB;IACA;IALF,MAAM,CAAS;IACf,KAAK,GAA4D,IAAI,GAAG,EAAE,CAAC;IAErF,YACY,IAAY,EACZ,OAAe;QADf,SAAI,GAAJ,IAAI,CAAQ;QACZ,YAAO,GAAP,OAAO,CAAQ;QAEzB,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CACtB;YACE,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,EACD;YACE,YAAY,EAAE;gBACZ,KAAK,EAAE,EAAE;aACV;SACF,CACF,CAAC;QAEF,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC;IAED;;OAEG;IACO,YAAY,CAAC,UAAgB,EAAE,OAAoB;QAC3D,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3D,CAAC;IAED;;OAEG;IACK,aAAa;Q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,CAAC,CAAC;IACL,CAAC;IAOD;;OAEG;IACH,KAAK,CAAC,QAAQ;QACZ,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;QAExB,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC7C,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAErC,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,OAAO,mBAAmB,CAAC,CAAC;IAClE,CAAC;IAED;;OAEG;IACH,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF"}
@@ -0,0 +1,2 @@
1
+ export { BaseMCPServer, type ToolHandler } from './base-server.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { BaseMCPServer } from './base-server.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAoB,MAAM,kBAAkB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@pero-mcp/core",
3
+ "version": "0.1.0",
4
+ "description": "Base MCP server implementation with stdio/http transport",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "dependencies": {
15
+ "@modelcontextprotocol/sdk": "^1.0.4",
16
+ "@pero-mcp/shared": "0.1.0"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^5.7.2"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsc",
26
+ "dev": "tsc --watch",
27
+ "clean": "rm -rf dist *.tsbuildinfo",
28
+ "lint": "tsc --noEmit"
29
+ }
30
+ }