Skip to content

Commit e27659b

Browse files
committed
Updating helper files around mcp-server template
1 parent 65b9e78 commit e27659b

File tree

3 files changed

+126
-27
lines changed

3 files changed

+126
-27
lines changed

mcp-server/README.md

Lines changed: 124 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
# mcp-server
22

3-
Functions to run a remote MCP server for Twilio API tools
4-
5-
## Pre-requisites
6-
7-
### Environment variables
8-
9-
This project requires some environment variables to be set. A file named `.env` is used to store the values for those environment variables. To keep your tokens and secrets secure, make sure to not commit the `.env` file in git. When setting up the project with `twilio serverless:init ...` the Twilio CLI will create a `.gitignore` file that excludes `.env` from the version history.
10-
11-
In your `.env` file, set the following values:
12-
13-
* ACCOUNT_SID
14-
* AUTH_TOKEN
15-
* API_KEY
16-
* API_SECRET
3+
Functions to run an MCP server for Twilio API tools
174

185
## Create a new project with the template
196

207
1. Install the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart#install-twilio-cli)
8+
9+
Recommended method, homebrew:
10+
```shell
11+
brew tap twilio/brew && brew install twilio
12+
twilio login
13+
```
14+
2115
2. Install the [serverless toolkit](https://www.twilio.com/docs/labs/serverless-toolkit/getting-started)
2216

2317
```shell
@@ -26,37 +20,142 @@ twilio plugins:install @twilio-labs/plugin-serverless
2620

2721
3. Initiate a new project
2822

29-
```
23+
```shell
3024
twilio serverless:init example --template=mcp-server && cd example
3125
```
3226

33-
4. Start the server with the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart):
27+
4. Set environment variables
3428

35-
```
29+
This MCP server uses API keys to authenticate the Twilio API requests. A file named `.env` is used to store the values for those environment variables.
30+
31+
In your `.env` file, set the following values:
32+
33+
* API_KEY
34+
* API_SECRET
35+
36+
ℹ️ You can generate a new API key in the [Twilio Console](https://www.twilio.com/console/project/api-keys)
37+
38+
5. Start the server with the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart):
39+
40+
```shell
3641
twilio serverless:start
3742
```
3843

3944
ℹ️ Check the developer console and terminal for any errors, make sure you've set your environment variables.
4045

46+
6. Test your local MCP server using the [MCP Inspector](https://modelcontextprotocol.io/docs/tools/inspector)
47+
48+
```shell
49+
npx @modelcontextprotocol/inspector
50+
```
51+
52+
For local testing, use the following configuration:
53+
54+
* **Transport Type**: Streamable HTTP
55+
* **URL**: http://localhost:{port}/mcp
56+
57+
ℹ️ When testing the protected /mcp endpoint locally, no authentication header is required.
58+
4159
## Deploying
4260

43-
Deploy your functions and assets with either of the following commands. Note: you must run these commands from inside your project folder. [More details in the docs.](https://www.twilio.com/docs/labs/serverless-toolkit)
61+
Deploy your functions with the following command. Note: you must run this command from inside your project folder. [More details in the docs.](https://www.twilio.com/docs/labs/serverless-toolkit)
4462

4563
With the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart):
4664

47-
```
65+
```shell
4866
twilio serverless:deploy
4967
```
5068

69+
### Testing your MCP server
70+
71+
🐛 We're aware of a reported issue with the MCP Inspector project and the authentication header. You'll need to use your MCP client to connect to your deployed MCP server.
72+
5173
## Integration with MCP clients
5274

53-
`https://<functions-domain>.twil.io/mcp?services=`
75+
* **URL**: `https://{functions-domain}.twil.io/mcp?services=`
76+
* **Authentication Header**:
77+
* Key: `x-twilio-signature`
78+
* Value: {Valid Twilio signature}
79+
80+
### Code samples for generating a Twilio signature
81+
82+
Learn more about the use of `x-twilio-signature` header for executing `protected` Functions. https://www.twilio.com/docs/serverless/functions-assets/visibility#protected
83+
84+
The sample code below assuming that the following environment variables are set:
85+
* TWILIO_AUTH_TOKEN
86+
* TWILIO_BASE_DOMAIN
87+
88+
#### JavaScript
89+
90+
```javascript
91+
const crypto = require("crypto");
92+
const dotenv = require("dotenv");
93+
94+
dotenv.config();
95+
96+
function getSignature(authToken, url, params) {
97+
var data = Object.keys(params)
98+
.sort()
99+
.reduce((acc, key) => acc + key + params[key], url);
100+
101+
return (
102+
crypto
103+
.createHmac("sha1", authToken)
104+
.update(Buffer.from(data, "utf-8"))
105+
.digest("base64")
106+
);
107+
}
108+
109+
const signature =
110+
twilio.getExpectedTwilioSignature(
111+
process.env.TWILIO_AUTH_TOKEN,
112+
`${process.env.TWILIO_DOMAIN_NAME}/mcp`,
113+
{}
114+
);
115+
```
116+
117+
Or, with the `twilio` Node package installed:
118+
119+
```javascript
120+
const twilio = require("twilio");
121+
const dotenv = require("dotenv");
122+
123+
dotenv.config();
124+
125+
const signature =
126+
twilio.getExpectedTwilioSignature(
127+
process.env.TWILIO_AUTH_TOKEN,
128+
`${process.env.TWILIO_DOMAIN_NAME}/mcp`,
129+
{}
130+
);
131+
```
132+
133+
#### Python
134+
135+
```python
136+
import os
137+
from dotenv import load_dotenv
138+
from twilio.request_validator import RequestValidator
139+
140+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
141+
base_domain = os.environ.get("TWILIO_DOMAIN_NAME")
142+
143+
url = f"{base_domain}/mcp?services=Messaging"
144+
validator = RequestValidator(auth_token)
145+
signature = validator.compute_signature(url, {})
146+
```
147+
148+
### Services
149+
150+
Use the querystring parameter `?services=` to specify a set of tools based on the needs of your MCP client. Set multiple services by passing multiple `services` key/value pairs.
151+
152+
**Examples:**
54153

55-
Header: x-twilio-signature
154+
* `/mcp?services=Voice`
155+
* `/mcp?services=Messaging&services=PhoneNumbers`
56156

57-
@TODO: Code samples to generate x-twilio-signature
157+
**Available services:**
58158

59-
Available services
60159
* Messaging (default)
61160
* Voice
62161
* VoiceAddOns
@@ -77,8 +176,8 @@ Available services
77176

78177
## Security recommendations
79178

80-
This remote MCP server function will provide Tools to your LLM that provide access to your Twilio account. We recommend the following considerations when giving clients access to your server:
179+
This MCP server function will provide Tools to your LLM that provide access to your Twilio account. We recommend the following considerations when giving clients access to your server:
81180

82-
- Always set the `requires_approval` field to ensure that there are no unintended actions taken within your account.
181+
- Always set your MCP client to require tool approval to ensure that there are no unintended actions taken within your account.
83182
- Use scoped permissions for your Twilio API Key. Not all endpoints support scoped permissions, but some do. See https://www.twilio.com/docs/iam/api-keys/restricted-api-keys for more information about which actions are supported per API Service.
84183
- To ensure privacy, do not use other MCP servers in conjunction with your Twilio MCP server.

mcp-server/assets/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ <h1>
3939
<img src="https://twilio-labs.github.io/function-templates/static/v1/success.svg" />
4040
<div>
4141
<p>Welcome!</p>
42-
<p>Your remote MCP server!</p>
42+
<p>Your MCP server!</p>
4343
</div>
4444
</h1>
4545
<section>

templates.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@
353353
{
354354
"id": "mcp-server",
355355
"name": "MCP Server",
356-
"description": "Functions to run a remote MCP server for Twilio API tools"
356+
"description": "Functions to run an MCP server for Twilio API tools"
357357
}
358358
]
359359
}

0 commit comments

Comments
 (0)