import {z} from 'zod' export type ToolCallParams = {name: 'att-fiber-coverage-checker' | 'att-fiber-plan-recommender' | 'att-fiber-journey-data' | 'att-device-shop' | 'att-plan-shop' | 'att-addons-accessories' | 'att-cart-checkout' | 'att-widget-api' | 'att-store-finder' | 'att-address-autocomplete' | 'att-address-validate', arguments?: {[key: string]: unknown} | undefined}; export type GenericParams = {[key: string]: unknown}; export type JsonRpcRequest = {jsonrpc: '2.0', id?: (string | number) | undefined, method: 'initialize' | 'notifications/initialized' | 'tools/list' | 'tools/call', params?: (ToolCallParams | GenericParams) | undefined}; export type CreateApiChatgptappsMcpArgs = {body: JsonRpcRequest}; export type CreateApiChatgptappsMcpOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const jsonRpcError = z.object({code: z.number().int(), message: z.string(), data: z.unknown()}).and(z.record(z.string(), z.unknown())); export const jsonRpcResponse = z.object({jsonrpc: z.literal("2.0"), id: z.union([z.string(), z.number().int()]).nullable(), result: z.record(z.string(), z.unknown()).optional(), error: jsonRpcError.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Send an MCP JSON-RPC request * * POST /chatgptapps/mcp */ export const createApiChatgptappsMcp = async (args: CreateApiChatgptappsMcpArgs, options?: CreateApiChatgptappsMcpOptions) => { const {baseUrl = "https://myhomenetwork.att.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/chatgptapps/mcp${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(jsonRpcResponse, await res.json()) };