使用 NetSuite MCP 讓 n8n、Claude Code 或 Claude Desktop 從 NetSuite 生成發票!以下是各種方法的詳細說明:
技術架構概覽
[Claude Desktop/Code/n8n] ↔ [NetSuite MCP 伺服器] ↔ [NetSuite API] ↔ [NetSuite ERP系統]
方案一:Claude Desktop 配合 NetSuite MCP
設定需求:
- NetSuite MCP 伺服器:實作 MCP 協議的本地伺服器
- NetSuite API 存取權限:RESTlets 或 SuiteScript 端點
- 身份驗證:OAuth 2.0 或基於令牌的認證
實作方式:
// Claude Desktop MCP 配置
{
"mcpServers": {
"netsuite": {
"command": "node",
"args": ["netsuite-mcp-server.js"],
"env": {
"NETSUITE_ACCOUNT_ID": "您的帳戶ID",
"NETSUITE_CONSUMER_KEY": "您的消費者金鑰",
"NETSUITE_CONSUMER_SECRET": "您的消費者秘鑰",
"NETSUITE_TOKEN_ID": "您的令牌ID",
"NETSUITE_TOKEN_SECRET": "您的令牌秘鑰"
}
}
}
}
使用範例:
您:「為客戶 ABC 企業生成一張 5,000 元的諮詢服務發票」
Claude:我來為您在 NetSuite 中建立這張發票。
[使用 MCP 呼叫 NetSuite API]
✓ 發票 #INV-2024-001 已成功為 ABC 企業建立
- 金額:NT$5,000.00
- 服務:諮詢服務
- 狀態:待審核
方案二:Claude Code 整合
CLI 指令結構:
# 安裝具有 NetSuite MCP 的 Claude Code
claude-code install netsuite-mcp
# 透過命令列生成發票
claude-code "為客戶 'XYZ 有限公司' 建立發票,項目包括:
- 軟體授權:NT$60,000
- 實施服務:NT$90,000
- 付款條件設為 30 天"
自動化工作流程:
# 建立具有發票生成功能的專案
claude-code create-project invoice-automation --with-netsuite
# 執行發票生成腳本
claude-code run generate-monthly-invoices.py
方案三:n8n 工作流程自動化
工作流程設計:
觸發條件:
- Webhook(來自 CRM 或其他系統)
- 排程(每月定期執行)
- 手動執行
處理流程:
1. 驗證輸入資料
2. 查詢 NetSuite 客戶資訊
3. 計算發票金額
4. 透過 MCP 生成發票
5. 發送通知
6. 更新外部系統
n8n 節點配置:
{
"nodes": [
{
"name": "NetSuite MCP",
"type": "HTTP Request",
"parameters": {
"url": "http://localhost:3000/mcp/netsuite",
"method": "POST",
"body": {
"action": "create_invoice",
"customer_id": "={{$json.customerId}}",
"items": "={{$json.invoiceItems}}",
"terms": "Net 30"
}
}
}
]
}
NetSuite MCP 伺服器實作
核心功能:
// netsuite-mcp-server.js
class NetSuiteMCPServer {
async createInvoice(params) {
const invoice = {
entity: params.customerId,
item: params.items,
terms: params.terms,
location: params.location
};
const response = await this.netsuiteAPI.post('/invoice', invoice);
return {
success: true,
invoiceId: response.id,
invoiceNumber: response.tranid,
amount: response.total
};
}
async getCustomerInfo(customerId) {
return await this.netsuiteAPI.get(`/customer/${customerId}`);
}
async getItems() {
return await this.netsuiteAPI.get('/items');
}
}
NetSuite 必要設定
1. API 權限設定:
- 交易權限:建立、讀取、更新發票
- 記錄權限:存取客戶、項目和稅務記錄
- 清單權限:存取價格清單和付款條件
2. 自訂欄位(如需要):
// 用於增強發票資料的自訂欄位
const customFields = {
'custbody_source_system': 'MCP_Generated',
'custbody_ai_generated': true,
'custbody_generation_timestamp': new Date().toISOString()
};
3. SuiteScript 整合:
// 用於建立發票的 RESTlet
function createInvoice(requestBody) {
try {
const invoice = record.create({
type: record.Type.INVOICE,
isDynamic: true
});
invoice.setValue('entity', requestBody.customerId);
invoice.setValue('terms', requestBody.terms);
// 新增明細項目
requestBody.items.forEach((item, index) => {
invoice.selectNewLine('item');
invoice.setCurrentLineValue('item', 'item', item.id);
invoice.setCurrentLineValue('item', 'quantity', item.quantity);
invoice.setCurrentLineValue('item', 'rate', item.rate);
invoice.commitLine('item');
});
const invoiceId = invoice.save();
return { success: true, id: invoiceId };
} catch (error) {
return { success: false, error: error.message };
}
}
安全性考量
身份驗證:
- 使用 OAuth 2.0 進行安全的 API 存取
- 安全儲存憑證(環境變數)
- 實作令牌更新機制
存取控制:
- 限制 API 權限僅用於發票相關操作
- 在 NetSuite 中使用角色式存取控制
- 實作稽核軌跡
資料驗證:
const validateInvoiceData = (data) => {
const required = ['customerId', 'items', 'amount'];
const missing = required.filter(field => !data[field]);
if (missing.length > 0) {
throw new Error(`缺少必要欄位:${missing.join(', ')}`);
}
if (data.amount <= 0) {
throw new Error('發票金額必須為正數');
}
};
實際應用範例
1. 簡單發票生成:
指令:「為 Acme 公司建立一張 10 小時諮詢服務的發票,每小時 150 元」
回應:正在為 Acme 公司建立發票...
✓ 發票 INV-2024-0156 已生成
- 客戶:Acme 公司
- 服務:諮詢(10 小時 @ 150元/小時)
- 小計:NT$1,500.00
- 稅金:NT$120.00
- 總計:NT$1,620.00
2. 批次發票處理:
指令:「根據服務合約為所有定期客戶生成月度發票」
回應:正在處理定期發票...
✓ 已生成 25 張發票,總計 NT$1,435,500.00
- 已送至審核流程
- 已發送客戶通知
- 已更新 CRM 記錄
3. 多項目複雜發票:
指令:「為 Tech Solutions Inc 建立發票:
- 軟體授權(年度):NT$150,000
- 培訓服務:20 小時 @ NT$6,000/小時
- 支援套件:NT$36,000
- 給予 10% 大量折扣」
回應:已生成綜合發票...
✓ 發票 INV-2024-0157 已建立
- 小計:NT$306,000.00
- 大量折扣(10%):-NT$30,600.00
- 淨額:NT$275,400.00
進階功能與最佳實務
1. 自動稅務計算:
// 根據客戶地區自動計算稅務
const calculateTax = (customer, amount) => {
const taxRates = {
'TW': 0.05, // 台灣營業稅 5%
'US': 0.08, // 美國銷售稅 8%
'EU': 0.20 // 歐盟增值稅 20%
};
return amount * (taxRates[customer.country] || 0);
};
2. 多幣別支援:
// 支援多種貨幣的發票生成
const currencies = {
'TWD': '新台幣',
'USD': '美元',
'EUR': '歐元',
'JPY': '日圓'
};
3. 自動編號系統:
// 智慧發票編號生成
const generateInvoiceNumber = (company, date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const sequence = getNextSequence(company, year, month);
return `${company}-${year}${month}-${sequence.toString().padStart(4, '0')}`;
};
整合效益
1. 自然語言介面
- 用描述的方式而非點擊表單來建立發票
- 支援中文指令和回應
2. 自動化處理
- 與其他業務流程無縫整合
- 減少人工錯誤和處理時間
3. 一致性管理
- 標準化發票生成流程
- 統一的格式和編號規則
4. 稽核追蹤
- 所有 AI 生成的發票都有完整記錄
- 便於合規和審計
5. 效率提升
- 快速發票建立和處理
- 批次作業和排程執行
部署建議
1. 測試階段:
- 先在 NetSuite 沙盒環境測試
- 建立完整的測試案例
- 驗證各種業務情境
2. 正式上線:
- 採用漸進式部署策略
- 監控系統效能和準確性
- 準備緊急回復計劃
3. 持續優化:
- 收集使用者回饋
- 優化 AI 模型效能
- 更新業務邏輯規則
這個整合方案能夠大幅提升您公司的發票處理效率,同時確保資料準確性和合規性。您希望我針對哪個特定部分提供更詳細的實作指導嗎?