Skip to main content
Glama
javerthl

ServiceNow MCP Server

by javerthl

add_file_to_changeset

Add a file to a ServiceNow changeset by providing the changeset ID, file path, and file content to manage change-related documentation and attachments.

Instructions

Add a file to a changeset in ServiceNow

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
changeset_idYesChangeset ID or sys_id
file_contentYesContent of the file
file_pathYesPath of the file to add

Implementation Reference

  • The handler function that executes the tool logic: validates parameters, constructs API request to ServiceNow's sys_update_xml table, and adds the file to the specified changeset.
    def add_file_to_changeset(
        auth_manager: AuthManager,
        server_config: ServerConfig,
        params: Union[Dict[str, Any], AddFileToChangesetParams],
    ) -> Dict[str, Any]:
        """
        Add a file to a changeset in ServiceNow.
    
        Args:
            auth_manager: The authentication manager.
            server_config: The server configuration.
            params: The parameters for adding a file to a changeset. Can be a dictionary or a AddFileToChangesetParams object.
    
        Returns:
            The result of the add file operation.
        """
        # Unwrap and validate parameters
        result = _unwrap_and_validate_params(
            params, 
            AddFileToChangesetParams, 
            required_fields=["changeset_id", "file_path", "file_content"]
        )
        
        if not result["success"]:
            return result
        
        validated_params = result["params"]
        
        # Get the instance URL
        instance_url = _get_instance_url(auth_manager, server_config)
        if not instance_url:
            return {
                "success": False,
                "message": "Cannot find instance_url in either server_config or auth_manager",
            }
        
        # Get the headers
        headers = _get_headers(auth_manager, server_config)
        if not headers:
            return {
                "success": False,
                "message": "Cannot find get_headers method in either auth_manager or server_config",
            }
        
        # Add Content-Type header
        headers["Content-Type"] = "application/json"
        
        # Prepare the request data for adding a file
        data = {
            "update_set": validated_params.changeset_id,
            "name": validated_params.file_path,
            "payload": validated_params.file_content,
            "type": "file",
        }
        
        # Make the API request
        url = f"{instance_url}/api/now/table/sys_update_xml"
        
        try:
            response = requests.post(url, json=data, headers=headers)
            response.raise_for_status()
            
            result = response.json()
            
            return {
                "success": True,
                "message": "File added to changeset successfully",
                "file": result["result"],
            }
        except requests.exceptions.RequestException as e:
            logger.error(f"Error adding file to changeset: {e}")
            return {
                "success": False,
                "message": f"Error adding file to changeset: {str(e)}",
            } 
  • Pydantic BaseModel defining the input schema for the tool parameters: changeset_id, file_path, and file_content.
    class AddFileToChangesetParams(BaseModel):
        """Parameters for adding a file to a changeset."""
    
        changeset_id: str = Field(..., description="Changeset ID or sys_id")
        file_path: str = Field(..., description="Path of the file to add")
        file_content: str = Field(..., description="Content of the file")
  • Registration of the tool in the central tool_definitions dictionary, linking the handler function, input schema, return type hint, description, and serialization method.
    "add_file_to_changeset": (
        add_file_to_changeset_tool,
        AddFileToChangesetParams,
        str,
        "Add a file to a changeset in ServiceNow",
        "str",  # Tool returns simple message
    ),
  • Import of the handler function aliased for use in tool registration.
        add_file_to_changeset as add_file_to_changeset_tool,
    )
  • Export/import of the tool handler in the tools package __init__.py for easy access.
    from servicenow_mcp.tools.changeset_tools import (
        add_file_to_changeset,
        commit_changeset,
        create_changeset,
        get_changeset_details,
        list_changesets,
        publish_changeset,
        update_changeset,
    )

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/javerthl/servicenow-mcp'

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