openapi-directory-mcp
Version:
Model Context Protocol server for accessing enhanced triple-source OpenAPI directory (APIs.guru + additional APIs + custom imports)
79 lines • 2.36 kB
JavaScript
import { getAllPrompts } from "./templates.js";
export class PromptHandler {
constructor() {
this.prompts = new Map();
this.initialized = false;
// Initialize prompts asynchronously
this.initializePrompts().catch(console.error);
}
async initializePrompts() {
const allPrompts = await getAllPrompts();
allPrompts.forEach((prompt) => {
this.prompts.set(prompt.name, prompt);
});
this.initialized = true;
}
async ensureInitialized() {
if (!this.initialized) {
await this.initializePrompts();
}
}
/**
* Handle the prompts/list request
*/
async listPrompts() {
await this.ensureInitialized();
const prompts = Array.from(this.prompts.values()).map((template) => ({
name: template.name,
description: template.description,
arguments: template.arguments || [],
}));
return { prompts };
}
/**
* Handle the prompts/get request
*/
async getPrompt(request) {
await this.ensureInitialized();
const template = this.prompts.get(request.name);
if (!template) {
throw new Error(`Prompt not found: ${request.name}`);
}
// Validate required arguments
if (template.arguments) {
for (const arg of template.arguments) {
if (arg.required &&
(!request.arguments || !(arg.name in request.arguments))) {
throw new Error(`Missing required argument: ${arg.name}`);
}
}
}
const messages = template.generateMessages(request.arguments || {});
return {
description: template.description,
messages,
};
}
/**
* Get all prompt names
*/
async getPromptNames() {
await this.ensureInitialized();
return Array.from(this.prompts.keys());
}
/**
* Check if a prompt exists
*/
async hasPrompt(name) {
await this.ensureInitialized();
return this.prompts.has(name);
}
/**
* Get prompt template by name
*/
async getPromptTemplate(name) {
await this.ensureInitialized();
return this.prompts.get(name);
}
}
//# sourceMappingURL=handler.js.map