本项目是一个MCP(模型上下文协议)服务器,借助FastMCP和vgamepad实现了Xbox控制器的模拟功能。通过该服务器,你可以方便地模拟Xbox控制器的各种操作,为开发和测试提供便利。
uv sync
此命令将创建一个虚拟环境,并从pyproject.toml文件中安装所有依赖项。
如果你更喜欢使用pip,可以执行以下命令:
pip install -r requirements.txt
若要启用实际的控制器输入(而非模拟模式),请按以下步骤操作:
ViGEmBus_Setup_x64.exe。若未安装ViGEmBus驱动,服务器将以模拟模式运行,此时仅跟踪控制器状态,不会向系统发送实际输入。
使用uv启动:
uv run main.py
或者直接使用Python启动:
python main.py
服务器启动后将监听MCP连接,并自动检测是使用硬件模式(需安装ViGEmBus)还是模拟模式。
press_button(button):按下指定的控制器按键。release_button(button):释放指定的控制器按键。tap_button(button, duration=0.1):点击指定按键(按下并释放)。set_left_stick(x, y):设置左摇杆的位置(取值范围为 -1.0 到 1.0)。set_right_stick(x, y):设置右摇杆的位置(取值范围为 -1.0 到 1.0)。set_triggers(left, right):设置扳机的值(取值范围为 0.0 到 1.0)。reset_controller():将控制器重置为中立状态。get_controller_state():获取当前控制器的状态。list_available_buttons():列出所有可用的按键名称。get_system_info():获取系统信息和设置状态。A、B、X、YLB、RBBACK、STARTLS、RSDPAD_UP、DPAD_DOWN、DPAD_LEFT、DPAD_RIGHT# 按下A键
await client.call_tool("press_button", {"button": "A"})
# 将左摇杆移动到右上方
await client.call_tool("set_left_stick", {"x": 0.5, "y": 0.5})
# 将两个扳机都设置为半按状态
await client.call_tool("set_triggers", {"left": 0.5, "right": 0.5})
# 点击X键0.2秒
await client.call_tool("tap_button", {"button": "X", "duration": 0.2})
# 重置所有设置
await client.call_tool("reset_controller", {})
main.py:实现了带有Xbox控制器模拟功能的主MCP服务器。pyproject.toml:项目配置和依赖文件(兼容uv/pip)。requirements.txt:供pip用户使用的Python依赖文件。uv.lock:用于精确锁定依赖版本的锁文件(uv)。mcp-config.json:MCP客户端配置文件。README.md:本项目文档。若要将此服务器与Claude Desktop或其他MCP客户端配合使用,请将mcp-config.json中的配置添加到你的MCP客户端设置中:
{
"mcpServers": {
"xbox-controller": {
"command": "python",
"args": ["main.py"],
"cwd": "c:\\Users\\blain\\Documents\\git\\controller_mcp",
"env": {},
"description": "Xbox Controller Emulator MCP Server - Provides tools to emulate Xbox controller inputs including buttons, analog sticks, and triggers. Supports both hardware mode (with ViGEmBus) and simulation mode."
}
}
}
如果你使用的是uv,也可以将其配置为使用uv执行:
{
"mcpServers": {
"xbox-controller": {
"command": "uv",
"args": ["run", "main.py"],
"cwd": "c:\\Users\\blain\\Documents\\git\\controller_mcp",
"env": {},
"description": "Xbox Controller Emulator MCP Server - Provides tools to emulate Xbox controller inputs including buttons, analog sticks, and triggers. Supports both hardware mode (with ViGEmBus) and simulation mode."
}
}
}
服务器会自动检测ViGEmBus驱动是否可用:
启动服务器时,可查看控制台输出以确定当前使用的模式。
get_system_info()工具检查当前模式和设置状态。本项目采用现代Python工具:
服务器采用模块化设计。若要添加新功能,请按以下步骤操作:
main.py中的XboxControllerEmulator类中添加新方法。@mcp.tool()装饰器创建相应的MCP工具。uv sync --dev
uv run main.py
get_system_info()工具验证设置。本项目采用MIT许可证,详情请参阅LICENSE文件。
欢迎贡献代码!请随时提交拉取请求或提出问题,反馈Bug和功能需求。