@juspay/neurolink 1.5.1 → 1.5.2
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/CHANGELOG.md +49 -0
- package/README.md +1 -1
- package/dist/cli/commands/config.d.ts +35 -35
- package/dist/cli/index.js +63 -19
- package/dist/core/factory.js +12 -11
- package/dist/lib/core/factory.d.ts +40 -0
- package/dist/lib/core/factory.js +162 -0
- package/dist/lib/core/types.d.ts +111 -0
- package/dist/lib/core/types.js +68 -0
- package/dist/lib/index.d.ts +56 -0
- package/dist/lib/index.js +62 -0
- package/dist/lib/mcp/context-manager.d.ts +164 -0
- package/dist/lib/mcp/context-manager.js +273 -0
- package/dist/lib/mcp/factory.d.ts +144 -0
- package/dist/lib/mcp/factory.js +141 -0
- package/dist/lib/mcp/orchestrator.d.ts +170 -0
- package/dist/lib/mcp/orchestrator.js +372 -0
- package/dist/lib/mcp/registry.d.ts +188 -0
- package/dist/lib/mcp/registry.js +373 -0
- package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.d.ts +21 -0
- package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +215 -0
- package/dist/lib/mcp/servers/ai-providers/ai-core-server.d.ts +10 -0
- package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +303 -0
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +101 -0
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +428 -0
- package/dist/lib/neurolink.d.ts +53 -0
- package/dist/lib/neurolink.js +155 -0
- package/dist/lib/providers/amazonBedrock.d.ts +11 -0
- package/dist/lib/providers/amazonBedrock.js +256 -0
- package/dist/lib/providers/anthropic.d.ts +34 -0
- package/dist/lib/providers/anthropic.js +308 -0
- package/dist/lib/providers/azureOpenAI.d.ts +37 -0
- package/dist/lib/providers/azureOpenAI.js +339 -0
- package/dist/lib/providers/googleAIStudio.d.ts +30 -0
- package/dist/lib/providers/googleAIStudio.js +216 -0
- package/dist/lib/providers/googleVertexAI.d.ts +30 -0
- package/dist/lib/providers/googleVertexAI.js +409 -0
- package/dist/lib/providers/index.d.ts +30 -0
- package/dist/lib/providers/index.js +25 -0
- package/dist/lib/providers/openAI.d.ts +10 -0
- package/dist/lib/providers/openAI.js +169 -0
- package/dist/lib/utils/logger.d.ts +12 -0
- package/dist/lib/utils/logger.js +25 -0
- package/dist/lib/utils/providerUtils.d.ts +17 -0
- package/dist/lib/utils/providerUtils.js +73 -0
- package/dist/mcp/servers/ai-providers/ai-core-server.js +11 -10
- package/dist/neurolink.js +13 -12
- package/dist/providers/amazonBedrock.js +22 -21
- package/dist/providers/anthropic.js +21 -20
- package/dist/providers/azureOpenAI.js +21 -20
- package/dist/providers/googleAIStudio.js +13 -12
- package/dist/providers/googleVertexAI.js +27 -26
- package/dist/providers/openAI.js +12 -11
- package/dist/utils/logger.d.ts +12 -0
- package/dist/utils/logger.js +25 -0
- package/dist/utils/providerUtils.d.ts +0 -3
- package/dist/utils/providerUtils.js +3 -2
- package/package.json +1 -1
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink MCP Context Management System
|
|
3
|
+
* Unified context creation and management for all tool executions
|
|
4
|
+
* Ensures rich context flows through tool chain with session tracking
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Context manager for creating and managing execution contexts
|
|
8
|
+
* Provides rich context for all tool executions with session tracking
|
|
9
|
+
*/
|
|
10
|
+
export class ContextManager {
|
|
11
|
+
sessionCounter = 0;
|
|
12
|
+
activeContexts = new Map();
|
|
13
|
+
/**
|
|
14
|
+
* Create a new execution context with rich information
|
|
15
|
+
*
|
|
16
|
+
* @param request Context creation request with optional fields
|
|
17
|
+
* @returns Complete execution context ready for tool chain
|
|
18
|
+
*/
|
|
19
|
+
createContext(request = {}) {
|
|
20
|
+
// Generate session ID if not provided
|
|
21
|
+
const sessionId = request.sessionId || this.generateSessionId();
|
|
22
|
+
// Create comprehensive context
|
|
23
|
+
const context = {
|
|
24
|
+
// Session Management
|
|
25
|
+
sessionId,
|
|
26
|
+
userId: request.userId,
|
|
27
|
+
// AI Provider Context (from existing NeuroLink)
|
|
28
|
+
aiProvider: request.aiProvider,
|
|
29
|
+
modelId: request.modelId,
|
|
30
|
+
temperature: request.temperature,
|
|
31
|
+
maxTokens: request.maxTokens,
|
|
32
|
+
// Business Context (new for MCP)
|
|
33
|
+
organizationId: request.organizationId,
|
|
34
|
+
projectId: request.projectId,
|
|
35
|
+
environmentType: request.environmentType || 'development',
|
|
36
|
+
// Framework Context (new for MCP)
|
|
37
|
+
frameworkType: request.frameworkType,
|
|
38
|
+
// Tool Execution Context (initialized empty)
|
|
39
|
+
toolChain: [],
|
|
40
|
+
parentToolId: undefined,
|
|
41
|
+
// Security & Permissions
|
|
42
|
+
permissions: request.permissions || [],
|
|
43
|
+
securityLevel: request.securityLevel || 'private',
|
|
44
|
+
// Copy any additional custom fields
|
|
45
|
+
...this.extractCustomFields(request)
|
|
46
|
+
};
|
|
47
|
+
// Store context for session management
|
|
48
|
+
this.activeContexts.set(sessionId, context);
|
|
49
|
+
return context;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Add a tool to the execution chain
|
|
53
|
+
*
|
|
54
|
+
* @param context Execution context to modify
|
|
55
|
+
* @param toolName Name of the tool being executed
|
|
56
|
+
*/
|
|
57
|
+
addToToolChain(context, toolName) {
|
|
58
|
+
if (!context.toolChain) {
|
|
59
|
+
context.toolChain = [];
|
|
60
|
+
}
|
|
61
|
+
context.toolChain.push(toolName);
|
|
62
|
+
// Update the active context
|
|
63
|
+
this.activeContexts.set(context.sessionId, context);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get the current tool chain for a context
|
|
67
|
+
*
|
|
68
|
+
* @param context Execution context
|
|
69
|
+
* @returns Array of tool names in execution order
|
|
70
|
+
*/
|
|
71
|
+
getToolChain(context) {
|
|
72
|
+
return context.toolChain || [];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Set parent tool for nested tool execution
|
|
76
|
+
*
|
|
77
|
+
* @param context Execution context to modify
|
|
78
|
+
* @param parentToolId ID of the parent tool
|
|
79
|
+
*/
|
|
80
|
+
setParentTool(context, parentToolId) {
|
|
81
|
+
context.parentToolId = parentToolId;
|
|
82
|
+
this.activeContexts.set(context.sessionId, context);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create child context for nested tool execution
|
|
86
|
+
*
|
|
87
|
+
* @param parentContext Parent execution context
|
|
88
|
+
* @param childToolName Name of the child tool
|
|
89
|
+
* @returns New child context with inherited properties
|
|
90
|
+
*/
|
|
91
|
+
createChildContext(parentContext, childToolName) {
|
|
92
|
+
const childContext = {
|
|
93
|
+
// Inherit all parent context
|
|
94
|
+
...parentContext,
|
|
95
|
+
// Create new session ID for child
|
|
96
|
+
sessionId: this.generateSessionId(),
|
|
97
|
+
// Set parent tool reference
|
|
98
|
+
parentToolId: parentContext.toolChain?.[parentContext.toolChain.length - 1],
|
|
99
|
+
// Reset tool chain for child (will be populated as child executes tools)
|
|
100
|
+
toolChain: []
|
|
101
|
+
};
|
|
102
|
+
// Store child context
|
|
103
|
+
this.activeContexts.set(childContext.sessionId, childContext);
|
|
104
|
+
return childContext;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get context by session ID
|
|
108
|
+
*
|
|
109
|
+
* @param sessionId Session identifier
|
|
110
|
+
* @returns Execution context or undefined if not found
|
|
111
|
+
*/
|
|
112
|
+
getContext(sessionId) {
|
|
113
|
+
return this.activeContexts.get(sessionId);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Update context with new information
|
|
117
|
+
*
|
|
118
|
+
* @param sessionId Session identifier
|
|
119
|
+
* @param updates Partial context updates
|
|
120
|
+
*/
|
|
121
|
+
updateContext(sessionId, updates) {
|
|
122
|
+
const context = this.activeContexts.get(sessionId);
|
|
123
|
+
if (context) {
|
|
124
|
+
const updatedContext = { ...context, ...updates };
|
|
125
|
+
this.activeContexts.set(sessionId, updatedContext);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Remove context from active tracking
|
|
130
|
+
*
|
|
131
|
+
* @param sessionId Session identifier
|
|
132
|
+
*/
|
|
133
|
+
removeContext(sessionId) {
|
|
134
|
+
this.activeContexts.delete(sessionId);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get all active contexts (for debugging/monitoring)
|
|
138
|
+
*
|
|
139
|
+
* @returns Array of all active contexts
|
|
140
|
+
*/
|
|
141
|
+
getActiveContexts() {
|
|
142
|
+
return Array.from(this.activeContexts.values());
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Clear all active contexts
|
|
146
|
+
*/
|
|
147
|
+
clearAllContexts() {
|
|
148
|
+
this.activeContexts.clear();
|
|
149
|
+
this.sessionCounter = 0;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get context statistics
|
|
153
|
+
*
|
|
154
|
+
* @returns Context usage statistics
|
|
155
|
+
*/
|
|
156
|
+
getStats() {
|
|
157
|
+
const contexts = Array.from(this.activeContexts.values());
|
|
158
|
+
const totalToolChainLength = contexts.reduce((sum, ctx) => sum + (ctx.toolChain?.length || 0), 0);
|
|
159
|
+
return {
|
|
160
|
+
activeContexts: contexts.length,
|
|
161
|
+
totalSessionsCreated: this.sessionCounter,
|
|
162
|
+
averageToolChainLength: contexts.length > 0
|
|
163
|
+
? totalToolChainLength / contexts.length
|
|
164
|
+
: 0
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Generate unique session ID
|
|
169
|
+
*
|
|
170
|
+
* @returns Unique session identifier
|
|
171
|
+
*/
|
|
172
|
+
generateSessionId() {
|
|
173
|
+
this.sessionCounter++;
|
|
174
|
+
const timestamp = Date.now();
|
|
175
|
+
const random = Math.random().toString(36).substring(2, 8);
|
|
176
|
+
return `nlmcp-${timestamp}-${this.sessionCounter}-${random}`;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Extract custom fields from request (excluding known fields)
|
|
180
|
+
*
|
|
181
|
+
* @param request Context creation request
|
|
182
|
+
* @returns Custom fields object
|
|
183
|
+
*/
|
|
184
|
+
extractCustomFields(request) {
|
|
185
|
+
const knownFields = new Set([
|
|
186
|
+
'sessionId', 'userId', 'aiProvider', 'modelId', 'temperature', 'maxTokens',
|
|
187
|
+
'organizationId', 'projectId', 'environmentType', 'frameworkType',
|
|
188
|
+
'permissions', 'securityLevel'
|
|
189
|
+
]);
|
|
190
|
+
const customFields = {};
|
|
191
|
+
for (const [key, value] of Object.entries(request)) {
|
|
192
|
+
if (!knownFields.has(key)) {
|
|
193
|
+
customFields[key] = value;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return customFields;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Default context manager instance
|
|
201
|
+
* Can be used across the application for consistent context management
|
|
202
|
+
*/
|
|
203
|
+
export const defaultContextManager = new ContextManager();
|
|
204
|
+
/**
|
|
205
|
+
* Utility function to create context with defaults
|
|
206
|
+
*
|
|
207
|
+
* @param request Optional context request
|
|
208
|
+
* @returns Execution context with sensible defaults
|
|
209
|
+
*/
|
|
210
|
+
export function createExecutionContext(request = {}) {
|
|
211
|
+
return defaultContextManager.createContext(request);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Utility function to add tool to default context manager
|
|
215
|
+
*
|
|
216
|
+
* @param context Execution context
|
|
217
|
+
* @param toolName Tool name to add
|
|
218
|
+
*/
|
|
219
|
+
export function addToolToChain(context, toolName) {
|
|
220
|
+
defaultContextManager.addToToolChain(context, toolName);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Context validation utilities
|
|
224
|
+
*/
|
|
225
|
+
export class ContextValidator {
|
|
226
|
+
/**
|
|
227
|
+
* Validate context has required fields for tool execution
|
|
228
|
+
*
|
|
229
|
+
* @param context Execution context to validate
|
|
230
|
+
* @returns Validation result with details
|
|
231
|
+
*/
|
|
232
|
+
static validateContext(context) {
|
|
233
|
+
const errors = [];
|
|
234
|
+
const warnings = [];
|
|
235
|
+
// Required field validation
|
|
236
|
+
if (!context.sessionId) {
|
|
237
|
+
errors.push('sessionId is required');
|
|
238
|
+
}
|
|
239
|
+
// Optional field validation with warnings
|
|
240
|
+
if (!context.environmentType) {
|
|
241
|
+
warnings.push('environmentType not specified, defaulting to development');
|
|
242
|
+
}
|
|
243
|
+
if (!context.securityLevel) {
|
|
244
|
+
warnings.push('securityLevel not specified, defaulting to private');
|
|
245
|
+
}
|
|
246
|
+
// Tool chain validation
|
|
247
|
+
if (context.toolChain && context.toolChain.length > 10) {
|
|
248
|
+
warnings.push('Tool chain is getting long (>10 tools), consider breaking into smaller workflows');
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
isValid: errors.length === 0,
|
|
252
|
+
errors,
|
|
253
|
+
warnings
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Validate context permissions for tool execution
|
|
258
|
+
*
|
|
259
|
+
* @param context Execution context
|
|
260
|
+
* @param requiredPermissions Permissions required by tool
|
|
261
|
+
* @returns Whether context has required permissions
|
|
262
|
+
*/
|
|
263
|
+
static hasPermissions(context, requiredPermissions) {
|
|
264
|
+
if (!requiredPermissions || requiredPermissions.length === 0) {
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
const contextPermissions = context.permissions || [];
|
|
268
|
+
// Check if context has all required permissions
|
|
269
|
+
return requiredPermissions.every(permission => contextPermissions.includes(permission) ||
|
|
270
|
+
contextPermissions.includes('*') // Wildcard permission
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink MCP Server Factory
|
|
3
|
+
* Factory-First Architecture: MCP servers create tools for internal orchestration
|
|
4
|
+
* Compatible with Lighthouse MCP patterns for seamless migration
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
/**
|
|
8
|
+
* MCP Server Categories for organization and discovery
|
|
9
|
+
*/
|
|
10
|
+
export type MCPServerCategory = 'ai-providers' | 'frameworks' | 'development' | 'business' | 'content' | 'data' | 'integrations' | 'automation' | 'analysis' | 'custom';
|
|
11
|
+
/**
|
|
12
|
+
* Tool execution context - Rich context passed to every tool execution
|
|
13
|
+
* Based on Lighthouse patterns with NeuroLink enhancements
|
|
14
|
+
*/
|
|
15
|
+
export interface NeuroLinkExecutionContext {
|
|
16
|
+
sessionId: string;
|
|
17
|
+
userId?: string;
|
|
18
|
+
aiProvider?: string;
|
|
19
|
+
modelId?: string;
|
|
20
|
+
temperature?: number;
|
|
21
|
+
maxTokens?: number;
|
|
22
|
+
organizationId?: string;
|
|
23
|
+
projectId?: string;
|
|
24
|
+
environmentType?: 'development' | 'staging' | 'production';
|
|
25
|
+
frameworkType?: 'react' | 'vue' | 'svelte' | 'next' | 'nuxt' | 'sveltekit';
|
|
26
|
+
toolChain?: string[];
|
|
27
|
+
parentToolId?: string;
|
|
28
|
+
permissions?: string[];
|
|
29
|
+
securityLevel?: 'public' | 'private' | 'organization';
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Tool execution result - Standardized result format
|
|
34
|
+
*/
|
|
35
|
+
export interface ToolResult {
|
|
36
|
+
success: boolean;
|
|
37
|
+
data?: any;
|
|
38
|
+
error?: string;
|
|
39
|
+
usage?: {
|
|
40
|
+
tokens?: number;
|
|
41
|
+
cost?: number;
|
|
42
|
+
provider?: string;
|
|
43
|
+
model?: string;
|
|
44
|
+
executionTime?: number;
|
|
45
|
+
};
|
|
46
|
+
metadata?: {
|
|
47
|
+
toolName?: string;
|
|
48
|
+
serverId?: string;
|
|
49
|
+
serverTitle?: string;
|
|
50
|
+
sessionId?: string;
|
|
51
|
+
timestamp?: number;
|
|
52
|
+
executionTime?: number;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* MCP Tool Interface - Lighthouse compatible with NeuroLink enhancements
|
|
57
|
+
*/
|
|
58
|
+
export interface NeuroLinkMCPTool {
|
|
59
|
+
name: string;
|
|
60
|
+
description: string;
|
|
61
|
+
execute: (params: unknown, context: NeuroLinkExecutionContext) => Promise<ToolResult>;
|
|
62
|
+
inputSchema?: z.ZodSchema;
|
|
63
|
+
outputSchema?: z.ZodSchema;
|
|
64
|
+
isImplemented?: boolean;
|
|
65
|
+
category?: string;
|
|
66
|
+
permissions?: string[];
|
|
67
|
+
version?: string;
|
|
68
|
+
metadata?: Record<string, any>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* MCP Server Interface - 99% Lighthouse compatible
|
|
72
|
+
* Minimal required fields for easy adoption
|
|
73
|
+
*/
|
|
74
|
+
export interface NeuroLinkMCPServer {
|
|
75
|
+
id: string;
|
|
76
|
+
title: string;
|
|
77
|
+
description?: string;
|
|
78
|
+
version?: string;
|
|
79
|
+
category?: MCPServerCategory;
|
|
80
|
+
visibility?: 'public' | 'private' | 'organization';
|
|
81
|
+
tools: Record<string, NeuroLinkMCPTool>;
|
|
82
|
+
registerTool: (tool: NeuroLinkMCPTool) => NeuroLinkMCPServer;
|
|
83
|
+
metadata?: Record<string, any>;
|
|
84
|
+
dependencies?: string[];
|
|
85
|
+
capabilities?: string[];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* MCP Server Configuration for creation
|
|
89
|
+
*/
|
|
90
|
+
export interface MCPServerConfig {
|
|
91
|
+
id: string;
|
|
92
|
+
title: string;
|
|
93
|
+
description?: string;
|
|
94
|
+
version?: string;
|
|
95
|
+
category?: MCPServerCategory;
|
|
96
|
+
visibility?: 'public' | 'private' | 'organization';
|
|
97
|
+
metadata?: Record<string, any>;
|
|
98
|
+
dependencies?: string[];
|
|
99
|
+
capabilities?: string[];
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Create MCP Server Factory Function
|
|
103
|
+
*
|
|
104
|
+
* Core factory function for creating Lighthouse-compatible MCP servers.
|
|
105
|
+
* Follows Factory-First architecture where tools are internal implementation.
|
|
106
|
+
*
|
|
107
|
+
* @param config Server configuration with minimal required fields
|
|
108
|
+
* @returns Fully configured MCP server ready for tool registration
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* const aiCoreServer = createMCPServer({
|
|
113
|
+
* id: 'neurolink-ai-core',
|
|
114
|
+
* title: 'NeuroLink AI Core',
|
|
115
|
+
* description: 'Core AI provider tools',
|
|
116
|
+
* category: 'ai-providers'
|
|
117
|
+
* });
|
|
118
|
+
*
|
|
119
|
+
* aiCoreServer.registerTool({
|
|
120
|
+
* name: 'generate-text',
|
|
121
|
+
* description: 'Generate text using AI providers',
|
|
122
|
+
* execute: async (params, context) => {
|
|
123
|
+
* // Tool implementation
|
|
124
|
+
* return { success: true, data: result };
|
|
125
|
+
* }
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export declare function createMCPServer(config: MCPServerConfig): NeuroLinkMCPServer;
|
|
130
|
+
/**
|
|
131
|
+
* Utility function to validate tool interface
|
|
132
|
+
*/
|
|
133
|
+
export declare function validateTool(tool: NeuroLinkMCPTool): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Utility function to get server info
|
|
136
|
+
*/
|
|
137
|
+
export declare function getServerInfo(server: NeuroLinkMCPServer): {
|
|
138
|
+
id: string;
|
|
139
|
+
title: string;
|
|
140
|
+
description?: string;
|
|
141
|
+
category?: MCPServerCategory;
|
|
142
|
+
toolCount: number;
|
|
143
|
+
capabilities: string[];
|
|
144
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink MCP Server Factory
|
|
3
|
+
* Factory-First Architecture: MCP servers create tools for internal orchestration
|
|
4
|
+
* Compatible with Lighthouse MCP patterns for seamless migration
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
/**
|
|
8
|
+
* Input validation schemas
|
|
9
|
+
*/
|
|
10
|
+
const ServerConfigSchema = z.object({
|
|
11
|
+
id: z.string().min(1, 'Server ID is required'),
|
|
12
|
+
title: z.string().min(1, 'Server title is required'),
|
|
13
|
+
description: z.string().optional(),
|
|
14
|
+
version: z.string().optional(),
|
|
15
|
+
category: z.enum([
|
|
16
|
+
'ai-providers',
|
|
17
|
+
'frameworks',
|
|
18
|
+
'development',
|
|
19
|
+
'business',
|
|
20
|
+
'content',
|
|
21
|
+
'data',
|
|
22
|
+
'integrations',
|
|
23
|
+
'automation',
|
|
24
|
+
'analysis',
|
|
25
|
+
'custom'
|
|
26
|
+
]).optional(),
|
|
27
|
+
visibility: z.enum(['public', 'private', 'organization']).optional(),
|
|
28
|
+
metadata: z.record(z.any()).optional(),
|
|
29
|
+
dependencies: z.array(z.string()).optional(),
|
|
30
|
+
capabilities: z.array(z.string()).optional()
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Create MCP Server Factory Function
|
|
34
|
+
*
|
|
35
|
+
* Core factory function for creating Lighthouse-compatible MCP servers.
|
|
36
|
+
* Follows Factory-First architecture where tools are internal implementation.
|
|
37
|
+
*
|
|
38
|
+
* @param config Server configuration with minimal required fields
|
|
39
|
+
* @returns Fully configured MCP server ready for tool registration
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const aiCoreServer = createMCPServer({
|
|
44
|
+
* id: 'neurolink-ai-core',
|
|
45
|
+
* title: 'NeuroLink AI Core',
|
|
46
|
+
* description: 'Core AI provider tools',
|
|
47
|
+
* category: 'ai-providers'
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* aiCoreServer.registerTool({
|
|
51
|
+
* name: 'generate-text',
|
|
52
|
+
* description: 'Generate text using AI providers',
|
|
53
|
+
* execute: async (params, context) => {
|
|
54
|
+
* // Tool implementation
|
|
55
|
+
* return { success: true, data: result };
|
|
56
|
+
* }
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function createMCPServer(config) {
|
|
61
|
+
// Validate configuration
|
|
62
|
+
const validatedConfig = ServerConfigSchema.parse(config);
|
|
63
|
+
// Create server with sensible defaults
|
|
64
|
+
const server = {
|
|
65
|
+
// Required fields
|
|
66
|
+
id: validatedConfig.id,
|
|
67
|
+
title: validatedConfig.title,
|
|
68
|
+
// Optional fields with defaults
|
|
69
|
+
description: validatedConfig.description,
|
|
70
|
+
version: validatedConfig.version || '1.0.0',
|
|
71
|
+
category: validatedConfig.category || 'custom',
|
|
72
|
+
visibility: validatedConfig.visibility || 'private',
|
|
73
|
+
// Tool management
|
|
74
|
+
tools: {},
|
|
75
|
+
// Tool registration method
|
|
76
|
+
registerTool(tool) {
|
|
77
|
+
// Validate tool has required fields
|
|
78
|
+
if (!tool.name || !tool.description || !tool.execute) {
|
|
79
|
+
throw new Error(`Invalid tool: name, description, and execute are required`);
|
|
80
|
+
}
|
|
81
|
+
// Check for duplicate tool names
|
|
82
|
+
if (this.tools[tool.name]) {
|
|
83
|
+
throw new Error(`Tool '${tool.name}' already exists in server '${this.id}'`);
|
|
84
|
+
}
|
|
85
|
+
// Register the tool
|
|
86
|
+
this.tools[tool.name] = {
|
|
87
|
+
...tool,
|
|
88
|
+
// Add server metadata to tool
|
|
89
|
+
metadata: {
|
|
90
|
+
...tool.metadata,
|
|
91
|
+
serverId: this.id,
|
|
92
|
+
serverCategory: this.category,
|
|
93
|
+
registeredAt: Date.now()
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return this;
|
|
97
|
+
},
|
|
98
|
+
// Extension points
|
|
99
|
+
metadata: validatedConfig.metadata || {},
|
|
100
|
+
dependencies: validatedConfig.dependencies || [],
|
|
101
|
+
capabilities: validatedConfig.capabilities || []
|
|
102
|
+
};
|
|
103
|
+
return server;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Utility function to validate tool interface
|
|
107
|
+
*/
|
|
108
|
+
export function validateTool(tool) {
|
|
109
|
+
try {
|
|
110
|
+
// Check required fields
|
|
111
|
+
if (!tool.name || typeof tool.name !== 'string')
|
|
112
|
+
return false;
|
|
113
|
+
if (!tool.description || typeof tool.description !== 'string')
|
|
114
|
+
return false;
|
|
115
|
+
if (!tool.execute || typeof tool.execute !== 'function')
|
|
116
|
+
return false;
|
|
117
|
+
// Validate optional schemas if present
|
|
118
|
+
if (tool.inputSchema && !(tool.inputSchema instanceof z.ZodSchema))
|
|
119
|
+
return false;
|
|
120
|
+
if (tool.outputSchema && !(tool.outputSchema instanceof z.ZodSchema))
|
|
121
|
+
return false;
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Utility function to get server info
|
|
130
|
+
*/
|
|
131
|
+
export function getServerInfo(server) {
|
|
132
|
+
return {
|
|
133
|
+
id: server.id,
|
|
134
|
+
title: server.title,
|
|
135
|
+
description: server.description,
|
|
136
|
+
category: server.category,
|
|
137
|
+
toolCount: Object.keys(server.tools).length,
|
|
138
|
+
capabilities: server.capabilities || []
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
// Types are already exported above via export interface declarations
|