Skip to main content
Glama
mcp-bridge.ts6.32 kB
import { Handler } from '@netlify/functions'; // Serverless version of the MCP bridge for Netlify Functions const API_KEY = process.env.MCP_API_KEY || 'mcp_default_key'; // In-memory storage (resets on each function invocation) // For production, use Netlify Blobs or external database let knowledgeBase: any = { personal: {}, organizational: {} }; let activeAgent = 'default'; // Agent templates (simplified for serverless) const agents: Record<string, any> = { default: { name: "Default Assistant", role: "General Purpose AI Assistant", instructions: "You are a helpful assistant." }, "it-expert": { name: "IT Expert", role: "Information Technology Specialist", instructions: "You are an IT expert with deep knowledge of systems, networks, and infrastructure." }, "ethical-hacker": { name: "Ethical Hacker", role: "Cybersecurity Professional", instructions: "You are an ethical hacker specializing in penetration testing and security." } }; // Helper to parse JSON body const parseBody = (body: string | null) => { if (!body) return {}; try { return JSON.parse(body); } catch { return {}; } }; // CORS headers const headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'Content-Type, Authorization', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Content-Type': 'application/json' }; export const handler: Handler = async (event, context) => { // Handle CORS preflight if (event.httpMethod === 'OPTIONS') { return { statusCode: 200, headers, body: '' }; } // Check API key const authHeader = event.headers.authorization; if (!authHeader || !authHeader.startsWith('Bearer ')) { return { statusCode: 401, headers, body: JSON.stringify({ error: 'Missing or invalid API key' }) }; } const providedKey = authHeader.substring(7); if (providedKey !== API_KEY) { return { statusCode: 401, headers, body: JSON.stringify({ error: 'Invalid API key' }) }; } const path = event.path.replace('/.netlify/functions/mcp-bridge', ''); const method = event.httpMethod; const body = parseBody(event.body); try { // Route handling if (path === '/health' && method === 'GET') { return { statusCode: 200, headers, body: JSON.stringify({ status: 'healthy', version: '1.0.0' }) }; } if (path === '/onboard' && method === 'POST') { const { name, role, preferences } = body; knowledgeBase.personal = { name: name || 'User', role: role || 'User', preferences: preferences || {}, createdAt: new Date().toISOString() }; return { statusCode: 200, headers, body: JSON.stringify({ success: true, message: `Welcome ${name}! Your profile has been created.`, profile: knowledgeBase.personal }) }; } if (path === '/agents' && method === 'GET') { return { statusCode: 200, headers, body: JSON.stringify({ agents: Object.keys(agents), active: activeAgent, details: agents }) }; } if (path === '/agents/switch' && method === 'POST') { const { agent } = body; if (!agents[agent]) { return { statusCode: 404, headers, body: JSON.stringify({ error: 'Agent not found' }) }; } activeAgent = agent; return { statusCode: 200, headers, body: JSON.stringify({ success: true, message: `Switched to ${agents[agent].name}`, agent: agents[agent] }) }; } if (path === '/knowledge' && method === 'GET') { return { statusCode: 200, headers, body: JSON.stringify(knowledgeBase) }; } if (path === '/knowledge' && method === 'PUT') { const { category, key, value } = body; if (!knowledgeBase[category]) { knowledgeBase[category] = {}; } knowledgeBase[category][key] = value; return { statusCode: 200, headers, body: JSON.stringify({ success: true, message: 'Knowledge updated', knowledge: knowledgeBase }) }; } if (path === '/chat' && method === 'POST') { const { message, command } = body; // Handle special commands if (command === 'mcp-instruct-onboarding') { return { statusCode: 200, headers, body: JSON.stringify({ type: 'onboarding', message: 'Welcome to MCP Instruct! Please provide your name and role.', fields: ['name', 'role', 'preferences'] }) }; } if (command?.startsWith('switch:')) { const agentName = command.replace('switch:', ''); if (!agents[agentName]) { return { statusCode: 404, headers, body: JSON.stringify({ error: 'Agent not found' }) }; } activeAgent = agentName; return { statusCode: 200, headers, body: JSON.stringify({ type: 'agent_switch', message: `Switched to ${agents[agentName].name}`, agent: agents[agentName] }) }; } // Regular chat response return { statusCode: 200, headers, body: JSON.stringify({ type: 'response', message: `${agents[activeAgent].name} received: ${message}`, agent: activeAgent, context: knowledgeBase }) }; } // 404 for unmatched routes return { statusCode: 404, headers, body: JSON.stringify({ error: 'Endpoint not found' }) }; } catch (error) { console.error('Error:', error); return { statusCode: 500, headers, body: JSON.stringify({ error: 'Internal server error' }) }; } };

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/hlsitechio/mcp-instruct'

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