G1 UART MCP Server

G1 UART MCP Server

Enables communication with G1 Bluetooth devices using the Nordic UART protocol over BLE. Supports device discovery, connection management, and message exchange for G1 audio devices through natural language commands.

Category
访问服务器

README

G1 UART MCP Server

A Model Context Protocol (MCP) server that provides tools for communicating with G1 devices over Bluetooth Low Energy (BLE) using the Nordic UART protocol.

Overview

This project implements an MCP server that enables AI assistants and other MCP clients to:

  • Scan for G1 Bluetooth devices
  • Connect to G1 devices
  • Send and receive messages using the Nordic BLE UART protocol
  • Monitor connection status and device information
  • NEW: Automatically monitor and maintain stable connections
  • NEW: Auto-reconnect on connection loss
  • NEW: Configurable connection monitoring parameters

The server is particularly designed for working with Even G1 devices, which appear to be paired left/right devices for audio applications.

Features

  • Device Discovery: Scan for available G1 devices with automatic side detection (left/right)
  • BLE Connection Management: Connect, disconnect, and monitor connection status
  • Nordic UART Protocol: Full support for the Nordic UART service (6E400001-B5A3-F393-E0A9-E50E24DCCA9E)
  • Message Communication: Send hex-formatted messages and receive responses
  • MCP Integration: Seamless integration with MCP-compatible AI assistants and tools
  • 🆕 Connection Monitoring: Automatic heartbeat and health checks to maintain stable connections
  • 🆕 Auto-Reconnection: Automatically attempt to reconnect if the connection is lost
  • 🆕 Configurable Settings: Adjustable heartbeat intervals, timeouts, and reconnection parameters

Prerequisites

  • Python 3.8+
  • macOS (tested on darwin 24.3.0)
  • Bluetooth Low Energy support
  • Access to G1 devices

Installation

Option 1: Quick Start with uvx (Recommended)

Run directly from GitHub without installation:

uvx --from git+https://github.com/danroblewis/g1_uart_mcp g1-device-mcp

For MCP Configuration: Add this to your mcp.json:

{
  "mcpServers": {
    "g1-device-mcp": {
      "command": "uvx",
      "args": [
        "--from",
        "git+https://github.com/danroblewis/g1_uart_mcp",
        "g1-device-mcp"
      ]
    }
  }
}

Option 2: Local Installation

  1. Clone the repository:
git clone <repository-url>
cd g1_uart_mcp
  1. Create a virtual environment:
python3 -m venv venv
source venv/bin/activate  # On macOS/Linux
  1. Install dependencies:
pip install -r requirements.txt

Configuration

The MCP server is configured via mcp.json. For the recommended uvx method, use this configuration:

{
  "mcpServers": {
    "g1-device-mcp": {
      "command": "uvx",
      "args": [
        "--from",
        "git+https://github.com/danroblewis/g1_uart_mcp",
        "g1-device-mcp"
      ]
    }
  }
}

Alternative: Local Installation If you prefer local installation, update the paths to match your system:

{
  "mcpServers": {
    "g1-device-mcp": {
      "command": "/path/to/your/venv/bin/python",
      "args": [
        "/path/to/your/g1_uart_mcp/mcp_server.py"
      ]
    }
  }
}

Usage

Starting the MCP Server

The server can be started directly or through an MCP client:

With uvx (Recommended)

uvx --from git+https://github.com/danroblewis/g1_uart_mcp g1-device-mcp

Local Installation

python mcp_server.py

Available Tools

1. Scan for G1 Devices

scan_g1_devices()

Scans for available G1 devices and returns a list with device information including name, ID, side (left/right), and signal strength.

2. Connect to G1 Device

connect_g1_device(address)

Connects to a specific G1 device by its address or UUID.

3. Get Connection Status

get_g1_connection_status()

Returns detailed information about the current connection including device details, UART service availability, and connection monitoring statistics.

4. Send Message

send_g1_message(hex_data)

Sends a hex-formatted message to the connected device and waits for a response. Now includes automatic connection health checks and reconnection attempts.

5. Disconnect

disconnect_g1_device()

Disconnects from the currently connected device.

6. 🆕 Configure Connection Settings

configure_g1_connection_settings(
    heartbeat_interval=5.0,
    connection_timeout=30.0,
    auto_reconnect_enabled=True,
    max_reconnect_attempts=3,
    reconnect_delay=2.0
)

Configures connection monitoring and auto-reconnection parameters.

Example Workflow

  1. Scan for devices:

    devices = scan_g1_devices()
    # Returns list of available G1 devices
    
  2. Connect to a device:

    result = connect_g1_device("device_uuid_or_address")
    
  3. Configure connection monitoring (optional):

    configure_g1_connection_settings(
        heartbeat_interval=3.0,  # More frequent heartbeats
        auto_reconnect_enabled=True
    )
    
  4. Send a message:

    response = send_g1_message("2506")
    # Sends command 0x25 with data 0x06
    # Connection is automatically monitored and maintained
    
  5. Check status:

    status = get_g1_connection_status()
    # Now includes connection duration, last activity, and reconnection info
    
  6. Disconnect:

    disconnect_g1_device()
    

Connection Monitoring & Auto-Reconnection

How It Works

The enhanced MCP server now includes robust connection management:

  1. Heartbeat Monitoring: Sends periodic ping messages (every 5 seconds by default) to keep the connection alive
  2. Health Checks: Regularly verifies the connection is still responsive
  3. Auto-Detection: Automatically detects when the connection is lost
  4. Smart Reconnection: Attempts to reconnect up to 3 times with configurable delays
  5. Background Monitoring: All monitoring happens in the background without blocking message sending

Configuration Options

  • heartbeat_interval: How often to send heartbeat messages (default: 5 seconds)
  • connection_timeout: Maximum connection duration before health check (default: 30 seconds)
  • auto_reconnect_enabled: Whether to automatically attempt reconnection (default: True)
  • max_reconnect_attempts: Maximum number of reconnection attempts (default: 3)
  • reconnect_delay: Delay between reconnection attempts (default: 2 seconds)

Benefits

  • Stable Connections: Significantly reduces unexpected disconnections
  • Automatic Recovery: No manual intervention needed when connections drop
  • Better Reliability: Especially important for the critical send_g1_message command
  • Configurable: Adjust settings based on your specific use case and device behavior

Example Files

  • example_connect.py - Demonstrates device connection
  • example_scan.py - Shows device scanning functionality
  • example_send_message.py - Example of sending messages
  • example_tools.py - Comprehensive tool usage examples
  • 🆕 test_connection_monitoring.py - Demonstrates the new connection monitoring features

Architecture

Core Components

  • mcp_server.py: Main MCP server implementation with tool definitions and connection configuration
  • g1_uart_manager.py: Enhanced BLE UART protocol manager with connection monitoring and auto-reconnection
  • mcp.json: MCP server configuration

BLE UART Protocol

The server implements the Nordic UART service:

  • Service UUID: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
  • TX Characteristic: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E (Write)
  • RX Characteristic: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E (Notify)

Dependencies

  • mcp: Model Context Protocol implementation
  • bleak: Cross-platform Bluetooth Low Energy library

Troubleshooting

Common Issues

  1. Permission Denied: Ensure Bluetooth permissions are granted to your application
  2. Device Not Found: Verify the device is in range and discoverable
  3. Connection Failed: Check if the device is already connected to another client
  4. Message Timeout: Ensure the device is responsive and supports the Nordic UART protocol
  5. 🆕 Frequent Disconnections: Try adjusting connection monitoring settings or check device power management

Debug Mode

Enable debug logging by modifying the logging level in mcp_server.py:

logging.basicConfig(level=logging.DEBUG)

Connection Issues

If you're experiencing frequent disconnections:

  1. Check connection monitoring settings:

    status = get_g1_connection_status()
    print(f"Auto-reconnect: {status['auto_reconnect_enabled']}")
    print(f"Reconnect attempts: {status['reconnect_attempts']}")
    
  2. Adjust heartbeat frequency:

    configure_g1_connection_settings(heartbeat_interval=3.0)
    
  3. Increase reconnection attempts:

    configure_g1_connection_settings(max_reconnect_attempts=5)
    
  4. Check device power settings: Some devices may go to sleep mode, causing disconnections

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

[Add your license information here]

Support

For issues and questions, please create an issue or contact the maintainers.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选