import {z} from 'zod' export type FunctionCall = {name: string, arguments: string}; export type ToolCall = {id?: string | undefined, type?: 'function' | undefined, function: FunctionCall}; export type CreateApiV1McpToolExecuteArgs = {body: ToolCall}; export type CreateApiV1McpToolExecuteOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const messageRole = z.enum(["user", "assistant", "system", "tool"]); export const imageURLStruct = z.object({url: z.string(), detail: z.enum(["low", "high", "auto"]).optional()}); export const contentBlock = z.discriminatedUnion("type", [z.object({type: z.literal("text"), text: z.string()}), z.object({type: z.literal("image_url"), image_url: imageURLStruct})]); export const functionCall = z.object({name: z.string(), arguments: z.string()}); export const toolCall = z.object({id: z.string().optional(), type: z.literal("function").optional(), function: functionCall}); export const citation = z.object({start_index: z.number().int(), end_index: z.number().int(), title: z.string(), url: z.string().optional(), sources: z.unknown(), type: z.string().optional()}); export const annotation = z.object({type: z.string(), url_citation: citation}); export const bifrostMessage = z.object({role: messageRole, content: z.union([z.string(), z.array(contentBlock)]).optional(), tool_call_id: z.string().optional(), tool_calls: z.array(toolCall).optional(), refusal: z.string().optional(), annotations: z.array(annotation).optional(), thought: z.string().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 }; /** * Execute MCP Tool * * POST /v1/mcp/tool/execute */ export const createApiV1McpToolExecute = async (args: CreateApiV1McpToolExecuteArgs, options?: CreateApiV1McpToolExecuteOptions) => { const {baseUrl = "http://localhost:8080", 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(/\/+$/, '')}/v1/mcp/tool/execute${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(bifrostMessage, await res.json()) };