import { Payments, EnvironmentName } from '@nevermined-io/payments'
import { z } from 'zod'
const payments = Payments.getInstance({
nvmApiKey: process.env.NVM_API_KEY!,
environment: 'sandbox' as EnvironmentName,
})
// Configure MCP
payments.mcp.configure({
agentId: process.env.NVM_AGENT_ID!,
serverName: 'weather-agent',
})
// Register multiple tools
payments.mcp.registerTool(
'get_current_weather',
{
title: 'Get Current Weather',
description: 'Get real-time weather for a location',
inputSchema: z.object({
city: z.string(),
country: z.string().optional(),
}),
},
async (args) => {
const weather = await fetchWeather(args.city, args.country)
return {
content: [{ type: 'text', text: JSON.stringify(weather) }],
}
},
{ credits: 1n }
)
payments.mcp.registerTool(
'get_forecast',
{
title: 'Get Weather Forecast',
description: 'Get 7-day weather forecast',
inputSchema: z.object({
city: z.string(),
days: z.number().min(1).max(7).default(3),
}),
},
async (args) => {
const forecast = await fetchForecast(args.city, args.days)
return {
content: [{ type: 'text', text: JSON.stringify(forecast) }],
}
},
{ credits: 2n } // Forecasts cost more
)
// Register resource
payments.mcp.registerResource(
'weather-alerts',
'weather://alerts',
{
title: 'Weather Alerts',
description: 'Active weather alerts',
},
async (uri) => {
const alerts = await fetchAlerts()
return {
contents: [{
uri: 'weather://alerts',
mimeType: 'application/json',
text: JSON.stringify(alerts),
}],
}
},
{ credits: 1n }
)
// Start server
const { info, stop } = await payments.mcp.start({
port: 5001,
agentId: process.env.NVM_AGENT_ID!,
serverName: 'weather-agent',
version: '1.0.0',
})
console.log(`Weather MCP Agent running at ${info.baseUrl}`)
console.log(`Register in Nevermined App with:`)
console.log(` - mcp://weather-agent/tools/*`)
console.log(` - mcp://weather-agent/resources/*`)
// Graceful shutdown
process.on('SIGINT', async () => {
await stop()
process.exit(0)
})
// Mock functions
async function fetchWeather(city: string, country?: string) {
return { city, temp: 72, condition: 'sunny' }
}
async function fetchForecast(city: string, days: number) {
return { city, days, forecast: [] }
}
async function fetchAlerts() {
return { alerts: [] }
}