imean-service-engine 1.3.0 → 1.4.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/mod.cjs CHANGED
@@ -85,7 +85,8 @@ function Action(options) {
85
85
  printError: options.printError ?? false,
86
86
  cache: options.cache,
87
87
  ttl: options.ttl,
88
- stream: options.stream
88
+ stream: options.stream,
89
+ mcp: options.mcp
89
90
  };
90
91
  prototype[ACTION_METADATA] = existingMetadata;
91
92
  });
@@ -121,6 +122,8 @@ var ScheduleMode = /* @__PURE__ */ ((ScheduleMode2) => {
121
122
  ScheduleMode2["FIXED_DELAY"] = "FIXED_DELAY";
122
123
  return ScheduleMode2;
123
124
  })(ScheduleMode || {});
125
+ var Plugin = class {
126
+ };
124
127
  var logger = winston__default.default.createLogger({
125
128
  level: "info",
126
129
  transports: [
@@ -783,7 +786,16 @@ var Microservice = class {
783
786
  this.options = {
784
787
  prefix: "/api",
785
788
  name: "microservice",
789
+ version: "0.0.1",
790
+ env: "default",
791
+ printError: false,
792
+ disableCache: false,
793
+ generateClient: false,
794
+ etcd: false,
795
+ events: {},
796
+ websocket: { enabled: false },
786
797
  cacheAdapter: new MemoryCacheAdapter(),
798
+ plugins: [],
787
799
  ...options
788
800
  };
789
801
  this.cache = this.options.cacheAdapter;
@@ -802,6 +814,7 @@ var Microservice = class {
802
814
  this.initStatsEventManager();
803
815
  }
804
816
  await this.registerService(true);
817
+ await this.initPlugins();
805
818
  }
806
819
  async initModules() {
807
820
  for (const ModuleClass of this.options.modules) {
@@ -826,7 +839,7 @@ var Microservice = class {
826
839
  );
827
840
  this.actionHandlers.set(`${moduleName}.${actionName}`, handler);
828
841
  logger_default.info(
829
- `[ \u6CE8\u518C\u52A8\u4F5C ] ${moduleName}.${actionName} ${actionMetadata.description}`
842
+ `[ \u6CE8\u518C\u52A8\u4F5C ] ${moduleName}.${actionName} ${actionMetadata.description} ${actionMetadata.mcp ? "MCP:" + actionMetadata.mcp?.type : ""}`
830
843
  );
831
844
  }
832
845
  const schedules = getScheduleMetadata(ModuleClass.prototype);
@@ -1189,32 +1202,16 @@ Received SIGINT signal`);
1189
1202
  return ctx.json({ success: false, error: error.message });
1190
1203
  }
1191
1204
  };
1192
- async init() {
1193
- await this.waitingInitialization;
1194
- }
1195
- /**
1196
- * 获取所有注册的服务
1197
- */
1198
- async getServices() {
1199
- if (!this.etcdClient) {
1200
- throw new Error("etcd is not configured");
1205
+ async initPlugins() {
1206
+ for (const plugin of this.options.plugins) {
1207
+ logger_default.info(`[ \u521D\u59CB\u5316\u63D2\u4EF6 ] ${plugin.constructor.name}`);
1208
+ await plugin.initialize(this).catch((e) => {
1209
+ logger_default.error(`[ \u521D\u59CB\u5316\u63D2\u4EF6\u5931\u8D25 ] ${plugin.constructor.name}: ${e}`);
1210
+ });
1201
1211
  }
1202
- const namespace = this.options.etcd?.namespace ? `${this.options.etcd.namespace}/` : "";
1203
- const servicesKey = `${namespace}services`;
1204
- const services = await this.etcdClient.getAll().prefix(servicesKey);
1205
- return Object.values(services).map((value) => JSON.parse(value));
1206
1212
  }
1207
- /**
1208
- * 获取指定服务的实例
1209
- */
1210
- async getServiceInstances(serviceName) {
1211
- if (!this.etcdClient) {
1212
- throw new Error("etcd is not configured");
1213
- }
1214
- const namespace = this.options.etcd?.namespace ? `${this.options.etcd.namespace}/` : "";
1215
- const serviceKey = `${namespace}services/${serviceName}`;
1216
- const instances = await this.etcdClient.getAll().prefix(serviceKey);
1217
- return Object.values(instances).map((value) => JSON.parse(value));
1213
+ async init() {
1214
+ await this.waitingInitialization;
1218
1215
  }
1219
1216
  };
1220
1217
 
@@ -1249,6 +1246,7 @@ exports.CacheAdapter = CacheAdapter;
1249
1246
  exports.MemoryCacheAdapter = MemoryCacheAdapter;
1250
1247
  exports.Microservice = Microservice;
1251
1248
  exports.Module = Module;
1249
+ exports.Plugin = Plugin;
1252
1250
  exports.RedisCacheAdapter = RedisCacheAdapter;
1253
1251
  exports.Schedule = Schedule;
1254
1252
  exports.ScheduleMode = ScheduleMode;
package/dist/mod.d.cts CHANGED
@@ -25,6 +25,9 @@ declare class MemoryCacheAdapter extends CacheAdapter {
25
25
  }
26
26
 
27
27
  type CacheFn = (...args: any[]) => any;
28
+ type McpOptions = {
29
+ type: "tool";
30
+ };
28
31
  interface ActionOptions {
29
32
  params?: z.ZodType<any>[];
30
33
  returns?: z.ZodType<any>;
@@ -34,6 +37,7 @@ interface ActionOptions {
34
37
  cache?: boolean | CacheFn;
35
38
  ttl?: number;
36
39
  stream?: boolean;
40
+ mcp?: McpOptions;
37
41
  }
38
42
  interface ActionMetadata extends ActionOptions {
39
43
  name: string;
@@ -139,7 +143,7 @@ interface MicroserviceOptions {
139
143
  disableCache?: boolean;
140
144
  modules: (new () => any)[];
141
145
  generateClient?: URL | boolean;
142
- etcd?: EtcdConfig;
146
+ etcd?: EtcdConfig | false;
143
147
  events?: StatisticsEventOptions & {
144
148
  onStats?: (event: StatisticsEvent) => void | Promise<void>;
145
149
  onRegister?: (serviceInfo: ServiceInfo) => void | Promise<void>;
@@ -150,6 +154,7 @@ interface MicroserviceOptions {
150
154
  timeout?: number;
151
155
  };
152
156
  cacheAdapter?: CacheAdapter;
157
+ plugins?: Plugin[];
153
158
  }
154
159
  interface ModuleInfo extends ModuleOptions {
155
160
  actions: Record<string, ActionMetadata>;
@@ -160,6 +165,9 @@ interface StreamResponse<T = any> {
160
165
  done: boolean;
161
166
  error?: string;
162
167
  }
168
+ declare abstract class Plugin {
169
+ abstract initialize(engine: Microservice): Promise<void>;
170
+ }
163
171
 
164
172
  declare class ActionHandler {
165
173
  private moduleInstance;
@@ -195,7 +203,7 @@ declare class Microservice {
195
203
  private actionHandlers;
196
204
  modules: Map<string, ModuleInfo>;
197
205
  readonly fetch: typeof fetch;
198
- options: MicroserviceOptions;
206
+ options: Required<MicroserviceOptions>;
199
207
  cache: CacheAdapter;
200
208
  serviceId: string;
201
209
  constructor(options: MicroserviceOptions);
@@ -224,7 +232,7 @@ declare class Microservice {
224
232
  /**
225
233
  * 获取所有模块的元数据
226
234
  */
227
- private getModules;
235
+ getModules(withTypes?: boolean): Record<string, ModuleInfo>;
228
236
  /**
229
237
  * 停止服务
230
238
  */
@@ -241,15 +249,8 @@ declare class Microservice {
241
249
  private updateStats;
242
250
  getActionHandler(moduleName: string, actionName: string): ActionHandler;
243
251
  private handleRequest;
252
+ private initPlugins;
244
253
  init(): Promise<void>;
245
- /**
246
- * 获取所有注册的服务
247
- */
248
- getServices(): Promise<ServiceInfo[]>;
249
- /**
250
- * 获取指定服务的实例
251
- */
252
- getServiceInstances(serviceName: string): Promise<ServiceInfo[]>;
253
254
  }
254
255
 
255
256
  interface PreStartChecker {
@@ -278,4 +279,4 @@ declare function Schedule(options: ScheduleOptions): Function;
278
279
 
279
280
  declare const logger: winston.Logger;
280
281
 
281
- export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, CacheAdapter, type CacheFn, type EtcdConfig, type EventServiceInfo, MemoryCacheAdapter, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, RedisCacheAdapter, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
282
+ export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, CacheAdapter, type CacheFn, type EtcdConfig, type EventServiceInfo, type McpOptions, MemoryCacheAdapter, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, Plugin, type PreStartChecker, RedisCacheAdapter, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
package/dist/mod.d.ts CHANGED
@@ -25,6 +25,9 @@ declare class MemoryCacheAdapter extends CacheAdapter {
25
25
  }
26
26
 
27
27
  type CacheFn = (...args: any[]) => any;
28
+ type McpOptions = {
29
+ type: "tool";
30
+ };
28
31
  interface ActionOptions {
29
32
  params?: z.ZodType<any>[];
30
33
  returns?: z.ZodType<any>;
@@ -34,6 +37,7 @@ interface ActionOptions {
34
37
  cache?: boolean | CacheFn;
35
38
  ttl?: number;
36
39
  stream?: boolean;
40
+ mcp?: McpOptions;
37
41
  }
38
42
  interface ActionMetadata extends ActionOptions {
39
43
  name: string;
@@ -139,7 +143,7 @@ interface MicroserviceOptions {
139
143
  disableCache?: boolean;
140
144
  modules: (new () => any)[];
141
145
  generateClient?: URL | boolean;
142
- etcd?: EtcdConfig;
146
+ etcd?: EtcdConfig | false;
143
147
  events?: StatisticsEventOptions & {
144
148
  onStats?: (event: StatisticsEvent) => void | Promise<void>;
145
149
  onRegister?: (serviceInfo: ServiceInfo) => void | Promise<void>;
@@ -150,6 +154,7 @@ interface MicroserviceOptions {
150
154
  timeout?: number;
151
155
  };
152
156
  cacheAdapter?: CacheAdapter;
157
+ plugins?: Plugin[];
153
158
  }
154
159
  interface ModuleInfo extends ModuleOptions {
155
160
  actions: Record<string, ActionMetadata>;
@@ -160,6 +165,9 @@ interface StreamResponse<T = any> {
160
165
  done: boolean;
161
166
  error?: string;
162
167
  }
168
+ declare abstract class Plugin {
169
+ abstract initialize(engine: Microservice): Promise<void>;
170
+ }
163
171
 
164
172
  declare class ActionHandler {
165
173
  private moduleInstance;
@@ -195,7 +203,7 @@ declare class Microservice {
195
203
  private actionHandlers;
196
204
  modules: Map<string, ModuleInfo>;
197
205
  readonly fetch: typeof fetch;
198
- options: MicroserviceOptions;
206
+ options: Required<MicroserviceOptions>;
199
207
  cache: CacheAdapter;
200
208
  serviceId: string;
201
209
  constructor(options: MicroserviceOptions);
@@ -224,7 +232,7 @@ declare class Microservice {
224
232
  /**
225
233
  * 获取所有模块的元数据
226
234
  */
227
- private getModules;
235
+ getModules(withTypes?: boolean): Record<string, ModuleInfo>;
228
236
  /**
229
237
  * 停止服务
230
238
  */
@@ -241,15 +249,8 @@ declare class Microservice {
241
249
  private updateStats;
242
250
  getActionHandler(moduleName: string, actionName: string): ActionHandler;
243
251
  private handleRequest;
252
+ private initPlugins;
244
253
  init(): Promise<void>;
245
- /**
246
- * 获取所有注册的服务
247
- */
248
- getServices(): Promise<ServiceInfo[]>;
249
- /**
250
- * 获取指定服务的实例
251
- */
252
- getServiceInstances(serviceName: string): Promise<ServiceInfo[]>;
253
254
  }
254
255
 
255
256
  interface PreStartChecker {
@@ -278,4 +279,4 @@ declare function Schedule(options: ScheduleOptions): Function;
278
279
 
279
280
  declare const logger: winston.Logger;
280
281
 
281
- export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, CacheAdapter, type CacheFn, type EtcdConfig, type EventServiceInfo, MemoryCacheAdapter, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, RedisCacheAdapter, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
282
+ export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, CacheAdapter, type CacheFn, type EtcdConfig, type EventServiceInfo, type McpOptions, MemoryCacheAdapter, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, Plugin, type PreStartChecker, RedisCacheAdapter, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
package/dist/mod.js CHANGED
@@ -76,7 +76,8 @@ function Action(options) {
76
76
  printError: options.printError ?? false,
77
77
  cache: options.cache,
78
78
  ttl: options.ttl,
79
- stream: options.stream
79
+ stream: options.stream,
80
+ mcp: options.mcp
80
81
  };
81
82
  prototype[ACTION_METADATA] = existingMetadata;
82
83
  });
@@ -112,6 +113,8 @@ var ScheduleMode = /* @__PURE__ */ ((ScheduleMode2) => {
112
113
  ScheduleMode2["FIXED_DELAY"] = "FIXED_DELAY";
113
114
  return ScheduleMode2;
114
115
  })(ScheduleMode || {});
116
+ var Plugin = class {
117
+ };
115
118
  var logger = winston.createLogger({
116
119
  level: "info",
117
120
  transports: [
@@ -774,7 +777,16 @@ var Microservice = class {
774
777
  this.options = {
775
778
  prefix: "/api",
776
779
  name: "microservice",
780
+ version: "0.0.1",
781
+ env: "default",
782
+ printError: false,
783
+ disableCache: false,
784
+ generateClient: false,
785
+ etcd: false,
786
+ events: {},
787
+ websocket: { enabled: false },
777
788
  cacheAdapter: new MemoryCacheAdapter(),
789
+ plugins: [],
778
790
  ...options
779
791
  };
780
792
  this.cache = this.options.cacheAdapter;
@@ -793,6 +805,7 @@ var Microservice = class {
793
805
  this.initStatsEventManager();
794
806
  }
795
807
  await this.registerService(true);
808
+ await this.initPlugins();
796
809
  }
797
810
  async initModules() {
798
811
  for (const ModuleClass of this.options.modules) {
@@ -817,7 +830,7 @@ var Microservice = class {
817
830
  );
818
831
  this.actionHandlers.set(`${moduleName}.${actionName}`, handler);
819
832
  logger_default.info(
820
- `[ \u6CE8\u518C\u52A8\u4F5C ] ${moduleName}.${actionName} ${actionMetadata.description}`
833
+ `[ \u6CE8\u518C\u52A8\u4F5C ] ${moduleName}.${actionName} ${actionMetadata.description} ${actionMetadata.mcp ? "MCP:" + actionMetadata.mcp?.type : ""}`
821
834
  );
822
835
  }
823
836
  const schedules = getScheduleMetadata(ModuleClass.prototype);
@@ -1180,32 +1193,16 @@ Received SIGINT signal`);
1180
1193
  return ctx.json({ success: false, error: error.message });
1181
1194
  }
1182
1195
  };
1183
- async init() {
1184
- await this.waitingInitialization;
1185
- }
1186
- /**
1187
- * 获取所有注册的服务
1188
- */
1189
- async getServices() {
1190
- if (!this.etcdClient) {
1191
- throw new Error("etcd is not configured");
1196
+ async initPlugins() {
1197
+ for (const plugin of this.options.plugins) {
1198
+ logger_default.info(`[ \u521D\u59CB\u5316\u63D2\u4EF6 ] ${plugin.constructor.name}`);
1199
+ await plugin.initialize(this).catch((e) => {
1200
+ logger_default.error(`[ \u521D\u59CB\u5316\u63D2\u4EF6\u5931\u8D25 ] ${plugin.constructor.name}: ${e}`);
1201
+ });
1192
1202
  }
1193
- const namespace = this.options.etcd?.namespace ? `${this.options.etcd.namespace}/` : "";
1194
- const servicesKey = `${namespace}services`;
1195
- const services = await this.etcdClient.getAll().prefix(servicesKey);
1196
- return Object.values(services).map((value) => JSON.parse(value));
1197
1203
  }
1198
- /**
1199
- * 获取指定服务的实例
1200
- */
1201
- async getServiceInstances(serviceName) {
1202
- if (!this.etcdClient) {
1203
- throw new Error("etcd is not configured");
1204
- }
1205
- const namespace = this.options.etcd?.namespace ? `${this.options.etcd.namespace}/` : "";
1206
- const serviceKey = `${namespace}services/${serviceName}`;
1207
- const instances = await this.etcdClient.getAll().prefix(serviceKey);
1208
- return Object.values(instances).map((value) => JSON.parse(value));
1204
+ async init() {
1205
+ await this.waitingInitialization;
1209
1206
  }
1210
1207
  };
1211
1208
 
@@ -1231,4 +1228,4 @@ async function startCheck(checkers, pass) {
1231
1228
  if (pass) await pass();
1232
1229
  }
1233
1230
 
1234
- export { Action, CacheAdapter, MemoryCacheAdapter, Microservice, Module, RedisCacheAdapter, Schedule, ScheduleMode, ServiceContext, logger_default as logger, startCheck };
1231
+ export { Action, CacheAdapter, MemoryCacheAdapter, Microservice, Module, Plugin, RedisCacheAdapter, Schedule, ScheduleMode, ServiceContext, logger_default as logger, startCheck };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imean-service-engine",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "microservice engine",
5
5
  "keywords": [
6
6
  "microservice",
@@ -39,6 +39,7 @@
39
39
  "dependencies": {
40
40
  "@hono/node-server": "^1.13.7",
41
41
  "@hono/node-ws": "^1.0.6",
42
+ "@modelcontextprotocol/sdk": "^1.8.0",
42
43
  "dayjs": "^1.11.13",
43
44
  "ejson": "^2.2.3",
44
45
  "etcd3": "^1.1.2",
@@ -46,6 +47,7 @@
46
47
  "hono": "^4.6.17",
47
48
  "lru-cache": "^11.0.2",
48
49
  "prettier": "^3.4.2",
50
+ "ulid": "^3.0.0",
49
51
  "winston": "^3.17.0",
50
52
  "zod": "^3.24.1"
51
53
  },