gitlab_create_snippet
Create and manage code snippets on GitLab for saving reusable code, sharing solutions, and documenting examples. Specify title, file name, and content, with optional description and visibility settings.
Instructions
Create a new code snippet Creates: New snippet with specified content and metadata Use when: Saving reusable code, sharing solutions, documenting examples Required: title, file_name, content Optional: description, visibility
Example usage: { "title": "Docker Compose Template", "file_name": "docker-compose.yml", "content": "version: '3.8'\nservices:\n app:\n image: nginx", "description": "Basic Docker Compose setup", "visibility": "internal" }
Returns: Created snippet with ID and URLs
Related tools:
gitlab_update_snippet: Modify after creation
gitlab_list_snippets: View created snippets
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Snippet content Type: string Format: Raw text content of the snippet Example: 'console.log("Hello World");' Note: Can be code, text, or any content type | |
| description | No | Snippet description Type: string Format: Optional description of the snippet Example: 'Helper script for database migrations' Note: Provides context about the snippet's purpose | |
| file_name | Yes | Snippet file name Type: string Format: File name with extension Example: 'migration.sql', 'helper.py', 'config.yaml' Note: Used for syntax highlighting and display | |
| project_id | No | Project identifier (auto-detected if not provided) Type: integer OR string Format: numeric ID or 'namespace/project' Optional: Yes - auto-detects from current git repository Examples: - 12345 (numeric ID) - 'gitlab-org/gitlab' (namespace/project path) - 'my-group/my-subgroup/my-project' (nested groups) Note: If in a git repo with GitLab remote, this can be omitted | |
| title | Yes | Snippet title Type: string Format: Descriptive title for the snippet Example: 'Database migration script' Note: Required when creating snippets | |
| visibility | No | Snippet visibility Type: string Format: Visibility level for the snippet Options: 'private' | 'internal' | 'public' Default: 'private' Examples: - 'private' (only visible to author) - 'internal' (visible to authenticated users) - 'public' (visible to everyone) | private |
Implementation Reference
- src/mcp_gitlab/tool_handlers.py:550-567 (handler)The handler function that implements the core logic for the gitlab_create_snippet tool. Extracts required arguments like title, file_name, content, and calls the GitLabClient's create_snippet method.def handle_create_snippet(client: GitLabClient, arguments: Optional[Dict[str, Any]]) -> Dict[str, Any]: """Handle creating a snippet""" project_id = require_project_id(client, arguments) title = require_argument(arguments, "title") file_name = require_argument(arguments, "file_name") content = require_argument(arguments, "content") description = get_argument(arguments, "description") visibility = get_argument(arguments, "visibility", "private") return client.create_snippet( project_id=project_id, title=title, file_name=file_name, content=content, description=description, visibility=visibility )
- Pydantic/MCP schema definition for the gitlab_create_snippet tool, specifying input parameters, types, descriptions, and required fields.types.Tool( name=TOOL_CREATE_SNIPPET, description=desc.DESC_CREATE_SNIPPET, inputSchema={ "type": "object", "properties": { "project_id": {"type": "string", "description": desc.DESC_PROJECT_ID}, "title": {"type": "string", "description": desc.DESC_SNIPPET_TITLE}, "file_name": {"type": "string", "description": desc.DESC_SNIPPET_FILE_NAME}, "content": {"type": "string", "description": desc.DESC_SNIPPET_CONTENT}, "description": {"type": "string", "description": desc.DESC_SNIPPET_DESCRIPTION}, "visibility": {"type": "string", "description": desc.DESC_SNIPPET_VISIBILITY, "enum": ["private", "internal", "public"], "default": "private"} }, "required": ["title", "file_name", "content"] } ),
- src/mcp_gitlab/tool_handlers.py:1041-1041 (registration)Maps the tool name TOOL_CREATE_SNIPPET to its handler function in the TOOL_HANDLERS dictionary, used by the MCP server to route calls.TOOL_CREATE_SNIPPET: handle_create_snippet,
- src/mcp_gitlab/constants.py:207-207 (helper)Constant definition for the tool name 'gitlab_create_snippet', used across schema, handlers, and registration.TOOL_CREATE_SNIPPET = "gitlab_create_snippet"