UNPKG

@smartbear/mcp

Version:

MCP server for interacting SmartBear Products

142 lines (141 loc) 5.22 kB
import { z } from "zod"; import { MCP_SERVER_NAME, MCP_SERVER_VERSION } from "../common/info.js"; // Apply backward compatibility for API_HUB_API_KEY import "./config-utils.js"; import { SwaggerAPI, SwaggerConfiguration, TOOLS, } from "./client/index.js"; const ConfigurationSchema = z.object({ api_key: z.string().describe("Swagger API key for authentication"), }); // Tool definitions for API Hub API client export class SwaggerClient { api; name = "Swagger"; toolPrefix = "swagger"; configPrefix = "Swagger"; config = ConfigurationSchema; async configure(_server, config, _cache) { this.api = new SwaggerAPI(new SwaggerConfiguration({ token: config.api_key }), `${MCP_SERVER_NAME}/${MCP_SERVER_VERSION}`); return true; } getApi() { if (!this.api) throw new Error("Client not configured"); return this.api; } // Delegate API methods to the SwaggerAPI instance async getPortals() { return this.getApi().getPortals(); } async createPortal(body) { return this.getApi().createPortal(body); } async getPortal(args) { return this.getApi().getPortal(args.portalId); } async updatePortal(args) { const { portalId, ...body } = args; return this.getApi().updatePortal(portalId, body); } async getPortalProducts(args) { return this.getApi().getPortalProducts(args.portalId); } async createPortalProduct(args) { const { portalId, ...body } = args; return this.getApi().createPortalProduct(portalId, body); } async getPortalProduct(args) { return this.getApi().getPortalProduct(args.productId); } async deletePortalProduct(args) { return this.getApi().deletePortalProduct(args.productId); } async updatePortalProduct(args) { const { productId, ...body } = args; return this.getApi().updatePortalProduct(productId, body); } async publishPortalProduct(args) { const { productId, preview } = args; return this.getApi().publishPortalProduct(productId, preview); } async getPortalProductSections(args) { const { productId, ...params } = args; return this.getApi().getPortalProductSections(productId, params); } async createTableOfContents(args) { const { sectionId, ...body } = args; return this.getApi().createTableOfContents(sectionId, body); } async getTableOfContents(args) { return this.getApi().getTableOfContents(args); } async getDocument(args) { return this.getApi().getDocument(args); } async updateDocument(args) { return this.getApi().updateDocument(args); } async deleteTableOfContents(args) { return this.getApi().deleteTableOfContents(args); } // Registry API methods for SwaggerHub Design functionality async searchApis(args = {}) { return this.getApi().searchApis(args); } async getApiDefinition(args) { return this.getApi().getApiDefinition(args); } async createOrUpdateApi(args) { return this.getApi().createOrUpdateApi(args); } async createApiFromTemplate(args) { return this.getApi().createApiFromTemplate(args); } // User Management API methods async getOrganizations(args) { return this.getApi().getOrganizations(args); } async createApiFromPrompt(args) { return this.getApi().createApiFromPrompt(args); } async scanStandardization(args) { return this.getApi().scanStandardization(args); } async standardizeApi(args) { return this.getApi().standardizeApi(args); } registerTools(register, _getInput) { TOOLS.forEach((tool) => { const { handler, formatResponse, ...toolParams } = tool; register(toolParams, async (args, _extra) => { try { // Dynamic method invocation const handlerFn = this[handler]; if (typeof handlerFn !== "function") { throw new Error(`Handler '${handler}' not found on SwaggerClient`); } const result = await handlerFn.call(this, args); // Use custom formatter if available, otherwise return JSON const formattedResult = formatResponse ? formatResponse(result) : result; const responseText = typeof formattedResult === "string" ? formattedResult : JSON.stringify(formattedResult); return { content: [{ type: "text", text: responseText }], }; } catch (error) { return { content: [ { type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }); }); } }