Deploy your Own Twilio MCP Server with Serverless Functions
Time to read: 7 minutes
Model Context Protocol, or MCP, standardizes how apps can provide context to LLMs. You can speed up your development by deploying a Twilio MCP server, then connecting it to agents with support for remote MCP servers. Our friends at OpenAI just added support for Remote MCPs to their Responses API, so we want to show you an example of developing with the Responses API and your Twilio MCP server.
In this tutorial, we’ll show you how to get set up, deploy, and then use your own Twilio MCP server. You’ll first run it locally to populate a few necessary variables, then deploy it to Twilio Functions, and finally use it with the OpenAI Responses API to purchase a phone number and send an SMS to the Twilio Virtual Phone.
We’ll provide code to use this setup with the Node.js OpenAI SDK for the tutorial (but you could use any of their supported SDKs if you’re comfortable writing your own). Let’s get started.
We also have a video tutorial on our TwilioDevs YouTube channel:
Prerequisites
Before completing this tutorial, you’ll need a Twilio account and a few tools installed. Here’s what you’ll need:
- Twilio Account (you can sign up for free, here)
- The Twilio CLI installed on your machine (before using the Twilio Serverless Toolkit). See the instructions below.
- The Twilio Serverless Toolkit for this tutorial. (If you don’t have this installed already, it’ll be installed in the steps below)
- An OpenAI API Key to use with the Responses API.
In a new terminal window, run these two commands to install the prerequisite CLI tools.
Deploy the Server
If you have never used the Serverless Toolkit or Twilio Functions before, this process may seem magical. To create the server, we are going to first fetch the Twilio MCP server code and run it locally in an environment that simulates the deployed environment. Deploying to production is a straightforward process through the CLI, and in the end, you’ll receive a URL for your MCP server that is hosted on Twilio’s serverless Functions.
Initialize the Function Template
Lets first initialize our serverless directory and create the code needed for the MCP server. The --template
flag tells the serverless plugin which code to start out with. In this case, it’s the MCP server function template that we will be installing (feel free to check out the code at this point and come back).


Set Environment Variables
We will need to set some variables to use in the deployment process and the runtime environment of the function. cd mcp-tutorial
into your new directory; there will be an .env file waiting for us. Open the .env file and you’ll see:
The ACCOUNT_SID
and AUTH_TOKEN
may already be filled in (if you authenticated when you first ran the CLI), but can be found in the Console.
Your API_KEY
and API_SECRET
can be generated in the account management page.
Once created, the API Key we want to use in our .env
file is the same value as the SID.
By default, the toolkit will add .env
to the .gitignore
file, so no need to worry about accidentally committing this file.
Start the server locally
We’re now ready to run our MCP server. Run twilio serverless:start
and you should see output that lists the Twilio Functions and Twilio Assets we have available.


That’s it! Your MCP server is now running locally.
Let’s connect to it with the MCP Inspector and take a look at what our server exposes. In a separate terminal, run npx @modelcontextprotocol/inspector
. When it’s started up, the URL for the UI will be shown.


Navigate to the UI and in the left side pane, for Transport Type select Streamable HTTP and for the URL, enter http://localhost:3000/mcp
(your port may be different). In local mode, we don’t need to worry about the authentication just yet. Click Connect and you should see the green Connected icon.


Using the MCP Inspector, we can view the tools and resources available through our server. By default, the MCP server exposes a small subset of our larger API. Navigate to the Tools tab and click List Tools. You should see the list of tools available by default.


Let’s try accessing another set of APIs.
Change the Connection URL to http://localhost:3000/mcp?services=Serverless&services=Studio
and hit Reconnect. Now when we list the tools, we see both the Serverless and Studio API tools. The full list of filterable services can be found in the repo.
Deploying the server
The Twilio Serverless Toolkit can also handle deploying our functions for us. Our MCP server will be deployed as a serverless function, and we’ll get back the URL where we can access it.
Now run:
This can take a minute to build and deploy, but when it’s finished, you’ll see links to your functions in the output. When you visit the index.html
page listed under Assets, you should see a list of next steps.


Excellent! You now have a Twilio MCP server running on Twilio Functions. Now, let’s hook it up to the OpenAI Responses API to see how it works.
Use the server with the OpenAI Responses API
OpenAI’s Responses API is their API primitive for using OpenAI’s tools to build agents. It has support for remote MCP servers – and you’re now hosting a Twilio MCP server, so the possibilities are endless.
We’re going to write a short script to send our first MCP server-assisted response with the Node SDK from OpenAI. In a new terminal window, create a new directory, run npm init and set your package type to a “module”.
Install a few dependencies.
At this point your package.json looks like this:
Add the following .env file with the correct values from your accounts with Twilio and OpenAI.
You can use the following bit of code to send your first request to OpenAI using your Twilio MCP server. Copy and paste this into a new file called send_request.js at the root of mcp-demo-app. Replace the YOUR_MCP_URL_DOMAIN
with the domain of your deployed server (the same domain we used in the inspector, previously).
We’re all set to send our first request! Before we do, note that with the example prompt, two things should happen:
- It should purchase a phone number in your Twilio account
- Note: This is a paid action, but if you have phone numbers already available in your account with SMS capabilities you’d like to use, you can change the input prompt to use an existing phone number. Try modifying the prompt to start with “Use my existing Twilio phone number <number> to send an SMS…”.
- It should use this phone number to send an SMS on your behalf.
- We are sending this message to the Twilio Virtual Phone which does not require us to set up the new phone number with 10DLC registration. Normally, you’d have to do that first before sending an SMS to a live phone number.
Run the script with node send_request.js
, and after a short wait, you should get confirmation that the text message was sent.


Congratulations!! 🎉 You just set up your own remote MCP Server hosted on Twilio Functions and used the Responses API to purchase a new phone number and send a text message. (The future is wild, right?)
We can verify these actions using the Console (or CLI). In your Twilio Console, you will see your new phone number in the number management page. Here’s the one that was created for me:


We can verify that the message was sent successfully by looking in the message logs for SMS.


Visibility
Now that you’ve sent our first message (or, if you’re like me and got excited, several by this point!) we can dig a bit into where we can view the logs from our server. Serverless Functions offer us Live Log Tailing with the Serverless Toolkit, and also from inside the Console Functions UI.
To start live tailing your logs with the toolkit, run twilio serverless:logs --tail
. (This will tail all of the logs in your account, so if you have other running functions, you may get logs from those too.)
You can pass in a specific function to tail using the --function-sid
flag. Run your script again and logs should start appearing in your terminal.


To view the logs live in the Console, we first need to change the edit visibility of our newly created service. Navigate to the Functions UI and view your services list. Find the SID found for our mcp-tutorial
service, copy it, and then run with:
twilio api:serverless:v1:services:update --sid <YOUR_SERVICE_SID> --ui-editable
Once that's done, open up the Functions IDE by clicking on the service name. You should now see an IDE with your Functions and Assets.Toggle the Live logs switch in the bottom right corner to ON.


Now, when you run the script again, we’ll see the logs coming into the bottom pane of our IDE.


Next Steps
This is just the start of what you can do with your MCP server. Next, try adding other services with prompts using something like “use Studio to build an IVR” or “use Serverless Assets to create a single page application hosted on Functions”. You can even write more complicated prompts that combine services, like “create a Studio Flow that handles answering phone calls, then transcribes and sends me the conversation when it has ended.”
You can find other samples that are ready to run in our openai-mcp-samples repository.
Whatever it is you build with your Twilio MCP server and the Responses API, we can’t wait to see it!
Vinnie Giarrusso is a Principal Engineer on Twilio’s Emerging Technology & Innovation team. He is based in the Bay Area, California and spends his free time playing music and hiking with his dogs. You can reach him at vgiarrusso [at] twilio.com
Kousha Talebian is a Principal Engineer from Vancouver, BC, working on the Emerging Technology and Innovation team. You can reach him at ktalebain [at] twilio.com . Outside of work, Kousha enjoys running with his dogs and experimenting with interesting cuisines worldwide.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.