Skip to main content
Glama
formatters.ts1.31 kB
import { XMLBuilder } from 'fast-xml-parser'; /** * Formats a response by combining a message with structured data. * Converts the provided object to XML format and appends the message. */ export function formatResponse( message: string | null, object: Record<string, unknown> = {}, ): string { const formattedObject: Record<string, unknown> = {}; for (const [key, value] of Object.entries(object)) { if (!value) continue; formattedObject[key] = value; } const builder = new XMLBuilder({ format: true, indentBy: ' ', suppressEmptyNode: true, ignoreAttributes: false, processEntities: false, }); const contextString = builder.build(formattedObject); return `${message}: \n${contextString}\n`; } /** * Ensures that a value is an array. */ export function ensureArray<T>(value: T | T[] | null | undefined): T[] { if (value === null || value === undefined) { return []; } return Array.isArray(value) ? value : [value]; } /** * Pluralizes a string based on a count. * Returns singular form for count of 1, plural form otherwise. */ export function pluralize( n: number | unknown[], singular: string, plural?: string, ): string { const num = Array.isArray(n) ? n.length : n; return num === 1 ? singular : plural || `${singular}s`; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/contentful/contentful-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server