import {z} from 'zod' export type MCPServerInstanceCreateWithoutSpec = {auth_config_id?: (string | null) | undefined, context_secret?: (string | null) | undefined, description?: (string | null) | undefined, json_spec?: {[key: string]: unknown} | undefined, name: string}; export type MCPServerCreate = {cmd?: (Array | null) | undefined, description: string, docker_image_url?: (string | null) | undefined, env_schema?: (Array<{[key: string]: unknown}> | null) | undefined, is_public?: boolean | undefined, json_spec?: ({[key: string]: unknown} | null) | undefined, name: string, registry_url?: (string | null) | undefined, remote_url?: (string | null) | undefined, tags?: Array | undefined, version?: string | undefined}; export type MCPServerConnectionCreateRequest = {instance: MCPServerInstanceCreateWithoutSpec, server: MCPServerCreate}; export type CreateApiV1WorkspacesWorkspaceMcpServerInstancesWithSpecArgs = {workspace: string, body: MCPServerConnectionCreateRequest}; export type CreateApiV1WorkspacesWorkspaceMcpServerInstancesWithSpecOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const mCPTransport = z.enum(["url", "docker", "command", "bundle"]); export const mCPServerInstanceResponse = z.object({auth_config_id: z.union([z.string(), z.string()]).nullable().optional(), cost: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string(), description: z.string().nullable(), events: z.record(z.string(), z.unknown()).nullable().optional(), id: z.string(), is_owner: z.boolean().optional(), json_spec: z.record(z.string(), z.unknown()), last_dispatch: z.record(z.string(), z.unknown()).nullable().optional(), managed_by: z.string().nullable().optional(), name: z.string(), personal: z.boolean().optional(), server_spec_id: z.string(), tools: z.array(z.record(z.string(), z.unknown())).nullable().optional(), transport: mCPTransport, updated_at: z.string(), verification: z.record(z.string(), z.unknown())}); 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 }; /** * Create Mcp Server Connection * * POST /v1/workspaces/{workspace}/mcp-server-instances/with-spec */ export const createApiV1WorkspacesWorkspaceMcpServerInstancesWithSpec = async (args: CreateApiV1WorkspacesWorkspaceMcpServerInstancesWithSpecArgs, options: CreateApiV1WorkspacesWorkspaceMcpServerInstancesWithSpecOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace))}/mcp-server-instances/with-spec${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(mCPServerInstanceResponse, await res.json()) };