/**
* Azure Functions Handler for MCP Server
*
* This module provides an Azure Functions-compatible handler that wraps
* the MCP server implementation for deployment to Azure Functions.
*
* @module azure-handler
*/
import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
/**
* Azure Functions HTTP Trigger Handler
*
* This function handles all HTTP requests to the Azure Function and routes
* them appropriately. It supports the same endpoints as the Cloudflare Workers
* version: /health, /mcp, CORS, and default responses.
*
* @param req - HTTP request object
* @param context - Azure Functions invocation context
* @returns HTTP response
*/
async function httpTrigger(
req: HttpRequest,
context: InvocationContext
): Promise<HttpResponseInit> {
// Extract path from URL
const url = new URL(req.url);
const path = url.pathname;
const method = req.method.toUpperCase();
context.log(`HTTP ${method} ${path}`);
// CORS headers for all responses
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
// Handle CORS preflight requests
if (method === 'OPTIONS') {
return {
status: 204,
headers: corsHeaders,
};
}
try {
// Health check endpoint
if (path === '/' && method === 'GET') {
return {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders,
},
jsonBody: {
status: 'healthy',
timestamp: new Date().toISOString(),
version: '1.0.0',
platform: 'azure-functions',
capabilities: ['tools', 'prompts', 'resources', 'logging'],
},
};
}
// Dedicated health endpoint
if (path === '/health' && method === 'GET') {
return {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders,
},
jsonBody: {
status: 'healthy',
timestamp: new Date().toISOString(),
version: '1.0.0',
platform: 'azure-functions',
},
};
}
// MCP protocol endpoint
if (path === '/mcp' && method === 'POST') {
const body = await req.json() as any;
context.log('MCP request received:', body);
// TODO: Implement full MCP protocol handling here
// For now, echo back the request with metadata
return {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders,
},
jsonBody: {
jsonrpc: '2.0',
id: body?.id || null,
result: {
received: body,
message: 'MCP protocol endpoint - Full implementation pending',
capabilities: ['tools', 'prompts', 'resources', 'logging'],
},
},
};
}
// Default response for unknown routes
return {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders,
},
jsonBody: {
name: 'cloudflare-mcp-server',
version: '1.0.0',
platform: 'azure-functions',
description: 'Model Context Protocol server running on Azure Functions',
endpoints: {
health: '/health',
mcp: '/mcp (POST)',
},
capabilities: ['tools', 'prompts', 'resources', 'logging'],
},
};
} catch (error) {
context.error('Error handling request:', error);
return {
status: 500,
headers: {
'Content-Type': 'application/json',
...corsHeaders,
},
jsonBody: {
error: 'Internal server error',
message: error instanceof Error ? error.message : 'Unknown error',
},
};
}
}
// Register the HTTP trigger
app.http('httpTrigger', {
methods: ['GET', 'POST', 'OPTIONS'],
authLevel: 'anonymous',
route: '{*segments}',
handler: httpTrigger,
});