getSpreadsheet
Retrieve basic information and list of sheets from a Google Spreadsheet using the provided URL via the Spreadsheet MCP Server.
Instructions
スプレッドシートの基本情報と含まれるシート一覧を取得
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | スプレッドシートのURL |
Implementation Reference
- src/tools/getSpreadsheet.ts:19-60 (handler)The main handler function for the 'getSpreadsheet' tool. It extracts the spreadsheet ID from the provided URL, fetches the spreadsheet information using getSpreadsheetInfo, formats the result, and returns it as content. Handles errors appropriately.handler: async ({ url }: { url: string }) => { try { const spreadsheetId = extractSpreadsheetId(url); if (!spreadsheetId) { return { content: [ { type: "text" as const, text: "有効なスプレッドシートURLではありません。Google SpreadsheetのURLを入力してください。" } ], isError: true }; } const spreadsheetInfo = await getSpreadsheetInfo(spreadsheetId); // フォーマットして返す const formattedResult = formatSpreadsheetInfo(spreadsheetInfo); return { content: [ { type: "text" as const, text: formattedResult } ] }; } catch (error) { console.error("スプレッドシート情報取得エラー:", error); return { content: [ { type: "text" as const, text: `スプレッドシート情報の取得中にエラーが発生しました: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
- src/tools/getSpreadsheet.ts:16-18 (schema)Input schema for the tool using Zod validation. Requires a 'url' parameter which is a string describing the spreadsheet URL.schema: { url: z.string().describe("スプレッドシートのURL") },
- src/server.ts:27-32 (registration)Registration of the getSpreadsheetTool with the MCP server inside the registerTools() function.server.tool( getSpreadsheetTool.name, getSpreadsheetTool.description, getSpreadsheetTool.schema, getSpreadsheetTool.handler );
- src/api/spreadsheet.ts:47-70 (helper)Helper function getSpreadsheetInfo that fetches the actual spreadsheet data from Google Apps Script Web App API or mock data. Called by the tool handler.export async function getSpreadsheetInfo(spreadsheetId: string): Promise<SpreadsheetInfo> { // モックモードの場合はモックデータを返す if (config.isMockMode) { return getMockSpreadsheetInfo(spreadsheetId); } // API呼び出し用のURLを生成 const apiUrl = new URL(config.gas.webAppUrl); apiUrl.searchParams.append('action', 'getSpreadsheet'); apiUrl.searchParams.append('id', spreadsheetId); apiUrl.searchParams.append('apiKey', config.gas.apiKey); // APIリクエスト const response = await fetch(apiUrl); // ステータスコードのチェック if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || `API Error: ${response.status}`); } // レスポンスをJSONとしてパース return await response.json() as SpreadsheetInfo; }
- src/api/spreadsheet.ts:14-39 (helper)Helper function to extract spreadsheet ID from the input URL using regex, supports mock URLs.export function extractSpreadsheetId(url: string): string | null { try { // URL自体がnullまたはundefinedの場合 if (!url) { return null; } // 実際のGoogle SpreadsheetのURLからIDを抽出 // 例: https://docs.google.com/spreadsheets/d/1a2b3c4d5e6f7g8h9i0j/edit#gid=0 const regex = /\/d\/([a-zA-Z0-9_-]+)/; const match = url.match(regex); if (match && match[1]) { return match[1]; } // モック用に特殊なURLも処理 if (config.isMockMode && (url.includes("budget") || url.includes("project"))) { return url.includes("budget") ? "mock_budget" : "mock_project"; } return null; } catch (error) { console.error("URL解析エラー:", error); return null; }