Skip to main content
Glama
mohalmah

Google Apps Script MCP Server

by mohalmah

update_script_content

Modify Google Apps Script project files by updating their source code and content directly through the MCP server integration.

Instructions

Updates the content of a specified Google Apps Script project.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptIdYesThe ID of the script project to update.
filesYesThe files to be updated in the script project.

Implementation Reference

  • The core handler function `executeFunction` that performs the HTTP PUT request to the Google Apps Script API to update project content. Handles OAuth token retrieval, request construction, API call, error handling, and logging.
    const executeFunction = async ({ scriptId, files }) => {
      const baseUrl = 'https://script.googleapis.com';
      const startTime = Date.now();
    
      try {
        logger.info('SCRIPT_UPDATE_CONTENT', 'Starting script content update', { 
          scriptId, 
          filesCount: files?.length || 0 
        });
    
        // Get OAuth access token
        const token = await getOAuthAccessToken();
        
        // Construct the URL for the request
        const url = `${baseUrl}/v1/projects/${scriptId}/content`;
    
        // Set up headers for the request
        const headers = {
          'Authorization': `Bearer ${token}`,
          'Content-Type': 'application/json',
          'Accept': 'application/json'
        };
    
        // Prepare the body of the request
        const requestBody = { scriptId, files };
        const body = JSON.stringify(requestBody);
    
        logger.logAPICall('PUT', url, headers, requestBody);
    
        // Perform the fetch request
        const fetchStartTime = Date.now();
        const response = await fetch(url, {
          method: 'PUT',
          headers,
          body
        });
        
        const fetchDuration = Date.now() - fetchStartTime;
        const responseSize = response.headers.get('content-length') || 'unknown';
        
        logger.logAPIResponse('PUT', url, response.status, fetchDuration, responseSize);
    
        // Check if the response was successful
        if (!response.ok) {
          const errorText = await response.text();
          let errorData;
          
          try {
            errorData = JSON.parse(errorText);
          } catch (parseError) {
            errorData = { message: errorText };
          }
    
          const detailedError = {
            status: response.status,
            statusText: response.statusText,
            url,
            errorResponse: errorData,
            duration: Date.now() - startTime,
            scriptId,
            filesCount: files?.length || 0,
            timestamp: new Date().toISOString()
          };
    
          logger.error('SCRIPT_UPDATE_CONTENT', 'API request failed', detailedError);
          
          console.error('❌ API Error Details:', JSON.stringify(detailedError, null, 2));
          
          throw new Error(`API Error (${response.status}): ${errorData.error?.message || errorData.message || 'Unknown error'}`);
        }
    
        // Parse and return the response data
        const data = await response.json();
        
        logger.info('SCRIPT_UPDATE_CONTENT', 'Successfully updated script content', {
          scriptId,
          filesCount: files?.length || 0,
          duration: Date.now() - startTime
        });
        
        console.log('✅ Successfully updated script content');
        return data;
      } catch (error) {
        const errorDetails = {
          message: error.message,
          stack: error.stack,
          scriptId,
          filesCount: files?.length || 0,
          duration: Date.now() - startTime,
          timestamp: new Date().toISOString(),
          errorType: error.name || 'Unknown'
        };
    
        logger.error('SCRIPT_UPDATE_CONTENT', 'Error updating script project content', errorDetails);
        
        console.error('❌ Error updating script project content:', errorDetails);
        
        // Return detailed error information for debugging
        return { 
          error: true,
          message: error.message,
          details: errorDetails,
          rawError: {
            name: error.name,
            stack: error.stack
          }
        };
      }
    };
  • Input schema (JSON Schema) defining required parameters: `scriptId` (string) and `files` (array of file objects with properties like name, type, source).
    parameters: {
      type: 'object',
      properties: {
        scriptId: {
          type: 'string',
          description: 'The ID of the script project to update.'
        },
        files: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              name: {
                type: 'string',
                description: 'The name of the file.'
              },
              lastModifyUser: {
                type: 'object',
                properties: {
                  photoUrl: { type: 'string' },
                  domain: { type: 'string' },
                  name: { type: 'string' },
                  email: { type: 'string' }
                }
              },
              type: {
                type: 'string',
                description: 'The type of the file.'
              },
              updateTime: { type: 'string' },
              source: { type: 'string' },
              createTime: { type: 'string' },
              functionSet: {
                type: 'object',
                properties: {
                  values: {
                    type: 'array',
                    items: {
                      type: 'object',
                      properties: {
                        parameters: {
                          type: 'array',
                          items: {
                            type: 'object',
                            properties: {
                              value: { type: 'string' }
                            }
                          }
                        },
                        name: { type: 'string' }
                      }
                    }
                  }
                }
              }
            }
          },
          description: 'The files to be updated in the script project.'
        }
      },
      required: ['scriptId', 'files']
    }
  • Local tool registration as `apiTool` export, containing the handler function reference and full tool definition including name 'update_script_content' and schema.
    const apiTool = {
      function: executeFunction,
      definition: {
        type: 'function',
        function: {
          name: 'update_script_content',
          description: 'Updates the content of a specified Google Apps Script project.',
          parameters: {
            type: 'object',
            properties: {
              scriptId: {
                type: 'string',
                description: 'The ID of the script project to update.'
              },
              files: {
                type: 'array',
                items: {
                  type: 'object',
                  properties: {
                    name: {
                      type: 'string',
                      description: 'The name of the file.'
                    },
                    lastModifyUser: {
                      type: 'object',
                      properties: {
                        photoUrl: { type: 'string' },
                        domain: { type: 'string' },
                        name: { type: 'string' },
                        email: { type: 'string' }
                      }
                    },
                    type: {
                      type: 'string',
                      description: 'The type of the file.'
                    },
                    updateTime: { type: 'string' },
                    source: { type: 'string' },
                    createTime: { type: 'string' },
                    functionSet: {
                      type: 'object',
                      properties: {
                        values: {
                          type: 'array',
                          items: {
                            type: 'object',
                            properties: {
                              parameters: {
                                type: 'array',
                                items: {
                                  type: 'object',
                                  properties: {
                                    value: { type: 'string' }
                                  }
                                }
                              },
                              name: { type: 'string' }
                            }
                          }
                        }
                      }
                    }
                  }
                },
                description: 'The files to be updated in the script project.'
              }
            },
            required: ['scriptId', 'files']
          }
        }
      }
    };
  • lib/tools.js:8-64 (registration)
    Central dynamic registration/discovery of all tools: imports modules from toolPaths, validates apiTool export, wraps handler with logging, and returns array of registered tools.
    export async function discoverTools() {
      logger.info('DISCOVERY', `Starting tool discovery for ${toolPaths.length} tool paths`);
      
      const toolPromises = toolPaths.map(async (file) => {
        try {
          logger.debug('DISCOVERY', `Loading tool from: ${file}`);
          const module = await import(`../tools/${file}`);
          
          if (!module.apiTool) {
            logger.warn('DISCOVERY', `Tool file missing apiTool export: ${file}`);
            return null;
          }
    
          const toolName = module.apiTool.definition?.function?.name;
          if (!toolName) {
            logger.warn('DISCOVERY', `Tool missing function name: ${file}`);
            return null;
          }
    
          // Wrap the original function with logging
          const originalFunction = module.apiTool.function;
          const wrappedFunction = withLogging(toolName, originalFunction);
    
          logger.debug('DISCOVERY', `Successfully loaded tool: ${toolName}`, {
            file,
            toolName,
            description: module.apiTool.definition?.function?.description
          });
    
          return {
            ...module.apiTool,
            function: wrappedFunction,
            path: file,
          };
        } catch (error) {
          logger.error('DISCOVERY', `Failed to load tool: ${file}`, {
            file,
            error: {
              message: error.message,
              stack: error.stack
            }
          });
          return null;
        }
      });
      
      const tools = (await Promise.all(toolPromises)).filter(Boolean);
      
      logger.info('DISCOVERY', `Tool discovery completed`, {
        totalPaths: toolPaths.length,
        successfullyLoaded: tools.length,
        failed: toolPaths.length - tools.length,
        toolNames: tools.map(t => t.definition?.function?.name).filter(Boolean)
      });
      
      return tools;
    }
  • tools/paths.js:1-18 (registration)
    Configuration file listing all tool file paths for discovery, including the path to the update_script_content tool implementation.
    export const toolPaths = [
      'google-app-script-api/apps-script-api/script-projects-deployments-delete.js',
      'google-app-script-api/apps-script-api/script-projects-create.js',
      'google-app-script-api/apps-script-api/script-projects-versions-create.js',
      'google-app-script-api/apps-script-api/script-projects-deployments-create.js',
      'google-app-script-api/apps-script-api/script-projects-deployments-update.js',
      'google-app-script-api/apps-script-api/script-projects-deployments-list.js',
      'google-app-script-api/apps-script-api/script-projects-update-content.js',
      'google-app-script-api/apps-script-api/script-projects-deployments-get.js',
      'google-app-script-api/apps-script-api/script-scripts-run.js',
      'google-app-script-api/apps-script-api/script-projects-get.js',
      'google-app-script-api/apps-script-api/script-processes-list-script-processes.js',
      'google-app-script-api/apps-script-api/script-projects-get-metrics.js',
      'google-app-script-api/apps-script-api/script-projects-get-content.js',
      'google-app-script-api/apps-script-api/script-projects-versions-list.js',
      'google-app-script-api/apps-script-api/script-projects-versions-get.js',
      'google-app-script-api/apps-script-api/script-processes-list.js'
    ];
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. It states this is an update operation but doesn't disclose critical behavioral traits: whether this overwrites existing content, requires specific permissions, has rate limits, returns confirmation or error details, or affects running scripts. For a mutation tool with zero annotation coverage, this leaves significant gaps in understanding its behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's function without unnecessary words. It's appropriately sized and front-loaded, with every word contributing to understanding the core purpose.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given this is a mutation tool with no annotations and no output schema, the description is incomplete. It doesn't address what happens during the update (e.g., overwrite behavior, error handling), what permissions are needed, or what the return value looks like. With rich sibling tools and complex parameters (files array with nested objects), more context is needed for effective use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with both parameters (scriptId, files) well-documented in the schema. The description adds no additional parameter semantics beyond implying content update, which is already clear from the tool name and schema. This meets the baseline of 3 when schema does the heavy lifting, but doesn't compensate with extra context like file format expectations or update constraints.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Updates') and resource ('content of a specified Google Apps Script project'), making the purpose immediately understandable. However, it doesn't differentiate this tool from similar siblings like 'script_projects_deployments_update' or 'script_projects_versions_create', which also involve updates to script-related entities.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With multiple sibling tools involving script updates (e.g., deployments_update, versions_create), there's no indication of whether this is for modifying source code versus other aspects, or any prerequisites like needing the script ID first from get_content tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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/mohalmah/google-appscript-mcp-server'

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