create_user
Create a new user in ServiceNow by defining username, name, email, roles, and other details. Streamlines user account setup within the ServiceNow platform.
Instructions
Create a new user in ServiceNow
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| active | No | Whether the user account is active | |
| department | No | Department the user belongs to | |
| Yes | Email address of the user | ||
| first_name | Yes | First name of the user | |
| last_name | Yes | Last name of the user | |
| location | No | Location of the user | |
| manager | No | Manager of the user (sys_id or username) | |
| mobile_phone | No | Mobile phone number of the user | |
| password | No | Password for the user account | |
| phone | No | Phone number of the user | |
| roles | No | Roles to assign to the user | |
| title | No | Job title of the user | |
| user_name | Yes | Username for the user |
Implementation Reference
- The main handler function that implements the create_user tool logic by making a POST request to ServiceNow's sys_user table and handling role assignments.def create_user( config: ServerConfig, auth_manager: AuthManager, params: CreateUserParams, ) -> UserResponse: """ Create a new user in ServiceNow. Args: config: Server configuration. auth_manager: Authentication manager. params: Parameters for creating the user. Returns: Response with the created user details. """ api_url = f"{config.api_url}/table/sys_user" # Build request data data = { "user_name": params.user_name, "first_name": params.first_name, "last_name": params.last_name, "email": params.email, "active": str(params.active).lower(), } if params.title: data["title"] = params.title if params.department: data["department"] = params.department if params.manager: data["manager"] = params.manager if params.phone: data["phone"] = params.phone if params.mobile_phone: data["mobile_phone"] = params.mobile_phone if params.location: data["location"] = params.location if params.password: data["user_password"] = params.password # Make request try: response = requests.post( api_url, json=data, headers=auth_manager.get_headers(), timeout=config.timeout, ) response.raise_for_status() result = response.json().get("result", {}) # Handle role assignments if provided if params.roles and result.get("sys_id"): assign_roles_to_user(config, auth_manager, result.get("sys_id"), params.roles) return UserResponse( success=True, message="User created successfully", user_id=result.get("sys_id"), user_name=result.get("user_name"), ) except requests.RequestException as e: logger.error(f"Failed to create user: {e}") return UserResponse( success=False, message=f"Failed to create user: {str(e)}", )
- Pydantic model defining the input parameters and validation schema for the create_user tool.class CreateUserParams(BaseModel): """Parameters for creating a user.""" user_name: str = Field(..., description="Username for the user") first_name: str = Field(..., description="First name of the user") last_name: str = Field(..., description="Last name of the user") email: str = Field(..., description="Email address of the user") title: Optional[str] = Field(None, description="Job title of the user") department: Optional[str] = Field(None, description="Department the user belongs to") manager: Optional[str] = Field(None, description="Manager of the user (sys_id or username)") roles: Optional[List[str]] = Field(None, description="Roles to assign to the user") phone: Optional[str] = Field(None, description="Phone number of the user") mobile_phone: Optional[str] = Field(None, description="Mobile phone number of the user") location: Optional[str] = Field(None, description="Location of the user") password: Optional[str] = Field(None, description="Password for the user account") active: Optional[bool] = Field(True, description="Whether the user account is active")
- src/servicenow_mcp/utils/tool_utils.py:727-733 (registration)Registration of the create_user tool in the central tool_definitions dictionary, associating the handler function alias, schema, description, and serialization method."create_user": ( create_user_tool, CreateUserParams, Dict[str, Any], # Expects dict "Create a new user in ServiceNow", "raw_dict", # Tool returns raw dict ),
- src/servicenow_mcp/utils/tool_utils.py:220-220 (registration)Import of the create_user function aliased as create_user_tool for use in tool registration.create_user as create_user_tool,
- Helper function called by create_user to assign roles to the newly created user.def assign_roles_to_user( config: ServerConfig, auth_manager: AuthManager, user_id: str, roles: List[str], ) -> bool: """ Assign roles to a user in ServiceNow. Args: config: Server configuration. auth_manager: Authentication manager. user_id: User ID or sys_id. roles: List of roles to assign. Returns: Boolean indicating success. """ # For each role, create a user_role record api_url = f"{config.api_url}/table/sys_user_has_role" success = True for role in roles: # First check if the role exists role_id = get_role_id(config, auth_manager, role) if not role_id: logger.warning(f"Role '{role}' not found, skipping assignment") continue # Check if the user already has this role if check_user_has_role(config, auth_manager, user_id, role_id): logger.info(f"User already has role '{role}', skipping assignment") continue # Create the user role assignment data = { "user": user_id, "role": role_id, } try: response = requests.post( api_url, json=data, headers=auth_manager.get_headers(), timeout=config.timeout, ) response.raise_for_status() except requests.RequestException as e: logger.error(f"Failed to assign role '{role}' to user: {e}") success = False return success