import {z} from 'zod' export type GetApiInspectContractCodeArgs = {chain_id: string, address: string, file_name?: string | undefined}; export type GetApiInspectContractCodeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({next_call: z.object({tool_name: z.string(), params: z.object({cursor: z.string()}).and(z.record(z.string(), z.unknown()))})}); export const toolResponse = z.object({data: z.union([z.object({}), z.array(z.unknown()), z.string(), z.number(), z.boolean()]).nullable(), data_description: z.array(z.string()).nullable().optional(), notes: z.array(z.string()).nullable().optional(), instructions: z.array(z.string()).nullable().optional(), pagination: pagination.nullable().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 }; /** * Inspect contract code * * GET /inspect_contract_code */ export const getApiInspectContractCode = async (args: GetApiInspectContractCodeArgs, options?: GetApiInspectContractCodeOptions) => { const {baseUrl = "https://mcp.blockscout.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.chain_id != null) query.append("chain_id", String(args.chain_id)) if (args.address != null) query.append("address", String(args.address)) if (args.file_name != null) query.append("file_name", String(args.file_name)) const url = `${baseUrl.replace(/\/+$/, '')}/inspect_contract_code${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(toolResponse, await res.json()) };