#!/usr/bin/env node
import express from 'express';
import cors from 'cors';
import { CalendarData } from './calendar-data.js';
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import { auditLogMiddleware } from './auth.js';
const app = express();
const PORT = parseInt(process.env.PORT || '3000', 10);
const calendarData = new CalendarData();
app.use(cors());
app.use(express.json());
// Create MCP server instance
const server = new Server(
{
name: 'school-vacation-mcp',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
// Setup tool handlers
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: 'check_school_vacation',
description: 'Check if a specific date is a school vacation day in a given region',
inputSchema: {
type: 'object',
properties: {
date: {
type: 'string',
description: 'Date in DD/MM/YYYY format (e.g., "01/01/2019")',
},
region: {
type: 'string',
description: 'Region to check (flanders, wallonia, north-netherlands, middle-netherlands, south-netherlands, luxembourg)',
enum: ['flanders', 'wallonia', 'north-netherlands', 'middle-netherlands', 'south-netherlands', 'luxembourg']
},
},
required: ['date', 'region'],
},
},
{
name: 'get_vacation_periods',
description: 'Get all school vacation periods for a region, optionally filtered by year',
inputSchema: {
type: 'object',
properties: {
region: {
type: 'string',
description: 'Region to get vacation periods for',
enum: ['flanders', 'wallonia', 'north-netherlands', 'middle-netherlands', 'south-netherlands', 'luxembourg']
},
year: {
type: 'number',
description: 'Optional year to filter vacation periods (2019-2028)',
minimum: 2019,
maximum: 2028
},
},
required: ['region'],
},
},
{
name: 'get_supported_regions',
description: 'Get list of all supported regions for school vacation lookups',
inputSchema: {
type: 'object',
properties: {},
},
},
],
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case 'check_school_vacation': {
const { date, region } = args as { date: string; region: string };
const isVacation = calendarData.isSchoolVacation(date, region);
return {
content: [
{
type: 'text',
text: JSON.stringify({
date,
region,
isSchoolVacation: isVacation,
message: isVacation
? `${date} is a school vacation day in ${region}`
: `${date} is not a school vacation day in ${region}`
}, null, 2),
},
],
};
}
case 'get_vacation_periods': {
const { region, year } = args as { region: string; year?: number };
const periods = calendarData.getVacationPeriods(region, year);
return {
content: [
{
type: 'text',
text: JSON.stringify({
region,
year: year || 'all years',
vacationPeriods: periods,
totalPeriods: periods.length
}, null, 2),
},
],
};
}
case 'get_supported_regions': {
const regions = calendarData.getSupportedRegions();
return {
content: [
{
type: 'text',
text: JSON.stringify({
supportedRegions: regions,
description: 'These are the available regions for school vacation lookups'
}, null, 2),
},
],
};
}
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error) {
return {
content: [
{
type: 'text',
text: JSON.stringify({
error: error instanceof Error ? error.message : 'Unknown error occurred',
tool: name,
arguments: args
}, null, 2),
},
],
isError: true,
};
}
});
// Health check endpoint
app.get('/health', (_req, res) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
service: 'school-vacation-mcp',
mode: 'stateless-http'
});
});
// Ping endpoint
app.get('/ping', (_req, res) => {
res.json({
message: 'pong',
timestamp: new Date().toISOString()
});
});
// Stateless HTTP endpoint for MCP (no SSE, pure JSON responses)
app.post('/mcp', auditLogMiddleware, async (req, res) => {
console.log('Stateless HTTP MCP request received');
try {
// Create a new stateless transport for each request
// sessionIdGenerator: undefined means stateless mode - no sessions
// enableJsonResponse: true means JSON responses instead of SSE streams
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
enableJsonResponse: true,
});
// Connect the transport to the MCP server
await server.connect(transport);
// Handle the request - returns JSON response, not SSE stream
await transport.handleRequest(req, res, req.body);
} catch (error) {
console.error('Error handling HTTP request:', error);
if (!res.headersSent) {
res.status(500).json({
jsonrpc: '2.0',
error: {
code: -32603,
message: 'Internal server error',
data: error instanceof Error ? error.message : 'Unknown error'
},
id: null
});
}
}
});
// Handle DELETE for session termination (return 405 since we're stateless)
app.delete('/mcp', (_req, res) => {
res.status(405).json({
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Method not allowed - server is stateless'
},
id: null
});
});
// Handle GET for SSE (return 405 since we're stateless, no SSE)
app.get('/mcp', (_req, res) => {
res.status(405).json({
jsonrpc: '2.0',
error: {
code: -32000,
message: 'SSE not supported - server uses stateless HTTP mode'
},
id: null
});
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`School Vacation MCP Server (Stateless HTTP) running on port ${PORT}`);
console.log(`Health check: http://localhost:${PORT}/health`);
console.log(`MCP endpoint: http://localhost:${PORT}/mcp`);
console.log('Mode: Stateless HTTP (no SSE, pure JSON responses)');
console.log('Waiting for connections...');
});