How to integrate Cal MCP with Vercel AI SDK

Framework Integration Gradient
Cal Logo
Vercel AI SDK Logo
divider

Introduction

This guide walks you through connecting Cal to Vercel AI SDK using the Composio tool router. By the end, you'll have a working Cal agent that can check if my google calendar is synced, cancel a meeting using its unique id, see if my calendar is free tomorrow afternoon, confirm a team booking by its uid through natural language commands.

This guide will help you understand how to give your Vercel AI SDK agent real control over a Cal account through Composio's Cal MCP server.

Before we dive in, let's take a quick look at the key ideas and tools involved.

TL;DR

Here's what you'll learn:
  • How to set up and configure a Vercel AI SDK agent with Cal integration
  • Using Composio's Tool Router to dynamically load and access Cal tools
  • Creating an MCP client connection using HTTP transport
  • Building an interactive CLI chat interface with conversation history management
  • Handling tool calls and results within the Vercel AI SDK framework

What is Vercel AI SDK?

The Vercel AI SDK is a TypeScript library for building AI-powered applications. It provides tools for creating agents that can use external services and maintain conversation state.

Key features include:

  • streamText: Core function for streaming responses with real-time tool support
  • MCP Client: Built-in support for Model Context Protocol
  • Step Counting: Control multi-step tool execution
  • OpenAI Provider: Native integration with OpenAI models

What is the Cal MCP server, and what's possible with it?

The Cal MCP server is an implementation of the Model Context Protocol that connects your AI agent and assistants like Claude, Cursor, etc directly to your Cal account. It provides structured and secure access to your scheduling and calendar management tools, so your agent can perform actions like confirming bookings, checking calendar availability, managing team members, and handling integrations on your behalf.

  • Instant meeting confirmation and cancellation: Ask your agent to confirm or cancel any meeting booking using a unique identifier, streamlining the back-and-forth of scheduling.
  • Real-time calendar availability checks: Let your agent fetch free/busy slots from your connected calendars to suggest optimal meeting times without revealing event details.
  • Team and organization management: Effortlessly add new members to teams or update organization attribute options, making group scheduling and administration smooth.
  • Integration status monitoring: Have your agent verify the synchronization status of connected calendars like Google Calendar, check Stripe payment integration, or review webhook subscriptions for reliability.
  • Calendar feed verification: Use your agent to validate and check accessibility of ICS calendar feeds, ensuring external calendars are synced and up-to-date.

Supported Tools & Triggers

Tools
Add member to teamAdds a new member to a specified team within an organization by creating a team membership.
Add organization attribute optionAdds a new option to an organization's attribute, requiring a display `value` and a `slug` unique for that attribute.
Delete oauth client webhookPermanently deletes a specific webhook subscription of an oauth client in the cal application, stopping its notifications.
Cancel booking via uidCancels an existing and active cal.
Check calendar availabilityRetrieves free/busy availability for a specified calendar to aid scheduling without revealing event details; requires an existing, accessible calendar, noting that data granularity can vary.
Check gcal synchronization statusCall this read-only action to verify the connection and synchronization status of a user's google calendar integration with cal.
Check ics feed calendar endpointChecks an ics feed url (expected as a query parameter) to verify its validity, accessibility, and icalendar data integrity.
Check Stripe statusVerifies if stripe is correctly connected to the cal scheduling system and functional for processing payments, reporting only on the integration's status.
Check team stripe integration statusRetrieves the stripe integration status and related information for a team, primarily to verify account connection, subscription details, or payment setup; this is a read-only operation that does not modify stripe settings.
Confirm booking by uidConfirms an existing booking by `bookinguid` if the booking exists and is in a state allowing confirmation (e.
Connect to calendarInitiates or checks the external connection status for a specified calendar, possibly returning a redirect url for user authorization to complete integration, without altering calendar data.
Create membership for organizationCreates a new membership or updates an existing one for a user within an organization; the user specified by userid must already exist in the system.
Create oauth client userCreates a new managed user for an oauth client, setting profile and scheduling preferences; if `timezone` is not provided, a default schedule (mon-fri, 9am-5pm) is not created, requiring manual setup via `/schedules` endpoint before bookings are possible.
Create OAuth client webhook configurationCreates a webhook for an existing oauth client to send real-time cal.
Create organization attributesCreates a new custom attribute for an existing organization, used to enhance data collection for event bookings or user profiles.
Create organization webhook by org IDCreates a webhook for a specified, existing organization, sending notifications for selected trigger events to a designated, publicly accessible subscriber url that accepts post requests.
Create or update team profileCreates a new team profile, or updates one if a 'slug' matches, customizing branding, scheduling, privacy, and operational details.
Create phone call eventSchedules a phone call event in cal.
Create phone call for event typeCreates a phone call template associated with an existing event type and team, for defining reusable call configurations like numbers and scripts; note this does not initiate an actual call.
Create team event typeCreates a new event type for a specified team in cal.
Create team event types with custom optionsCreates a highly customizable cal.
Create team in organizationCreates a new team with customizable attributes within an existing and accessible cal.
Create team membership with roleAdds a user to a team with a specified role, acceptance status, and impersonation settings; ensure `teamid` and `userid` refer to existing, valid entities.
Create user availability scheduleCreates a cal.
Create user schedule in organizationCreates a new schedule defining a user's availability with weekly slots and date-specific overrides in an organization; setting 'isdefault' to true may replace an existing default schedule for the user.
Create webhook for event typeCreates a webhook for an existing `eventtypeid` in cal.
Decline booking with reasonDeclines a pending booking using its bookinguid, optionally with a reason; this action is irreversible and applies only to bookings awaiting confirmation.
Delete conference app connectionDisconnects the specified conferencing application (e.
Delete event type by idPermanently deletes an existing event type by its id, which invalidates its scheduling links; the operation is irreversible, and while existing bookings are unaffected, no new bookings can be made for this event type.
Delete event type in teamPermanently removes an event type's configuration from a team's scheduling options (e.
Delete membership in teamUse to permanently remove a user's membership from a specific team within an organization, which revokes their team-associated access but does not remove them from the organization.
Delete oauth client userIrreversibly revokes a specific user's association with a given oauth client, without deleting the user's cal account.
Delete organization attributePermanently deletes an existing attribute (specified by `attributeid`) from an existing organization (specified by `orgid`); this action is irreversible and may affect features dependent on the attribute.
Delete organization attribute optionPermanently deletes a specified option from an organization's attribute, typically to remove an unnecessary configuration choice.
Delete organization membershipIrreversibly deletes a user's membership from an organization, removing all associated access and permissions; the response confirms deletion without returning details of the deleted membership.
Delete org webhookPermanently deletes an organization's webhook, which stops all its notifications; this action is irreversible and the webhook cannot be recovered.
Delete schedule by idPermanently deletes a specific schedule using its unique identifier, which must correspond to an existing schedule.
Delete selected calendarsRemoves a specified, currently selected calendar from the user's active list within the application, without deleting it from the external provider.
Delete selected slotDeletes a previously selected time slot from the cal schedule using its `uid`; the slot must exist and this action is irreversible.
Delete team by idPermanently and irreversibly deletes an existing team and all its associated data from the cal system, using the team's unique `teamid`.
Delete team from organizationPermanently and irreversibly deletes a specific team (and all its associated data, members, and linked projects/events) from an organization, when the team is no longer needed or during organizational restructuring.
Delete team memberships by idIrreversibly removes a user's team membership in the cal application, revoking access to that specific team; the user's overall cal account remains active.
Delete user attribute optionRemoves a custom attribute option (e.
Delete user from organizationPermanently removes a user from a specific organization (user's system-wide account is unaffected), revoking their access rights therein; this action is irreversible via api and expects the user to be a current member.
Delete user schedulePermanently deletes a specific user's schedule, provided the organization, user, and schedule (identified by `orgid`, `userid`, and `scheduleid`) exist.
Delete webhook by idPermanently deletes an existing webhook by its `webhookid`, stopping future notifications; this action is irreversible.
Delete webhook for event typePermanently deletes a specific webhook for an event type, halting its real-time notifications; this operation is irreversible and leaves the event type and other webhooks untouched.
Delete webhooks for event typeCall this to irreversibly delete all webhooks for a specific `eventtypeid` if the event type exists; details of deleted webhooks are not returned.
Disconnect calendar using credential idDisconnects a calendar integration by its provider name and credential id, irreversibly revoking cal's access; external calendar data remains unaffected.
Fetch all bookingsFetches a list of bookings, optionally filtered by status, attendee, date range, or by event/team ids (which must belong to/include the authenticated user respectively), with support for pagination and sorting.
Fetch event type detailsFetches all configuration settings and characteristics for a single event type (identified by orgid, teamid, and eventtypeid), which must exist and be accessible; this read-only action cannot list, create, or modify event types.
Fetch organization attribute by idRetrieves a specific attribute of an organization, useful for fetching a single data point instead of the entire organization record.
Fetch provider access tokenFetches an oauth access token for the specified `clientid` to authenticate api calls; this action only retrieves the token, not managing scheduling or calendar events.
Fetch schedule by idFetches comprehensive details for a specific, existing schedule using its `scheduleid`.
Fetch user schedule by org idFetches a specific user's schedule, potentially including events and availability, using `orgid`, `userid`, and `scheduleid`.
Fetch webhook by event type idRetrieves details for a single, specific webhook using its `webhookid` and associated `eventtypeid`.
Force refresh user oauth clientForces an immediate refresh of oauth tokens for a specified user and client, bypassing the normal expiration cycle, typically when existing tokens are suspected to be invalid or fresh credentials are required; ensure the user exists, the client is registered, and prior authorization is valid.
Get all timezonesRetrieves all supported time zone identifiers (e.
Get available slots infoRetrieves available time slots for scheduling by considering existing bookings and availability, based on criteria like a specified time range and event type.
Get conference OAuth authorization urlGenerates an oauth 2.
Get default schedule detailsRetrieves the cal system's global default schedule configuration, not custom or user-specific ones.
Get event type by team idRetrieves a specific event type by its id, requiring that the event type is associated with the given team id.
Get google calendar oauth authentication urlGenerates the initial google calendar oauth 2.
Get oauth clients userRetrieves users associated with a specific oauth client (identified by a valid `clientid`) for auditing access or managing permissions.
Get organization IDRetrieves all organization ids associated with the currently authenticated user.
Get organization schedulesRetrieves a list of schedules (e.
Get organization teams event typesRetrieves event types, including names, durations, and custom settings for team scheduling, for all teams within an existing organization specified by `orgid`.
Get organization user schedulesFetches all schedule information for a specific user within an organization; handle potential pagination for extensive schedules.
Get schedule for user in teamFetches all scheduled events or appointments for a specified user within their team and organization.
Get stripe connect infoRetrieves stripe connect account details (id, charges/payouts status, verification, settings) for the user's linked cal.
Get team details by organization ID and team IDRetrieves comprehensive details (e.
Get team information by team IDFetches comprehensive details for an existing team using its unique id; returned fields may vary by team configuration and user permissions.
Get teams listRetrieves all teams the user belongs to, including their names and members.
Get webhook by idRetrieves details for an existing and accessible webhook by its id; this is a read-only operation.
Handle conferencing oauth callback for appProcesses an oauth 2.
List event typesRetrieves cal event types, filterable by `username` (required if `eventslug` is provided), multiple `usernames`, or organization details (`orgslug` or `orgid`).
List organization membershipsRetrieves all memberships for a given organization, including user details, roles, status, and membership dates.
List team event types by org and team idRetrieves all event types for a specific team within an organization, optionally filtering by a specific event slug.
Mark booking absent for UIDMarks the host and/or specified attendees as absent for an existing booking, typically used after a scheduled event to record no-shows.
Modify organization membership by idUpdates an organization membership's status (accepted), role, or impersonation settings, identified by `orgid` and `membershipid` in the path; requires at least one of these fields in the request to apply changes.
Modify org attribute by idPartially updates an organization attribute using `orgid` and `attributeid`, allowing modification of its name, slug, type, or enabled status; changing the 'type' may affect existing data.
Patch organization attribute optionPartially updates a specific option for an organization's attribute, modifying its 'value' and/or 'slug'; at least one of 'value' or 'slug' must be provided.
Patch organization user detailsPartially updates details for a user that exists within the specified organization.
Patch team details by IDUpdates specified details for an existing team identified by `teamid`; unspecified fields remain unchanged.
Patch webhook event typeUpdates configuration (e.
Post calendar credentialsUse to submit/update authentication credentials (passed in the request body) for an existing calendar, enabling cal to connect with external calendar services for synchronization.
Connect conferencing appConnects or reconnects cal.
Post a new booking requestCreates a new booking for an active event type, scheduling it for a specified start time (preferably in the future) with primary attendee details and optional customizations.
Assign or create attribute option for userAssigns an existing attribute option (using `attributeoptionid`) or creates a new one (using `value`) for a user, linking it to a specified `attributeid` which must already exist within the organization.
Add selected calendarLinks a new external calendar or updates an existing link to one, enabling synchronization with the cal application by specifying the `integration` provider, the calendar's `externalid`, and the `credentialid`.
Post user to organizationAdds a new user to an existing organization (identified by `orgid` in path), requiring user's `email` and allowing extensive optional profile customization.
Post webhook eventCreates a new cal.
Reassign booking to another userReassigns an existing, active booking to a specified, authorized user; does not notify participants of this change.
Reassign booking with uidReassigns the specified booking to a new team member, who is determined by the system rather than being specified in the request.
Refresh oauth token for client idRefreshes an oauth access token for a specified `clientid` and managed user using their `refreshtoken`, enabling continued api access when the current token is near or past expiry.
Reschedule booking by uidReschedules an existing booking (identified by `bookinguid`) to a new time, provided the new slot's availability is confirmed beforehand; all other rescheduling parameters (e.
Reserve slot for eventTemporarily reserves an available time slot for an existing and bookable event type, useful for high-demand slots to prevent double-bookings while the user completes the booking.
Retrieve attribute options for orgRetrieves all available options for a specific attribute within a designated organization, requiring valid and associated `orgid` and `attributeid`.
Retrieve booking details by uidFetches comprehensive details for an existing booking, identified by its `bookinguid`.
Retrieve calendar busy timesTo find busy calendar slots for scheduling/conflict detection, call this with a valid `credentialid`, an `externalid` accessible by it, and a recognized iana `loggedinuserstz`; returns only busy intervals, not event details or free slots.
Retrieve calendar listRetrieves a list of all calendar summaries (no event details) associated with the authenticated user's account.
Retrieve current team for organizationRetrieves details of the team(s) for the currently authenticated user within the specified organization `orgid`.
Retrieve default conferencing settingsRetrieves an account's or organization's read-only default conferencing settings in cal.
Retrieve event type by idRetrieves comprehensive details for a specific, existing cal.
Retrieve membership from organizationRetrieves detailed information about a specific membership within a particular organization.
Retrieve my informationRetrieves the authenticated user's core profile information (e.
Retrieve OAuth client user by IDRetrieves detailed information for an existing user specifically associated with an existing oauth client.
Retrieve oauth client webhook by idRetrieves a specific webhook using its `webhookid` and the `clientid` of its associated oauth client.
Retrieve organization attributesRetrieves detailed attributes (e.
Retrieve organization attributes optionsFetches all available attribute options for a specific user within a given organization, provided the organization and user exist and the user is part of the organization.
Retrieve organization webhook by idRetrieves detailed information, including configuration and status, for a specific webhook by its id (`webhookid`) within a given organization (`orgid`).
Retrieve organization webhooks by org IDRetrieves a list of webhooks for an organization specified by `orgid`, supporting pagination; this is a read-only operation.
Retrieve provider detailsRetrieves detailed information for an existing provider in the cal scheduling system using their unique client id.
Retrieve schedules listRetrieves a comprehensive list of all schedules associated with the authenticated user's cal.
Retrieve team details in organizationRetrieves a paginated list of teams and their details for a specific organization id; individual team member details or schedules are not included.
Retrieve team event typesRetrieves event types for a team within the cal scheduling system; this action does not provide details on scheduled instances or member availability.
Retrieve team membership by idRetrieves detailed information, including role and status, for a specific team membership using its id, for a given organization and team.
Retrieve team membership detailsRetrieves detailed attributes for a specific team membership by its id and the team id, such as member information, role, and status; does not list all team members.
Retrieve team membershipsRetrieves a list of memberships, detailing member roles and statuses, for an existing team specified by `teamid`, with support for pagination.
Retrieve team memberships for organizationRetrieves all user memberships, including their roles and states, for a specific team within an organization.
Retrieve users in organizationRetrieves users associated with a specific organization id, excluding individual scheduling or calendar data; the `orgid` must be a valid identifier for an existing organization.
Retrieve v2 conferencing infoRetrieves an authenticated cal user's or organization's video conferencing configurations, capabilities, and installed apps, useful for understanding options before scheduling or verifying setups; provider availability may vary by subscription or settings.
Retrieve webhook details for oauth clientRetrieves a list of webhooks for a specific oauth client, supporting pagination.
Retrieve webhooks for event typeRetrieves a paginated list of webhooks (including urls, subscribed events, and status) for a specified, existing event type id, useful for auditing configurations or troubleshooting.
Retrieve webhooks listRetrieves a paginated list of webhooks from the user's cal scheduling system account, which are used for real-time notifications on events like new bookings, cancellations, or updates.
Save calendar entrySaves or updates a calendar's settings using a get request, typically for data already on the server or simple updates via query parameters.
Save calendar ics feedsImports and saves one or more publicly accessible external icalendar (ics) feed urls into the cal.
Save OAuth credentials via GCal APICompletes the google calendar oauth 2.
Save stripe detailsCompletes the stripe oauth flow by saving stripe details; call this when a user is redirected back from stripe with an authorization `code` and `state`.
Set default conferencing appSets the specified, valid, and configured conferencing application as the default for new meetings for the authenticated user.
Update destination calendar integrationUpdates the destination calendar for syncing events, using `integration` and `externalid` (typically from `/calendars` endpoint).
Update OAuth client user settingsUpdates specified profile and scheduling preference fields for a user associated with an oauth client; `defaultscheduleid`, if provided, must be an existing, valid schedule for the user.
Update oauth client webhookUpdates specified properties of an existing webhook for an oauth client; omitted fields remain unchanged.
Update schedule by IDUpdates an existing schedule by its id, allowing partial modification of properties; providing `availability` or `overrides` replaces them entirely.
Update team information by idUpdates an existing team's information by its id within a specified organization; the `slug`, if provided, must be unique within the organization.
Update team membership by idUpdates specified properties (e.
Update team membership propertiesUpdates attributes like acceptance status, role, or impersonation settings for an existing team membership; only provided fields are changed, and this action cannot create or delete memberships.
Update user profile detailsUpdates the profile information and preferences for the authenticated user, affecting only the fields provided in the request.
Update user schedule in organizationModifies an existing schedule for a specified user within an organization by updating only the provided fields; the organization, user, and schedule must already exist.
Update webhook by idUpdates an existing cal.
Update webhook for organizationUse this action to modify settings of an already created webhook for an organization, such as its target url, active status, or the events that trigger it.

What is the Composio tool router, and how does it fit here?

What is Tool Router?

Composio's Tool Router helps agents find the right tools for a task at runtime. You can plug in multiple toolkits (like Gmail, HubSpot, and GitHub), and the agent will identify the relevant app and action to complete multi-step workflows. This can reduce token usage and improve the reliability of tool calls. Read more here: Getting started with Tool Router

The tool router generates a secure MCP URL that your agents can access to perform actions.

How the Tool Router works

The Tool Router follows a three-phase workflow:

  1. Discovery: Searches for tools matching your task and returns relevant toolkits with their details.
  2. Authentication: Checks for active connections. If missing, creates an auth config and returns a connection URL via Auth Link.
  3. Execution: Executes the action using the authenticated connection.

Step-by-step Guide

Prerequisites

Before you begin, make sure you have:
  • Node.js and npm installed
  • A Composio account with API key
  • An OpenAI API key

Getting API Keys for OpenAI and Composio

OpenAI API Key
  • Go to the OpenAI dashboard and create an API key. You'll need credits to use the models, or you can connect to another model provider.
  • Keep the API key safe.
Composio API Key
  • Log in to the Composio dashboard.
  • Navigate to your API settings and generate a new API key.
  • Store this key securely as you'll need it for authentication.

Install required dependencies

bash
npm install @ai-sdk/openai @ai-sdk/mcp @composio/core ai dotenv

First, install the necessary packages for your project.

What you're installing:

  • @ai-sdk/openai: Vercel AI SDK's OpenAI provider
  • @ai-sdk/mcp: MCP client for Vercel AI SDK
  • @composio/core: Composio SDK for tool integration
  • ai: Core Vercel AI SDK
  • dotenv: Environment variable management

Set up environment variables

bash
OPENAI_API_KEY=your_openai_api_key_here
COMPOSIO_API_KEY=your_composio_api_key_here
COMPOSIO_USER_ID=your_user_id_here

Create a .env file in your project root.

What's needed:

  • OPENAI_API_KEY: Your OpenAI API key for GPT model access
  • COMPOSIO_API_KEY: Your Composio API key for tool access
  • COMPOSIO_USER_ID: A unique identifier for the user session

Import required modules and validate environment

typescript
import "dotenv/config";
import { openai } from "@ai-sdk/openai";
import { Composio } from "@composio/core";
import * as readline from "readline";
import { streamText, type ModelMessage, stepCountIs } from "ai";
import { experimental_createMCPClient as createMCPClient } from "@ai-sdk/mcp";

const composioAPIKey = process.env.COMPOSIO_API_KEY;
const composioUserID = process.env.COMPOSIO_USER_ID;

if (!process.env.OPENAI_API_KEY) throw new Error("OPENAI_API_KEY is not set");
if (!composioAPIKey) throw new Error("COMPOSIO_API_KEY is not set");
if (!composioUserID) throw new Error("COMPOSIO_USER_ID is not set");

const composio = new Composio({
  apiKey: composioAPIKey,
});
What's happening:
  • We're importing all necessary libraries including Vercel AI SDK's OpenAI provider and Composio
  • The dotenv/config import automatically loads environment variables
  • The MCP client import enables connection to Composio's tool server

Create Tool Router session and initialize MCP client

typescript
async function main() {
  // Create a tool router session for the user
  const { session } = await composio.create(composioUserID!, {
    toolkits: ["cal"],
  });

  const mcpUrl = session.mcp.url;
What's happening:
  • We're creating a Tool Router session that gives your agent access to Cal tools
  • The create method takes the user ID and specifies which toolkits should be available
  • The returned mcp object contains the URL and authentication headers needed to connect to the MCP server
  • This session provides access to all Cal-related tools through the MCP protocol

Connect to MCP server and retrieve tools

typescript
const mcpClient = await createMCPClient({
  transport: {
    type: "http",
    url: mcpUrl,
    headers: session.mcp.headers, // Authentication headers for the Composio MCP server
  },
});

const tools = await mcpClient.tools();
What's happening:
  • We're creating an MCP client that connects to our Composio Tool Router session via HTTP
  • The mcp.url provides the endpoint, and mcp.headers contains authentication credentials
  • The type: "http" is important - Composio requires HTTP transport
  • tools() retrieves all available Cal tools that the agent can use

Initialize conversation and CLI interface

typescript
let messages: ModelMessage[] = [];

console.log("Chat started! Type 'exit' or 'quit' to end the conversation.\n");
console.log(
  "Ask any questions related to cal, like summarize my last 5 emails, send an email, etc... :)))\n",
);

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: "> ",
});

rl.prompt();
What's happening:
  • We initialize an empty messages array to maintain conversation history
  • A readline interface is created to accept user input from the command line
  • Instructions are displayed to guide the user on how to interact with the agent

Handle user input and stream responses with real-time tool feedback

typescript
rl.on("line", async (userInput: string) => {
  const trimmedInput = userInput.trim();

  if (["exit", "quit", "bye"].includes(trimmedInput.toLowerCase())) {
    console.log("\nGoodbye!");
    rl.close();
    process.exit(0);
  }

  if (!trimmedInput) {
    rl.prompt();
    return;
  }

  messages.push({ role: "user", content: trimmedInput });
  console.log("\nAgent is thinking...\n");

  try {
    const stream = streamText({
      model: openai("gpt-5"),
      messages,
      tools,
      toolChoice: "auto",
      stopWhen: stepCountIs(10),
      onStepFinish: (step) => {
        for (const toolCall of step.toolCalls) {
          console.log(`[Using tool: ${toolCall.toolName}]`);
          }
          if (step.toolCalls.length > 0) {
            console.log(""); // Add space after tool calls
          }
        },
      });

      for await (const chunk of stream.textStream) {
        process.stdout.write(chunk);
      }

      console.log("\n\n---\n");

      // Get final result for message history
      const response = await stream.response;
      if (response?.messages?.length) {
        messages.push(...response.messages);
      }
    } catch (error) {
      console.error("\nAn error occurred while talking to the agent:");
      console.error(error);
      console.log(
        "\nYou can try again or restart the app if it keeps happening.\n",
      );
    } finally {
      rl.prompt();
    }
  });

  rl.on("close", async () => {
    await mcpClient.close();
    console.log("\nšŸ‘‹ Session ended.");
    process.exit(0);
  });
}

main().catch((err) => {
  console.error("Fatal error:", err);
  process.exit(1);
});
What's happening:
  • We use streamText instead of generateText to stream responses in real-time
  • toolChoice: "auto" allows the model to decide when to use Cal tools
  • stopWhen: stepCountIs(10) allows up to 10 steps for complex multi-tool operations
  • onStepFinish callback displays which tools are being used in real-time
  • We iterate through the text stream to create a typewriter effect as the agent responds
  • The complete response is added to conversation history to maintain context
  • Errors are caught and displayed with helpful retry suggestions

Complete Code

Here's the complete code to get you started with Cal and Vercel AI SDK:

typescript
import "dotenv/config";
import { openai } from "@ai-sdk/openai";
import { Composio } from "@composio/core";
import * as readline from "readline";
import { streamText, type ModelMessage, stepCountIs } from "ai";
import { experimental_createMCPClient as createMCPClient } from "@ai-sdk/mcp";

const composioAPIKey = process.env.COMPOSIO_API_KEY;
const composioUserID = process.env.COMPOSIO_USER_ID;

if (!process.env.OPENAI_API_KEY) throw new Error("OPENAI_API_KEY is not set");
if (!composioAPIKey) throw new Error("COMPOSIO_API_KEY is not set");
if (!composioUserID) throw new Error("COMPOSIO_USER_ID is not set");

const composio = new Composio({
  apiKey: composioAPIKey,
});

async function main() {
  // Create a tool router session for the user
  const { session } = await composio.create(composioUserID!, {
    toolkits: ["cal"],
  });

  const mcpUrl = session.mcp.url;

  const mcpClient = await createMCPClient({
    transport: {
      type: "http",
      url: mcpUrl,
      headers: session.mcp.headers, // Authentication headers for the Composio MCP server
    },
  });

  const tools = await mcpClient.tools();

  let messages: ModelMessage[] = [];

  console.log("Chat started! Type 'exit' or 'quit' to end the conversation.\n");
  console.log(
    "Ask any questions related to cal, like summarize my last 5 emails, send an email, etc... :)))\n",
  );

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: "> ",
  });

  rl.prompt();

  rl.on("line", async (userInput: string) => {
    const trimmedInput = userInput.trim();

    if (["exit", "quit", "bye"].includes(trimmedInput.toLowerCase())) {
      console.log("\nGoodbye!");
      rl.close();
      process.exit(0);
    }

    if (!trimmedInput) {
      rl.prompt();
      return;
    }

    messages.push({ role: "user", content: trimmedInput });
    console.log("\nAgent is thinking...\n");

    try {
      const stream = streamText({
        model: openai("gpt-5"),
        messages,
        tools,
        toolChoice: "auto",
        stopWhen: stepCountIs(10),
        onStepFinish: (step) => {
          for (const toolCall of step.toolCalls) {
            console.log(`[Using tool: ${toolCall.toolName}]`);
          }
          if (step.toolCalls.length > 0) {
            console.log(""); // Add space after tool calls
          }
        },
      });

      for await (const chunk of stream.textStream) {
        process.stdout.write(chunk);
      }

      console.log("\n\n---\n");

      // Get final result for message history
      const response = await stream.response;
      if (response?.messages?.length) {
        messages.push(...response.messages);
      }
    } catch (error) {
      console.error("\nAn error occurred while talking to the agent:");
      console.error(error);
      console.log(
        "\nYou can try again or restart the app if it keeps happening.\n",
      );
    } finally {
      rl.prompt();
    }
  });

  rl.on("close", async () => {
    await mcpClient.close();
    console.log("\nšŸ‘‹ Session ended.");
    process.exit(0);
  });
}

main().catch((err) => {
  console.error("Fatal error:", err);
  process.exit(1);
});

Conclusion

You've successfully built a Cal agent using the Vercel AI SDK with streaming capabilities! This implementation provides a powerful foundation for building AI applications with natural language interfaces and real-time feedback.

Key features of this implementation:

  • Real-time streaming responses for a better user experience with typewriter effect
  • Live tool execution feedback showing which tools are being used as the agent works
  • Dynamic tool loading through Composio's Tool Router with secure authentication
  • Multi-step tool execution with configurable step limits (up to 10 steps)
  • Comprehensive error handling for robust agent execution
  • Conversation history maintenance for context-aware responses

You can extend this further by adding custom error handling, implementing specific business logic, or integrating additional Composio toolkits to create multi-app workflows.

How to build Cal MCP Agent with another framework

FAQ

What are the differences in Tool Router MCP and Cal MCP?

With a standalone Cal MCP server, the agents and LLMs can only access a fixed set of Cal tools tied to that server. However, with the Composio Tool Router, agents can dynamically load tools from Cal and many other apps based on the task at hand, all through a single MCP endpoint.

Can I use Tool Router MCP with Vercel AI SDK?

Yes, you can. Vercel AI SDK fully supports MCP integration. You get structured tool calling, message history handling, and model orchestration while Tool Router takes care of discovering and serving the right Cal tools.

Can I manage the permissions and scopes for Cal while using Tool Router?

Yes, absolutely. You can configure which Cal scopes and actions are allowed when connecting your account to Composio. You can also bring your own OAuth credentials or API configuration so you keep full control over what the agent can do.

How safe is my data with Composio Tool Router?

All sensitive data such as tokens, keys, and configuration is fully encrypted at rest and in transit. Composio is SOC 2 Type 2 compliant and follows strict security practices so your Cal data and credentials are handled as safely as possible.

Used by agents from

Context
Letta
glean
HubSpot
Agent.ai
Altera
DataStax
Entelligence
Rolai
Context
Letta
glean
HubSpot
Agent.ai
Altera
DataStax
Entelligence
Rolai
Context
Letta
glean
HubSpot
Agent.ai
Altera
DataStax
Entelligence
Rolai

Never worry about agent reliability

We handle tool reliability, observability, and security so you never have to second-guess an agent action.