使用说明
copilot_mcp_t工具
copilot_mcp_tool 是一个基于Rust的模型上下文协议(MCP)服务器,旨在充当像Gemini这样的大型语言模型(LLM)之间的桥梁(通过 copilot-client)以及一系列嵌套的工具。本项目演示了如何使用MCP创建多级嵌套工具调用系统。
项目结构
该项目已被重构为客户端守护进程架构,由单个二进制文件管理: copilot_mcp_tool.
copilot_mcp_tool(主二进制): 这是核心应用程序,同时充当服务器和客户端。- 服务器模式: 当开始时
start,它作为一个独立的后台进程运行,监听MCP连接。 - 客户端模式: 当与以下命令一起使用时
list或call,它连接到正在运行的服务器实例以执行命令。
- 服务器模式: 当开始时
mcp_web_client(二进制): 一个简单的基于web的GUI,连接到copilot_mcp_tool服务器提供用于调用工具的用户界面。- 嵌套工具模块: 该项目仍包含
WeatherTool,TimeTool,以及EchoTool演示多级嵌套工具调用。
先决条件
在开始之前,请确保已安装以下内容:
- 锈蚀和货物: https://www.rust-lang.org/tools/install
- GitHub命令行界面(
gh): https://cli.github.com/ (确保您已使用登录gh auth login). - GitHub代币: 个人访问令牌(PAT)
copilot需要范围(或足够的副驾驶访问范围)。
安装和设置
-
克隆存储库:
git clone https://github.com/jmikedupont2/copilot_mcp_tool.git cd copilot_mcp_tool -
设置您的GitHub令牌: 该项目依赖于
GITHUB_TOKEN用于使用Copilot API进行身份验证的环境变量。要设置此项,您可以提取GitHub CLI令牌(通常具有必要的作用域)并将其保存到
.env文件。# Extract your GitHub token using gh CLI and save to .env # Note: This requires gh CLI to be logged in. # On Windows PowerShell: (gh auth token).Trim() | Set-Content -Path .\.env -Value { "GITHUB_TOKEN=" + $_ } # On Linux/macOS or Git Bash: # gh auth token > .env # echo "GITHUB_TOKEN=$(cat .env)" > .env # Note: The above two lines are a quick hack. A safer way is: # echo "GITHUB_TOKEN=$(gh auth token)" > .env重要提示: 对于
GITHUB_TOKEN由以下人员取走cargo run,你 必须 在执行命令之前,确保它已加载到shell的环境中。- 手动(Windows命令提示符):
set GITHUB_TOKEN=YOUR_TOKEN_HERE - 手动(Windows PowerShell):
$env:GITHUB_TOKEN="YOUR_TOKEN_HERE" - 手动(Linux/macOS/Git Bash):
export GITHUB_TOKEN="YOUR_TOKEN_HERE" - 使用
direnv(建议开发): 安装direnv并添加eval "$(direnv hook bash)"(或与shell等效的)到shell配置。然后,简单地说export GITHUB_TOKEN=YOUR_TOKEN_HERE在你的.envrc文件,或者如果您更喜欢重用.env上面创建的文件,putsource .env在你的.envrc.
- 手动(Windows命令提示符):
-
构建项目:
cargo build
用法
这 copilot_mcp_tool 是一个命令行工具,用于管理MCP服务器并与之交互。
管理服务器
首先,构建项目:
cargo build
检查服务器状态: 默认情况下,不带参数运行该工具会显示服务器状态。
cargo run --bin copilot_mcp_tool
# Or, more explicitly:
cargo run --bin copilot_mcp_tool -- status
# Output: Server is STOPPED.
启动服务器: 这将启动服务器作为后台进程。
cargo run --bin copilot_mcp_tool -- start
# Output:
# Server starting in background...
# Server is RUNNING on port 58361 (PID: 12345).
停止服务器:
cargo run --bin copilot_mcp_tool -- stop
# Output:
# INFO copilot_mcp_tool: Stopping server (PID: 12345)...
# INFO copilot_mcp_tool: Server stopped.
与服务器交互
服务器运行后,您可以使用客户端命令。
列出可用工具:
cargo run --bin copilot_mcp_tool -- list
这将连接到正在运行的服务器并打印可用工具的JSON列表(echo_message 和 kill_process).
调用工具:
这 call 命令使用a tool_name 然后是参数的键值对。
# Example with a simple message
cargo run --bin copilot_mcp_tool -- call echo_message message=hello
# Example with spaces in the value (use quotes)
cargo run --bin copilot_mcp_tool -- call echo_message message="hello world"
该工具将连接到服务器,执行命令,并打印JSON结果。
使用Web GUI
该项目还包括一个简单的web客户端。
-
启动MCP服务器:
cargo run --bin copilot_mcp_tool -- start -
运行web客户端: 在单独的终端中:
cargo run --bin mcp_web_clientweb客户端将启动并继续监听
http://localhost:3000. -
打开浏览器 并导航到
http://localhost:3000通过web界面与服务器交互。
贡献
请随时打开问题或拉取请求,以改进嵌套MCP工具调用的演示。