@zenflux/core 0.1.6 → 0.1.8
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/src/bases/controller-base.d.ts +4 -4
- package/dist/src/bases/index.d.ts +2 -2
- package/dist/src/clients/http.d.ts +3 -3
- package/dist/src/command-bases/command-base.d.ts +3 -3
- package/dist/src/command-bases/command-internal.d.ts +1 -1
- package/dist/src/command-bases/command-public.d.ts +1 -1
- package/dist/src/command-bases/command-restful.d.ts +1 -1
- package/dist/src/command-bases/index.d.ts +4 -4
- package/dist/src/errors/command-already-registered.d.ts +1 -1
- package/dist/src/errors/controller-already-registered.d.ts +1 -1
- package/dist/src/errors/controller-already-set.d.ts +1 -1
- package/dist/src/errors/force-method-implementation.d.ts +1 -1
- package/dist/src/errors/index-public.d.ts +1 -1
- package/dist/src/errors/index.d.ts +5 -5
- package/dist/src/exports-index.d.ts +5 -5
- package/dist/src/exports.d.ts +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/interfaces/index.d.ts +4 -4
- package/dist/src/managers/commands.d.ts +4 -4
- package/dist/src/managers/controllers.d.ts +2 -2
- package/dist/src/managers/export.d.ts +5 -5
- package/dist/src/managers/index.d.ts +4 -4
- package/dist/src/managers/internal.d.ts +1 -1
- package/dist/src/managers/restful.d.ts +5 -5
- package/dist/zenflux-core.cjs +39 -40
- package/dist/zenflux-core.d.ts +448 -3
- package/dist/zenflux-core.mjs +39 -40
- package/package.json +11 -2
- package/src/bases/controller-base.ts +5 -5
- package/src/bases/index.ts +6 -2
- package/src/bases/initialize-base.ts +32 -0
- package/src/bases/object-base.ts +15 -43
- package/src/clients/http.ts +3 -3
- package/src/command-bases/command-base.ts +4 -4
- package/src/command-bases/command-internal.ts +1 -1
- package/src/command-bases/command-public.ts +1 -1
- package/src/command-bases/command-restful.ts +3 -3
- package/src/command-bases/index.ts +5 -4
- package/src/errors/command-already-registered.ts +1 -1
- package/src/errors/controller-already-registered.ts +1 -1
- package/src/errors/controller-already-set.ts +1 -1
- package/src/errors/error-with-metadata.ts +14 -0
- package/src/errors/force-method-implementation.ts +3 -11
- package/src/errors/index-public.ts +1 -1
- package/src/errors/index.ts +9 -5
- package/src/exports-index.ts +5 -5
- package/src/exports.ts +6 -5
- package/src/index.ts +3 -2
- package/src/interfaces/index.ts +5 -4
- package/src/interfaces/logger.ts +3 -2
- package/src/managers/commands.ts +6 -6
- package/src/managers/controllers.ts +3 -3
- package/src/managers/export.ts +5 -5
- package/src/managers/index.ts +5 -4
- package/src/managers/internal.ts +1 -1
- package/src/managers/restful.ts +5 -5
- package/src/modules/debugger.ts +113 -0
- package/src/modules/event-bus/event-bus.ts +204 -0
- package/src/modules/logger/config.ts +44 -0
- package/src/modules/logger/index.ts +82 -0
- package/src/modules/mcp-server/mcp-server.ts +139 -0
- package/src/modules/mcp-server/mcp-service.ts +80 -0
- package/src/modules/service/service-base.ts +55 -0
- package/src/modules/service/service-locator.ts +205 -0
- package/src/modules/service/service-with-dependencies-base.ts +51 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import EventEmitter from "events";
|
|
2
|
+
|
|
3
|
+
import { ObjectBase } from "../../bases/object-base";
|
|
4
|
+
|
|
5
|
+
export class EventBus extends ObjectBase {
|
|
6
|
+
protected static instance: EventBus | null = null;
|
|
7
|
+
|
|
8
|
+
private objects = new Map<
|
|
9
|
+
string,
|
|
10
|
+
{
|
|
11
|
+
object: ObjectBase | ObjectBase[];
|
|
12
|
+
methods: Function[];
|
|
13
|
+
}
|
|
14
|
+
>();
|
|
15
|
+
|
|
16
|
+
private eventEmitter = new EventEmitter();
|
|
17
|
+
|
|
18
|
+
private lastEmittedEvents = new Map<string, any[]>();
|
|
19
|
+
|
|
20
|
+
public static getName() {
|
|
21
|
+
return "ZenFlux/Core/Modules/EventBus";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static getInstance(): EventBus {
|
|
25
|
+
if ( !EventBus.instance ) {
|
|
26
|
+
EventBus.instance = new EventBus();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return EventBus.instance;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public static get $() {
|
|
33
|
+
return EventBus.getInstance();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public getObjectNames() {
|
|
37
|
+
return Array.from( this.objects.keys() );
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public getEventNames() {
|
|
41
|
+
this.eventEmitter.eventNames();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public getEventName( objectName: string, methodName: string ) {
|
|
45
|
+
return `${ objectName }::${ methodName }`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public on( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
|
|
49
|
+
// Validate that event exists
|
|
50
|
+
if ( !this.objects.has( objectName ) ) {
|
|
51
|
+
throw new Error( `Object ${ objectName } is not registered` );
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this.eventEmitter.on( this.getEventName( objectName, methodName ), callback );
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Function onCalledBeforeDoInvoke(): The difference between this function and on()
|
|
59
|
+
* is that this function will call the callback if the event already happened.
|
|
60
|
+
*/
|
|
61
|
+
public onCalledBeforeDoInvoke( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
|
|
62
|
+
this.on( objectName, methodName, callback );
|
|
63
|
+
|
|
64
|
+
// If event already happened, call the callback
|
|
65
|
+
if ( this.objects.has( objectName ) ) {
|
|
66
|
+
const objectEntry = this.objects.get( objectName );
|
|
67
|
+
|
|
68
|
+
if ( objectEntry ) {
|
|
69
|
+
// We don't need to iterate through objects anymore, just check if the event has been emitted
|
|
70
|
+
const method = objectEntry.methods.find( ( method ) => method.name === methodName );
|
|
71
|
+
|
|
72
|
+
if ( method && this.lastEmittedEvents.has( this.getEventName( objectName, methodName ) ) ) {
|
|
73
|
+
callback( ...this.lastEmittedEvents.get( this.getEventName( objectName, methodName ) )! );
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public off( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
|
|
80
|
+
this.eventEmitter.off( this.getEventName( objectName, methodName ), callback );
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public register<T extends ObjectBase>( object: T, methods: Function[] ) {
|
|
84
|
+
if ( this.objects.has( object.getName() ) ) {
|
|
85
|
+
throw new Error( `Error in: '${ this.getName() }', object: '${ object.getName() }' is already registered` );
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.objects.set( object.getName(), {
|
|
89
|
+
object,
|
|
90
|
+
methods
|
|
91
|
+
} );
|
|
92
|
+
|
|
93
|
+
this.hook( object, methods );
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public registerMultiInstances<T extends ObjectBase>( object: T, methods: Function[] ) {
|
|
97
|
+
const objectName = object.getName();
|
|
98
|
+
|
|
99
|
+
// If we already have an entry for this name, check if we need to convert to array
|
|
100
|
+
if ( this.objects.has( objectName ) ) {
|
|
101
|
+
const existingEntry = this.objects.get( objectName )!;
|
|
102
|
+
|
|
103
|
+
// If object is already an array, append to it
|
|
104
|
+
if ( Array.isArray( existingEntry.object ) ) {
|
|
105
|
+
( existingEntry.object as ObjectBase[] ).push( object );
|
|
106
|
+
}
|
|
107
|
+
// Otherwise, convert to array containing both existing and new objects
|
|
108
|
+
else {
|
|
109
|
+
existingEntry.object = [ existingEntry.object as ObjectBase, object ];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Otherwise, create a new entry with the object directly (not in an array)
|
|
113
|
+
else {
|
|
114
|
+
this.objects.set( objectName, {
|
|
115
|
+
object,
|
|
116
|
+
methods
|
|
117
|
+
} );
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Hook the object
|
|
121
|
+
this.hook( object, methods );
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public unregister( objectName: string ) {
|
|
125
|
+
const entry = this.objects.get( objectName );
|
|
126
|
+
|
|
127
|
+
if ( !entry ) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Handle both single object and array cases
|
|
132
|
+
if ( Array.isArray( entry.object ) ) {
|
|
133
|
+
( entry.object as ObjectBase[] ).forEach( obj => {
|
|
134
|
+
this.unhookObject( obj, entry.methods );
|
|
135
|
+
} );
|
|
136
|
+
} else {
|
|
137
|
+
this.unhookObject( entry.object as ObjectBase, entry.methods );
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
this.objects.delete( objectName );
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Helper method to unhook a single object
|
|
144
|
+
private unhookObject( object: ObjectBase, methods: Function[] ) {
|
|
145
|
+
methods.forEach( ( method ) => {
|
|
146
|
+
this.ensureFunction( object, method );
|
|
147
|
+
|
|
148
|
+
this.eventEmitter.removeAllListeners( this.getEventName( object.getName(), method.name ) );
|
|
149
|
+
|
|
150
|
+
( object as any )[ method.name ] = method;
|
|
151
|
+
} );
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private unhook( entry: { object: ObjectBase | ObjectBase[]; methods: Function[] } ) {
|
|
155
|
+
if ( Array.isArray( entry.object ) ) {
|
|
156
|
+
( entry.object as ObjectBase[] ).forEach( obj => {
|
|
157
|
+
this.unhookObject( obj, entry.methods );
|
|
158
|
+
} );
|
|
159
|
+
} else {
|
|
160
|
+
this.unhookObject( entry.object as ObjectBase, entry.methods );
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private emit<T extends ObjectBase>( object: T, method: Function, ...args: any[] ) {
|
|
165
|
+
const eventName = this.getEventName( object.getName(), method.name );
|
|
166
|
+
|
|
167
|
+
this.lastEmittedEvents.set( eventName, args );
|
|
168
|
+
|
|
169
|
+
return this.eventEmitter.emit( eventName, ...args );
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private hook<T extends ObjectBase>( object: T, methods: Function[] ) {
|
|
173
|
+
methods.forEach( ( method ) => {
|
|
174
|
+
this.ensureFunction( object, method );
|
|
175
|
+
|
|
176
|
+
const originalMethod = method;
|
|
177
|
+
|
|
178
|
+
const eventBusHook = async( ...args: any[] ) => {
|
|
179
|
+
const result = await originalMethod.apply( object, args );
|
|
180
|
+
|
|
181
|
+
this.emit( object, method, ...args );
|
|
182
|
+
|
|
183
|
+
return result;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// TODO: Use better design pattern to handle losing of function names.
|
|
187
|
+
( object as any )[ method.name ] = new Proxy( eventBusHook, {
|
|
188
|
+
get( target, prop ) {
|
|
189
|
+
if ( prop === "name" ) {
|
|
190
|
+
return method.name;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return target[ prop as keyof typeof target ];
|
|
194
|
+
}
|
|
195
|
+
} );
|
|
196
|
+
} );
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private ensureFunction<T extends ObjectBase>( object: T, method: Function ) {
|
|
200
|
+
if ( "function" !== typeof method ) {
|
|
201
|
+
throw new Error( `Method ${ method } is not a function on ${ object.getName() }` );
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import process from "process";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_LOG_LEVEL = "5";
|
|
4
|
+
|
|
5
|
+
export function getLoggerDefaultLevel(): string {
|
|
6
|
+
return DEFAULT_LOG_LEVEL;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function getLoggerLogLevel(): number {
|
|
10
|
+
return parseInt( process.env.LOGGER_LOG_LEVEL || DEFAULT_LOG_LEVEL, 10 );
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getLoggerLogLevelString(): string {
|
|
14
|
+
switch ( getLoggerLogLevel() ) {
|
|
15
|
+
case 0:
|
|
16
|
+
return "NONE";
|
|
17
|
+
case 1:
|
|
18
|
+
return "ERROR";
|
|
19
|
+
case 2:
|
|
20
|
+
return "WARN";
|
|
21
|
+
case 3:
|
|
22
|
+
return "ADMIN";
|
|
23
|
+
case 4:
|
|
24
|
+
return "INFO";
|
|
25
|
+
case 5:
|
|
26
|
+
return "LOG";
|
|
27
|
+
case 6:
|
|
28
|
+
return "DEBUG";
|
|
29
|
+
default:
|
|
30
|
+
return "UNKNOWN";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function isLoggerDebugEnabled(): boolean {
|
|
35
|
+
return getLoggerLogLevel() >= 6;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isLoggerDisabled(): boolean {
|
|
39
|
+
return process.env.LOGGER_DISABLED === "true";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function isLoggerPreviousSourceDisabled(): boolean {
|
|
43
|
+
return process.env.LOGGER_LOG_PREVIOUS_CALLER_SOURCE_DISABLED === "true";
|
|
44
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLoggerLogLevel,
|
|
3
|
+
getLoggerLogLevelString,
|
|
4
|
+
isLoggerDebugEnabled
|
|
5
|
+
} from "./config";
|
|
6
|
+
|
|
7
|
+
import { classes } from "../../exports";
|
|
8
|
+
|
|
9
|
+
import type { interfaces } from "../../interfaces";
|
|
10
|
+
import type { ObjectBase } from "../../bases/object-base";
|
|
11
|
+
|
|
12
|
+
type TLoggerOwner = ObjectBase | typeof ObjectBase | string;
|
|
13
|
+
|
|
14
|
+
type TLoggerImplementation = {
|
|
15
|
+
new ( owner: TLoggerOwner, options?: any ): interfaces.ILogger;
|
|
16
|
+
getName?: () => string;
|
|
17
|
+
getLogLevelString?: () => string;
|
|
18
|
+
getLogLevel?: () => number;
|
|
19
|
+
isDebugEnabled?: () => boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const FALLBACK_LOGGER_NAME = "ZenFlux/Core/Modules/LoggerProxy";
|
|
23
|
+
|
|
24
|
+
function resolveLoggerImplementation(): TLoggerImplementation {
|
|
25
|
+
if ( globalThis?.zCore?.classes?.Logger && globalThis.zCore.classes.Logger !== Logger ) {
|
|
26
|
+
return globalThis.zCore.classes.Logger as unknown as TLoggerImplementation;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return classes.Logger as unknown as TLoggerImplementation;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class Logger {
|
|
33
|
+
public constructor( owner: TLoggerOwner, options?: any ) {
|
|
34
|
+
const Implementation = resolveLoggerImplementation();
|
|
35
|
+
|
|
36
|
+
if ( Implementation === Logger ) {
|
|
37
|
+
throw new Error( "Circular logger proxy detected. No logger implementation registered." );
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return new Implementation( owner, options );
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public static getName(): string {
|
|
44
|
+
const Implementation = resolveLoggerImplementation();
|
|
45
|
+
|
|
46
|
+
if ( typeof Implementation?.getName === "function" ) {
|
|
47
|
+
return Implementation.getName();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return FALLBACK_LOGGER_NAME;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public static getLogLevelString(): string {
|
|
54
|
+
const Implementation = resolveLoggerImplementation();
|
|
55
|
+
|
|
56
|
+
if ( typeof Implementation?.getLogLevelString === "function" ) {
|
|
57
|
+
return Implementation.getLogLevelString();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return getLoggerLogLevelString();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public static getLogLevel(): number {
|
|
64
|
+
const Implementation = resolveLoggerImplementation();
|
|
65
|
+
|
|
66
|
+
if ( typeof Implementation?.getLogLevel === "function" ) {
|
|
67
|
+
return Implementation.getLogLevel();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return getLoggerLogLevel();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public static isDebugEnabled(): boolean {
|
|
74
|
+
const Implementation = resolveLoggerImplementation();
|
|
75
|
+
|
|
76
|
+
if ( typeof Implementation?.isDebugEnabled === "function" ) {
|
|
77
|
+
return Implementation.isDebugEnabled();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return isLoggerDebugEnabled();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { FastMCP } from "fastmcp";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
import { ObjectBase } from "../../bases";
|
|
5
|
+
|
|
6
|
+
// Keep logs in memory (simple approach)
|
|
7
|
+
const MAX_LOGS = 5000; // Define a maximum number of logs to store
|
|
8
|
+
const logs: any[] = [];
|
|
9
|
+
|
|
10
|
+
export class McpServer extends ObjectBase {
|
|
11
|
+
private server: FastMCP;
|
|
12
|
+
|
|
13
|
+
public static getName(): string {
|
|
14
|
+
return "ZenFlux/Core/Modules/McpServer";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public constructor() {
|
|
18
|
+
super();
|
|
19
|
+
|
|
20
|
+
this.server = new FastMCP( {
|
|
21
|
+
name: "ZenfluxBackendLogger",
|
|
22
|
+
version: "0.1.0",
|
|
23
|
+
} );
|
|
24
|
+
|
|
25
|
+
this.defineMcpTools();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private defineMcpTools(): void {
|
|
29
|
+
// Define the getRecentLogs tool using the fastmcp API
|
|
30
|
+
this.server.addTool( {
|
|
31
|
+
name: "getRecentLogs",
|
|
32
|
+
description: "Retrieves the most recent logs received by the MCP server, optionally filtered by time range.",
|
|
33
|
+
parameters: z.object( {
|
|
34
|
+
limit: z.number().optional().default( 50 ).describe( "Maximum number of logs to return" ),
|
|
35
|
+
startTime: z.number().optional().describe( "Optional start timestamp (ms) to filter logs after" ),
|
|
36
|
+
endTime: z.number().optional().describe( "Optional end timestamp (ms) to filter logs before" ),
|
|
37
|
+
} ),
|
|
38
|
+
execute: async( args ) => {
|
|
39
|
+
const { limit, startTime, endTime } = args;
|
|
40
|
+
|
|
41
|
+
// Get initial slice based on limit (most recent)
|
|
42
|
+
let recentLogs = logs.slice( -limit );
|
|
43
|
+
|
|
44
|
+
// Apply time filters if provided
|
|
45
|
+
if ( startTime ) {
|
|
46
|
+
recentLogs = recentLogs.filter( log => log.timestamp >= startTime );
|
|
47
|
+
}
|
|
48
|
+
if ( endTime ) {
|
|
49
|
+
recentLogs = recentLogs.filter( log => log.timestamp <= endTime );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return JSON.stringify( recentLogs, null, 2 );
|
|
53
|
+
},
|
|
54
|
+
} );
|
|
55
|
+
|
|
56
|
+
// Define the getLogsInfo tool
|
|
57
|
+
this.server.addTool( {
|
|
58
|
+
name: "getLogsInfo",
|
|
59
|
+
description: "Provides information about stored logs, including count, and first/last timestamps.",
|
|
60
|
+
parameters: z.object( {} ), // No parameters
|
|
61
|
+
execute: async() => {
|
|
62
|
+
const logCount = logs.length;
|
|
63
|
+
const firstTimestamp = logCount > 0 ? logs[ 0 ]?.timestamp : null;
|
|
64
|
+
const lastTimestamp = logCount > 0 ? logs[ logCount - 1 ]?.timestamp : null;
|
|
65
|
+
|
|
66
|
+
const info = {
|
|
67
|
+
logCount,
|
|
68
|
+
firstTimestamp,
|
|
69
|
+
lastTimestamp
|
|
70
|
+
};
|
|
71
|
+
return JSON.stringify( info, null, 2 );
|
|
72
|
+
},
|
|
73
|
+
} );
|
|
74
|
+
|
|
75
|
+
// Define the getLogsRange tool
|
|
76
|
+
this.server.addTool( {
|
|
77
|
+
name: "getLogsRange",
|
|
78
|
+
description: "Retrieves logs within a specific time range.",
|
|
79
|
+
parameters: z.object( {
|
|
80
|
+
startTime: z.number().describe( "Required start timestamp (ms) to filter logs after" ),
|
|
81
|
+
endTime: z.number().describe( "Required end timestamp (ms) to filter logs before" ),
|
|
82
|
+
} ),
|
|
83
|
+
execute: async( args ) => {
|
|
84
|
+
const { startTime, endTime } = args;
|
|
85
|
+
|
|
86
|
+
// Validate timestamps
|
|
87
|
+
if ( startTime >= endTime ) {
|
|
88
|
+
throw new Error( "Start time must be before end time." );
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const filteredLogs = logs.filter( log =>
|
|
92
|
+
log.timestamp >= startTime && log.timestamp <= endTime
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
// Consider adding a safety limit here if the range could be huge
|
|
96
|
+
// const MAX_RANGE_RESULTS = 1000;
|
|
97
|
+
// if (filteredLogs.length > MAX_RANGE_RESULTS) {
|
|
98
|
+
// throw new Error(`Log range too large (${filteredLogs.length} logs). Maximum allowed: ${MAX_RANGE_RESULTS}`);
|
|
99
|
+
// }
|
|
100
|
+
|
|
101
|
+
return JSON.stringify( filteredLogs, null, 2 );
|
|
102
|
+
},
|
|
103
|
+
} );
|
|
104
|
+
|
|
105
|
+
console.log( "MCP Tools defined: getRecentLogs, getLogsInfo, getLogsRange" );
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Start the fastmcp server
|
|
109
|
+
public async start( port: number = 3000, host: string = "0.0.0.0" ): Promise<void> {
|
|
110
|
+
try {
|
|
111
|
+
// Removed server name from log as config access is different
|
|
112
|
+
console.log( "Attempting to start fastmcp server..." );
|
|
113
|
+
await this.server.start( {
|
|
114
|
+
transportType: "httpStream", // Explicitly use SSE
|
|
115
|
+
httpStream: {
|
|
116
|
+
port,
|
|
117
|
+
host,
|
|
118
|
+
endpoint: "/mcp" // Standard MCP endpoint
|
|
119
|
+
}
|
|
120
|
+
} );
|
|
121
|
+
} catch ( err ) {
|
|
122
|
+
console.error( "Error starting fastmcp server:", err );
|
|
123
|
+
process.exit( 1 );
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Stop the fastmcp server
|
|
128
|
+
public async stop(): Promise<void> {
|
|
129
|
+
console.log( "Stopping fastmcp server..." );
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Method to add a log entry, now with capping
|
|
133
|
+
public addLog( logEntry: any ): void {
|
|
134
|
+
if ( logs.length >= MAX_LOGS ) {
|
|
135
|
+
logs.shift(); // Remove the oldest log if the limit is reached
|
|
136
|
+
}
|
|
137
|
+
logs.push( logEntry );
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { McpServer } from "./mcp-server";
|
|
2
|
+
|
|
3
|
+
import { EventBus } from "../event-bus/event-bus";
|
|
4
|
+
|
|
5
|
+
import { ServiceBase } from "../service/service-base";
|
|
6
|
+
|
|
7
|
+
// TODO: Get port/host from configuration/environment variables
|
|
8
|
+
const MCP_PORT = 3090;
|
|
9
|
+
const MCP_HOST = "0.0.0.0";
|
|
10
|
+
|
|
11
|
+
export class MCPService extends ServiceBase {
|
|
12
|
+
private static mcpServer: McpServer;
|
|
13
|
+
|
|
14
|
+
public static getName(): string {
|
|
15
|
+
return "ZenFlux/Core/Modules/MCPService";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public constructor() {
|
|
19
|
+
super();
|
|
20
|
+
|
|
21
|
+
// Initialization logic is handled in the initialize method by ServiceBase
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected async initialize(): Promise<void> {
|
|
25
|
+
this.logger.log( this.initialize, "Initializing MCP Service..." );
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
MCPService.mcpServer = new McpServer();
|
|
29
|
+
|
|
30
|
+
// Start the MCP server (Fastify instance)
|
|
31
|
+
// Consider adding error handling or status checking
|
|
32
|
+
await MCPService.mcpServer.start( MCP_PORT, MCP_HOST );
|
|
33
|
+
|
|
34
|
+
EventBus.$.on( "ZenFlux/Core/Modules/Logger", "outputInternal", this.onLoggerOutput.bind( this ) );
|
|
35
|
+
|
|
36
|
+
this.logger.info( this.initialize, `MCP Server started on http://${ MCP_HOST }:${ MCP_PORT }` );
|
|
37
|
+
} catch ( error ) {
|
|
38
|
+
this.logger.error( this.initialize, "Failed to start MCP Server", error );
|
|
39
|
+
// Rethrow or handle appropriately - ServiceBase might catch this
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Optional: Add a method to explicitly stop the server if needed.
|
|
46
|
+
* ServiceBase doesn't seem to have a standard shutdown hook, so manual call might be necessary.
|
|
47
|
+
*/
|
|
48
|
+
public async stopServer(): Promise<void> {
|
|
49
|
+
if ( MCPService.mcpServer ) {
|
|
50
|
+
this.logger.log( this.stopServer, "Stopping MCP Server..." );
|
|
51
|
+
await MCPService.mcpServer.stop();
|
|
52
|
+
this.logger.info( this.stopServer, "MCP Server stopped." );
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// You can add methods here to interact with McpServer if needed,
|
|
57
|
+
// e.g., retrieving logs or status, although tools will likely handle log retrieval.
|
|
58
|
+
private onLoggerOutput( prefix: string, timeDiff: string, source: string, messagePrefix: string, message: string, params: any[] ) {
|
|
59
|
+
// Create a log entry with all the relevant information
|
|
60
|
+
const logEntry = {
|
|
61
|
+
timestamp: new Date().getTime(),
|
|
62
|
+
prefix,
|
|
63
|
+
timeDiff,
|
|
64
|
+
source,
|
|
65
|
+
messagePrefix,
|
|
66
|
+
message,
|
|
67
|
+
params: params && params.length ? params : [],
|
|
68
|
+
formatted: `${ prefix }[+${ timeDiff }ms][${ source }]${ messagePrefix }: ${ message }`
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
if ( MCPService.mcpServer ) {
|
|
72
|
+
// Call the addLog method directly on the static McpServer instance
|
|
73
|
+
try {
|
|
74
|
+
MCPService.mcpServer.addLog( logEntry );
|
|
75
|
+
} catch ( error ) {
|
|
76
|
+
console.error( "MCPService.onLoggerOutput: Failed to add log via addLog method:", error );
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ObjectBase } from "../../bases/object-base";
|
|
2
|
+
|
|
3
|
+
import { Logger } from "../logger";
|
|
4
|
+
|
|
5
|
+
export abstract class ServiceBase extends ObjectBase {
|
|
6
|
+
protected logger: Logger;
|
|
7
|
+
|
|
8
|
+
private readonly initialization: {
|
|
9
|
+
promise: Promise<void>;
|
|
10
|
+
state: "pending" | "resolved" | "rejected";
|
|
11
|
+
reason?: Error;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
public static getName(): string {
|
|
15
|
+
return "ZenFlux/Core/Modules/ServiceBase";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
+
public constructor( ...args: any[] ) {
|
|
20
|
+
super();
|
|
21
|
+
|
|
22
|
+
this.logger = new Logger( this );
|
|
23
|
+
|
|
24
|
+
if ( this.initialize ) {
|
|
25
|
+
this.initialization = {
|
|
26
|
+
promise: this.initialize(),
|
|
27
|
+
state: "pending"
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
this.initialization.promise
|
|
31
|
+
.then( () => ( this.initialization.state = "resolved" ) )
|
|
32
|
+
.catch( ( reason ) => {
|
|
33
|
+
this.initialization.state = "rejected";
|
|
34
|
+
this.initialization.reason = reason;
|
|
35
|
+
} );
|
|
36
|
+
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.initialization = {
|
|
41
|
+
promise: Promise.resolve(),
|
|
42
|
+
state: "resolved"
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected async initialize?(): Promise<void>;
|
|
47
|
+
|
|
48
|
+
public isWithDependencies() {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public getInitialization() {
|
|
53
|
+
return this.initialization;
|
|
54
|
+
}
|
|
55
|
+
}
|