MCP 代码示例
概览
一个简单的MCP服务器,提供工具、资源和提示,用于使用Google Sheets和Google Forms。 - list_spreadsheets - 列出你的谷歌表格 - read_sheet - 从谷歌表格读取数据 - write_sheet - 将数据写入谷歌表格 - append_sheet - 将数据添加到谷歌表格 - create_spreadsheet - 创建新电子表格 - create_form - 创建新的谷歌表单 - sheet://{spreadsheet_id}/{range_name} - 访问表数据作为资源 - analyze_sheet_data - 用于分析表格数据的模板 - create_report_template - 创建报告电子表格的模板 - form_to_sheet - 表单到表格工作流程的模板 1. 进入 Google Cloud 控制台 2.创建新项目或选择现有项目 3.启用以下 API:- Google Sheets API - Google 表单 API - Google Drive API 4.进入“凭据”→“创建凭据”→“OAuth client ID” 5.选择“桌面应用”作为应用类型 6。下载凭据 JSON 文件 7.将其保存为凭据。
概述(AI 翻译)
一个简单的MCP服务器,提供工具、资源和提示,用于使用Google Sheets和Google Forms。
特色
工具
list_spreadsheets- 列出你的谷歌电子表格read_sheet- 从谷歌表格读取数据write_sheet- 将数据写入 Google 表格append_sheet- 将数据添加到Google表格create_spreadsheet- 创建一个新的电子表格create_form- 创建一个新的谷歌表单
资源
sheet://{spreadsheet_id}/{range_name}- 访问表数据作为资源
提示
analyze_sheet_data- 分析图纸数据的模板create_report_template- 创建报告电子表格的模板form_to_sheet- 表单到表格工作流程模板
布置
1.创建虚拟环境
2.安装依赖
3.获取Google API凭证
- 访问谷歌云控制台
- 创建新项目或选择现有项目
- 启用以下API:
- Google 表格 API
- Google 表单 API
- Google Drive API
- 进入“凭据”→“创建凭据”→“OAuth 客户端ID”
- 选择“桌面应用”作为应用类型
- 下载凭据JSON文件
- 在此目录中保存为
credentials.json
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
Google Sheets & Forms MCP Server
A simple MCP server that provides tools, resources, and prompts for working with Google Sheets and Google Forms.
Features
Tools
list_spreadsheets- List your Google Spreadsheetsread_sheet- Read data from a Google Sheetwrite_sheet- Write data to a Google Sheetappend_sheet- Append data to a Google Sheetcreate_spreadsheet- Create a new spreadsheetcreate_form- Create a new Google Form
Resources
sheet://{spreadsheet_id}/{range_name}- Access sheet data as a resource
Prompts
analyze_sheet_data- Template for analyzing sheet datacreate_report_template- Template for creating report spreadsheetsform_to_sheet- Template for form-to-sheet workflow
Setup
1. Create virtual environment
cd gsheets-mcp-server
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
2. Install dependencies
pip install -r requirements.txt
3. Get Google API Credentials
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable the following APIs:
- Google Sheets API
- Google Forms API
- Google Drive API
- Go to “Credentials” → “Create Credentials” → “OAuth client ID”
- Choose “Desktop app” as application type
- Download the credentials JSON file
- Save it as
credentials.jsonin this directory
4. Test the server with MCP Inspector
npx @modelcontextprotocol/inspector python gsheets_server.py
Claude Desktop Configuration
Add this to your Claude Desktop config file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"gsheets": {
"command": "python",
"args": [
"/FULL/PATH/TO/gsheets-mcp-server/gsheets_server.py"
],
"env": {
"GOOGLE_CREDENTIALS_PATH": "/FULL/PATH/TO/gsheets-mcp-server/credentials.json"
}
}
}
}
Important: Replace /FULL/PATH/TO/ with the actual absolute path to this directory.
First Run
On first run, the server will open a browser window for Google authentication. After you authorize the app, a token.pickle file will be created to store your credentials for future use.
Example Usage in Claude Desktop
Once configured, you can ask Claude:
- “List my spreadsheets”
- “Read the data from spreadsheet ID ‘abc123’ in range ‘Sheet1!A1:C10’”
- “Create a new spreadsheet called ‘Q1 Report’”
- “Append this data to my sheet: [[‘Name’, ‘Email’], [‘John’, ‘[email protected]’]]”
- “Use the analyze_sheet_data prompt”
- “Show me the sheet resource for spreadsheet ‘abc123’”
Getting Spreadsheet IDs
The spreadsheet ID is in the URL:
https://docs.google.com/spreadsheets/d/SPREADSHEET_ID_HERE/edit
Troubleshooting
- Authentication fails: Delete
token.pickleand run again
安装
npx @modelcontextprotocol/inspector python gsheets_server.py配置
{
"mcpServers": {
"gsheets": {
"command": "python",
"args": [
"/FULL/PATH/TO/gsheets-mcp-server/gsheets_server.py"
],
"env": {
"GOOGLE_CREDENTIALS_PATH": "/FULL/PATH/TO/gsheets-mcp-server/credentials.json"
}
}
}
}