🚀 Tesla MCP Server
Tesla MCP Server 是一個可流式傳輸的 HTTP MCP 服務器,藉助 Tessie API 實現對特斯拉車輛的控制。
作者:overment
⚠️ 重要提示
你需自行承擔將此服務器連接到 MCP 客戶端的責任。語言模型可能會出錯、誤解指令或執行意外操作。在執行命令之前,請務必進行驗證,特別是對於解鎖、打開後備箱或發送導航目的地等操作。
HTTP 層是為開發過程中的便利性而設計的,並非具備生產級別的安全性。如果要進行遠程部署,請加強安全措施,包括進行適當的令牌驗證、安全存儲、TLS 終止、嚴格的 CORS/源檢查、速率限制和審計日誌記錄。
🚀 快速開始
本項目有兩種運行方式:
- 作為 Node/Hono 服務器,適用於本地工作流程。
- 作為 Cloudflare Worker,用於遠程交互。
✨ 主要特性
- ✅ 狀態監測 — 可獲取電池電量、續航里程、車輛位置、車內氣候、車門狀態和充電狀態等信息。
- ✅ 指令控制 — 支持鎖定/解鎖車輛、調節車內氣候、控制後備箱、開啟哨兵模式和設置導航等指令。
- ✅ 位置感知 — 提供 GPS 座標,實現基於位置的交互。
- ✅ 雙運行環境 — 支持在 Node.js/Bun 或 Cloudflare Workers 環境中運行。
設計原則
- 對大語言模型友好:提供兩個統一的工具,而非 1:1 的 API 鏡像。
- 便於監控:專為具有位置上下文的 AI 代理設計。
- 安全可靠:Tessie API 密鑰以秘密方式存儲,客戶端使用單獨的承載令牌進行身份驗證。
- 反饋清晰:提供詳細的指令執行結果和車輛狀態信息。
📦 安裝指南
前提條件
運行方式(任選其一)
- 本地開發 — 使用承載令牌認證的標準設置。
- Cloudflare Worker(本地開發) — 在本地進行 Worker 測試。
- Cloudflare Worker(部署到生產環境) — 用於遠程生產環境。
1. 本地開發 — 快速開始
- 獲取 Tessie 憑證:
- 配置環境:
cd tesla-mcp
bun install
cp .env.example .env
編輯 .env 文件:
PORT=3000
AUTH_ENABLED=true
AUTH_STRATEGY=bearer
# 使用以下命令生成:openssl rand -hex 32
BEARER_TOKEN=your-random-auth-token
# Tessie 憑證
TESSIE_ACCESS_TOKEN=your-tessie-access-token
TESSIE_VIN=your-vehicle-vin
- 運行:
bun dev
Claude Desktop / Cursor 配置:
{
"mcpServers": {
"tesla": {
"command": "npx",
"args": ["mcp-remote", "http://localhost:3000/mcp", "--transport", "http-only"],
"env": { "NO_PROXY": "127.0.0.1,localhost" }
}
}
}
2. Cloudflare Worker(本地開發)
bun x wrangler dev --local | cat
創建 .dev.vars 文件存儲本地機密信息:
BEARER_TOKEN=your_random_auth_token
TESSIE_ACCESS_TOKEN=your_tessie_token
TESSIE_VIN=your_vehicle_vin
端點地址:http://127.0.0.1:8787/mcp
3. Cloudflare Worker(部署到生產環境)
- 創建用於會話存儲的 KV 命名空間:
bun x wrangler kv:namespace create TOKENS
輸出結果會顯示:
將以下內容添加到你的 wrangler.toml 文件中:
[[kv_namespaces]]
binding = "TOKENS"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- 使用你的 KV 命名空間 ID 更新
wrangler.toml 文件:
[[kv_namespaces]]
binding = "TOKENS"
id = "your-kv-namespace-id-from-step-1"
- 設置機密信息:
openssl rand -hex 32
bun x wrangler secret put BEARER_TOKEN
bun x wrangler secret put TESSIE_ACCESS_TOKEN
bun x wrangler secret put TESSIE_VIN
- 部署:
bun x wrangler deploy
端點地址:https://<worker-name>.<account>.workers.dev/mcp
💻 使用示例
1. 獲取車輛狀態
{
"name": "tesla_state",
"arguments": {}
}
2. 鎖定車輛
{
"name": "tesla_command",
"arguments": {
"command": "lock"
}
}
3. 將溫度設置為 22°C
{
"name": "tesla_command",
"arguments": {
"command": "set_temperature",
"temperature": 22
}
}
4. 在出發前開啟車內氣候控制
{
"name": "tesla_command",
"arguments": {
"command": "start_climate"
}
}
5. 導航到目的地
{
"name": "tesla_command",
"arguments": {
"command": "share",
"destination": "Golden Gate Bridge, San Francisco"
}
}
📚 詳細文檔
客戶端配置
Alice 應用
添加為 MCP 服務器,配置如下:
- URL:
https://your-worker.workers.dev/mcp
- 類型:
streamable-http
- 請求頭:
Authorization: Bearer <your-BEARER_TOKEN>
Claude Desktop / Cursor(本地服務器)
{
"mcpServers": {
"tesla": {
"command": "npx",
"args": ["mcp-remote", "http://127.0.0.1:3000/mcp", "--transport", "http-only"],
"env": { "NO_PROXY": "127.0.0.1,localhost" }
}
}
}
Claude Desktop / Cursor(Cloudflare Worker)
{
"mcpServers": {
"tesla": {
"command": "npx",
"args": ["mcp-remote", "https://your-worker.workers.dev/mcp", "--transport", "http-only"]
}
}
}
MCP 檢查器(快速測試)
bunx @modelcontextprotocol/inspector
工具說明
tesla_state
獲取特斯拉車輛的當前狀態。
{}
{
display_name: string;
battery_level: number;
battery_range_km: number;
charging: {
state: string;
minutes_remaining: number | null;
charge_limit: number;
};
location: {
latitude: number;
longitude: number;
heading: number;
speed: number | null;
};
locked: boolean;
sentry_mode: boolean;
climate: {
is_on: boolean;
inside_temp: number;
outside_temp: number;
target_temp: number;
is_defrosting: boolean;
};
doors: {
front_left: boolean;
front_right: boolean;
rear_left: boolean;
rear_right: boolean;
frunk: boolean;
trunk: boolean;
charge_port: boolean;
};
state: "online" | "asleep" | "offline";
odometer_km: number;
last_updated: string;
}
tesla_command
在特斯拉車輛上執行指令。
{
command: "lock" | "unlock" | "start_climate" | "stop_climate" |
"set_temperature" | "start_defrost" | "stop_defrost" |
"open_frunk" | "open_trunk" | "open_charge_port" |
"close_charge_port" | "enable_sentry" | "disable_sentry" |
"flash" | "honk" | "share";
temperature?: number;
destination?: string;
locale?: string;
}
{
success: boolean;
command: string;
message: string;
}
指令參考:
| 指令 |
描述 |
參數 |
lock |
鎖定車輛 |
— |
unlock |
解鎖車輛 |
— |
start_climate |
開啟車內氣候控制 |
— |
stop_climate |
關閉車內氣候控制 |
— |
set_temperature |
設置車內溫度 |
temperature(15 - 28°C) |
start_defrost |
開啟最大除霜功能 |
— |
stop_defrost |
關閉除霜功能 |
— |
open_frunk |
打開前備箱 |
— |
open_trunk |
打開或關閉後備箱 |
— |
open_charge_port |
打開充電口門 |
— |
close_charge_port |
關閉充電口門 |
— |
enable_sentry |
開啟哨兵模式 |
— |
disable_sentry |
關閉哨兵模式 |
— |
flash |
閃爍車燈 |
— |
honk |
鳴笛 |
— |
share |
發送目的地到導航 |
destination, locale? |
認證流程
┌─────────────────────────────────────────────────────────────────┐
│ 客戶端(Alice 應用、Claude Desktop) │
│ │ │
│ │ Authorization: Bearer <BEARER_TOKEN> │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Cloudflare Worker / Node.js 服務器 │ │
│ │ │ │
│ │ 1. 驗證 BEARER_TOKEN(客戶端認證) │ │
│ │ 2. 使用 TESSIE_ACCESS_TOKEN(內部 API 密鑰) │ │
│ │ │ │
│ │ env.TESSIE_ACCESS_TOKEN ──┐ │ │
│ │ env.TESSIE_VIN ───────────┼──► TessieClient │ │
│ │ │ │ │ │
│ │ │ ▼ │ │
│ │ │ api.tessie.com │ │
│ └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
關鍵點:
BEARER_TOKEN:你生成的隨機令牌,用於客戶端向 MCP 服務器進行身份驗證。
TESSIE_ACCESS_TOKEN:你的 Tessie API 密鑰,由服務器內部使用。
- 客戶端永遠不會看到你的 Tessie 憑證。
HTTP 端點
| 端點 |
方法 |
用途 |
/mcp |
POST |
MCP JSON-RPC 2.0 |
/health |
GET |
健康檢查 |
開發命令
bun dev
bun run typecheck
bun run lint
bun run build
bun start
項目架構
src/
├── shared/
│ └── tools/
│ ├── tesla-state.ts # 獲取車輛狀態
│ └── tesla-command.ts # 執行指令
├── services/
│ └── tessie.service.ts # Tessie API 客戶端
├── schemas/
│ ├── commands.ts # 指令定義
│ ├── outputs.ts # 工具輸出模式
│ └── tessie.ts # Tessie API 響應模式
├── config/
│ └── metadata.ts # 服務器和工具描述
├── index.ts # Node.js 入口文件
└── worker.ts # Workers 入口文件
環境變量
Node.js(.env)
| 變量 |
是否必需 |
描述 |
TESSIE_ACCESS_TOKEN |
✓ |
Tessie API 訪問令牌 |
TESSIE_VIN |
✓ |
特斯拉車輛 VIN 碼 |
BEARER_TOKEN |
✓ |
MCP 客戶端認證令牌 |
PORT |
|
服務器端口(默認:3000) |
HOST |
|
服務器主機(默認:127.0.0.1) |
AUTH_ENABLED |
|
啟用認證(默認:true) |
AUTH_STRATEGY |
|
bearer(默認) |
Cloudflare Workers(wrangler.toml + 機密信息)
wrangler.toml 變量:
AUTH_ENABLED = "true"
AUTH_STRATEGY = "bearer"
機密信息(通過 wrangler secret put 設置):
BEARER_TOKEN — 客戶端隨機認證令牌
TESSIE_ACCESS_TOKEN — Tessie API 訪問令牌
TESSIE_VIN — 你的車輛 VIN 碼
KV 命名空間:
[[kv_namespaces]]
binding = "TOKENS"
id = "your-kv-namespace-id"
故障排除
| 問題 |
解決方案 |
| 401 Unauthorized |
檢查 BEARER_TOKEN 是否已設置,並且客戶端發送了 Authorization: Bearer <token> 請求頭 |
| "TESSIE_ACCESS_TOKEN not configured" |
設置機密信息:wrangler secret put TESSIE_ACCESS_TOKEN |
| "TESSIE_VIN not configured" |
設置機密信息:wrangler secret put TESSIE_VIN |
| "Tessie API error" |
在 developer.tessie.com 上驗證 TESSIE_ACCESS_TOKEN 是否有效 |
| 車輛未找到 |
檢查 TESSIE_VIN 是否正確(應為 17 位字符) |
| 車輛離線 |
車輛可能處於深度睡眠狀態。執行指令會喚醒它(大約需要 30 秒) |
| 指令超時 |
Tessie 最多會等待 90 秒讓車輛喚醒。請重試 |
| KV 命名空間錯誤 |
運行 wrangler kv:namespace create TOKENS 並更新 wrangler.toml 文件 |
| "ReadableStream is not defined" |
Node.js 版本過舊(需要 18 及以上版本)。使用較新版本的 Node.js 完整路徑 |
| "spawn bunx ENOENT" |
Claude Desktop 找不到 bunx。請使用 npx 代替 |
調試方法
使用 MCP 檢查器進行測試:
bunx @modelcontextprotocol/inspector
查看 Worker 日誌:
wrangler tail
📄 許可證
本項目採用 MIT 許可證。