Skip to main contentSkip to navigation
← Back to documentation

Mastra Integration

Mastra(https://mastra.ai) is a TypeScript-first AI agent framework with native Model Context Protocol (MCP)(https://modelcontextprotocol.io) support.…

BuyWhere + Mastra

Mastra is a TypeScript-first AI agent framework with native Model Context Protocol (MCP) support. Because BuyWhere ships an MCP-compatible endpoint, you can connect BuyWhere tools to any Mastra agent in minutes.

What You Get

Once connected, your Mastra agent has access to:

ToolDescription
search_productsKeyword search with country, category, and price filters
get_productFull product details by ID
compare_productsSide-by-side comparison across merchants
get_dealsCurrent discounted products
find_best_priceCheapest option across all retailers
list_categoriesProduct category taxonomy

Prerequisites

Quick Start

npm install @mastra/core @mastra/mcp

const buywhere = new MastraMCPClient({
  name: 'buywhere',
  server: {
    url: new URL('https://api.buywhere.ai/mcp'),
    requestInit: {
      headers: {
        'Authorization': `Bearer ${process.env.BUYWHERE_API_KEY}`,
      },
    },
  },
});

const mastra = new Mastra({
  agents: {
    shoppingAgent: {
      name: 'Shopping Agent',
      instructions: `You are a helpful shopping assistant. Use BuyWhere tools to 
find products, compare prices across merchants, and identify the best deals 
in Singapore, US, and Southeast Asia. Always include affiliate links.`,
      model: {
        provider: 'ANTHROPIC',
        name: 'claude-3-5-sonnet-20241022',
      },
      tools: await buywhere.getTools(),
    },
  },
});

const agent = mastra.getAgent('shoppingAgent');
const result = await agent.text(
  'Find me the best price for AirPods Pro in Singapore'
);
console.log(result.text);

Full Working Example

Clone the runnable example repository:

git clone https://github.com/BuyWhere/buywhere-mastra-example
cd buywhere-mastra-example
npm install
BUYWHERE_API_KEY=bw_live_... npm start

The example demonstrates:

  • Connecting BuyWhere MCP to a Mastra agent
  • Multi-turn shopping conversations
  • Price comparison across merchants
  • Streaming responses

Step-by-Step Setup

1. Install dependencies

npm install @mastra/core @mastra/mcp @anthropic-ai/sdk

2. Configure the MCP client


const buywhere = new MastraMCPClient({
  name: 'buywhere',
  server: {
    url: new URL('https://api.buywhere.ai/mcp'),
    requestInit: {
      headers: {
        'Authorization': `Bearer ${process.env.BUYWHERE_API_KEY}`,
      },
    },
  },
});

// List available tools
const tools = await buywhere.getTools();
console.log('Available tools:', Object.keys(tools));

3. Create an agent


const mastra = new Mastra({
  agents: {
    shopping: {
      name: 'BuyWhere Shopping Agent',
      instructions: 'You help users find the best prices for products.',
      model: {
        provider: 'ANTHROPIC',
        name: 'claude-3-5-sonnet-20241022',
      },
      tools: await buywhere.getTools(),
    },
  },
});

const agent = mastra.getAgent('shopping');

4. Run queries

// Simple text query
const response = await agent.text('What laptops are available under SGD 2000?');
console.log(response.text);

// Streaming response
const stream = await agent.stream('Compare prices for iPhone 15 Pro');
for await (const chunk of stream.textStream) {
  process.stdout.write(chunk);
}

Environment Variables

VariableDescriptionRequired
BUYWHERE_API_KEYYour BuyWhere API keyYes

Error Handling

try {
  const tools = await buywhere.getTools();
  // ...
} catch (error) {
  if (error.message.includes('401')) {
    console.error('Invalid API key — check BUYWHERE_API_KEY');
  } else {
    console.error('BuyWhere connection failed:', error.message);
  }
}

Supported Regions

RegionCountry Codes
Singaporesg
Malaysiamy
Thailandth
Vietnamvn
Indonesiaid
United Statesus

Pass a region with the search_products tool:

// The agent passes parameters automatically based on your query:
await agent.text('Find gaming chairs in Malaysia under MYR 800');
// → searches with country_code: "my"

Resources

Get the best deals weekly

Price drops, new markets, and AI shopping tips — straight to your inbox.