Skip to content

Commit 436df48

Browse files
authored
Merge pull request #597 from bpartridge83/update-mcp-server-template-001
MCP Server: Documentation Update
2 parents 65b9e78 + bed5fbc commit 436df48

File tree

3 files changed

+152
-27
lines changed

3 files changed

+152
-27
lines changed

mcp-server/README.md

Lines changed: 150 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,168 @@ 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+
ℹ️ As the generated signature depends on the full Function endpoint that you are executing, and the URL includes the set of services (e.g. `/mcp?services=PhoneNumbers` vs `/mcp?services=Messaging`), you will need to generate a valid signature every time you update any part of the URL. We recommend dynamically generating the signature whenever you initialize the MCP client configuration with your Twilio MCP server.
89+
90+
#### JavaScript
91+
92+
```javascript
93+
const crypto = require("crypto");
94+
const dotenv = require("dotenv");
95+
96+
dotenv.config();
97+
98+
function getSignature(authToken, url, params) {
99+
var data = Object.keys(params)
100+
.sort()
101+
.reduce((acc, key) => acc + key + params[key], url);
102+
103+
return (
104+
crypto
105+
.createHmac("sha1", authToken)
106+
.update(Buffer.from(data, "utf-8"))
107+
.digest("base64")
108+
);
109+
}
110+
111+
const signature =
112+
twilio.getExpectedTwilioSignature(
113+
process.env.TWILIO_AUTH_TOKEN,
114+
`${process.env.TWILIO_DOMAIN_NAME}/mcp?services=...`,
115+
{}
116+
);
117+
```
118+
119+
or, with the [`twilio`](https://www.npmjs.com/package/twilio) Node package installed:
120+
121+
```javascript
122+
const twilio = require("twilio");
123+
const dotenv = require("dotenv");
124+
125+
dotenv.config();
126+
127+
const signature =
128+
twilio.getExpectedTwilioSignature(
129+
process.env.TWILIO_AUTH_TOKEN,
130+
`${process.env.TWILIO_DOMAIN_NAME}/mcp?services=...`,
131+
{}
132+
);
133+
```
134+
135+
or, with the [`twilio-signature`](https://www.npmjs.com/package/twilio-signature) Node package installed, a lightweight alternative:
136+
137+
```javascript
138+
import { createTwilioSignature } from 'twilio-signature';
139+
const dotenv = require("dotenv");
140+
141+
dotenv.config();
142+
143+
const signature =
144+
createTwilioSignature(
145+
process.env.TWILIO_AUTH_TOKEN,
146+
`${process.env.TWILIO_DOMAIN_NAME}/mcp?services=...`,
147+
{}
148+
);
149+
```
150+
151+
#### npx with [`twilio-signature-cli`](https://www.npmjs.com/package/twilio-signature-cli)
152+
153+
This CLI tool makes it easy to generate Twilio signatures for webhook validation without writing any code. It's perfect for testing and debugging Twilio webhook integrations, or for generating signatures in automated scripts.
154+
155+
```shell
156+
npx twilio-signature-cli -t TWILIO_AUTH_TOKEN -u YOUR_FUNCTION_URL
157+
```
158+
159+
#### Python
160+
161+
```python
162+
import os
163+
from dotenv import load_dotenv
164+
from twilio.request_validator import RequestValidator
165+
166+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
167+
base_domain = os.environ.get("TWILIO_DOMAIN_NAME")
168+
169+
url = f"{base_domain}/mcp?services=..."
170+
validator = RequestValidator(auth_token)
171+
signature = validator.compute_signature(url, {})
172+
```
173+
174+
### Filtering tools by service
175+
176+
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.
177+
178+
**Examples:**
54179

55-
Header: x-twilio-signature
180+
* `/mcp?services=Voice`
181+
* `/mcp?services=Messaging&services=PhoneNumbers`
56182

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

59-
Available services
60185
* Messaging (default)
61186
* Voice
62187
* VoiceAddOns
@@ -77,8 +202,8 @@ Available services
77202

78203
## Security recommendations
79204

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:
205+
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:
81206

82-
- Always set the `requires_approval` field to ensure that there are no unintended actions taken within your account.
207+
- Always set your MCP client to require tool approval to ensure that there are no unintended actions taken within your account.
83208
- 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.
84209
- 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)