本项目提供了一个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=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: [ ... ] 的输出,并显示每个操作的结果。按照以上说明,您可以使用 Python 和 TypeScript/Node.js 使用各种工具测试 MCP 服务器和客户端!
list_directory 工具python client.py --server-path /Users/dazzagreenwood/filesystem/dist/index.js --allowed-directory /Users/dazzagreenwood/mcp-hello/module1/files --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-directory /Users/dazzagreenwood/mcp-hello/module1/files --tool read_file --args '{"path": "/Users/dazzagreenwood/mcp-hello/module1/files/test.txt"}'
如果配置正确,您应该能够看到每个操作的输出,确认其成功执行。