Here's a simple tool that fetches data from a public API:
import { z } from 'zod'
export default defineMcpTool({
description: 'Get current weather for a city',
inputSchema: {
city: z.string().describe('City name'),
},
cache: '15m',
handler: async ({ city }) => {
try {
const data = await $fetch(`https://wttr.in/${city}?format=j1`)
return {
content: [{
type: 'text',
text: JSON.stringify(data, null, 2),
}],
}
}
catch (error) {
return {
content: [{
type: 'text',
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
}],
isError: true,
}
}
},
})
To use Nuxt server utilities like useEvent() in your handlers, enable asyncContext:
export default defineNuxtConfig({
experimental: {
asyncContext: true,
},
})
Then you can access the H3 event and use Nuxt server composables:
import { z } from 'zod'
import { queryCollection } from '@nuxt/content/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) {
return {
content: [{ type: 'text', text: 'Page not found' }],
isError: true,
}
}
return {
content: [{ type: 'text', text: JSON.stringify(page, null, 2) }],
}
},
})
useEvent() is auto-imported when asyncContext is enabled.