import { createMCPServer } from 'mcp-use/server'
import express from 'express'
// Mock weather data
const weatherData: Record<string, any> = {
'new-york': { temp: 72, condition: 'Partly cloudy', humidity: 65 },
'london': { temp: 59, condition: 'Rainy', humidity: 80 },
'tokyo': { temp: 78, condition: 'Sunny', humidity: 55 },
'paris': { temp: 64, condition: 'Overcast', humidity: 70 }
}
// Create server
const server = createMCPServer('weather-mcp-server', {
version: '1.0.0',
description: 'A weather information MCP server'
})
// Add weather lookup tool
server.tool({
name: 'get_weather',
description: 'Get current weather for a city',
inputs: [
{
name: 'city',
type: 'string',
description: 'City name',
required: true
}
],
cb: async ({ city }) => {
const cityKey = city.toLowerCase().replace(' ', '-')
const data = weatherData[cityKey]
if (!data) {
return {
content: [{
type: 'text',
text: `Weather data not available for ${city}`
}]
}
}
return {
content: [{
type: 'text',
text: `Weather in ${city}:\n` +
`Temperature: ${data.temp}°F\n` +
`Condition: ${data.condition}\n` +
`Humidity: ${data.humidity}%`
}]
}
}
})
// Add alerts resource
server.resource({
name: 'weather_alerts',
uri: 'weather://alerts',
title: 'Current Weather Alerts',
mimeType: 'application/json',
readCallback: async () => ({
contents: [{
uri: 'weather://alerts',
mimeType: 'application/json',
text: JSON.stringify([
{
type: 'warning',
title: 'Heavy Rain Warning',
areas: ['London']
}
], null, 2)
}]
})
})
// Add health endpoint
server.get('/health', (req, res) => {
res.json({ status: 'healthy' })
})
// Start server
const PORT = 3000
server.listen(PORT).then(() => {
console.log(`Weather MCP Server running on port ${PORT}`)
console.log(`MCP endpoint: http://localhost:${PORT}/mcp`)
console.log(`Inspector: http://localhost:${PORT}/inspector`)
})