AI工作区MCP服务器
一种模型上下文协议(MCP)服务器,为AI提供了一个用于文件管理和Python脚本执行的安全工作区。设计为在Vercel上作为无服务器功能运行。
特性
文件管理工具
- 创建文件 -创建包含内容的新文件
- read_file -读取文件内容
- update_file -更新现有文件
- 删除文件 -删除文件
- 列表文件 -列出文件和目录
- create_directory -创建新目录
代码执行
- execute_python -执行带参数的Python脚本(超时30秒)
Vercel上的设置
1.安装Vercel CLI(可选)
npm install -g vercel2.项目结构
你的项目应该是这样的:
ai-workspace-mcp/
├── api/
│ └── mcp.py # Serverless function
├── vercel.json # Vercel configuration
├── requirements.txt # Python dependencies
└── README.md # This file3.部署到Vercel
选项A:通过Vercel仪表板进行部署
- 首选 vercel.com
- 点击“添加新”→ “项目”
- 导入Git仓库(或上传文件)
- Vercel将自动检测Python并部署
选项B:通过CLI部署
# Login to Vercel
vercel login
# Deploy
vercel
# Deploy to production
vercel --prod4.获取您的部署URL
部署后,Vercel将为您提供一个URL,如下所示: https://your-project-name.vercel.app
API终点
部署后,您的服务器将具有以下端点:
得到/
返回服务器信息和状态
curl https://your-project.vercel.app/GET/健康
健康检查端点
curl https://your-project.vercel.app/healthGET/工具
列出所有可用工具
curl https://your-project.vercel.app/toolsPOST/执行
执行工具
curl -X POST https://your-project.vercel.app/execute \
-H "Content-Type: application/json" \
-d '{
"tool": "create_file",
"arguments": {
"filepath": "hello.py",
"content": "print(\"Hello World!\")"
}
}'与AI客户端一起使用
Claude桌面配置
将此添加到您的Claude Desktop配置中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"ai-workspace": {
"command": "curl",
"args": [
"-X", "POST",
"https://your-project.vercel.app/execute",
"-H", "Content-Type: application/json",
"-d", "@-"
]
}
}
}直接使用API
您可以将其与任何支持HTTP工具调用的AI集成:
import requests
# Create a file
response = requests.post(
"https://your-project.vercel.app/execute",
json={
"tool": "create_file",
"arguments": {
"filepath": "script.py",
"content": "print('Hello from AI!')"
}
}
)
print(response.json())
# Execute the file
response = requests.post(
"https://your-project.vercel.app/execute",
json={
"tool": "execute_python",
"arguments": {
"filepath": "script.py"
}
}
)
print(response.json())安全功能
- 沙盒工作区:所有文件操作仅限于
/tmp/workspace - 路径验证:防止目录遍历攻击
- 执行超时:Python脚本限制为30秒
- CORS已启用:允许跨源请求
- 无服务器隔离:每个请求都在隔离的环境中运行
工具示例
创建并执行Python脚本
# Create a file
curl -X POST https://your-project.vercel.app/execute \
-H "Content-Type: application/json" \
-d '{
"tool": "create_file",
"arguments": {
"filepath": "hello.py",
"content": "print(\"Hello from Vercel!\")"
}
}'
# Execute it
curl -X POST https://your-project.vercel.app/execute \
-H "Content-Type: application/json" \
-d '{
"tool": "execute_python",
"arguments": {
"filepath": "hello.py"
}
}'列出文件
curl -X POST https://your-project.vercel.app/execute \
-H "Content-Type: application/json" \
-d '{
"tool": "list_files",
"arguments": {}
}'创建目录结构
curl -X POST https://your-project.vercel.app/execute \
-H "Content-Type: application/json" \
-d '{
"tool": "create_directory",
"arguments": {
"dirpath": "scripts"
}
}'响应格式
所有工具执行都返回JSON:
成功响应:
{
"success": true,
"message": "Successfully created file: hello.py\nSize: 26 bytes"
}错误响应:
{
"success": false,
"error": "File not found: nonexistent.py"
}执行Python响应:
{
"success": true,
"exit_code": 0,
"stdout": "Hello from Vercel!\n",
"stderr": ""
}重要说明
Vercel限制
- 临时存储:文件在
/tmp是短暂的,在调用之间清除 - 10秒超时:免费版Vercel功能在10秒后超时(专业版为25秒)
- 冷启动:由于冷启动,第一次请求可能会变慢
- 无持久状态:每次函数调用都会重新开始
用于持久存储
如果您需要持久文件存储,请考虑:
- 使用Vercel KV、Postgres或Blob存储
- 与AWS S3、谷歌云存储等集成。
- 使用数据库存储文件内容
环境变量(可选)
您可以在Vercel仪表板中设置环境变量:
WORKSPACE_PATH-自定义工作区路径(默认:/tmp/workspace)EXECUTION_TIMEOUT-Python执行超时(秒)(默认值:30)
本地开发
部署前进行本地测试:
# Install dependencies
pip install -r requirements.txt
# Run with Python's built-in server
cd api
python -m http.server 8000
# Or use Vercel CLI
vercel dev然后用以下方法进行测试:
curl http://localhost:8000/health故障排除
“找不到模块”错误
确保 requirements.txt 位于项目根目录中,包含所有依赖项。
超时错误
- 降低Python脚本的复杂性
- 升级到Vercel Pro以获得更长的超时时间
- 尽可能使用异步操作
文件未持久化
记得: /tmp Vercel上的存储是短暂的。文件在调用之间不会持久。
高级用法
自定义MCP客户端
class VercelMCPClient:
def __init__(self, base_url):
self.base_url = base_url
def call_tool(self, tool_name, arguments):
response = requests.post(
f"{self.base_url}/execute",
json={"tool": tool_name, "arguments": arguments}
)
return response.json()
def list_tools(self):
response = requests.get(f"{self.base_url}/tools")
return response.json()
# Usage
client = VercelMCPClient("https://your-project.vercel.app")
result = client.call_tool("create_file", {
"filepath": "test.py",
"content": "print('test')"
})贡献
请随时使用其他工具扩展此服务器:
- 将工具定义添加到
get_tools() - 在中实现处理程序
execute_tool() - 更新文档
许可证
MIT许可证-根据需要进行修改和使用。
支持
- Vercel文档: vercel.com/docs
- MCP协议: 模型上下文协议.io
- 问题:打开问题或根据需要修改代码
