Docs

MCP Tools Overview

Learn about MCP (Model Context Protocol) tools and how they enable AI assistants to interact with your application

MCP Tools Overview

MCP (Model Context Protocol) tools allow AI assistants like ChatGPT to interact with your application's data and functionality. These tools act as bridges between AI models and your backend systems.

What are MCP Tools?

MCP tools are functions that AI models can call to:

  • Query data from your database (users, organizations, payments)
  • Execute operations (send emails, update records, trigger workflows)
  • Access real-time information that isn't in the model's training data
  • Perform actions on behalf of authenticated users

How MCP Works

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│   ChatGPT   │────▶│  MCP Server │────▶│  Your API   │
│   Client    │◄────│  (Route)    │◄────│  (Tools)    │
└─────────────┘     └─────────────┘     └─────────────┘
  1. User asks ChatGPT a question that requires data from your app
  2. ChatGPT decides which MCP tool to call based on the tool's description
  3. MCP Server receives the request and validates authentication
  4. Your Tool executes the logic and returns data
  5. ChatGPT formats the response for the user

Built-in Admin Tools

The starter kit includes a set of admin tools available at /api/adminx/mcp:

ToolDescription
get_admin_statsGet comprehensive statistics about users, organizations, payments, and sessions
get_user_by_emailLook up a user by their email address
get_user_by_idRetrieve user information by ID
list_usersList all users with optional pagination
list_organizationsList all organizations
get_payment_recordsRetrieve payment transaction history
get_payments_by_emailGet payments for a specific user
get_active_sessionsList currently active user sessions
get_organization_by_idRetrieve organization details by ID

Tool Structure

Each MCP tool follows a consistent pattern:

server.tool(
  "tool_name",           // Unique identifier
  "Tool description",    // Description for the AI model
  {                      // Zod schema for parameters
    param1: z.string(),
    param2: z.number().optional(),
  },
  async ({ param1, param2 }) => {
    // Tool implementation
    return {
      content: [
        {
          type: "text",
          text: JSON.stringify(result, null, 2),
        },
      ],
    };
  },
);

Authentication

MCP tools can be protected using your existing auth system:

import { auth } from "@repo/auth/server";
import { headers } from "next/headers";

async function requireAuth() {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

  if (!session?.user?.id) {
    throw new Error("Unauthorized");
  }

  return session;
}

Next Steps

On this page