UNPKG

firebase-tools

Version:
77 lines (76 loc) 2.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OneMcpServer = void 0; const types_js_1 = require("@modelcontextprotocol/sdk/types.js"); const apiv2_1 = require("../../apiv2"); const error_1 = require("../../error"); const ensureApiEnabled_1 = require("../../ensureApiEnabled"); class OneMcpServer { constructor(feature, serverUrl, meta) { this.feature = feature; this.serverUrl = serverUrl; this.meta = meta; this.listClient = new apiv2_1.Client({ urlPrefix: this.serverUrl, auth: false, }); this.callClient = new apiv2_1.Client({ urlPrefix: this.serverUrl, auth: true, }); } async listTools() { try { const res = await this.listClient.post("/mcp", { method: "tools/list", jsonrpc: "2.0", id: 1, }); const parsed = types_js_1.ListToolsResultSchema.parse(res.body.result); return parsed.tools.map((mcpTool) => ({ mcp: { ...mcpTool, name: `${this.feature}_${mcpTool.name}`, _meta: { ...this.meta, feature: this.feature }, }, fn: (args, ctx) => this.callTool(mcpTool.name, args, ctx), isAvailable: () => Promise.resolve(true), })); } catch (error) { throw new error_1.FirebaseError("Failed to fetch remote tools for " + this.serverUrl + ": " + JSON.stringify(error)); } } async callTool(toolName, args, ctx) { await (0, ensureApiEnabled_1.ensure)(ctx.projectId, this.serverUrl, this.feature, true); try { const res = await this.callClient.post("/mcp", { method: "tools/call", params: { name: toolName, arguments: args, }, jsonrpc: "2.0", id: 1, }, ctx.projectId ? { headers: { "x-goog-user-project": ctx.projectId, }, } : {}); return types_js_1.CallToolResultSchema.parse(res.body.result); } catch (error) { if (error instanceof error_1.FirebaseError) { const firebaseError = error; const body = firebaseError.context?.body; if (body?.result?.isError) { return types_js_1.CallToolResultSchema.parse(body.result); } } throw error; } } } exports.OneMcpServer = OneMcpServer;