TeslaMate MCP Server 是一个基于 Model Context Protocol (MCP) 的服务器,它能够连接到你的 TeslaMate 数据库,让 AI 助手可以查询特斯拉车辆数据和分析信息,为用户提供便捷的车辆数据查询体验。
本 MCP 服务器可连接到你的 TeslaMate PostgreSQL 数据库,并提供各种工具来检索特斯拉车辆信息、驾驶统计数据、充电数据、电池健康状况、效率指标和位置分析信息。它可与如 Claude Desktop 等兼容 MCP 的 AI 助手配合使用,支持通过自然语言查询你的特斯拉数据。
git clone https://github.com/yourusername/teslamate-mcp.git
cd teslamate-mcp
uv sync
或者使用 pip 安装:pip install -r requirements.txt
.env
文件:DATABASE_URL=postgresql://username:password@hostname:port/teslamate
对于使用 Docker 进行远程部署,可按以下步骤快速开始:
# 克隆并进入仓库
git clone https://github.com/yourusername/teslamate-mcp.git
cd teslamate-mcp
# 运行部署脚本
./deploy.sh deploy
# 或者手动操作
cp env.example .env
# 使用你的数据库凭证编辑 .env
docker-compose up -d
远程服务器将在以下地址可用:
http://localhost:8888/mcp
若要使用承载令牌身份验证来保护你的远程 MCP 服务器,可按以下步骤操作:
.env
文件中设置一个承载令牌:AUTH_TOKEN=your-secret-bearer-token-here
生成一个安全的令牌:# 使用提供的令牌生成器
python3 generate_token.py
# 或者使用 openssl 手动生成
openssl rand -base64 32
# 或者使用其他方法创建一个安全的随机字符串
{
"mcpServers": {
"teslamate-remote": {
"url": "http://your-server:8888/mcp",
"transport": "streamable_http",
"headers": {
"Authorization": "Bearer your-secret-bearer-token-here"
}
}
}
}
curl -H "Authorization: Bearer your-secret-bearer-token-here" \
http://localhost:8888/mcp
uv run python main.py
配置好 MCP 客户端后,你可以按类别提出自然语言问题:
注意:run_sql
工具仅允许 SELECT 查询,为确保安全,严格禁止所有数据修改操作(INSERT、UPDATE、DELETE、DROP 等)。
MCP 服务器提供 20 种工具用于查询你的 TeslaMate 数据:
get_basic_car_information
- 基本车辆详细信息(VIN、型号、名称、颜色等)get_current_car_status
- 当前状态、位置、电池电量和温度get_software_update_history
- 软件更新时间线get_battery_health_summary
- 电池损耗和健康指标get_battery_degradation_over_time
- 历史电池容量趋势get_daily_battery_usage_patterns
- 每日电池消耗模式get_tire_pressure_weekly_trends
- 轮胎气压历史和趋势get_monthly_driving_summary
- 月度行驶距离、效率和驾驶时间get_daily_driving_patterns
- 每日驾驶习惯和模式get_longest_drives_by_distance
- 按距离排名的顶级行程详细信息get_total_distance_and_efficiency
- 总体驾驶统计数据get_drive_summary_per_day
- 每日驾驶总结get_efficiency_by_month_and_temperature
- 按温度进行的效率分析get_average_efficiency_by_temperature
- 温度对效率的影响get_unusual_power_consumption
- 异常电力使用检测get_charging_by_location
- 按位置划分的充电统计数据get_all_charging_sessions_summary
- 完整的充电历史总结get_most_visited_locations
- 经常访问的地点get_database_schema
- 返回完整的数据库架构(表、列、数据类型)run_sql
- 执行带有安全验证的自定义 SELECT 查询
DATABASE_URL
:用于连接你的 TeslaMate 数据库的 PostgreSQL 连接字符串若要在 Claude Desktop 中使用此服务器,请在 MCP 配置文件中添加以下内容:
macOS:~/Library/Application Support/Claude/claude_desktop_config.json
Windows:%APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"teslamate": {
"command": "uv",
"args": ["run", "python", "/path/to/teslamate-mcp/main.py"],
"env": {
"DATABASE_URL": "postgresql://username:password@hostname:port/teslamate"
}
}
}
}
若要连接到远程服务器:
{
"mcpServers": {
"TeslaMate": {
"command": "npx",
"args": [
"-y",
"mcp-remote",
"http://your-private-server:8888/mcp",
"--allow-http"
]
}
}
}
启用身份验证时:
{
"mcpServers": {
"TeslaMate": {
"command": "npx",
"args": [
"-y",
"mcp-remote",
"http://your-private-server:8888/mcp",
"--allow-http",
"--header",
"Authorization:${AUTH_HEADER}"
],
"env": {
"AUTH_HEADER": "Bearer "
}
}
}
}
queries/
目录中创建一个新的 SQL 文件main.py
中添加相应的工具函数本项目采用 MIT 许可证,详情请参阅 LICENSE 文件。
若有 bug 报告或功能请求,请在 GitHub 上提交问题。