import {z} from 'zod' export type AiMcpCredentialScope = 'shared' | 'user'; export type AiMcpServerAuthType = 'none' | 'bearer' | 'oauth'; export type ApiCreateAiMcpServer = {credentials?: ({clientSecret?: string | undefined, clientId?: string | undefined, bearerToken?: string | undefined} | null) | undefined, credentialScope?: AiMcpCredentialScope | undefined, allowOAuthCredentialSharing?: boolean | undefined, authType: AiMcpServerAuthType, url: string, name: string}; export type CreateApiApiV1ProjectsProjectUuidAiAgentsMcpServersArgs = {projectUuid: string, body: ApiCreateAiMcpServer}; export type CreateApiApiV1ProjectsProjectUuidAiAgentsMcpServersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const aiMcpServerConnectionStatus = z.enum(["not_connected", "connecting", "connected", "error"]); export const aiMcpCredentialScope = z.enum(["shared", "user"]); export const aiMcpServerAuthType = z.enum(["none", "bearer", "oauth"]); export const aiMcpServer = z.object({updatedAt: z.string(), createdAt: z.string(), connectedByUserUuid: z.string().nullable(), error: z.string().nullable(), connectionStatus: aiMcpServerConnectionStatus.nullable(), credentialScope: aiMcpCredentialScope.nullable(), hasCredentials: z.boolean(), allowOAuthCredentialSharing: z.boolean(), authType: aiMcpServerAuthType, iconUrl: z.string().nullable(), url: z.string(), name: z.string(), projectUuid: z.string(), uuid: z.string()}); export const apiAiMcpServerResponse = z.object({results: aiMcpServer, status: z.literal("ok")}); 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 }; /** * POST /api/v1/projects/{projectUuid}/aiAgents/mcpServers */ export const createApiApiV1ProjectsProjectUuidAiAgentsMcpServers = async (args: CreateApiApiV1ProjectsProjectUuidAiAgentsMcpServersArgs, options: CreateApiApiV1ProjectsProjectUuidAiAgentsMcpServersOptions) => { const {baseUrl, 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(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.projectUuid))}/aiAgents/mcpServers${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(apiAiMcpServerResponse, await res.json()) };