Skip to main content
Glama
weather.tool.ts2.8 kB
import { Injectable } from '@nestjs/common'; import { QueryBus, CommandBus } from '@nestjs/cqrs'; import { Tool, Context } from '@rekog/mcp-nest'; import { CreateWeatherCommand } from 'src/application/weather/commands/create-weather/create-weather.command'; import { CreateWeatherResponse } from 'src/application/weather/commands/create-weather/create-weather.response'; import { GetWeathersQuery } from 'src/application/weather/queries/get-weathers/get-weathers.query'; import { GetWeatherResponse } from 'src/application/weather/queries/get-weathers/get-weathers.response'; import { z } from 'zod'; @Injectable() export class WeatherTool { constructor( private _queryBus: QueryBus, private _commandBus: CommandBus, ) { } @Tool({ name: 'weather-tool', description: 'Returns weather information with progress updates', parameters: z.object({ name: z.string().default('World'), }), }) async greetWithWeather({ name }, context: Context) { await context.reportProgress({ progress: 50, total: 100, message: 'Fetching weather data...' }); const result = await this._queryBus.execute<GetWeathersQuery, GetWeatherResponse[]>(new GetWeathersQuery()) await context.reportProgress({ progress: 100, total: 100 }); return `Hello, ${name}! The weather is ${result[0].temperature}.`; } @Tool({ name: 'weather-tool-interactive', description: 'Create new weather data interactively', parameters: z.object({ name: z.string(), }), }) async interactiveTemperature({ name }, context: Context) { const response = await context.mcpServer.server.elicitInput({ message: 'Do you want to create a new temperature?', requestedSchema: { type: 'object', properties: { newTemperature: { type: 'boolean', description: 'Do you want to create a new temperature?', }, temperature: { type: 'number', description: 'The temperature to create', } }, }, }); const createNewTemperature = response.action === 'accept' ? response.content.newTemperature : false; const weather = new CreateWeatherCommand(); weather.date = new Date().toISOString(); weather.temperature = createNewTemperature ? response.content.temperature as number : 0; const result = await this._commandBus.execute<CreateWeatherCommand, CreateWeatherResponse>(weather); return `Nice, ${name}! The new temperature is ${result.temperature}°C.`; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/RenzoReccio/nestjs-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server