Examples

Common Patterns

Real-world examples and patterns for using the Nuxt MCP module.

Overview

This page demonstrates common patterns and real-world examples for using the Nuxt MCP module.

Calculator Tool

A simple calculator with multiple operations:

server/mcp/tools/calculator.ts
import { z } from 'zod'

export default defineMcpTool({
  name: 'calculator',
  description: 'Perform basic arithmetic operations',
  inputSchema: {
    a: z.number().describe('First number'),
    b: z.number().describe('Second number'),
    operation: z.enum(['add', 'subtract', 'multiply', 'divide']).describe('Operation to perform'),
  },
  outputSchema: {
    result: z.number(),
  },
  handler: async ({ a, b, operation }) => {
    let result: number

    switch (operation) {
      case 'add':
        result = a + b
        break
      case 'subtract':
        result = a - b
        break
      case 'multiply':
        result = a * b
        break
      case 'divide':
        if (b === 0) {
          return {
            content: [{
              type: 'text',
              text: 'Error: Division by zero',
            }],
            isError: true,
          }
        }
        result = a / b
        break
    }

    return {
      content: [{
        type: 'text',
        text: `${a} ${operation} ${b} = ${result}`,
      }],
      structuredContent: { result },
    }
  },
})

BMI Calculator

Calculate Body Mass Index with category:

server/mcp/tools/bmi.ts
import { z } from 'zod'

export default defineMcpTool({
  name: 'calculate-bmi',
  title: 'BMI Calculator',
  description: 'Calculate Body Mass Index from weight and height',
  inputSchema: {
    weightKg: z.number().min(0).describe('Weight in kilograms'),
    heightM: z.number().min(0).max(3).describe('Height in meters'),
  },
  outputSchema: {
    bmi: z.number(),
    category: z.string(),
  },
  handler: async ({ weightKg, heightM }) => {
    const bmi = weightKg / (heightM * heightM)

    let category = 'Normal'
    if (bmi < 18.5) category = 'Underweight'
    else if (bmi >= 25 && bmi < 30) category = 'Overweight'
    else if (bmi >= 30) category = 'Obese'

    const roundedBmi = Math.round(bmi * 100) / 100

    return {
      content: [{
        type: 'text',
        text: `BMI: ${roundedBmi} (${category})`,
      }],
      structuredContent: {
        bmi: roundedBmi,
        category,
      },
    }
  },
})

Text Processing Tool

Process and transform text:

server/mcp/tools/text-processor.ts
import { z } from 'zod'

export default defineMcpTool({
  name: 'process-text',
  description: 'Process and transform text',
  inputSchema: {
    text: z.string().describe('Text to process'),
    operation: z.enum(['uppercase', 'lowercase', 'reverse', 'word-count', 'char-count']).describe('Operation to perform'),
  },
  handler: async ({ text, operation }) => {
    let result: string | number

    switch (operation) {
      case 'uppercase':
        result = text.toUpperCase()
        break
      case 'lowercase':
        result = text.toLowerCase()
        break
      case 'reverse':
        result = text.split('').reverse().join('')
        break
      case 'word-count':
        result = text.split(/\s+/).filter(word => word.length > 0).length
        break
      case 'char-count':
        result = text.length
        break
    }

    return {
      content: [{
        type: 'text',
        text: typeof result === 'string' ? result : `Count: ${result}`,
      }],
      structuredContent: {
        result,
        operation,
      },
    }
  },
})

Prompt with Dynamic Content

Create a prompt that generates dynamic content:

server/mcp/prompts/dynamic-greeting.ts
import { z } from 'zod'

export default defineMcpPrompt({
  name: 'dynamic-greeting',
  title: 'Dynamic Greeting',
  description: 'Generate a personalized greeting',
  inputSchema: {
    name: z.string().describe('Person name'),
    language: z.enum(['en', 'fr', 'es']).default('en').describe('Greeting language'),
  },
  handler: async ({ name, language }) => {
    const greetings: Record<string, string> = {
      en: 'Hello',
      fr: 'Bonjour',
      es: 'Hola',
    }

    const hour = new Date().getHours()
    const timeOfDay = hour < 12 ? 'morning' : hour < 18 ? 'afternoon' : 'evening'

    const greeting = greetings[language] || greetings.en

    return {
      messages: [{
        role: 'user',
        content: {
          type: 'text',
          text: `${greeting}, ${name}! Good ${timeOfDay}!`,
        },
      }],
    }
  },
})

Next Steps