forked from ilkerzg/fal-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-server.js
More file actions
1072 lines (976 loc) · 35.3 KB
/
mcp-server.js
File metadata and controls
1072 lines (976 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
/**
* FAL CLI MCP Server - Enhanced Model Context Protocol Implementation
*
* This server provides AI assistants with access to FAL AI's image generation capabilities
* through the Model Context Protocol (MCP). It includes advanced features like:
* - Intelligent cost management with $5 spending limits
* - Enhanced error handling with contextual suggestions
* - Smart parameter validation and optimization
* - Comprehensive model management and recommendations
*
* The server exposes tools for image generation, model discovery, prompt optimization,
* and cost calculation while ensuring user safety through spending controls.
*
* @version 0.0.1
* @author ilkerzg
*/
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
ListResourcesRequestSchema,
ReadResourceRequestSchema,
ListPromptsRequestSchema,
GetPromptRequestSchema
} from '@modelcontextprotocol/sdk/types.js';
import fs from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
// Import our shared business logic modules
// These modules contain the core functionality that's shared between CLI and MCP interfaces
import { generateSingleImage, generateBatch, calculateCost } from './core/image-generator.js';
import { optimizePrompt, optimizeBatch } from './core/prompt-optimizer.js';
import {
loadModels, // Loads all available model configurations from JSON files
getModelById, // Retrieves specific model configuration by ID
getFilteredModels, // Filters models based on criteria (cost, quality, etc.)
getModelStats, // Provides statistical analysis of available models
getModelRecommendations // Suggests optimal models based on user requirements
} from './core/model-manager.js';
import { retrieveApiKey, hasStoredApiKey, validateApiKey } from './secure-storage.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Load environment variables from .env file
// This ensures API keys and configuration are available throughout the application
dotenv.config();
// Initialize MCP Server with enhanced capabilities
// This creates the main server instance that will handle all MCP protocol communications
// The server provides tools, resources, and prompts to AI assistants
const server = new Server({
name: 'fal-cli-mcp',
version: '2.0.0',
description: 'Enhanced FAL AI image generation MCP server with advanced features'
}, {
capabilities: {
tools: {}, // Image generation, model management, and optimization tools
resources: {}, // Access to model configurations and generated images
prompts: {} // Pre-built prompt templates for common use cases
}
});
// ===== COST MANAGEMENT SYSTEM =====
// Implements spending protection to prevent accidental high-cost operations
// Users must explicitly confirm any operation that would cost more than $5 USD
const SPENDING_LIMIT = 5.0; // Maximum allowed spending without explicit confirmation
/**
* Calculate estimated cost for a generation request
*
* This function looks up the model's pricing information and calculates
* the total cost based on the number of images to be generated.
*
* @param {string} model - The model ID (e.g., 'fal-ai/flux-pro/kontext/text-to-image')
* @param {number} numImages - Number of images to generate (default: 1)
* @returns {Promise<number>} Estimated cost in USD
*/
async function calculateGenerationCost(model, numImages = 1) {
try {
// Load all available model configurations from the models/ directory
const models = await loadModels();
const modelConfig = models.find(m => m.id === model);
if (!modelConfig || !modelConfig.costPerImage) {
// Return a conservative estimate if model pricing is unknown
// This ensures we don't underestimate costs for new or unlisted models
return 0.05 * numImages; // $0.05 per image conservative estimate
}
return modelConfig.costPerImage * numImages;
} catch (error) {
console.warn('Could not calculate cost:', error.message);
return 0.05 * numImages; // Conservative fallback to protect users
}
}
/**
* Check if operation exceeds spending limit and requires user confirmation
*
* This is a safety mechanism to prevent accidental expensive operations.
* Any operation costing more than $5 USD requires explicit user confirmation.
*
* @param {number} estimatedCost - Estimated cost in USD
* @returns {boolean} True if confirmation is required
*/
function requiresSpendingConfirmation(estimatedCost) {
return estimatedCost > SPENDING_LIMIT;
}
/**
* Create a structured error for spending limit confirmation
*
* When an operation would exceed the spending limit, this function creates
* a detailed error message with cost breakdown and instructions for the user.
*
* @param {number} estimatedCost - Total estimated cost in USD
* @param {string} model - Model ID being used
* @param {number} numImages - Number of images to generate
* @returns {Error} Structured error with confirmation details
*/
function createSpendingConfirmationError(estimatedCost, model, numImages) {
return new Error(JSON.stringify({
type: 'SPENDING_CONFIRMATION_REQUIRED',
estimated_cost: estimatedCost,
spending_limit: SPENDING_LIMIT,
model: model,
num_images: numImages,
message: `This operation will cost approximately $${estimatedCost.toFixed(3)}, which exceeds the $${SPENDING_LIMIT} spending limit. Please confirm if you want to proceed.`,
confirmation_needed: true
}));
}
// ===== ERROR HANDLING SYSTEM =====
/**
* Enhanced error handling wrapper with detailed error context and performance tracking
*
* This wrapper provides comprehensive error handling for all MCP tool operations:
* - Tracks execution time for performance monitoring
* - Adds metadata to successful responses
* - Provides contextual error suggestions
* - Logs detailed error information for debugging
*
* @param {Function} handler - The async function to wrap with error handling
* @param {string} toolName - Name of the tool for error context (default: 'unknown')
* @returns {Function} Wrapped function with enhanced error handling
*/
function handleErrors(handler, toolName = 'unknown') {
return async (...args) => {
const startTime = Date.now();
try {
const result = await handler(...args);
const duration = Date.now() - startTime;
// Add performance metadata to successful responses
// This helps with monitoring and debugging performance issues
if (result && typeof result === 'object') {
result._metadata = {
...result._metadata,
tool: toolName,
duration: duration,
timestamp: new Date().toISOString(),
server_version: '2.0.0'
};
}
return result;
} catch (error) {
const duration = Date.now() - startTime;
// Log comprehensive error information for debugging
console.error(`MCP Server Error [${toolName}]:`, {
error: error.message,
stack: error.stack?.split('\n').slice(0, 3).join('\n'), // Truncated stack trace
duration: duration,
timestamp: new Date().toISOString(),
args: args.length > 0 ? JSON.stringify(args[0]).substring(0, 200) : 'no args'
});
// Create enhanced error response with helpful context for users
throw new Error(JSON.stringify({
error: error.message,
tool: toolName,
duration: duration,
timestamp: new Date().toISOString(),
suggestion: getSuggestionForError(error, toolName)
}));
}
};
}
// Get helpful suggestions based on error type
function getSuggestionForError(error, toolName) {
const message = error.message.toLowerCase();
if (message.includes('api key') || message.includes('unauthorized')) {
return 'Check your FAL_KEY environment variable or run the CLI setup command';
}
if (message.includes('model') && message.includes('not found')) {
return 'Use list_models tool to see available models';
}
if (message.includes('prompt') && message.includes('empty')) {
return 'Provide a non-empty prompt for image generation';
}
if (message.includes('parameter') || message.includes('validation')) {
return 'Check the model configuration for valid parameter ranges';
}
return 'Check the tool documentation or try again with different parameters';
}
// ===== API KEY MANAGEMENT =====
/**
* Enhanced API key management with validation
*
* This function retrieves and validates the FAL API key from multiple sources:
* 1. Environment variable (FAL_KEY) - preferred for production
* 2. Secure storage - encrypted local storage for development
*
* The function also validates the API key format to catch configuration errors early.
*
* @returns {Promise<string>} Valid FAL API key
* @throws {Error} If no API key is found or format is invalid
*/
async function getApiKey() {
let apiKey = null;
// Try to get from environment first (preferred method)
if (process.env.FAL_KEY) {
apiKey = process.env.FAL_KEY;
} else if (await hasStoredApiKey()) {
// Try to get from secure storage (development fallback)
apiKey = await retrieveApiKey();
}
if (!apiKey) {
throw new Error('No FAL API key found. Set FAL_KEY environment variable or configure via CLI.');
}
// Enhanced API key validation to catch format errors early
if (!validateApiKeyFormat(apiKey)) {
throw new Error('Invalid API key format. Expected format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
}
return apiKey;
}
/**
* Validate API key format
*
* FAL API keys follow a specific format: UUID:TOKEN
* This function validates both parts to ensure the key is properly formatted.
*
* @param {string} apiKey - The API key to validate
* @returns {boolean} True if the API key format is valid
*/
function validateApiKeyFormat(apiKey) {
if (!apiKey || typeof apiKey !== 'string') return false;
// FAL API keys have format: uuid:token (two parts separated by colon)
const parts = apiKey.split(':');
if (parts.length !== 2) return false;
const [uuid, token] = parts;
// Validate UUID format (8-4-4-4-12 hexadecimal pattern)
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!uuidRegex.test(uuid)) return false;
// Token should be at least 32 characters (security requirement)
if (token.length < 32) return false;
return true;
}
// TOOLS IMPLEMENTATION
server.setRequestHandler(ListToolsRequestSchema, handleErrors(async () => {
return {
tools: [
{
name: 'generate_image',
description: 'Generate high-quality images using advanced FAL AI models with intelligent parameter optimization',
inputSchema: {
type: 'object',
properties: {
prompt: {
type: 'string',
description: 'Detailed text prompt for image generation. Be specific about style, lighting, composition, and details for best results.',
minLength: 3,
maxLength: 2000,
examples: [
'A majestic mountain landscape with golden hour lighting, photorealistic',
'Portrait of a wise elderly wizard with flowing robes, fantasy art style, detailed'
]
},
model: {
type: 'string',
description: 'FAL AI model ID. Use get_model_recommendations for optimal model selection.',
default: 'fal-ai/flux-pro/kontext/text-to-image',
examples: [
'fal-ai/flux-pro/v1.1-ultra',
'fal-ai/flux-pro/kontext/text-to-image',
'fal-ai/imagen4/preview/ultra'
]
},
parameters: {
type: 'object',
description: 'Advanced model-specific parameters with intelligent defaults. Leave empty for auto-optimization.',
properties: {
aspect_ratio: {
type: 'string',
description: 'Image aspect ratio for optimal composition',
enum: ['1:1', '4:3', '3:4', '16:9', '9:16', '21:9', '9:21'],
default: '16:9'
},
guidance_scale: {
type: 'number',
description: 'Prompt adherence strength (1.0-20.0). Higher = closer to prompt, lower = more creative',
minimum: 1.0,
maximum: 20.0,
default: 3.5
},
num_images: {
type: 'number',
description: 'Number of variations to generate (1-4). Higher count = more options but higher cost',
minimum: 1,
maximum: 4,
default: 1
},
num_inference_steps: {
type: 'number',
description: 'Quality vs speed tradeoff (4-50). Higher = better quality but slower',
minimum: 4,
maximum: 50,
default: 28
},
seed: {
type: 'number',
description: 'Random seed for reproducible results (0-4294967295). Leave empty for random',
minimum: 0,
maximum: 4294967295
}
}
},
save_to_disk: {
type: 'boolean',
description: 'Save images locally with organized folder structure and metadata',
default: false
},
output_directory: {
type: 'string',
description: 'Custom output directory path. Creates organized subfolders by date and model if not specified.'
},
confirm_spending: {
type: 'boolean',
description: 'Confirm spending for operations over $5 USD. Required when estimated cost exceeds spending limit.',
default: false
}
},
required: ['prompt'],
additionalProperties: false
}
},
{
name: 'list_models',
description: 'List available FAL AI models with intelligent filtering and performance metadata',
inputSchema: {
type: 'object',
properties: {
type: {
type: 'string',
description: 'Filter by generation type for targeted results',
enum: ['image', 'video', 'audio', 'text'],
examples: ['image']
},
provider: {
type: 'string',
description: 'Filter by AI provider for specific capabilities',
examples: ['fal-ai', 'anthropic', 'openai']
},
max_cost: {
type: 'number',
description: 'Maximum cost per generation (USD). Helps find budget-friendly options',
minimum: 0.001,
maximum: 1.0,
examples: [0.05, 0.1]
},
quality: {
type: 'string',
description: 'Quality preference for model recommendations',
enum: ['fast', 'balanced', 'high', 'ultra']
}
},
additionalProperties: false
}
},
{
name: 'get_model_info',
description: 'Get comprehensive model information including capabilities, pricing, and optimization tips',
inputSchema: {
type: 'object',
properties: {
model_id: {
type: 'string',
description: 'Model identifier'
}
},
required: ['model_id']
}
},
{
name: 'optimize_prompt',
description: 'Optimize a prompt for better image generation results',
inputSchema: {
type: 'object',
properties: {
prompt: {
type: 'string',
description: 'Original prompt to optimize'
},
model: {
type: 'string',
description: 'Target model for optimization',
default: 'fal-ai/flux-pro/kontext/text-to-image'
},
style: {
type: 'string',
enum: ['detailed', 'artistic', 'technical', 'creative'],
description: 'Optimization style preference',
default: 'detailed'
},
max_length: {
type: 'number',
description: 'Maximum length of optimized prompt',
default: 500
}
},
required: ['prompt']
}
},
{
name: 'batch_generate',
description: 'Generate images with multiple prompts and/or models',
inputSchema: {
type: 'object',
properties: {
tasks: {
type: 'array',
description: 'Array of generation tasks',
items: {
type: 'object',
properties: {
prompt: { type: 'string' },
model: { type: 'string' },
parameters: { type: 'object' }
},
required: ['prompt', 'model']
}
},
batch_size: {
type: 'number',
description: 'Number of concurrent generations',
default: 1,
minimum: 1,
maximum: 5
},
output_directory: {
type: 'string',
description: 'Directory to save all generated images'
}
},
required: ['tasks']
}
},
{
name: 'calculate_cost',
description: 'Calculate estimated cost for generation tasks',
inputSchema: {
type: 'object',
properties: {
tasks: {
type: 'array',
description: 'Array of generation tasks',
items: {
type: 'object',
properties: {
model: { type: 'string' },
image_count: { type: 'number', default: 1 }
},
required: ['model']
}
}
},
required: ['tasks']
}
},
{
name: 'get_model_recommendations',
description: 'Get model recommendations based on criteria',
inputSchema: {
type: 'object',
properties: {
budget: {
type: 'number',
description: 'Maximum budget per image'
},
type: {
type: 'string',
description: 'Preferred model type'
},
quality: {
type: 'string',
enum: ['high', 'medium', 'fast'],
description: 'Quality preference',
default: 'high'
},
speed: {
type: 'string',
enum: ['fast', 'medium', 'slow'],
description: 'Speed preference',
default: 'medium'
}
}
}
}
]
};
}));
server.setRequestHandler(CallToolRequestSchema, handleErrors(async (request) => {
const { name, arguments: args } = request.params;
switch (name) {
case 'generate_image': {
const apiKey = await getApiKey();
const {
prompt,
model = 'fal-ai/flux-pro/kontext/text-to-image',
parameters = {},
save_to_disk = false,
output_directory = null,
confirm_spending = false
} = args;
// Enhanced parameter validation
if (!prompt || prompt.trim().length < 3) {
throw new Error('Prompt must be at least 3 characters long');
}
if (prompt.length > 2000) {
throw new Error('Prompt must be less than 2000 characters');
}
// Calculate cost and check spending limit
const numImages = parameters.num_images || 1;
const estimatedCost = await calculateGenerationCost(model, numImages);
if (requiresSpendingConfirmation(estimatedCost) && !confirm_spending) {
throw createSpendingConfirmationError(estimatedCost, model, numImages);
}
// Intelligent output directory creation
const outputDir = save_to_disk ?
(output_directory || path.join(process.cwd(), 'generated-images',
new Date().toISOString().split('T')[0],
model.replace(/[^a-zA-Z0-9]/g, '_'))) :
null;
const result = await generateSingleImage({
model,
prompt,
parameters,
apiKey,
outputDir,
generationId: `gen_${Date.now()}_${Math.random().toString(36).substr(2, 5)}`
});
// Enhanced response with inline image display for visual preview
const imageDisplays = result.images ? result.images.map((url, index) => {
return ``;
}).join('\n\n') : '';
const displayText = `🎨 **Image Generated Successfully!**\n\n${imageDisplays}\n\n📋 **Details:**\n- **Model:** ${result.model}\n- **Prompt:** "${result.prompt}"\n- **Duration:** ${result.duration}ms\n- **Generation ID:** ${result.generationId}\n- **Images:** ${result.images?.length || 0}`;
return {
content: [
{
type: 'text',
text: displayText
}
],
success: result.success,
model: result.model,
prompt: result.prompt,
images: result.images || [],
parameters: result.parameters || {},
duration: result.duration,
generationId: result.generationId,
timestamp: result.timestamp,
metadata: {
...result.metadata,
prompt_length: prompt.length,
model_type: model.includes('ultra') ? 'ultra_quality' : 'standard',
estimated_cost: result.metadata?.estimated_cost || 'unknown'
}
};
}
case 'list_models': {
const { type, provider, max_cost, quality } = args || {};
// Load models with enhanced filtering
// Only pass filters if they're actually provided
const filters = {};
if (type) filters.type = type;
if (provider) filters.provider = provider;
if (max_cost) filters.maxCost = max_cost;
let models = await getFilteredModels(filters);
// Apply quality-based intelligent sorting
if (quality) {
models = models.sort((a, b) => {
switch (quality) {
case 'fast': return (a.defaultParams?.num_inference_steps || 28) - (b.defaultParams?.num_inference_steps || 28);
case 'high': case 'ultra': return (b.costPerImage || 0) - (a.costPerImage || 0);
case 'balanced': default: {
const scoreA = (a.costPerImage || 0) * (a.defaultParams?.num_inference_steps || 28);
const scoreB = (b.costPerImage || 0) * (b.defaultParams?.num_inference_steps || 28);
return scoreA - scoreB;
}
}
});
}
// Enhanced response with intelligent insights
const response = {
models: models.map(model => ({
id: model.id,
name: model.name,
description: model.description,
category: model.category || 'Standard',
costPerImage: model.costPerImage,
defaultParams: model.defaultParams,
supportedAspectRatios: model.supportedAspectRatios,
maxImages: model.maxImages,
recommendationScore: model.recommendationScore || 0,
configFile: model.configFile,
lastModified: model.lastModified
})),
metadata: {
total_models: models.length,
filters_applied: { type, provider, max_cost, quality },
average_cost: models.length > 0 ?
(models.reduce((sum, m) => sum + (m.costPerImage || 0), 0) / models.length).toFixed(4) : 0,
quality_distribution: {
ultra: models.filter(m => m.category === 'Ultra Quality').length,
professional: models.filter(m => m.category === 'Professional').length,
standard: models.filter(m => !m.category || m.category === 'Standard').length
},
recommended_for_beginners: models.filter(m =>
(m.costPerImage || 0) < 0.05 &&
(m.defaultParams?.num_inference_steps || 28) <= 28
).slice(0, 3).map(m => m.id)
}
};
return {
content: [
{
type: 'text',
text: JSON.stringify(response, null, 2)
}
]
};
}
case 'get_model_info': {
const { model_id } = args;
const model = await getModelById(model_id);
if (!model) {
throw new Error(`Model not found: ${model_id}`);
}
return {
content: [
{
type: 'text',
text: JSON.stringify(model, null, 2)
}
]
};
}
case 'optimize_prompt': {
const apiKey = await getApiKey();
const {
prompt,
model = 'fal-ai/flux-pro/kontext/text-to-image',
style = 'detailed',
max_length = 500
} = args;
const result = await optimizePrompt(prompt, model, apiKey, {
style,
maxLength: max_length
});
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2)
}
]
};
}
case 'batch_generate': {
const apiKey = await getApiKey();
const {
tasks,
batch_size = 1,
output_directory = null,
confirm_spending = false
} = args;
// Calculate total cost for all tasks
let totalEstimatedCost = 0;
for (const task of tasks) {
const numImages = task.parameters?.num_images || 1;
const taskCost = await calculateGenerationCost(task.model, numImages);
totalEstimatedCost += taskCost;
}
// Check spending limit for batch operation
if (requiresSpendingConfirmation(totalEstimatedCost) && !confirm_spending) {
throw new Error(JSON.stringify({
type: 'SPENDING_CONFIRMATION_REQUIRED',
estimated_cost: totalEstimatedCost,
spending_limit: SPENDING_LIMIT,
batch_tasks: tasks.length,
message: `This batch operation will cost approximately $${totalEstimatedCost.toFixed(3)}, which exceeds the $${SPENDING_LIMIT} spending limit. Please confirm if you want to proceed.`,
confirmation_needed: true
}));
}
const outputDir = output_directory ||
path.join(process.cwd(), 'generated-images', Date.now().toString());
const results = await generateBatch(tasks, apiKey, {
outputDir,
batchSize: batch_size,
generationId: Date.now().toString()
});
// Create visual display for all generated images
let allImageDisplays = [];
let taskIndex = 1;
results.forEach((result, index) => {
if (result.images && result.images.length > 0) {
const taskImages = result.images.map((url, imgIndex) => {
return ``;
}).join('\n');
allImageDisplays.push(`**Task ${taskIndex}: ${result.model}**\n${result.prompt}\n${taskImages}`);
taskIndex++;
}
});
const displayText = `🎨 **Batch Generation Completed!**\n\n${allImageDisplays.join('\n\n---\n\n')}\n\n📋 **Batch Summary:**\n- **Total Tasks:** ${tasks.length}\n- **Estimated Cost:** $${totalEstimatedCost.toFixed(3)}\n- **Output Directory:** ${outputDir}\n- **Batch Size:** ${batch_size}`;
return {
content: [
{
type: 'text',
text: displayText
}
],
success: true,
total_tasks: tasks.length,
estimated_cost: totalEstimatedCost,
spending_confirmed: confirm_spending,
results: results,
metadata: {
batch_size: batch_size,
output_directory: outputDir,
timestamp: new Date().toISOString()
}
};
}
case 'calculate_cost': {
const { tasks } = args;
const models = await loadModels();
const costEstimate = calculateCost(tasks, models);
return {
content: [
{
type: 'text',
text: JSON.stringify(costEstimate, null, 2)
}
]
};
}
case 'get_model_recommendations': {
const { budget, type, quality = 'high', speed = 'medium' } = args;
const recommendations = await getModelRecommendations({
budget,
type,
quality,
speed
});
return {
content: [
{
type: 'text',
text: JSON.stringify(recommendations, null, 2)
}
]
};
}
default:
throw new Error(`Unknown tool: ${name}`);
}
}));
// RESOURCES IMPLEMENTATION
server.setRequestHandler(ListResourcesRequestSchema, handleErrors(async () => {
const modelsDir = path.join(__dirname, 'models');
const generatedDir = path.join(__dirname, 'generated-images');
const resources = [];
// Add model configurations as resources
try {
const modelFiles = await fs.readdir(modelsDir);
for (const file of modelFiles.filter(f => f.endsWith('.json'))) {
resources.push({
uri: `file://models/${file}`,
name: `Model Config: ${file.replace('.json', '')}`,
description: `Configuration for ${file.replace('.json', '')} model`,
mimeType: 'application/json'
});
}
} catch (error) {
// Models directory might not exist
}
// Add generated images as resources (if directory exists)
try {
if (await fs.pathExists(generatedDir)) {
const sessions = await fs.readdir(generatedDir);
for (const session of sessions) {
const sessionPath = path.join(generatedDir, session);
if ((await fs.stat(sessionPath)).isDirectory()) {
resources.push({
uri: `file://generated-images/${session}`,
name: `Generated Images: ${session}`,
description: `Images generated in session ${session}`,
mimeType: 'application/json'
});
}
}
}
} catch (error) {
// Generated images directory might not exist
}
// Add model statistics as a resource
resources.push({
uri: 'fal://model-stats',
name: 'Model Statistics',
description: 'Statistics and summary of all available models',
mimeType: 'application/json'
});
return { resources };
}));
server.setRequestHandler(ReadResourceRequestSchema, handleErrors(async (request) => {
const { uri } = request.params;
if (uri.startsWith('file://models/')) {
const filename = uri.replace('file://models/', '');
const filepath = path.join(__dirname, 'models', filename);
if (await fs.pathExists(filepath)) {
const content = await fs.readFile(filepath, 'utf8');
return {
contents: [
{
uri,
mimeType: 'application/json',
text: content
}
]
};
}
} else if (uri.startsWith('file://generated-images/')) {
const sessionPath = uri.replace('file://generated-images/', '');
const dirPath = path.join(__dirname, 'generated-images', sessionPath);
if (await fs.pathExists(dirPath)) {
const files = await fs.readdir(dirPath, { recursive: true });
const imageFiles = files.filter(f =>
f.endsWith('.jpg') || f.endsWith('.png') || f.endsWith('.webp')
);
const manifest = {
session: sessionPath,
generatedAt: (await fs.stat(dirPath)).birthtime,
totalImages: imageFiles.length,
images: imageFiles.map(file => ({
filename: file,
path: path.join(dirPath, file),
size: null // Could add file size here
}))
};
return {
contents: [
{
uri,
mimeType: 'application/json',
text: JSON.stringify(manifest, null, 2)
}
]
};
}
} else if (uri === 'fal://model-stats') {
const stats = await getModelStats();
return {
contents: [
{
uri,
mimeType: 'application/json',
text: JSON.stringify(stats, null, 2)
}
]
};
}
throw new Error(`Resource not found: ${uri}`);
}));
// PROMPTS IMPLEMENTATION
server.setRequestHandler(ListPromptsRequestSchema, handleErrors(async () => {
return {
prompts: [
{
name: 'generate_artistic_image',
description: 'Generate an artistic image with optimized prompts',
arguments: [
{
name: 'subject',
description: 'Main subject of the image',
required: true
},
{
name: 'style',
description: 'Artistic style (e.g., oil painting, digital art)',
required: false
},
{
name: 'mood',
description: 'Mood or atmosphere',
required: false
}
]
},
{
name: 'generate_photorealistic_image',
description: 'Generate a photorealistic image with proper technical terms',
arguments: [
{
name: 'subject',
description: 'Main subject to photograph',
required: true
},
{
name: 'location',
description: 'Location or setting',
required: false
},
{