An MCP (Model Context Protocol) server that enables handling agent-assisted payments via the Twilio API, with enhanced features for asynchronous callbacks and guided workflow.
You can use this server directly via npx:
npx twilio-agent-payments-mcp-server <accountSid> <apiKey> <apiSecret>
Or install it globally:
npm install -g twilio-agent-payments-mcp-server
twilio-agent-payments-mcp-server <accountSid> <apiKey> <apiSecret>
The server requires the following parameters:
accountSid
: Your Twilio Account SID (must start with 'AC', will be validated)apiKey
: Your Twilio API Key (starts with 'SK')apiSecret
: Your Twilio API SecretstatusCallback
: URL for Twilio to send payment status updates toThe following environment variables are required:
TOKEN_TYPE
: Type of token to use for payments (e.g., 'reusable', 'one-time')CURRENCY
: Currency for payments (e.g., 'USD', 'EUR')PAYMENT_CONNECTOR
: Payment connector to use with TwilioThis server uses API Keys and Secrets instead of Auth Tokens for improved security. This approach provides better access control and the ability to revoke credentials if needed. For more information, see the Twilio API Keys documentation.
For local development (when the package is not published to npm), add the following to your Claude Desktop configuration file (~/Library/Application Support/Claude/claude_desktop_config.json
on macOS or %APPDATA%\Claude\claude_desktop_config.json
on Windows):
{
"mcpServers": {
"twilio-agent-payments": {
"command": "node",
"args": [
"/PATHTONODE/twilio-agent-payments-mcp-server/build/index.js",
"your_account_sid_here",
"your_api_key_here",
"your_api_secret_here",
"+1234567890",
"https://your-callback-url.com/payment-status"
],
"env": {
"TOKEN_TYPE": "reusable",
"CURRENCY": "USD",
"PAYMENT_CONNECTOR": "your_connector_name"
}
}
}
}
Replace the values with your actual Twilio credentials and configuration.
Once the package is published to npm, you can use the following configuration:
{
"mcpServers": {
"twilio-agent-payments": {
"command": "npx",
"args": [
"-y",
"twilio-agent-payments-mcp-server",
"your_account_sid_here",
"your_api_key_here",
"your_api_secret_here",
"+1234567890",
"https://your-callback-url.com/payment-status"
],
"env": {
"TOKEN_TYPE": "reusable",
"CURRENCY": "USD",
"PAYMENT_CONNECTOR": "your_connector_name"
}
}
}
}
One of the key advantages of the Model Context Protocol (MCP) is that it eliminates the need for extensive manual configuration of LLM context. The MCP server automatically provides all necessary tool definitions, resource templates, and capabilities to the LLM client.
To integrate this MCP server into your own host application:
No manual definition of tools or resources is required in your LLM's context - the MCP protocol handles this discovery automatically.
Here's a simplified example of how to integrate with an MCP client:
// Initialize your MCP client
const mcpClient = new McpClient();
// Connect to the Twilio Agent Payments MCP server
await mcpClient.connectToServer({
name: "twilio-agent-payments",
// Connection details depend on your specific MCP client implementation
// This could be a WebSocket URL, stdio connection, or other transport
});
// The client will automatically discover available tools and resources
// When the LLM wants to use a tool, your application can handle it like this:
function handleLlmToolRequest(toolRequest) {
// The toolRequest would contain:
// - server_name: "twilio-agent-payments"
// - tool_name: e.g., "startPaymentCapture"
// - arguments: e.g., { callSid: "CA1234567890abcdef" }
return mcpClient.callTool(toolRequest);
}
// Similarly for resources:
function handleLlmResourceRequest(resourceRequest) {
// The resourceRequest would contain:
// - server_name: "twilio-agent-payments"
// - uri: e.g., "payment://CA1234567890abcdef/PA9876543210abcdef/status"
return mcpClient.accessResource(resourceRequest);
}
The LLM only needs to know that it can use the Twilio Agent Payments MCP server for handling payments. A simple instruction in your system prompt is sufficient:
You have access to a Twilio Agent Payments MCP server that can help process secure payments during voice calls.
When a customer wants to make a payment, you can use the tools provided by this server to securely capture
payment information while maintaining PCI compliance.
The server will guide you through the payment process with contextual prompts at each step.
The MCP server itself provides all the detailed tool definitions, input schemas, and contextual prompts to guide the LLM through the payment flow.
Initiates a payment capture process for an active call.
Parameters:
callSid
: The Twilio Call SID for the active callReturns:
paymentSid
: The Twilio Payment SID for the new payment sessionprompt
: A markdown-formatted prompt to guide the LLM through the next stepsUpdates a payment field with a specific capture type.
Parameters:
callSid
: The Twilio Call SID for the active callpaymentSid
: The Twilio Payment SID for the payment sessioncaptureType
: The type of capture to perform (e.g., 'payment-card-number', 'security-code', 'expiration-date')Returns:
success
: Boolean indicating successprompt
: A markdown-formatted prompt to guide the LLM through the next stepsResets a payment field for re-entry when a customer makes a mistake.
Parameters:
callSid
: The Twilio Call SID for the active callpaymentSid
: The Twilio Payment SID for the payment sessionfield
: The field to reset ('cardNumber', 'securityCode', or 'expirationDate')Returns:
success
: Boolean indicating successprompt
: A markdown-formatted prompt to guide the LLM through the re-entry processCompletes a payment capture session.
Parameters:
callSid
: The Twilio Call SID for the active callpaymentSid
: The Twilio Payment SID for the payment sessionReturns:
success
: Boolean indicating successtoken
: The payment token (if successful)prompt
: A markdown-formatted prompt to guide the LLM through completionGets the current status of a payment session.
Parameters:
callSid
: The Twilio Call SID for the active callpaymentSid
: The Twilio Payment SID for the payment sessionReturns:
prompt
: A markdown-formatted prompt to guide the LLM based on the current stateGet the current status of a payment session as a JSON object.
Get a markdown-formatted prompt for the current payment state to guide the LLM through the next steps.
This MCP server implements an enhanced architecture for handling payment flows:
The server maintains an in-memory state store for payment sessions, tracking:
An Express server handles asynchronous callbacks from Twilio:
Dynamic resources provide access to payment state:
payment://{callSid}/{paymentSid}/status
: Current payment status as JSONpayment://{callSid}/{paymentSid}/prompt
: Contextual prompt for the current stateContextual prompts guide the LLM through the payment flow:
To build the project:
npm install
npm run build
To start the server manually for testing (outside of Claude Desktop):
# Run with actual credentials
node build/index.js "your_account_sid_here" "your_api_key_here" "your_api_secret" "+1234567890" "https://your-callback-url.com/payment-status"
# Or use the npm script (which uses ts-node for development)
npm run dev -- "your_account_sid_here" "your_api_key_here" "your_api_secret" "+1234567890" "https://your-callback-url.com/payment-status"
The server will start and wait for MCP client connections.
When using with Claude Desktop, the server is started automatically when Claude loads the configuration file. You don't need to manually start it.
This server helps with PCI compliance by tokenizing payment card information. The actual card data is handled by Twilio and never stored in your system. For more information on Twilio's PCI compliance, see the Twilio documentation on secure payments.
MIT
When using this server with the MCP Inspector, note that all logging is done via console.error()
instead of console.log()
. This is intentional and necessary for compatibility with the MCP protocol, which uses stdout for JSON communication.
If you're extending this server or debugging issues:
console.error()
for all logging to ensure logs go to stderrconsole.log()
as it will interfere with the MCP protocol's JSON messages on stdoutThis approach ensures that the MCP Inspector can properly parse the JSON messages exchanged between the server and client without interference from log messages.
The Twilio Agent Payments MCP server implements logging capabilities according to the MCP specification. Logging must be explicitly configured when initializing the MCP server:
const mcpServer = new McpServer(SERVER_CONFIG, {
capabilities: {
logging: {}
}
});
This configuration is critical - without it, any attempts to use logging functionality will result in runtime errors with messages like:
Error: Server does not support logging (required for notifications/message)
The server uses an event-based logging architecture:
CallbackHandler
and TwilioAgentPaymentServer
classes extend Node.js's EventEmitter
and emit 'log' events with level and message data.// Set up event listeners for callback handler logs
callbackHandler.on('log', forwardLogToMcp);
// Set up event listeners for Twilio agent payment server logs
twilioAgentPaymentServer.on('log', forwardLogToMcp);
forwardLogToMcp
function transforms these events into MCP-compatible log messages:const forwardLogToMcp = (data: { level: string, message: string }) => {
// Only use valid log levels: info, error, debug
// If level is 'warn', treat it as 'info'
const mcpLevel = data.level === 'warn' ? 'info' : data.level as "info" | "error" | "debug";
// Send the log message to the MCP server's underlying Server instance
mcpServer.server.sendLoggingMessage({
level: mcpLevel,
data: data.message,
});
};
The server supports the following log levels:
info
: General information messageserror
: Error messages and exceptionsdebug
: Detailed debugging informationwarn
: Warning messages (automatically converted to 'info' for MCP compatibility)If you encounter logging-related errors:
Server does not support logging (required for notifications/message)
: This indicates the logging capability is not properly configured.No description, website, or topics provided.
No releases published
No packages published