Prerequisites
Gemini CLI
Ensure you have the Gemini CLI installed
Node.js
Basic understanding of Node.js and npm
Step 1: Create a New Extension
The easiest way to start is by using a built-in template. We’ll use themcp-server example as our foundation.
Step 2: Understand the Extension Files
gemini-extension.json
The manifest file tells Gemini CLI how to load and use your extension.
Unique name for your extension
Version of your extension (semver format)
MCP server configurations. The
${extensionPath} variable is replaced with the absolute path to your extension directory.example.js
This file contains the MCP server implementation using the @modelcontextprotocol/sdk.
package.json
Standard Node.js configuration file that defines dependencies and scripts.
The template includes
@modelcontextprotocol/sdk and zod as dependencies for building MCP servers.Step 3: Add Extension Settings
Some extensions need configuration like API keys or user preferences. Let’s add a setting for an API key.Display name shown to the user
Description of what this setting is for
Environment variable name that will be set
When
true, the value is stored securely in the system keychain- Prompt them to enter the “API Key”
- Store it securely in the system keychain (because
sensitiveis true) - Inject it into the MCP server’s process as the
MY_SERVICE_API_KEYenvironment variable
Step 4: Link Your Extension
Link your extension to your Gemini CLI installation for local development.Link the extension
The Changes you make are reflected immediately.
link command creates a symbolic link from the Gemini CLI extensions directory to your development directory.Step 5: Add a Custom Command
Custom commands create shortcuts for complex prompts.Create directory structure
Create a Windows (PowerShell):
commands directory and subdirectory:macOS/Linux:Create command file
Create This command:
commands/fs/grep-code.toml:- Takes an argument (
{{args}}) - Runs the
grepshell command - Pipes results into a prompt for summarization
Custom Commands Reference
Learn more about custom command syntax and features
Step 6: Add Custom Context
Provide persistent context to the model by adding aGEMINI.md file.
Context files are loaded at the start of every session, so keep them concise to avoid consuming too many tokens.
Step 7: Add an Agent Skill (Optional)
Agent Skills bundle specialized expertise and workflows. Skills are activated only when needed, saving context tokens.Agent Skills Guide
Learn more about the agent skills system
Extension Structure Summary
Here’s what your complete extension looks like:Step 8: Release Your Extension
When your extension is ready, share it with others via a Git repository or GitHub Releases.Extension Releasing Guide
Learn how to publish and list your extension in the gallery
Testing Your Extension
Test MCP Tools
Test MCP Tools
Ask the model to use your tools:Verify the tool executes correctly and returns expected results.
Test Custom Commands
Test Custom Commands
Run your custom commands:Verify the command executes and the prompt is formatted correctly.
Test Context
Test Context
Verify the model has your context:The model should reference content from your
GEMINI.md file.Test Skills
Test Skills
Trigger your skill:Verify the skill is activated and provides specialized responses.
Best Practices
Keep Context Concise
Context files consume tokens in every session. Be brief and focused.
Use Skills for Complex Tasks
Move complex instructions to skills to save context when not needed.
Validate Inputs
Always validate tool inputs to prevent errors and security issues.
Handle Errors Gracefully
Return clear error messages when tools fail.
Document Your Extension
Include a README explaining what your extension does and how to use it.
Version Carefully
Follow semantic versioning for your extension releases.
Next Steps
Extension Reference
Detailed API reference and configuration options
Best Practices
Learn strategies for building great extensions
Publishing Guide
Share your extension with the community
MCP Server Guide
Deep dive into MCP server integration