article
README
🚀 运行MCP客户端
本项目提供了一个MCP客户端,用于与文件系统服务器进行交互。在运行此客户端之前,请确保服务器已启动并正常运行。有关更多详细信息,请参阅:https://github.com/dazzaji/filesystem
🚀 快速开始
📦 安装指南
步骤1:创建虚拟环境并正确安装依赖项
使用以下命令创建并进入项目目录:
mkdir mcp-client-py
cd mcp-client-py
接着,创建并激活虚拟环境:
python -m venv venv
source venv/bin/activate
最后,安装所需的依赖项:
pip install -r requirements.txt
此命令创建一个虚拟环境,激活它,然后将 mcp 和 python-dotenv(及其依赖项)安装到该环境中。
步骤2:确认或修复.env文件中的环境变量
创建一个 .env 文件,指定服务器路径和允许的目录。您还可以指定工具和参数以进行测试。
SERVER_PATH=/Users/dazzagreenwood/filesystem/dist/index.js
ALLOWED_DIRECTORY=/Users/dazzagreenwood/mcp-hello/module1/files
# 用于测试的可选TOOL和ARGS:
# TOOL=list_directory
# ARGS='{"path": "/Users/dazzagreenwood/mcp-hello/module1/files", "recursive": true}'
# 或使用其他工具进行测试
# TOOL=read_file
# ARGS='{"path": "/Users/dazzagreenwood/mcp-hello/module1/files/test.txt"}'
💻 使用示例
步骤3:运行Python客户端
运行客户端以测试服务器。
python client.py
- 如果您使用了可选的
TOOL和ARGS,请像这样添加这些参数:
python client.py --tool "list_directory" --args '{"path": "/Users/dazzagreenwood/mcp-hello/module1/files", "recursive": true}'
此命令将:
- 连接到文件系统服务器。
- 列出可用工具。
- 调用
write_file在指定目录中创建一个测试文件,如果未在.env文件中指定工具。 - 显示结果到控制台。
步骤4:验证结果
成功运行客户端后,请检查以下内容:
- 客户端输出:您应该看到类似
Available tools: [ ... ]的输出,并显示从write_file(或其他工具调用)的结果。 - 文件创建:确认
/Users/dazzagreenwood/mcp-hello/module1/files/目录中已创建testfile.txt文件,并且内容正确。
测试工作流程
启动服务器
- 打开终端。您需要从终端启动服务器。
- 导航到服务器目录。更改目录到包含服务器文件的文件夹:
cd /Users/dazzagreenwood/filesystem
- 运行服务器:
node dist/index.js "/Users/dazzagreenwood/mcp-hello/module1/files"
- 将
/Users/dazzagreenwood/filesystem/dist/index.js替换为您实际的编译文件路径。 - 将
/Users/dazzagreenwood/mcp-hello/module1/files替换为允许的目录。
使用客户端进行测试
- 打开另一个终端窗口 或拆分当前终端标签页。
- 导航到客户端目录:更改目录进入
mcp-client-py文件夹:
cd /Users/dazzagreenwood/mcp-client-server/mcp-client-py
source .venv/bin/activate
- 以默认模式运行客户端:
为了测试基本连接性和
write_file工具:
python client.py
此命令将尝试调用写文件,以便在 /Users/dazzagreenwood/mcp-hello/module1/files 目录中创建一个名为 test.txt 的文件,并将其内容设置为 Hello, World!。
- 运行其他测试: 您可以使用以下命令测试其他工具:
- 测试
list_directory工具:
python client.py --tool "list_directory" --args '{"path": "/Users/dazzagreenwood/mcp-hello/module1/files", "recursive": true}'
- 测试
read_file工具:
python client.py --server-path /Users/dazzagreenwood/filesystem/dist/index.js --allowed-dir /Users/dazzagreenwood/mcp-hello/module1/files --tool read_file --args '{"path": "/Users/dazzagreenwood/mcp-hello/module1/files/test.txt"}'
- 将上述命令中的路径替换为您自己的本地路径。
如果设置正确,您应该会看到确认每个操作的输出。
微信扫一扫