@becrafter/prompt-manager 0.0.15 → 0.0.18

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.
@@ -1,205 +0,0 @@
1
- import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3
- import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
4
- import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
5
- import { logger } from './logger.js';
6
- import { config } from './config.js';
7
- import { PromptManager } from './manager.js';
8
- import { handleSearchPrompts, handleGetPrompt, handleReloadPrompts } from './mcp.js';
9
-
10
- /**
11
- * MCP服务器管理类
12
- */
13
- export class MCPManager {
14
- constructor() {
15
- this.server = null;
16
- this.stdioTransport = null;
17
- this.httpTransport = null;
18
- this.promptManager = null;
19
- this.isInitialized = false;
20
- }
21
-
22
- /**
23
- * 初始化MCP服务器
24
- */
25
- async initialize() {
26
- if (this.isInitialized) {
27
- return;
28
- }
29
-
30
- try {
31
- // 初始化prompt管理器
32
- this.promptManager = new PromptManager(config.getPromptsDir());
33
- await this.promptManager.loadPrompts();
34
-
35
- // 创建MCP服务器
36
- this.server = new Server({
37
- name: 'Prompt Server MCP',
38
- version: config.serverVersion,
39
- }, {
40
- capabilities: {
41
- tools: {}
42
- }
43
- });
44
-
45
- // 注册工具处理器
46
- this.registerToolHandlers();
47
-
48
- // 启动Stdio传输
49
- this.stdioTransport = new StdioServerTransport();
50
- await this.server.connect(this.stdioTransport);
51
-
52
- this.isInitialized = true;
53
- logger.info('MCP服务器初始化完成');
54
- } catch (error) {
55
- logger.error('MCP服务器初始化失败:', error.message);
56
- throw error;
57
- }
58
- }
59
-
60
- /**
61
- * 注册工具处理器
62
- */
63
- registerToolHandlers() {
64
- // 注册工具列表处理器
65
- this.server.setRequestHandler(ListToolsRequestSchema, async () => {
66
- return {
67
- tools: [
68
- {
69
- name: 'search_prompts',
70
- description: 'For keyword search matching, retrieve or return all prompts. When a corresponding prompt word is matched, utilize the prompt ID to invoke the get_prompt tool to query the complete content of the prompt word.',
71
- inputSchema: {
72
- type: 'object',
73
- properties: {
74
- name: {
75
- type: 'string',
76
- description: 'Optional name filter for prompts'
77
- }
78
- }
79
- }
80
- },
81
- {
82
- name: 'get_prompt',
83
- description: 'Retrieve the complete content of a specific prompt by its ID, including all messages, arguments, and metadata.',
84
- inputSchema: {
85
- type: 'object',
86
- properties: {
87
- prompt_id: {
88
- type: 'string',
89
- description: 'The unique identifier of the prompt to retrieve'
90
- }
91
- },
92
- required: ['prompt_id']
93
- }
94
- },
95
- {
96
- name: 'reload_prompts',
97
- description: 'Force a reload of all preset prompts to overwrite the cache.',
98
- inputSchema: {
99
- type: 'object',
100
- properties: {}
101
- }
102
- }
103
- ]
104
- };
105
- });
106
-
107
- // 注册工具调用处理器
108
- this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
109
- const { name, arguments: args } = request.params;
110
-
111
- try {
112
- switch (name) {
113
- case 'search_prompts':
114
- return await handleSearchPrompts(args, this.promptManager);
115
- case 'get_prompt':
116
- return await handleGetPrompt(args, this.promptManager);
117
- case 'reload_prompts':
118
- // 重新加载prompts
119
- return await handleReloadPrompts(this.promptManager);
120
- default:
121
- throw new Error(`Unknown tool: ${name}`);
122
- }
123
- } catch (error) {
124
- logger.error(`执行工具 ${name} 时发生错误:`, error.message);
125
- return {
126
- content: [{ type: 'text', text: `Error: ${error.message}` }],
127
- isError: true
128
- };
129
- }
130
- });
131
- }
132
-
133
- /**
134
- * 处理HTTP请求
135
- */
136
- async handleHTTPRequest(req, res) {
137
- try {
138
- // 如果HTTP传输不存在,创建一个新的
139
- if (!this.httpTransport) {
140
- this.httpTransport = new StreamableHTTPServerTransport({
141
- sessionIdGenerator: undefined,
142
- enableJsonResponse: true
143
- });
144
-
145
- // 连接服务器到HTTP传输
146
- await this.server.connect(this.httpTransport);
147
- }
148
-
149
- // 处理请求
150
- await this.httpTransport.handleRequest(req, res, req.body);
151
- } catch (error) {
152
- logger.error('MCP HTTP请求处理失败:', error.message);
153
- if (!res.headersSent) {
154
- res.status(500).json({
155
- jsonrpc: '2.0',
156
- error: {
157
- code: -32603,
158
- message: 'Internal error'
159
- }
160
- });
161
- }
162
- }
163
- }
164
-
165
- /**
166
- * 关闭MCP服务器
167
- */
168
- async close() {
169
- try {
170
- if (this.httpTransport) {
171
- this.httpTransport.close();
172
- this.httpTransport = null;
173
- }
174
-
175
- if (this.server) {
176
- this.server.close();
177
- this.server = null;
178
- }
179
-
180
- this.isInitialized = false;
181
- logger.info('MCP服务器已关闭');
182
- } catch (error) {
183
- logger.error('关闭MCP服务器时发生错误:', error.message);
184
- }
185
- }
186
-
187
- /**
188
- * 重新加载prompts
189
- */
190
- async reloadPrompts() {
191
- if (this.promptManager) {
192
- return await this.promptManager.reloadPrompts();
193
- }
194
- }
195
-
196
- /**
197
- * 获取prompt管理器
198
- */
199
- getPromptManager() {
200
- return this.promptManager;
201
- }
202
- }
203
-
204
- // 导出单例实例
205
- export const mcpManager = new MCPManager();
File without changes