UNPKG

pollo-mcp

Version:

Pollo AI Model Context Protocol (MCP) Server for video generation

97 lines (96 loc) 4.01 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { CallToolRequestSchema, ListToolsRequestSchema, } from '@modelcontextprotocol/sdk/types.js'; import { z, ZodError } from 'zod'; import { jsonSchemaToZod } from 'json-schema-to-zod'; export default class MCPServer extends Server { constructor(options, tools) { super({ name: options.name, version: options.version }, { capabilities: { tools: {} } }); this.tools = tools; this.initialize(); } initialize() { this.setRequestHandler(ListToolsRequestSchema, () => __awaiter(this, void 0, void 0, function* () { return { tools: this.tools.map((tool) => ({ name: tool.getName(), description: tool.getDescription(), inputSchema: tool.getInputSchema(), })), }; })); this.setRequestHandler(CallToolRequestSchema, (request) => __awaiter(this, void 0, void 0, function* () { return this.callTool(request); })); } callTool(request) { return __awaiter(this, void 0, void 0, function* () { const tool = this.tools.find((tool) => tool.getName() === request.params.name); if (!tool) { return { content: [ { type: 'text', text: `Error: Unknown tool requested: ${request.params.name}`, }, ], }; } try { this.validateInput(tool, request); return tool.execute(request); } catch (error) { return { content: [ { type: 'text', text: error instanceof Error ? error.message : String(error), }, ], }; } }); } validateInput(tool, request) { try { this.getZodSchemaFromJsonSchema(tool.getInputSchema()).parse(request.params.arguments); } catch (error) { let message = ''; if (error instanceof ZodError) { message = `Invalid arguments for tool ${tool.getName()}: ${error.errors .map((e) => `${e.path.join('.')} (${e.code}): ${e.message}`) .join(', ')}`; } else { message = error instanceof Error ? error.message : String(error); } throw new Error(message); } } getZodSchemaFromJsonSchema(jsonSchema) { if (typeof jsonSchema !== 'object' || jsonSchema === null) { return z.object({}).passthrough(); } try { const zodSchemaString = jsonSchemaToZod(jsonSchema); const zodSchema = eval(zodSchemaString); if (typeof (zodSchema === null || zodSchema === void 0 ? void 0 : zodSchema.parse) !== 'function') { throw new Error('Eval did not produce a valid Zod schema.'); } return zodSchema; } catch (err) { return z.object({}).passthrough(); } } }