import { ToolHandler, ToolResult, Tool } from '../../src/types/index.js';
/**
* Example: Weather Tool
* This demonstrates how to create a new tool that can be added to the MCP server
*/
export class WeatherTool implements ToolHandler {
async execute(args: Record<string, any>): Promise<ToolResult> {
const { location } = args;
// In a real implementation, you would call a weather API here
const mockWeatherData = {
location,
temperature: Math.floor(Math.random() * 30) + 5, // Random temp between 5-35°C
condition: ['sunny', 'cloudy', 'rainy', 'snowy'][Math.floor(Math.random() * 4)],
humidity: Math.floor(Math.random() * 100),
};
return {
content: [
{
type: 'text',
text: `Weather in ${mockWeatherData.location}:\n` +
`Temperature: ${mockWeatherData.temperature}°C\n` +
`Condition: ${mockWeatherData.condition}\n` +
`Humidity: ${mockWeatherData.humidity}%`,
},
],
};
}
}
// Tool definition for registration
export const WEATHER_TOOL_DEFINITION: Tool = {
name: 'get_weather',
description: 'Get current weather information for a location',
inputSchema: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The location to get weather for',
},
},
required: ['location'],
},
};
/**
* To add this tool to your server:
*
* 1. Import the tool in your registry:
* import { WeatherTool, WEATHER_TOOL_DEFINITION } from '../examples/tools/example-tool.js';
*
* 2. Register it in the ToolRegistry constructor:
* this.registerTool('get_weather', new WeatherTool(), WEATHER_TOOL_DEFINITION);
*/