import {z} from 'zod' export type GetApiV1ProjectsProjectIdGeoTrafficMcpArgs = {projectId: string, days?: number | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiV1ProjectsProjectIdGeoTrafficMcpOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const geoMcpAnalyticsResponse = z.object({configured: z.boolean(), server: z.object({name: z.string(), version: z.string(), protocolVersion: z.string()}).nullable(), totals: z.object({requests: z.number().int(), previousRequests: z.number().int(), toolCalls: z.number().int(), previousToolCalls: z.number().int(), errors: z.number().int(), previousErrors: z.number().int(), sessions: z.number().int(), previousSessions: z.number().int(), users: z.number().int(), previousUsers: z.number().int(), p95Ms: z.number().int(), previousP95Ms: z.number().int()}), daily: z.array(z.object({day: z.string(), toolsCall: z.number().int(), resourcesRead: z.number().int(), promptsGet: z.number().int(), other: z.number().int(), errors: z.number().int()})), tools: z.array(z.object({name: z.string(), description: z.string(), calls: z.number().int(), errors: z.number().int(), users: z.number().int(), avgResponseBytes: z.number().int(), p50Ms: z.number().int(), p95Ms: z.number().int(), uiResource: z.string()})), clients: z.array(z.object({name: z.string(), sessions: z.number().int(), requests: z.number().int(), errors: z.number().int(), lastSeenAt: z.string()})), errorRequests: z.array(z.object({capturedAt: z.string(), clientName: z.string(), clientVersion: z.string(), method: z.string(), target: z.string(), status: z.enum(["ok", "tool_error", "rpc_error"]), errorCode: z.number().int(), errorMessage: z.string(), durationMs: z.number().int(), model: z.string()})), models: z.array(z.object({name: z.string(), sessions: z.number().int(), calls: z.number().int(), errors: z.number().int()})), intents: z.array(z.object({intent: z.string(), tool: z.string(), calls: z.number().int(), errors: z.number().int()})), elicitations: z.array(z.object({method: z.string(), tool: z.string(), requests: z.number().int(), accepted: z.number().int(), declined: z.number().int(), cancelled: z.number().int(), errors: z.number().int()})), uiRenders: z.array(z.object({resource: z.string(), tools: z.array(z.string()), renders: z.number().int(), sessions: z.number().int(), errors: z.number().int()})), catalog: z.object({listedAt: z.string(), unusedTools: z.array(z.object({name: z.string(), description: z.string()}))}).nullable(), organization: z.object({id: z.string(), slug: z.string(), name: z.string(), logo: z.string().nullable()})}); 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 }; /** * Get MCP server analytics * * GET /v1/projects/{projectId}/geo/traffic/mcp */ export const getApiV1ProjectsProjectIdGeoTrafficMcp = async (args: GetApiV1ProjectsProjectIdGeoTrafficMcpArgs, options?: GetApiV1ProjectsProjectIdGeoTrafficMcpOptions) => { const {baseUrl = "https://api.usenotra.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.days != null) query.append("days", String(args.days)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/geo/traffic/mcp${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(geoMcpAnalyticsResponse, await res.json()) };