本项目提供了一个MCP客户端,用于与文件系统服务器进行交互。在运行此客户端之前,请确保服务器已启动并正常运行。有关更多详细信息,请参阅:https://github.com/dazzaji/filesystem
使用以下命令创建并进入项目目录:
mkdir mcp-client-py
cd mcp-client-py
接着,创建并激活虚拟环境:
python -m venv venv
source venv/bin/activate
最后,安装所需的依赖项:
pip install -r requirements.txt
此命令创建一个虚拟环境,激活它,然后将 mcp 和 python-dotenv(及其依赖项)安装到该环境中。
创建一个 .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"}'
运行客户端以测试服务器。
python client.py
TOOL 和 ARGS,请像这样添加这些参数:python client.py --tool "list_directory" --args '{"path": "/Users/dazzagreenwood/mcp-hello/module1/files", "recursive": true}'
此命令将:
write_file 在指定目录中创建一个测试文件,如果未在 .env 文件中指定工具。成功运行客户端后,请检查以下内容:
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"}'
如果设置正确,您应该会看到确认每个操作的输出。