Skip to main content
Glama
TedTschopp

MCP Server on Cloudflare Workers & Azure Functions

by TedTschopp
azure-handler.ts4.04 kB
/** * 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, });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/TedTschopp/MCP-Server-on-CloudFlare'

If you have feedback or need assistance with the MCP directory API, please join our Discord server