Google Apps Script MCP(模型上下文协议)服务器是一个强大的工具,它允许你通过任何兼容MCP的客户端,如Claude Desktop、带有Cline的VS Code或Postman,来管理Google Apps Script项目、部署、版本和执行。
git clone https://github.com/mohalmah/google-apps-script-mcp-server.git
cd google-apps-script-mcp-server
npm install
请按照下面的详细OAuth设置指南进行操作。
npm run setup-oauth
npm start
克隆仓库:
git clone https://github.com/mohalmah/google-apps-script-mcp-server.git
cd google-apps-script-mcp-server
安装依赖:
npm install
对于Google Apps Script API:
配置范围(可选但推荐):
https://www.googleapis.com/auth/script.projectshttps://www.googleapis.com/auth/script.projects.readonlyhttps://www.googleapis.com/auth/script.deploymentshttps://www.googleapis.com/auth/script.deployments.readonlyhttps://www.googleapis.com/auth/script.metricshttps://www.googleapis.com/auth/script.processes添加测试用户(适用于外部应用):
http://localhost:3001/oauth/callback
1234567890-abcdefghijklmnop.apps.googleusercontent.comGOCSPX-abcdefghijklmnopqrstuvwxyz在项目根目录下创建一个.env文件:
# 在Windows上
type nul > .env
# 在macOS/Linux上
touch .env
编辑.env文件并添加你的凭证:
# Google Apps Script API OAuth配置
GOOGLE_APP_SCRIPT_API_CLIENT_ID=your_client_id_here
GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=your_client_secret_here
# 可选:日志级别
LOG_LEVEL=info
将占位符替换为实际值:
your_client_id_here替换为你的客户端ID。your_client_secret_here替换为你的客户端密钥。执行OAuth设置脚本:
npm run setup-oauth
此操作的作用:
http://localhost:3001上启动一个临时本地服务器。当浏览器打开时:
设置过程会安全地存储令牌:
npm start
你应该看到类似以下的输出:
Google Apps Script MCP Server running on stdio
OAuth tokens loaded successfully
Server ready to handle MCP requests
# 列出所有可用工具
npm run list-tools
# 测试OAuth连接
npm run test-oauth
# 启用调试日志
npm run debug
以下是使用script-projects-create工具创建新Google Apps Script项目的示例:
// 创建一个用于自动化任务的新脚本
{
"title": "My Automation Script",
"parentId": "1234567890"
}
以下是使用script-scripts-run工具远程触发脚本执行的示例:
// 假设你有一个名为'myFunction'的函数,并且脚本ID为'1ABC123def456GHI789jkl'
{
"scriptId": "1ABC123def456GHI789jkl",
"functionName": "myFunction",
"parameters": {
"param1": "value1",
"param2": "value2"
}
}
此MCP服务器提供16个全面的工具,用于管理Google Apps Script:
| 类别 | 工具 | 用途 |
|---|---|---|
| 项目管理 | create, get, get-content, update-content | 管理脚本项目和源代码 |
| 版本控制 | versions-create, versions-get, versions-list | 处理脚本版本控制 |
| 部署 | deployments-create, deployments-get, deployments-list, deployments-update, deployments-delete | 管理脚本部署 |
| 执行 | scripts-run | 执行脚本函数 |
| 监控 | processes-list, get-metrics | 监控执行和性能 |
开发工作流程:
script-projects-create创建新项目。script-projects-update-content上传代码。script-projects-versions-create创建稳定版本。script-projects-deployments-create进行生产部署。监控和调试:
script-processes-list查看执行历史。script-projects-get-metrics分析性能。script-projects-get-content备份源代码。生产管理:
script-projects-deployments-list查看所有部署。script-projects-deployments-update更新生产配置。script-scripts-run触发自动化工作流。MCP服务器(mcpServer.js)将你的自动化API工具暴露给兼容MCP的客户端,如Claude Desktop或Postman桌面应用程序。我们建议你先使用Postman测试服务器,然后再将其与大型语言模型(LLM)一起使用。
步骤1:从https://www.postman.com/downloads/下载最新的Postman桌面应用程序。
步骤2:阅读此处的文档文章,了解如何在Postman应用程序中创建MCP请求。
步骤3:将MCP请求的类型设置为STDIO,并将命令设置为node 。
对于Windows用户,可以通过以下命令获取Node.js的完整路径:
Get-Command node | Select-Object -ExpandProperty Source
对于macOS/Linux用户,可以通过以下命令获取Node.js的完整路径:
which node
要检查任何平台上的Node.js版本,请运行:
node --version
对于Windows用户,要获取mcpServer.js的绝对路径,请运行:
Get-Location | Select-Object -ExpandProperty Path
然后在路径后面追加\mcpServer.js。
对于macOS/Linux用户,要获取mcpServer.js的绝对路径,请运行:
realpath mcpServer.js
使用node命令后跟mcpServer.js的完整路径作为新Postman MCP请求的命令。然后点击“连接”按钮。你应该会看到在生成服务器之前选择的工具列表。在将MCP服务器连接到LLM之前,你可以在此处测试每个工具是否正常工作。
你可以将MCP服务器连接到各种MCP客户端。以下是针对Claude Desktop和VS Code的详细配置说明。
在配置任何MCP客户端之前,你需要Node.js和mcpServer.js文件的绝对路径。
获取Node.js路径:
Get-Command node | Select-Object -ExpandProperty Source
示例输出:C:\nvm4w\nodejs\node.exe
如果第一种方法不起作用,可以使用替代方法:
where.exe node
获取当前目录路径:
Get-Location | Select-Object -ExpandProperty Path
示例输出:C:\Users\mohal\Downloads\google-appscriot-mcp-server
完整的mcpServer.js路径:
Join-Path (Get-Location) "mcpServer.js"
示例输出:C:\Users\mohal\Downloads\google-appscriot-mcp-server\mcpServer.js
快速复制粘贴命令,获取两个路径:
Write-Host "Node.js path: $((Get-Command node).Source)"
Write-Host "mcpServer.js path: $(Join-Path (Get-Location) 'mcpServer.js')"
获取Node.js路径:
which node
示例输出:/usr/local/bin/node 或 /opt/homebrew/bin/node
获取mcpServer.js路径:
realpath mcpServer.js
示例输出:/Users/username/google-apps-script-mcp-server/mcpServer.js
替代方法:
echo "$(pwd)/mcpServer.js"
快速复制粘贴命令,获取两个路径:
echo "Node.js path: $(which node)"
echo "mcpServer.js path: $(realpath mcpServer.js)"
获取Node.js路径:
which node
示例输出:/usr/bin/node 或 /usr/local/bin/node
获取mcpServer.js路径:
realpath mcpServer.js
示例输出:/home/username/google-apps-script-mcp-server/mcpServer.js
快速复制粘贴命令,获取两个路径:
echo "Node.js path: $(which node)"
echo "mcpServer.js path: $(realpath mcpServer.js)"
在任何平台上,验证你的Node.js版本:
node --version
确保显示的版本为18或更高。
步骤1:记录上一节中的完整路径。
步骤2:打开Claude Desktop,导航到:
步骤3:添加你的MCP服务器配置:
{
"mcpServers": {
"google-apps-script": {
"command": "" ,
"args": ["" ],
"env": {
"GOOGLE_APP_SCRIPT_API_CLIENT_ID": "your_client_id_here",
"GOOGLE_APP_SCRIPT_API_CLIENT_SECRET": "your_client_secret_here"
}
}
}
}
{
"mcpServers": {
"google-apps-script": {
"command": "C:\\nvm4w\\nodejs\\node.exe",
"args": ["C:\\Users\\mohal\\Downloads\\google-appscriot-mcp-server\\mcpServer.js"],
"env": {
"GOOGLE_APP_SCRIPT_API_CLIENT_ID": "1234567890-abcdefghijk.apps.googleusercontent.com",
"GOOGLE_APP_SCRIPT_API_CLIENT_SECRET": "GOCSPX-abcdefghijklmnopqrstuvwxyz"
}
}
}
}
{
"mcpServers": {
"google-apps-script": {
"command": "/usr/local/bin/node",
"args": ["/Users/username/google-apps-script-mcp-server/mcpServer.js"],
"env": {
"GOOGLE_APP_SCRIPT_API_CLIENT_ID": "1234567890-abcdefghijk.apps.googleusercontent.com",
"GOOGLE_APP_SCRIPT_API_CLIENT_SECRET": "GOCSPX-abcdefghijklmnopqrstuvwxyz"
}
}
}
}
步骤4:将OAuth凭证替换为.env文件中的实际值。
步骤5:保存配置并重启Claude Desktop。
步骤6:通过检查Claude Desktop中MCP服务器旁边的绿色圆圈指示器来验证连接。
VS Code可以通过扩展(如Cline或其他兼容MCP的扩展)使用MCP服务器。
步骤1:从VS Code市场安装Cline扩展。
步骤2:打开VS Code设置(Windows/Linux使用Ctrl+,,macOS使用Cmd+,)。
步骤3:在设置中搜索“Cline”或“MCP”。
步骤4:添加你的MCP服务器配置:
添加到VS Code的settings.json(通过Ctrl+Shift+P → “首选项:打开设置(JSON)”访问):
{
"cline.mcpServers": {
"google-apps-script": {
"command": "C:\\nvm4w\\nodejs\\node.exe",
"args": ["C:\\Users\\mohal\\Downloads\\google-appscriot-mcp-server\\mcpServer.js"],
"env": {
"GOOGLE_APP_SCRIPT_API_CLIENT_ID": "your_client_id_here",
"GOOGLE_APP_SCRIPT_API_CLIENT_SECRET": "your_client_secret_here"
}
}
}
}
在项目根目录下创建一个.vscode/settings.json文件:
{
"cline.mcpServers": {
"google-apps-script": {
"command": "node",
"args": ["./mcpServer.js"],
"env": {
"GOOGLE_APP_SCRIPT_API_CLIENT_ID": "your_client_id_here",
"GOOGLE_APP_SCRIPT_API_CLIENT_SECRET": "your_client_secret_here"
}
}
}
}
%APPDATA%\Claude\claude_desktop_config.json~/Library/Application Support/Claude/claude_desktop_config.json~/.config/claude-desktop/claude_desktop_config.json%APPDATA%\Code\User\settings.json~/Library/Application Support/Code/User/settings.json~/.config/Code/User/settings.json将以下路径替换为你系统的实际路径:
{
"mcpServers": {
"google-apps-script": {
"command": "C:\\nvm4w\\nodejs\\node.exe",
"args": ["C:\\Users\\mohal\\Downloads\\google-appscriot-mcp-server\\mcpServer.js"],
"env": {
"GOOGLE_APP_SCRIPT_API_CLIENT_ID": "your_actual_client_id",
"GOOGLE_APP_SCRIPT_API_CLIENT_SECRET": "your_actual_client_secret"
}
}
}
}
{
"mcpServers": {
"google-apps-script": {
"command": "/usr/local/bin/node",
"args": ["/Users/username/google-apps-script-mcp-server/mcpServer.js"],
"env": {
"GOOGLE_APP_SCRIPT_API_CLIENT_ID": "your_actual_client_id",
"GOOGLE_APP_SCRIPT_API_CLIENT_SECRET": "your_actual_client_secret"
}
}
}
}
{
"mcpServers": {
"google-apps-script": {
"command": "/usr/bin/node",
"args": ["/home/username/google-apps-script-mcp-server/mcpServer.js"],
"env": {
"GOOGLE_APP_SCRIPT_API_CLIENT_ID": "your_actual_client_id",
"GOOGLE_APP_SCRIPT_API_CLIENT_SECRET": "your_actual_client_secret"
}
}
}
}
🔑 请记住:
your_actual_client_id和your_actual_client_secret替换为你的OAuth凭证。username。npm run setup-oauth。# 必需的OAuth凭证
GOOGLE_APP_SCRIPT_API_CLIENT_ID=your_client_id
GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=your_client_secret
# 可选配置
LOG_LEVEL=info # debug, info, warn, error
NODE_ENV=development # development, production
PORT=3001 # OAuth回调端口
debug:详细的调试信息info:一般信息消息warn:警告消息error:仅错误消息# 安装PM2
npm install -g pm2
# 使用PM2启动
pm2 start mcpServer.js --name "gas-mcp-server"
# 监控
pm2 status
pm2 logs gas-mcp-server
# 系统启动时自动重启
pm2 startup
pm2 save
构建Docker镜像:
docker build -t google-apps-script-mcp .
使用Docker运行:
docker run -i --rm --env-file=.env google-apps-script-mcp
Docker Compose设置:
version: '3.8'
services:
gas-mcp:
build: .
env_file:
- .env
stdin_open: true
tty: true
{
"mcpServers": {
"google-apps-script": {
"command": "docker",
"args": ["run", "-i", "--rm", "--env-file=.env", "google-apps-script-mcp"]
}
}
}
tools/google-app-script-api/apps-script-api/中创建一个新的工具文件:import { getAuthHeaders } from '../../../lib/oauth-helper.js';
const executeFunction = async ({ param1, param2 }) => {
const baseUrl = 'https://script.googleapis.com';
try {
const headers = await getAuthHeaders();
const response = await fetch(`${baseUrl}/v1/your-endpoint`, {
method: 'POST',
headers,
body: JSON.stringify({ param1, param2 })
});
return await response.json();
} catch (error) {
throw new Error(`API call failed: ${error.message}`);
}
};
export { executeFunction };
export const toolPaths = [
// ...现有路径...
'google-app-script-api/apps-script-api/your-new-tool.js'
];
import { getAuthHeaders } from '../../../lib/oauth-helper.js';
/**
* 工具描述和JSDoc注释
*/
const executeFunction = async (args) => {
const baseUrl = 'https://script.googleapis.com';
try {
// 1. 验证参数
if (!args.requiredParam) {
throw new Error('requiredParam is required');
}
// 2. 获取认证头
const headers = await getAuthHeaders();
// 3. 进行API调用
const response = await fetch(`${baseUrl}/v1/endpoint`, {
method: 'GET/POST/PUT/DELETE',
headers,
body: JSON.stringify(args) // 适用于POST/PUT
});
// 4. 处理响应
if (!response.ok) {
throw new Error(`API error: ${response.status} ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error('Tool execution failed:', error);
throw error;
}
};
export { executeFunction };
要实现与Web界面的实时通信:
npm run start-sse
服务器将以支持SSE的HTTP模式运行,用于流式响应。
NODE_ENV=development
LOG_LEVEL=debug
GOOGLE_APP_SCRIPT_API_CLIENT_ID=dev_client_id
GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=dev_client_secret
NODE_ENV=production
LOG_LEVEL=info
GOOGLE_APP_SCRIPT_API_CLIENT_ID=prod_client_id
GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=prod_client_secret
OAuth助手会自动将访问令牌缓存在内存中,并在需要时刷新。
对于多个操作,尽可能考虑批处理请求:
// 代替多个单独的调用
const results = await Promise.all([
tool1(args1),
tool2(args2),
tool3(args3)
]);
Google Apps Script API有速率限制。工具中包含了带有指数退避的自动重试逻辑。
.env文件提交到版本控制中。问题:MCP客户端无法找到Node.js可执行文件。 解决方案:
node --version验证Node.js版本是否为18或更高。问题:你的Node.js版本低于18。 解决方案:
node-fetch作为依赖项:npm install node-fetch
然后修改每个工具文件以导入fetch:import fetch from 'node-fetch';
问题:认证失败或令牌问题。 解决方案:
.env文件中的OAuth凭证是否正确。npm run setup-oauth。http://localhost:3001/oauth/callback。问题:Google OAuth同意屏幕配置问题。 解决方案:
问题:配置文件语法或路径问题。 解决方案:
问题:扩展未识别MCP服务器。 解决方案:
问题:文件权限问题。 解决方案:
mcpServer.js文件可执行:chmod +x mcpServer.js。node命令:node /path/to/mcpServer.js。问题:OAuth设置期间端口3001已被使用。 解决方案:
# 查找使用端口3001的进程
lsof -i :3001 # macOS/Linux
netstat -ano | findstr :3001 # Windows
# 杀死进程
kill -9 # macOS/Linux
taskkill /PID /F # Windows
oauth-setup.js中的端口。问题:OAuth令牌已过期或无效。 解决方案:
npm run setup-oauth。问题:无法执行脚本或访问项目。 解决方案:
npm start
如果它能正常启动且没有错误,说明你的基本设置正确。
npm run test-oauth
这将验证你的OAuth设置是否正常工作。
npm run debug
这将提供详细的日志记录,帮助你识别问题。
npm run list-tools
这将列出所有可用工具及其参数。
设置LOG_LEVEL环境变量:
# 在.env文件中
LOG_LEVEL=debug
# 或者使用调试模式运行
npm run debug
OAuth设置过程会提供详细的输出。注意以下内容:
成功消息:
OAuth tokens loaded successfullyServer ready to handle MCP requestsTool executed successfully警告消息:
Token refresh required(正常操作)Retrying API call with refreshed token错误消息:
OAuth credentials not found → 检查.env文件。Failed to refresh token → 重新运行OAuth设置。API call failed → 检查权限和配额。寻求帮助时,请提供以下信息:
node --version)。.env文件的内容(不包含机密信息)。# 启动MCP服务器
npm start
# 启动并支持SSE
npm run start-sse
# 启动并使用调试日志
npm run debug
# 启动SSE并使用调试日志
npm run debug-sse
# 列出所有可用工具及其描述
npm run list-tools
# 测试OAuth认证
npm run test-oauth
# 设置或刷新OAuth令牌
npm run setup-oauth
# 测试日志功能
npm run test-logging
npm run list-tools
示例输出:
可用工具:
Google Apps Script API:
script-projects-create
描述:创建一个新的Google Apps Script项目
参数:
- title(必需):新脚本项目的标题
- parentId(可选):父项目的ID
script-projects-get
描述:获取Google Apps Script项目的元数据
参数:
- scriptId(必需):要检索的脚本项目的ID
- fields(可选):要包含在响应中的特定字段
[... 其他参数 ...]
tools/文件夹中。tools/paths.js以包含新的工具引用。#mcp-lab频道。欢迎贡献代码!请按照以下步骤操作:
本项目采用MIT许可证。有关详细信息,请参阅LICENSE文件。