特斯拉MCP服务器
用于特斯拉车辆控制的流式HTTP MCP服务器 德西API.
作者 过度
\[!警告\] 您自行负责将此服务器连接到MCP客户端。语言模型可能会出错、误解指令或执行意外操作。在执行命令之前,请务必验证命令,特别是对于解锁、打开行李箱或发送导航目的地等操作。 HTTP层的设计是为了开发过程中的便利性,而不是生产级的安全性。如果远程部署,请加强它:适当的令牌验证、安全存储、TLS终止、严格的CORS/源代码检查、速率限制和审计日志记录。
通知
此repo以两种方式工作:
- 作为一个 节点/HONO服务器 用于本地工作流
- 作为一个 Cloudflare工作人员 用于远程交互
特性
- ✅ 状态 --电池、续航里程、位置、气候、车门、充电状态
- ✅ 命令 --锁定/解锁、气候、行李箱、哨兵、导航
- ✅ 感知 --用于上下文感知交互的GPS坐标
- ✅ 双运行时 --Node.js/Bun或Cloudflare Workers
设计原则
- LLM友好:两个统一的工具,而不是1:1 API镜像
- 准备观看:专为具有位置上下文的AI代理而设计
- 安全:Tessie API密钥存储为机密,客户端使用单独的承载令牌
- 清晰的反馈:详细的指挥结果和车辆状态
______________________________________________________________________
安装
跑步方式(选一种)
- 地方发展 --带有承载令牌身份验证的标准设置
- Cloudflare Worker(牧者开发) --本地工人测试
- Cloudflare Worker(部署) --远程生产
______________________________________________________________________
1.地方发展——快速起步
- 获取Tessie凭据:
- 访问 developer.tessie.com - 首选 开发人员设置 → 生成访问令牌 - 复制您的访问令牌 - 记下车辆的VIN
- 配置环境:
cd tesla-mcp
bun install
cp .env.example .env编辑 .env:
PORT=3000
AUTH_ENABLED=true
AUTH_STRATEGY=bearer
# Generate with: openssl rand -hex 32
BEARER_TOKEN=your-random-auth-token
# Tessie credentials
TESSIE_ACCESS_TOKEN=your-tessie-access-token
TESSIE_VIN=your-vehicle-vin- 运行:
bun dev
# MCP: http://127.0.0.1:3000/mcp克劳德桌面/光标:
{
"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输出将显示:
Add the following to your wrangler.toml:
[[kv_namespaces]]
binding = "TOKENS"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"- 更新
wrangler.toml使用您的KV命名空间ID:
[[kv_namespaces]]
binding = "TOKENS"
id = "your-kv-namespace-id-from-step-1"- 设置秘密:
# Generate a random token for client authentication
openssl rand -hex 32
bun x wrangler secret put BEARER_TOKEN
# Paste the generated token when prompted
# Tessie API credentials
bun x wrangler secret put TESSIE_ACCESS_TOKEN
# Paste your Tessie token when prompted
bun x wrangler secret put TESSIE_VIN
# Paste your VIN when prompted- 部署:
bun x wrangler deploy端点: https://..workers.dev/mcp
______________________________________________________________________
客户端配置
爱丽丝应用程序
添加为MCP服务器:
- 网址:
https://your-worker.workers.dev/mcp - 类型:
streamable-http - 头球
Authorization: Bearer
克劳德桌面/光标(本地服务器)
{
"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" }
}
}
}克劳德桌面/光标(Cloudflare Worker)
{
"mcpServers": {
"tesla": {
"command": "npx",
"args": ["mcp-remote", "https://your-worker.workers.dev/mcp", "--transport", "http-only"]
}
}
}MCP检查员(快速测试)
bunx @modelcontextprotocol/inspector
# Connect to: http://localhost:3000/mcp (local) or https://your-worker.workers.dev/mcp (remote)______________________________________________________________________
工具
tesla_state
获取您的特斯拉汽车的当前状态。
// Input
{}
// Output
{
display_name: string; // Vehicle name
battery_level: number; // 0-100%
battery_range_km: number; // Estimated range in km
charging: {
state: string; // "Disconnected", "Charging", "Complete", "Stopped"
minutes_remaining: number | null;
charge_limit: number; // Charge limit %
};
location: {
latitude: number;
longitude: number;
heading: number; // 0-359°
speed: number | null; // km/h or null if parked
};
locked: boolean;
sentry_mode: boolean;
climate: {
is_on: boolean;
inside_temp: number; // °C
outside_temp: number; // °C
target_temp: number; // °C
is_defrosting: boolean;
};
doors: {
front_left: boolean; // true = open
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; // ISO 8601
}tesla_command
在您的特斯拉汽车上执行命令。
// Input
{
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; // Required for set_temperature (15-28°C)
destination?: string; // Required for share
locale?: string; // Optional for share (e.g., "en-US")
}
// Output
{
success: boolean;
command: string;
message: string;
}命令参考:
| 命令 | 描述 | 参数 |
|---|---|---|
lock | 锁定车辆 | -- |
unlock | 解锁车辆 | -- |
start_climate | 启动气候控制 | -- |
stop_climate | 停止气候控制 | -- |
set_temperature | 设置机舱温度 | temperature (15-28摄氏度) |
start_defrost | 打开最大除霜 | -- |
stop_defrost | 关闭除霜功能 | -- |
open_frunk | 打开前行李箱 | -- |
open_trunk | 切换后行李箱 | -- |
open_charge_port | 打开充电口门 | -- |
close_charge_port | 关闭装料口门 | -- |
enable_sentry | 启用哨兵模式 | -- |
disable_sentry | 禁用哨兵模式 | -- |
flash | 闪光 | -- |
honk | 按喇叭 | -- |
share | 将目的地发送到导航 | destination, locale? |
______________________________________________________________________
例子
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"
}
}______________________________________________________________________
身份验证流程
┌─────────────────────────────────────────────────────────────────┐
│ Client (Alice App, Claude Desktop) │
│ │ │
│ │ Authorization: Bearer │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Cloudflare Worker / Node.js Server │ │
│ │ │ │
│ │ 1. Validate BEARER_TOKEN (client auth) │ │
│ │ 2. Use TESSIE_ACCESS_TOKEN (internal API key) │ │
│ │ │ │
│ │ env.TESSIE_ACCESS_TOKEN ──┐ │ │
│ │ env.TESSIE_VIN ───────────┼──► TessieClient │ │
│ │ │ │ │ │
│ │ │ ▼ │ │
│ │ │ api.tessie.com │ │
│ └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘要点:
BEARER_TOKEN:您生成的随机令牌--对MCP服务器的客户端进行身份验证TESSIE_ACCESS_TOKEN:您的Tessie API密钥-服务器内部使用- 客户永远看不到你的Tessie凭据
______________________________________________________________________
HTTP端点
| 端点 | 方法 | 目的 |
|---|---|---|
/mcp | 发布 | MCP JSON-RPC 2.0 |
/health | GET | 健康检查 |
______________________________________________________________________
发展
bun dev # Start with hot reload
bun run typecheck # TypeScript check
bun run lint # Lint code
bun run build # Production build
bun start # Run production______________________________________________________________________
建筑
src/
├── shared/
│ └── tools/
│ ├── tesla-state.ts # Get vehicle state
│ └── tesla-command.ts # Execute commands
├── services/
│ └── tessie.service.ts # Tessie API client
├── schemas/
│ ├── commands.ts # Command definitions
│ ├── outputs.ts # Tool output schemas
│ └── tessie.ts # Tessie API response schemas
├── config/
│ └── metadata.ts # Server & tool descriptions
├── index.ts # Node.js entry
└── worker.ts # Workers entry______________________________________________________________________
环境变量
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+机密)
牧马人。汤姆瓦尔斯:
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未经授权 | 检查 BEARER_TOKEN 已设置,客户端发送 Authorization: Bearer |
| “TESSIE_ACCESS_TOKEN未配置” | 设置密码: wrangler secret put TESSIE_ACCESS_TOKEN |
| “TESSIE_VIN未配置” | 设置密码: wrangler secret put TESSIE_VIN |
| “Tessie API错误” | 验证 TESSIE_ACCESS_TOKEN 在developer.tessie.com上有效 |
| 未找到车辆 | 检查 TESSIE_VIN 正确(17个字符) |
| 车辆离线 | 车辆可能处于深度睡眠状态。命令将唤醒它(大约需要30秒) |
| 命令超时 | Tessie等待车辆唤醒的时间长达90秒。再试一次。 |
| KV命名空间错误 | 运行 wrangler kv:namespace create TOKENS 并更新wrangler.toml |
| “ReadableStream未定义” | Node.js版本太旧(需要18+)。使用新节点的完整路径。 |
| “spawn bunx ENOENT” | 克劳德桌面找不到 bunx.使用 npx 相反。 |
调试
MCP检验员测试:
bunx @modelcontextprotocol/inspector
# Connect to your endpoint and test tools检查Worker日志:
wrangler tail______________________________________________________________________
许可证
麻省理工学院
