NetworkTables MCP服务器
一种模型上下文协议(MCP)服务器,为AI代理提供通过NetworkTables访问FRC机器人数据的权限。
特性
🔌 资源管理
- 主题作为资源:每个NT主题都作为MCP资源公开
nt://table/keyURI方案 - 分层发现:按NetworkTables结构组织的资源(SmartDashboard、LiveWindow、子系统)
- 动态注册:新主题发布后会自动成为可用资源
- 资源元数据:每个主题的类型信息、更新频率、发布者信息
🛠️ 工具设计模式
- 阅读工具:
nt_get(topic),nt_get_multiple(topics[])用于价值检索 - 写入工具:
nt_set(topic, value),nt_set_multiple(updates{})用于出版 - 订阅工具:
nt_subscribe(topics[], duration)用于实时监控 - 发现工具:
nt_list_topics(filter?),nt_get_info(topic)用于勘探
🌐 连接状态管理
- 连接作为上下文:MCP服务器跨工具调用维护NT连接状态
- 自动重新连接:优雅地处理机器人断开连接
- 连接配置:通过MCP配置团队编号、IP和端口
- 健康监测:连接质量和延迟作为可访问的指标
📊 数据类型处理
- 类型强制:用于MCP传输的NT类型和JSON之间的自动转换
- 架构验证:确保数据类型符合NT主题期望
- 数组处理:NT数组、结构和复杂类型的正确序列化
安装
uv run fastmcp install --with pyntcore nt_mcp_server.py使用示例
基本连接
# Connect to robot by team number
result = await mcp.call_tool("nt_connect", {
"team_number": 1234,
"identity": "Competition-Agent"
})
# Connect to simulation
result = await mcp.call_tool("nt_connect", {
"server_ip": "127.0.0.1",
"server_port": 5810,
"identity": "Sim-Agent"
})读取数据
# Get single value
battery_voltage = await mcp.call_tool("nt_get", {
"topic": "/SmartDashboard/Battery Voltage"
})
# Get multiple values efficiently
drivetrain_data = await mcp.call_tool("nt_get_multiple", {
"topics": [
"/SmartDashboard/Left Encoder",
"/SmartDashboard/Right Encoder",
"/SmartDashboard/Gyro Angle"
]
})写入数据
# Set single value
success = await mcp.call_tool("nt_set", {
"topic": "/SmartDashboard/Auto Selector",
"value": "Center Auto"
})
# Set multiple values atomically
results = await mcp.call_tool("nt_set_multiple", {
"updates": {
"/SmartDashboard/Drive Speed": 0.5,
"/SmartDashboard/Turn Rate": 0.0,
"/SmartDashboard/Target Acquired": True
}
})实时监控
# Subscribe to topics for duration
monitoring_data = await mcp.call_tool("nt_subscribe", {
"topics": [
"/Robot/Mode",
"/Robot/Enabled",
"/SmartDashboard/Match Time"
],
"duration": 15.0 # 15 seconds
})
# Process samples
for sample in monitoring_data["samples"]:
timestamp = sample["timestamp"]
values = sample["values"]
print(f"T+{timestamp}: {values}")主题发现
# List all topics
all_topics = await mcp.call_tool("nt_list_topics")
# Filter topics by pattern
dashboard_topics = await mcp.call_tool("nt_list_topics", {
"filter_pattern": "/SmartDashboard/*"
})
# Get detailed topic information
topic_info = await mcp.call_tool("nt_get_info", {
"topic": "/SmartDashboard/Battery Voltage"
})api参考
连接工具
nt_connect(team_number?, server_ip?, server_port?, identity?)-连接到NT服务器nt_disconnect()-断开与NT服务器的连接nt_connection_info()-获取连接信息nt_time_sync_info()-获取时间同步信息
数据访问工具
nt_get(topic)-获取单个主题值nt_get_multiple(topics[])-获取多个主题值nt_set(topic, value)-设置单个主题值nt_set_array(topic, value)-设置单个主题值-数组类型nt_set_multiple(updates{})-设置多个主题值
发现工具
nt_list_topics(topic_prefix?)-列出可用主题nt_get_info(topic)-获取详细的主题信息
监视工具
nt_subscribe(topic_prefixes[], duration)-记录一段时间内主题中的所有数据
资源URI
nt://topics-列出所有可用主题nt:///path/to/topic-将特定主题作为资源访问
故障排除
常见问题
连接失败
- 验证机器人/模拟器是否正在运行
- 检查团队编号或IP地址
- 确保NetworkTables端口(5810)可访问
缺少主题
- 主题必须首先由机器人代码发布
- 使用
nt_list_topics()查看可用主题 - 检查主题名称拼写和大小写
类型错误
- 验证数据类型是否符合NT主题期望
- 使用
nt_get_info()检查主题类型 - 在代码中处理类型强制
日志记录
启用调试的详细日志记录:
import logging
logging.basicConfig(level=logging.DEBUG)发展模式
对于开发和测试:
uv run fastmcp dev --with pyntcore nt_mcp_server.py 许可证
该项目根据麻省理工学院许可证发布,可供FRC团队和教育使用。
