Examples
API Integration
Integrate external APIs and use Nuxt server utilities in MCP tools.
Fetching External Data
Here's a simple tool that fetches data from a public API:
server/mcp/tools/get-weather.ts
import { z } from 'zod'
import { defineMcpTool } from '@nuxtjs/mcp-toolkit/server'
export default defineMcpTool({
description: 'Get current weather for a city',
inputSchema: {
city: z.string().describe('City name'),
},
cache: '15m',
handler: async ({ city }) => {
return await $fetch(`https://wttr.in/${city}?format=j1`)
},
})
Using Nuxt Server Utilities
To use Nuxt server utilities like useEvent() in your handlers, enable asyncContext:
nuxt.config.ts
export default defineNuxtConfig({
experimental: {
asyncContext: true,
},
})
Then you can access the H3 event and use Nuxt server composables:
server/mcp/tools/get-page.ts
import { z } from 'zod'
import { useEvent, createError } from 'h3'
import { queryCollection } from '@nuxt/content/server'
import { defineMcpTool } from '@nuxtjs/mcp-toolkit/server'
export default defineMcpTool({
description: 'Get a documentation page',
inputSchema: {
path: z.string().describe('Page path'),
},
cache: '1h',
handler: async ({ path }) => {
const event = useEvent()
const page = await queryCollection(event, 'docs')
.where('path', '=', path)
.first()
if (!page) throw createError({ statusCode: 404, message: 'Page not found' })
return page
},
})
useEvent() is auto-imported when asyncContext is enabled.Next Steps
- Common Patterns - More general examples
- File Operations - File operation examples
- Tools - Learn more about tools