/* * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js"; import { Variables } from "@modelcontextprotocol/sdk/shared/uriTemplate.js"; import { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js"; import { OppulenceBackendSDKCore } from "../core.js"; import { Result } from "../types/fp.js"; import { MCPScope } from "./scopes.js"; import { isAsyncIterable, isBinaryData, valueToBase64 } from "./shared.js"; export type ReadResourceCallback = ( client: OppulenceBackendSDKCore, uri: URL, extra: RequestHandlerExtra, ) => ReadResourceResult | Promise; export type ResourceDefinition = { name: string; description: string; scopes?: MCPScope[]; resource: string; read: ReadResourceCallback; }; export type ReadResourceTemplateCallback = ( client: OppulenceBackendSDKCore, uri: URL, vars: Variables, extra: RequestHandlerExtra, ) => ReadResourceResult | Promise; export type ResourceTemplateDefinition = { name: string; description: string; scopes?: MCPScope[]; resource: ResourceTemplate; read: ReadResourceTemplateCallback; }; export async function formatResult( result: Result, uri: URL, init: { response?: Response | undefined }, ): Promise { if (!result.ok) { throw result.error; } const { value } = result; if (typeof value === "undefined") { return { contents: [] }; } const { response } = init; const mimeType = response?.headers.get("content-type") ?? ""; let contents: ReadResourceResult["contents"] = []; if (mimeType.search(/\bjson\b/g)) { contents = [{ uri: uri.toString(), mimeType, text: JSON.stringify(value) }]; } else if ( mimeType.startsWith("text/event-stream") && isAsyncIterable(value) ) { contents = [ { uri: uri.toString(), mimeType: "application/json", text: await stringifySSEToJSON(value), }, ]; } else if (mimeType.startsWith("text/") && typeof value === "string") { contents = [{ uri: uri.toString(), mimeType, text: value }]; } else if (isBinaryData(value)) { const blob = await valueToBase64(value); contents = blob == null ? [] : [{ uri: uri.toString(), blob, mimeType }]; } else { throw new Error(`Unsupported content type: "${mimeType}"`); } return { contents }; } async function stringifySSEToJSON( value: AsyncIterable, ): Promise { const payloads = []; for await (const chunk of value) { payloads.push(chunk); } return JSON.stringify(payloads); }