@ldavis9000aws/swarmui-generator
Version:
A Model Context Protocol server for SwarmUI image generation with TypeScript
115 lines • 4.97 kB
JavaScript
/**
* List Models MCP Tool
* * This file contains the MCP tool definition for listing available stable diffusion models.
*/
/**
* Input schema for the list_models tool.
*/
export const listModelsSchema = {
description: 'Lists available Stable Diffusion models and model folders from the SwarmUI server. Use this tool to discover what models can be used for image generation. It supports optional parameters: "path" to specify a directory (e.g., "Flux"), "depth" for search depth, "subtype" (e.g., "Stable-Diffusion", "LoRA"), "sortBy" ("Name" or "Date"), "sortReverse" (boolean), and "allowRemote" (boolean). If no parameters are given, it lists contents from the root model directory.',
type: 'object',
properties: {
path: {
type: 'string',
description: 'Optional. The specific folder path within the SwarmUI model directory to search (e.g., "Stable-Diffusion", "Flux"). If omitted, searches the root.'
},
depth: {
type: 'integer',
description: 'Optional. Maximum depth of subfolders to search recursively. Default is 1 (searches only the specified path).'
},
subtype: {
type: 'string',
description: 'Optional. Filter by a specific model subtype (e.g., "Stable-Diffusion", "LoRA"). If omitted, applies server default.'
},
sortBy: {
type: 'string',
description: 'Optional. Criterion to sort models: "Name" or "Date". Defaults to "Name".'
},
sortReverse: {
type: 'boolean',
description: 'Optional. If true, reverses sort order. Defaults to false.'
},
allowRemote: {
type: 'boolean',
description: 'Optional. If true, includes models from remote backends. Defaults to server setting.'
},
},
required: [] // All parameters are optional
};
/**
* Handler for the list_models tool
*/
export async function listModelsHandler(params, swarmui) {
try {
const response = await swarmui.listModels(params);
const models = response.files;
const folders = response.folders;
if (models.length === 0 && folders.length === 0) {
let message = 'No models or folders found';
if (params.path) {
message += ` in path "${params.path}"`;
}
message += ' with the current filter parameters. Try listing from the root or a different folder.';
return {
content: [{
type: 'text',
text: message
}],
_meta: { modelCount: 0, folderCount: 0 }
};
}
let resultText = "";
const searchPath = params.path || "root model directory";
const searchDepth = params.depth === undefined ? 1 : params.depth;
resultText += `Listing for path: "${searchPath}" (depth: ${searchDepth})\n`;
if (params.subtype)
resultText += `Subtype filter: "${params.subtype}"\n`;
resultText += "\n";
if (folders.length > 0) {
resultText += `Available Folders (${folders.length}):\n`;
folders.forEach(folder => {
resultText += `- [Folder] ${folder}\n`;
});
resultText += "\n";
}
else {
resultText += "No sub-folders found matching the criteria.\n\n";
}
if (models.length > 0) {
resultText += `Available Models (${models.length}):\n\n`;
models.forEach(model => {
resultText += `- Name: ${model.Name}\n`;
resultText += ` Display Name: ${model.FormattedName || model.Name}\n`;
if (model.Description)
resultText += ` Description: ${model.Description}\n`;
resultText += ` Default Dims: ${model.DefaultWidth || 'N/A'}x${model.DefaultHeight || 'N/A'}\n`;
resultText += ` Status: ${model.IsActive ? 'Active' : 'Inactive'}\n`;
if (model.Path) {
resultText += ` File Path: ${model.Path}\n`;
}
resultText += `\n`;
});
}
else {
resultText += "No model files found matching the criteria in this path.\n";
}
return {
content: [{
type: 'text',
text: resultText.trim()
}],
_meta: { modelCount: models.length, folderCount: folders.length }
};
}
catch (error) {
return {
content: [{
type: 'text',
text: `Error listing models: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true,
_meta: { error: error instanceof Error ? error.message : String(error) }
};
}
}
//# sourceMappingURL=list-models.js.map