Skip to content

New AI assistant answers: ChatGPT, Gemini, Copilot and AI Mode by API

SerpKite
Get API key

TypeScript · @mastra/core

Mastra + SerpKite

Build a Google search tool with Mastra's createTool and a zod schema, attach it to an Agent and call generate. SerpKite returns Markdown the agent can read directly.

Overview

What Mastra is, and where SerpKite fits

Mastra is a TypeScript framework for agents, workflows and RAG. Tools are created with createTool from @mastra/core/tools, with a zod inputSchema and an execute function.

The example targets Mastra 1.x, where execute receives the validated input as its first argument, and calls SerpKite through the official serpkite TypeScript SDK.

Setup

Set it up in 4 steps

  1. 1

    Get an API key

    Sign up (no card), create a key in the dashboard and export it as SERPKITE_API_KEY. New accounts get 2,500 free credits, then 1,000 every month.

    export SERPKITE_API_KEY=skt_live_…
  2. 2

    Install

    Add Mastra to an existing project (or start with npm create mastra@latest).

    npm install serpkite @mastra/core zod
  3. 3

    Create the tool

    Define an input and output schema and call SerpKite in execute.

  4. 4

    Register it on an agent

    Tools are passed as an object keyed by name.

Code

Complete example

src/mastra/agents/researcher.ts
import { Agent } from "@mastra/core/agent";
import { createTool } from "@mastra/core/tools";
import { SerpKite, SerpKiteError } from "serpkite";
import { z } from "zod";

const sk = new SerpKite(); // reads SERPKITE_API_KEY

export const googleSearch = createTool({
  id: "google-search",
  description: "Search Google for current information. Returns the top results with links, People Also Ask and related searches as Markdown. Use for recent events, facts and anything after your training cutoff.",
  inputSchema: z.object({
    query: z.string().describe("The search query"),
    country: z.string().default("us").describe("Two-letter country code"),
  }),
  outputSchema: z.object({ markdown: z.string() }),
  execute: async ({ query, country }) => {
    try {
      return { markdown: await sk.search({ q: query, country, format: "markdown" }) };
    } catch (err) {
      if (err instanceof SerpKiteError) return { markdown: `Search failed: ${err.message}` };
      throw err;
    }
  },
});

export const researcher = new Agent({
  id: "researcher",
  name: "Researcher",
  instructions: "Search the web before answering. Cite the URLs you used.",
  model: "openai/gpt-5-mini",
  tools: { googleSearch },
});

const result = await researcher.generate("What are the best-rated coffee roasters in Berlin?");
console.log(result.text);

AI assistants

Ask ChatGPT, Gemini and other AI assistants

The serpkite SDK asks ChatGPT, Gemini, Copilot Search, Google AI Mode and Brave's AI answer as a logged-out visitor (sk.chatgpt, sk.gemini, sk.copilot, sk.aiMode, sk.braveAi) and returns the answer as Markdown with the sources it cited. Wrap one with createTool like the search tool. Give the agent a second opinion, or have it check what assistants say about your brand: track adds mentions of the brands and domains you list. See AI assistants.

ChatGPT 4, Gemini 3, Copilot Search 3, Google AI Mode 2 and Brave AI 2 credits per answer; failed, refused and empty answers are free. Brave answers in 1–10 s, ChatGPT in 10–30 s and Gemini in 5–30 s, so allow at least 120 seconds per call.

TypeScript
import { createTool } from "@mastra/core/tools";
import { SerpKite, SerpKiteError } from "serpkite";
import { z } from "zod";

const sk = new SerpKite(); // reads SERPKITE_API_KEY

export const askAiMode = createTool({
  id: "ask-google-ai-mode",
  description:
    "Ask Google AI Mode. Returns its answer as Markdown with the sources it cited. Takes 5–45 s.",
  inputSchema: z.object({
    prompt: z.string().describe("The question to ask"),
    country: z.string().default("us").describe("Two-letter country Google answers for"),
  }),
  outputSchema: z.object({ markdown: z.string() }),
  execute: async ({ prompt, country }) => {
    try {
      return { markdown: await sk.aiMode({ prompt, country, format: "markdown" }) };
    } catch (err) {
      if (err instanceof SerpKiteError) return { markdown: `AI Mode failed: ${err.message}` };
      throw err;
    }
  },
});

FAQ

Mastra and SerpKite: common questions

Start building

Give your Mastra project Google search

2,500 free credits, then 1,000 every month. No credit card.