|
| 1 | +/* eslint-disable callback-return */ |
| 2 | + |
| 3 | +const { randomUUID } = require('crypto'); |
| 4 | + |
| 5 | +const modules = Runtime.getFunctions(); |
| 6 | +const createServer = require(modules.server.path); |
| 7 | +const createReq = require(modules.req.path); |
| 8 | +const createRes = require(modules.res.path); |
| 9 | + |
| 10 | +const defaultMessaging = [ |
| 11 | + 'Api20100401Message', |
| 12 | + 'Api20100401IncomingPhoneNumber', |
| 13 | +]; |
| 14 | + |
| 15 | +const TWILIO_TAG_MAP = { |
| 16 | + Messaging: defaultMessaging, |
| 17 | + Voice: ['Api20100401Call'], |
| 18 | + VoiceAddOns: [ |
| 19 | + 'Api20100401Recording', |
| 20 | + 'Api20100401Transcription', |
| 21 | + 'Api20100401Conference', |
| 22 | + ], |
| 23 | + Conversations: [ |
| 24 | + 'ConversationsV1Conversation', |
| 25 | + 'ConversationsV1Message', |
| 26 | + 'ConversationsV1Participant', |
| 27 | + 'ConversationsV1Service', |
| 28 | + 'ConversationsV1User', |
| 29 | + ], |
| 30 | + Studio: [ |
| 31 | + 'StudioV2Execution', |
| 32 | + 'StudioV2ExecutionContext', |
| 33 | + 'StudioV2ExecutionStep', |
| 34 | + 'StudioV2Flow', |
| 35 | + 'StudioV2FlowRevision', |
| 36 | + 'StudioV2FlowValidate', |
| 37 | + ], |
| 38 | + TaskRouter: [ |
| 39 | + 'TaskrouterV1Activity', |
| 40 | + 'TaskrouterV1Event', |
| 41 | + 'TaskrouterV1Task', |
| 42 | + 'TaskrouterV1TaskChannel', |
| 43 | + 'TaskrouterV1TaskQueue', |
| 44 | + 'TaskrouterV1TaskReservation', |
| 45 | + 'TaskrouterV1Worker', |
| 46 | + 'TaskrouterV1WorkerChannel', |
| 47 | + 'TaskrouterV1WorkerReservation', |
| 48 | + 'TaskrouterV1Workflow', |
| 49 | + 'TaskrouterV1Workspace', |
| 50 | + 'TaskrouterV1WorkspaceStatistics', |
| 51 | + ], |
| 52 | + Serverless: [ |
| 53 | + 'ServerlessV1Asset', |
| 54 | + 'ServerlessV1AssetVersion', |
| 55 | + 'ServerlessV1Build', |
| 56 | + 'ServerlessV1Deployment', |
| 57 | + 'ServerlessV1Environment', |
| 58 | + 'ServerlessV1Function', |
| 59 | + 'ServerlessV1Service', |
| 60 | + 'ServerlessV1Variable', |
| 61 | + ], |
| 62 | + Account: ['Api20100401Account'], |
| 63 | + PhoneNumbers: ['Api20100401IncomingPhoneNumber', 'Api20100401Address'], |
| 64 | + Applications: ['Api20100401Application'], |
| 65 | + Auth: ['Api20100401Token'], |
| 66 | + AddOns: ['Api20100401AddOnResult'], |
| 67 | + Usage: ['Api20100401Usage'], |
| 68 | +}; |
| 69 | + |
| 70 | +const validateContext = (context, callback) => { |
| 71 | + if (!context.ACCOUNT_SID || !context.API_KEY || !context.API_SECRET) { |
| 72 | + const response = new Twilio.Response(); |
| 73 | + response.setStatusCode(400); |
| 74 | + response.setBody({ |
| 75 | + error: |
| 76 | + 'required context variables ACCOUNT_SID or API_KEY or API_SECRET not found', |
| 77 | + }); |
| 78 | + |
| 79 | + callback(null, response); |
| 80 | + |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + return true; |
| 85 | +}; |
| 86 | + |
| 87 | +const getTags = (event) => { |
| 88 | + if (!event.services) { |
| 89 | + return defaultMessaging; |
| 90 | + } |
| 91 | + |
| 92 | + const services = |
| 93 | + typeof event.services === 'string' ? [event.services] : event.services; |
| 94 | + // MCP does not like additoinal keys |
| 95 | + delete event.services; |
| 96 | + |
| 97 | + const tags = []; |
| 98 | + services.forEach((service) => { |
| 99 | + if (TWILIO_TAG_MAP[service]) { |
| 100 | + tags.push(...TWILIO_TAG_MAP[service]); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + if (tags.length === 0) { |
| 105 | + return ['Api20100401Message']; |
| 106 | + } |
| 107 | + |
| 108 | + return tags; |
| 109 | +}; |
| 110 | + |
| 111 | +exports.handler = async function (context, event, callback) { |
| 112 | + const id = randomUUID().substring(0, 6); |
| 113 | + console.log(`[${id}]`, 'STARTED MCP called with method', event.method); |
| 114 | + if (event.method === 'tools/call') { |
| 115 | + console.log( |
| 116 | + `[${id}]`, |
| 117 | + 'Calling tool', |
| 118 | + event.params.name, |
| 119 | + 'with arguments', |
| 120 | + event.params.arguments |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + if (!validateContext(context, callback)) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + try { |
| 129 | + const tags = getTags(event); |
| 130 | + const { transport } = await createServer(context, event, tags); |
| 131 | + const req = createReq(event); |
| 132 | + const res = createRes(callback); |
| 133 | + |
| 134 | + await transport.handleRequest(req, res); |
| 135 | + } catch (error) { |
| 136 | + console.log(`[${id}]`, 'FAILED MCP request with method', event.method); |
| 137 | + console.log(error); |
| 138 | + |
| 139 | + const response = new Twilio.Response(); |
| 140 | + response.setStatusCode(500); |
| 141 | + response.setBody({ error: error.message }); |
| 142 | + callback(null, response); |
| 143 | + } finally { |
| 144 | + console.log(`[${id}]`, 'COMPLETED MCP request with method', event.method); |
| 145 | + } |
| 146 | +}; |
0 commit comments