UNPKG

@civic/nexus-bridge

Version:

Stdio <-> HTTP/SSE MCP bridge with Civic auth handling

77 lines 2.56 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Utility functions used throughout the nexus bridge. */ import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema, ListResourcesResultSchema, ListPromptsResultSchema, } from "@modelcontextprotocol/sdk/types.js"; import { logger } from "./utils/logger.js"; /** * Maps MCP method names to their corresponding result schemas and processors */ export const METHOD_HANDLERS = { "tools/call": { schema: CallToolResultSchema, process: async (result) => result, // Will be processed by serviceAuthorizationHandler }, "tools/list": { schema: ListToolsResultSchema, process: async (result) => result, // Local tools added in Bridge.handleLocalRequest defaultResult: { tools: [] } }, "resources/list": { schema: ListResourcesResultSchema, process: async (result) => result, // No special processing defaultResult: { resources: [] } }, "prompts/list": { schema: ListPromptsResultSchema, process: async (result) => result, // No special processing defaultResult: { prompts: [] } }, }; /** * Check if a method is supported by the bridge * @param method The method name to check * @returns true if the method is supported, false otherwise */ export const isSupportedMethod = (method) => { return method in METHOD_HANDLERS; }; /** * Get the handler for a given MCP method * @param method The MCP method * @returns The handler for the method, or undefined if not supported */ export const getHandlerForMethod = (method) => { logger.info(`Getting handler for method: ${method}`); // Simply check if we have a handler for this method if (isSupportedMethod(method)) { return METHOD_HANDLERS[method]; } return undefined; }; /** * Extract a human-readable message from an error of any type * @param error The error to extract a message from * @returns A string representing the error message */ export const messageFromError = (error) => { if (error instanceof Error) return error.message; if (typeof error === "string") return error; try { return JSON.stringify(error); } catch { return String(error); } }; /** * Type guard using Zod schema */ export const is = (schema) => (data) => schema.safeParse(data).success; /** * Check if a request is a tool call request using Zod schema */ export const isToolCallRequest = is(CallToolRequestSchema); //# sourceMappingURL=utils.js.map