Skip to main content
Glama
HosakaKeigo

Spreadsheet MCP Server

by HosakaKeigo

getSpreadsheet

Retrieve spreadsheet metadata and sheet listings from Google Sheets using a URL to access document structure and contents.

Instructions

スプレッドシートの基本情報と含まれるシート一覧を取得

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYesスプレッドシートのURL

Implementation Reference

  • Complete definition of the getSpreadsheet tool, including schema, description, and the handler function that extracts the spreadsheet ID from the URL, fetches the spreadsheet information using helper functions, formats the result, and returns it as structured content or error.
    export const getSpreadsheetTool = { name: "getSpreadsheet", description: "スプレッドシートの基本情報と含まれるシート一覧を取得", schema: { url: z.string().describe("スプレッドシートのURL") }, 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/server.ts:27-32 (registration)
    Registers the getSpreadsheet tool with the MCP server using the server.tool method.
    server.tool( getSpreadsheetTool.name, getSpreadsheetTool.description, getSpreadsheetTool.schema, getSpreadsheetTool.handler );
  • Helper function that retrieves spreadsheet information by calling the Google Apps Script web app API (or mock data in mock mode). 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; }
  • Helper function to extract the spreadsheet ID from a Google Sheets URL using regex. Used in the tool handler.
    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; } }

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/HosakaKeigo/spreadsheet-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server