How to integrate Mailchimp MCP with Vercel AI SDK

Framework Integration Gradient
Mailchimp Logo
Vercel AI SDK Logo
divider

Introduction

This guide walks you through connecting Mailchimp to Vercel AI SDK using the Composio tool router. By the end, you'll have a working Mailchimp agent that can create a new email campaign for subscribers, add a new customer to my mailchimp store, set up a webhook for batch processing updates, add feedback to my latest campaign through natural language commands.

This guide will help you understand how to give your Vercel AI SDK agent real control over a Mailchimp account through Composio's Mailchimp 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 Mailchimp integration
  • Using Composio's Tool Router to dynamically load and access Mailchimp 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 Mailchimp MCP server, and what's possible with it?

The Mailchimp MCP server is an implementation of the Model Context Protocol that connects your AI agent and assistants like Claude, Cursor, etc directly to your Mailchimp account. It provides structured and secure access to your email marketing platform, so your agent can perform actions like creating campaigns, managing automations, adding customers, and organizing campaign folders on your behalf.

  • Automated campaign creation and management: Have your agent quickly set up, launch, or organize Mailchimp campaigns tailored to your audience segments.
  • Audience and customer management: Effortlessly add new customers to your store or update subscriber lists so your outreach is always up-to-date.
  • Workflow automation setup: Direct your agent to create classic automations or configure batch webhooks to streamline your marketing flows.
  • Campaign feedback and analysis: Let your agent add feedback to specific campaigns and help you keep track of campaign performance and improvements.
  • Store and site integration: Ask your agent to add carts, line items, or connect new sites and domains—making e-commerce and marketing work together seamlessly.

Supported Tools & Triggers

Tools
Triggers
Add automationCreate a new classic automation in your mailchimp account.
Add batch webhookConfigure a webhook that will fire whenever any batch request completes processing.
Add campaignCreate a new mailchimp campaign.
Add campaign feedbackAdd feedback on a specific campaign.
Add campaign folderCreate a new campaign folder.
Add cartAdd a new cart to a store.
Add cart line itemAdd a new line item to an existing cart.
Add connected siteCreate a new mailchimp connected site.
Add customerAdd a new customer to a store.
Add domain to accountAdd a domain to the account.
Add eventAdd an event for a list member.
Add exportCreate a new account export in your mailchimp account.
Add fileUpload a new image or file to the file manager.
Add folderCreate a new folder in the file manager.
Add interest categoryCreate a new interest category.
Add interest in categoryCreate a new interest or 'group name' for a specific category.
Add landing pageCreate a new mailchimp landing page.
Add listCreate a new list in your mailchimp account.
Add member noteAdd a new note for a specific subscriber.
Add member to listAdd a new member to the list.
Add member to segmentAdd a member to a static segment.
Add merge fieldAdd a new merge field for a specific audience.
Add orderAdd a new order to a store.
Add order line itemAdd a new line item to an existing order.
Add or remove member tagsAdd or remove tags from a list member.
Add or update customerAdd or update a customer.
Add or update list memberAdd or update a list member.
Add or update product variantAdd or update a product variant.
Add productAdd a new product to a store.
Add product imageAdd a new image to the product.
Add product variantAdd a new variant to the product.
Add promo codeAdd a new promo code to a store.
Add promo ruleAdd a new promo rule to a store.
Add segmentCreate a new segment in a specific list.
Add storeAdd a new store to your mailchimp account.
Add subscriber to workflow emailManually add a subscriber to a workflow, bypassing the default trigger settings.
Add templateCreate a new template for the account.
Add template folderCreate a new template folder.
Add webhookCreate a new webhook for a specific list.
Archive automationArchiving will permanently end your automation and keep the report data.
Archive list memberArchive a list member.
Batch add or remove membersBatch add/remove list members to static segment
Batch subscribe or unsubscribeBatch subscribe or unsubscribe list members.
Campaign abuse report detailsGet information about a specific abuse report for a campaign.
Campaign statistics feedbackGet feedback based on a campaign's statistics.
Cancel campaignCancel a regular or plain-text campaign after you send, before all of your recipients receive it.
Create a survey campaignUtilize the list id and survey id to generate a campaign that links to your survey.
Customer journeys api trigger for a contactCreate a trigger step in the customer journey builder via the api.
Customize signup formCustomize a list's default signup form.
Delete batch requestStops a batch request from running.
Delete batch webhookRemove a batch webhook.
Delete campaignRemove a campaign from your mailchimp account.
Delete campaign feedback messageRemove a specific feedback message for a campaign.
Delete campaign folderDelete a specific campaign folder, and mark all the campaigns in the folder as 'unfiled'.
Delete cartDelete a cart.
Delete cart line itemDelete a specific cart line item.
Delete connected siteRemove a connected site from your mailchimp account.
Delete customerDelete a customer from a store.
Delete domainDelete a verified domain from the account.
Delete fileRemove a specific file from the file manager.
Delete folderDelete a specific folder in the file manager.
Delete interest categoryDelete a specific interest category.
Delete interest in categoryDelete interests or group names in a specific category.
Delete landing pageDelete a landing page.
Delete listDelete a list from your mailchimp account.
Delete list memberDelete all personally identifiable information related to a list member, and remove them from a list.
Delete merge fieldDelete a specific merge field.
Delete noteDelete a specific note for a specific list member.
Delete orderDelete an order.
Delete order line itemDelete a specific order line item.
Delete productDelete a product.
Delete product imageDelete a product image.
Delete product variantDelete a product variant.
Delete promo codeDelete a promo code from a store.
Delete promo ruleDelete a promo rule from a store.
Delete segmentDelete a specific segment in a list.
Delete storeDelete a store.
Delete templateDelete a specific template.
Delete template folderDelete a specific template folder, and mark all the templates in the folder as 'unfiled'.
Delete webhookDelete a specific webhook in a list.
Delete workflow emailRemoves an individual classic automation workflow email.
Get abuse reportGet details about a specific abuse report.
Get account export infoGet information about a specific account export.
Get authorized app infoGet information about a specific authorized application.
Get automated email subscriberGet information about a specific subscriber in a classic automation email queue.
Get automation infoGet a summary of an individual classic automation workflow's settings and content.
Get batch operation statusGet the status of a batch request.
Get batch webhook infoGet information about a specific batch webhook.
Get campaign contentGet the the html and plain-text content for a campaign.
Get campaign feedback messageGet a specific feedback message from a campaign.
Get campaign folderGet information about a specific folder used to organize campaigns.
Get campaign infoGet information about a specific campaign.
Get campaign link detailsGet click details for a specific link in a campaign.
Get campaign recipient infoGet information about a specific campaign recipient.
Get campaign reportGet report details for a specific sent campaign.
Get campaign send checklistReview the send checklist for a campaign, and resolve any issues before sending.
Get cart infoGet information about a specific cart.
Get cart line itemGet information about a specific cart line item.
Get clicked link subscriberGet information about a specific subscriber who clicked a link in a specific campaign.
Get connected siteGet information about a specific connected site.
Get conversationApi endpoint for conversations is deprecated, replaced by inbox.
Get customer infoGet information about a specific customer.
Get domain infoGet the details for a single domain on the account.
Get facebook ad infoGet details of a facebook ad.
Get facebook ad reportGet report of a facebook ad.
Get fileGet information about a specific file in the file manager.
Get folderGet information about a specific folder in the file manager.
Get growth history by monthGet a summary of a specific list's growth activity for a specific month and year.
Get information about all surveys for a listGet information about all available surveys for a specific list.
Get interest category infoGet information about a specific interest category.
Get interest in categoryGet interests or 'group names' for a specific category.
Get landing page contentGet the the html for your landing page.
Get landing page infoGet information about a specific page.
Get landing page reportGet report of a landing page.
Get latest chimp chatterReturn the chimp chatter for this account ordered by most recent.
Get list infoGet information about a specific list in your mailchimp account.
Get lists infoGet information about all lists in the account.
Get member infoGet information about a specific list member, including a currently subscribed, unsubscribed, or bounced member.
Get member noteGet a specific note for a specific list member.
Get merge fieldGet information about a specific merge field.
Get messageThe conversation endpoint is outdated and replaced by inbox, which isn't supported by it.
Get opened campaign subscriberGet information about a specific subscriber who opened a campaign.
Get order infoGet information about a specific order.
Get order line itemGet information about a specific order line item.
Get product image infoGet information about a specific product image.
Get product infoGet information about a specific product.
Get product variant infoGet information about a specific product variant.
Get promo codeGet information about a specific promo code.
Get promo ruleGet information about a specific promo rule.
Get segment infoGet information about a specific segment.
Get store infoGet information about a specific store.
Get subscriber email activityGet a specific list member's activity in a campaign including opens, clicks, and bounces.
Get subscriber removed from workflowGet information about a specific subscriber who was removed from a classic automation workflow.
Get surveyGet details about a specific survey.
Get survey question reportGet report for a survey question.
Get survey reportGet report for a survey.
Get survey responseGet a single survey response.
Get template folderGet information about a specific folder used to organize templates.
Get template infoGet information about a specific template.
Get unsubscribed memberGet information about a specific list member who unsubscribed from a campaign.
Get webhook infoGet information about a specific webhook.
Get workflow email infoGet information about an individual classic automation workflow email.
List abuse reportsGet all abuse reports for a specific list.
List account exportsGet a list of account exports for a given account.
List account ordersGet information about an account's orders.
List answers for questionGet answers for a survey question.
List api root resourcesGet links to all other resources available in the api.
List authorized appsGet a list of an account's registered, connected applications.
List automated emailsGet a summary of the emails in a classic automation workflow.
List automated email subscribersGet information about a classic automation email queue.
List automationsGet a summary of an account's classic automations.
List batch requestsGet a summary of batch requests that have been made.
List batch webhooksGet all webhooks that have been configured for batches.
List campaign detailsGet information about clicks on specific links in your mailchimp campaigns.
List campaign feedbackGet team feedback while you're working together on a mailchimp campaign.
List campaign foldersGet all folders used to organize campaigns.
List campaign open detailsGet detailed information about any campaign emails that were opened by a list member.
List campaign product activityGet breakdown of product activity for a campaign
List campaign recipientsGet information about campaign recipients.
List campaign reportsGet campaign reports.
List campaignsGet all campaigns in an account.
List cart line itemsGet information about a cart's line items.
List cartsGet information about a store's carts.
List child campaign reportsGet a list of reports with child campaigns for a specific parent campaign.
List clicked link subscribersGet information about list members who clicked on a specific link in a campaign.
List connected sitesGet all connected sites in an account.
List conversationsList of conversations for an account is available via an endpoint, but it's deprecated in favor of inbox.
List customersGet information about a store's customers.
List domain performance statsGet statistics for the top-performing email domains in a campaign.
List eepurl activityGet a summary of social activity for the campaign, tracked by eepurl.
List email activityGet a list of member's subscriber activity in a specific campaign.
List facebook adsGet list of facebook ads.
List facebook ads reportsGet reports of facebook ads.
List facebook ecommerce reportGet breakdown of product activity for an outreach.
List foldersGet a list of all folders in the file manager.
List growth history dataGet a month-by-month summary of a specific list's growth activity.
List interest categoriesGet information about a list's interest categories.
List interests in categoryGet a list of this category's interests.
List landing pagesGet all landing pages.
List landing pages reportsGet reports of landing pages.
List locationsGet the locations (countries) that the list's subscribers have been tagged to based on geocoding their ip address.
List member eventsGet events for a contact.
List member goal eventsGet the last 50 goal events for a member on a specific list.
List members infoGet information about members in a specific mailchimp list.
List members in segmentGet information about members in a saved segment.
List member tagsGet the tags on a list member.
List merge fieldsGet a list of all merge fields for an audience.
List messagesLegacy "conversations" endpoint is deprecated; use "inbox" for new messages.
List order line itemsGet information about an order's line items.
List ordersGet information about a store's orders.
List productGet information about a store's products.
List product imagesGet information about a product's images.
List product variantsGet information about a product's variants.
List promo codesGet information about a store's promo codes.
List promo rulesGet information about a store's promo rules.
List recent activityGet up to the previous 180 days of daily detailed aggregated activity stats for a list, not including automation activity.
List recent member notesGet recent notes for a specific list member.
List segmentsGet information about all available segments for a specific list.
List sending domainsGet all of the sending domains on the account.
List signup formsGet signup forms for a specific list.
List stored filesGet a list of available images and files stored in the file manager for the account.
List storesGet information about all stores in the account.
List subscribers removed from workflowGet information about subscribers who were removed from a classic automation workflow.
List survey question reportsGet reports for survey questions.
List survey reportsGet reports for surveys.
List survey responsesGet responses to a survey.
List template foldersGet all folders used to organize templates.
List templatesGet a list of an account's available templates.
List top email clientsGet a list of the top email clients based on user-agent strings.
List top open activitiesGet top open locations for a specific campaign.
List unsubscribed membersGet information about members who have unsubscribed from a specific campaign.
List webhooksGet information about all webhooks for a specific list.
Pause automated emailPause an automated email.
Pause automation emailsPause all emails in a specific classic automation workflow.
Pause rss campaignPause an rss-driven campaign.
PingA health check for the api that won't return any account-specific information.
Publish a surveyPublish a survey that is in draft, unpublished, or has been previously published and edited.
Publish landing pagePublish a landing page that is in draft, unpublished, or has been previously published and edited.
Remove list member from segmentRemove a member from the specified static segment.
Remove subscriber from workflowRemove a subscriber from any point in a classic automation workflow; once removed, they cannot be re-added to that workflow.
Replicate campaignReplicate a campaign in saved or send status.
Resend campaignRemove the guesswork for resending a campaign to certain segments.
Resume rss campaignResume an rss-driven campaign.
Retrieve campaign abuse complaintsGet a list of abuse complaints for a specific campaign.
Retrieve folder contentsGet a list of available images and files stored in this folder.
Schedule campaignSchedule a campaign for delivery.
Search campaignsSearch all campaigns for the specified query terms.
Search for tags on a list by nameSearch for tags on a list by name.
Search membersSearch for list members.
Send campaignSend a mailchimp campaign.
Send test emailSend a test email.
Set campaign contentSet the content for a campaign.
Start automated emailStart an automated email.
Start automation emailsStart all emails in a classic automation workflow.
Start batch operationBegin processing a batch operations request.
Unpublish a surveyUnpublish a survey that has been published.
Unpublish landing pageUnpublish a landing page that is in draft or has been published.
Unschedule campaignUnschedule a scheduled campaign that hasn't started sending.
Update batch webhookUpdate a webhook that will fire whenever any batch request completes processing.
Update campaign feedback messageUpdate a specific feedback message for a campaign.
Update campaign folderUpdate a specific folder used to organize campaigns.
Update campaign settingsUpdate some or all of the settings for a specific campaign.
Update cartUpdate a specific cart.
Update cart line itemUpdate a specific cart line item.
Update customerUpdate a customer.
Update fileUpdate a file in the file manager.
Update folderUpdate a specific file manager folder.
Update interest categoryUpdate a specific interest category.
Update interest in categoryUpdate interests or 'group names' for a specific category.
Update landing pageUpdate a landing page.
Update list memberUpdate information for a specific list member.
Update listsUpdate the settings for a specific list.
Update merge fieldUpdate a specific merge field.
Update noteUpdate a specific note for a specific list member.
Update orderUpdate a specific order.
Update order line itemUpdate a specific order line item.
Update productUpdate a specific product.
Update product imageUpdate a product image.
Update product variantUpdate a product variant.
Update promo codeUpdate a promo code.
Update promo ruleUpdate a promo rule.
Update storeUpdate a store.
Update templateUpdate the name, html, or `folder id` of an existing template.
Update template folderUpdate a specific folder used to organize templates.
Update webhookUpdate the settings for an existing webhook.
Update workflow emailUpdate settings for a classic automation workflow email.
Verify connected site scriptVerify that the connected sites script has been installed, either via the script url or fragment.
Verify domainVerify a domain for sending.
View default contentGet the sections that you can edit in a template, including each section's default content.
View recent activityGet a member's activity on a specific list, including opens, clicks, and unsubscribes.
View recent activity 50Get the last 50 events of a member's activity on a specific list, including opens, clicks, and unsubscribes.

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: ["mailchimp"],
  });

  const mcpUrl = session.mcp.url;
What's happening:
  • We're creating a Tool Router session that gives your agent access to Mailchimp 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 Mailchimp-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 Mailchimp 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 mailchimp, 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 Mailchimp 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 Mailchimp 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: ["mailchimp"],
  });

  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 mailchimp, 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 Mailchimp 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 Mailchimp MCP Agent with another framework

FAQ

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

With a standalone Mailchimp MCP server, the agents and LLMs can only access a fixed set of Mailchimp tools tied to that server. However, with the Composio Tool Router, agents can dynamically load tools from Mailchimp 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 Mailchimp tools.

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

Yes, absolutely. You can configure which Mailchimp 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 Mailchimp data and credentials are handled as safely as possible.

Used by agents from

Context
ASU
Letta
glean
HubSpot
Agent.ai
Altera
DataStax
Entelligence
Rolai
Context
ASU
Letta
glean
HubSpot
Agent.ai
Altera
DataStax
Entelligence
Rolai
Context
ASU
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.