diff --git a/docs/getting-started-extensions.md b/docs/getting-started-extensions.md new file mode 100644 index 0000000000..d84b116131 --- /dev/null +++ b/docs/getting-started-extensions.md @@ -0,0 +1,213 @@ +# Getting Started with Gemini CLI Extensions + +This guide will walk you through creating your first Gemini CLI extension. You'll learn how to set up a new extension, add a custom tool via an MCP server, create a custom command, and provide context to the model with a `GEMINI.md` file. + +## Prerequisites + +Before you start, make sure you have the Gemini CLI installed and a basic understanding of Node.js and TypeScript. + +## Step 1: Create a New Extension + +The easiest way to start is by using one of the built-in templates. We'll use the `mcp-server` example as our foundation. + +Run the following command to create a new directory called `my-first-extension` with the template files: + +```bash +gemini extensions new my-first-extension mcp-server +``` + +This will create a new directory with the following structure: + +``` +my-first-extension/ +├── example.ts +├── gemini-extension.json +├── package.json +└── tsconfig.json +``` + +## Step 2: Understand the Extension Files + +Let's look at the key files in your new extension. + +### `gemini-extension.json` + +This is the manifest file for your extension. It tells Gemini CLI how to load and use your extension. + +```json +{ + "name": "my-first-extension", + "version": "1.0.0", + "mcpServers": { + "nodeServer": { + "command": "node", + "args": ["${extensionPath}${/}dist${/}example.js"], + "cwd": "${extensionPath}" + } + } +} +``` + +- `name`: The unique name for your extension. +- `version`: The version of your extension. +- `mcpServers`: This section defines one or more Model Context Protocol (MCP) servers. MCP servers are how you can add new tools for the model to use. + - `command`, `args`, `cwd`: These fields specify how to start your server. Notice the use of the `${extensionPath}` variable, which Gemini CLI replaces with the absolute path to your extension's installation directory. This allows your extension to work regardless of where it's installed. + +### `example.ts` + +This file contains the source code for your MCP server. It's a simple Node.js server that uses the `@modelcontextprotocol/sdk`. + +```typescript +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; +import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; +import { z } from 'zod'; + +const server = new McpServer({ + name: 'prompt-server', + version: '1.0.0', +}); + +// Registers a new tool named 'fetch_posts' +server.registerTool( + 'fetch_posts', + { + description: 'Fetches a list of posts from a public API.', + inputSchema: z.object({}).shape, + }, + async () => { + const apiResponse = await fetch( + 'https://jsonplaceholder.typicode.com/posts', + ); + const posts = await apiResponse.json(); + const response = { posts: posts.slice(0, 5) }; + return { + content: [ + { + type: 'text', + text: JSON.stringify(response), + }, + ], + }; + }, +); + +// ... (prompt registration omitted for brevity) + +const transport = new StdioServerTransport(); +await server.connect(transport); +``` + +This server defines a single tool called `fetch_posts` that fetches data from a public API. + +### `package.json` and `tsconfig.json` + +These are standard configuration files for a TypeScript project. The `package.json` file defines dependencies and a `build` script, and `tsconfig.json` configures the TypeScript compiler. + +## Step 3: Build and Link Your Extension + +Before you can use the extension, you need to compile the TypeScript code and link the extension to your Gemini CLI installation for local development. + +1. **Install dependencies:** + + ```bash + cd my-first-extension + npm install + ``` + +2. **Build the server:** + + ```bash + npm run build + ``` + + This will compile `example.ts` into `dist/example.js`, which is the file referenced in your `gemini-extension.json`. + +3. **Link the extension:** + + The `link` command creates a symbolic link from the Gemini CLI extensions directory to your development directory. This means any changes you make will be reflected immediately without needing to reinstall. + + ```bash + gemini extensions link . + ``` + +Now, restart your Gemini CLI session. The new `fetch_posts` tool will be available. You can test it by asking: "fetch posts". + +## Step 4: Add a Custom Command + +Custom commands provide a way to create shortcuts for complex prompts. Let's add a command that searches for a pattern in your code. + +1. Create a `commands` directory and a subdirectory for your command group: + + ```bash + mkdir -p commands/fs + ``` + +2. Create a file named `commands/fs/grep-code.toml`: + + ```toml + prompt = """ + Please summarize the findings for the pattern `{{args}}`. + + Search Results: + !{grep -r {{args}} .} + """ + ``` + + This command, `/fs:grep-code`, will take an argument, run the `grep` shell command with it, and pipe the results into a prompt for summarization. + +After saving the file, restart the Gemini CLI. You can now run `/fs:grep-code "some pattern"` to use your new command. + +## Step 5: Add a Custom `GEMINI.md` + +You can provide persistent context to the model by adding a `GEMINI.md` file to your extension. This is useful for giving the model instructions on how to behave or information about your extension's tools. Note that you may not always need this for extensions built to expose commands and prompts. + +1. Create a file named `GEMINI.md` in the root of your extension directory: + + ```markdown + # My First Extension Instructions + + You are an expert developer assistant. When the user asks you to fetch posts, use the `fetch_posts` tool. Be concise in your responses. + ``` + +2. Update your `gemini-extension.json` to tell the CLI to load this file: + + ```json + { + "name": "my-first-extension", + "version": "1.0.0", + "contextFileName": "GEMINI.md", + "mcpServers": { + "nodeServer": { + "command": "node", + "args": ["${extensionPath}${/}dist${/}example.js"], + "cwd": "${extensionPath}" + } + } + } + ``` + +Restart the CLI again. The model will now have the context from your `GEMINI.md` file in every session where the extension is active. + +## Step 6: Releasing Your Extension + +Once you are happy with your extension, you can share it with others. The two primary ways of releasing extensions are via a Git repository or through GitHub Releases. Using a public Git repository is the simplest method. + +For detailed instructions on both methods, please refer to the [Extension Releasing Guide](extension-releasing.md). + +## Conclusion + +You've successfully created a Gemini CLI extension! You learned how to: + +- Bootstrap a new extension from a template. +- Add custom tools with an MCP server. +- Create convenient custom commands. +- Provide persistent context to the model. +- Link your extension for local development. + +From here, you can explore more advanced features and build powerful new capabilities into the Gemini CLI. diff --git a/hello/GEMINI.md b/hello/GEMINI.md new file mode 100644 index 0000000000..22f6bbce5e --- /dev/null +++ b/hello/GEMINI.md @@ -0,0 +1,8 @@ +# Ink Library Screen Reader Guidance + +When building custom components, it's important to keep accessibility in mind. While Ink provides the building blocks, ensuring your components are accessible will make your CLIs usable by a wider audience. + +## General Principles + +Provide screen reader-friendly output: Use the useIsScreenReaderEnabled hook to detect if a screen reader is active. You can then render a more descriptive output for screen reader users. +Leverage ARIA props: For components that have a specific role (e.g., a checkbox or a button), use the aria-role, aria-state, and aria-label props on and to provide semantic meaning to screen readers. diff --git a/hello/gemini-extension.json b/hello/gemini-extension.json new file mode 100644 index 0000000000..c3fee9836e --- /dev/null +++ b/hello/gemini-extension.json @@ -0,0 +1,5 @@ +{ + "name": "context-example", + "version": "1.0.0", + "contextFileName": "GEMINI.md" +} diff --git a/packages/cli/src/commands/extensions/examples/mcp-server/gemini-extension.json b/packages/cli/src/commands/extensions/examples/mcp-server/gemini-extension.json index 27c5e36c24..62561dbf8d 100644 --- a/packages/cli/src/commands/extensions/examples/mcp-server/gemini-extension.json +++ b/packages/cli/src/commands/extensions/examples/mcp-server/gemini-extension.json @@ -1,10 +1,11 @@ { - "name": "mcp-server", + "name": "mcp-server-example", "version": "1.0.0", "mcpServers": { "nodeServer": { "command": "node", - "args": ["${extensionPath}${/}example.ts"] + "args": ["${extensionPath}${/}dist${/}example.js"], + "cwd": "${extensionPath}" } } } diff --git a/packages/cli/src/commands/extensions/examples/mcp-server/package.json b/packages/cli/src/commands/extensions/examples/mcp-server/package.json new file mode 100644 index 0000000000..d38f7ee99d --- /dev/null +++ b/packages/cli/src/commands/extensions/examples/mcp-server/package.json @@ -0,0 +1,18 @@ +{ + "name": "mcp-server-example", + "version": "1.0.0", + "description": "Example MCP Server for Gemini CLI Extension", + "type": "module", + "main": "example.js", + "scripts": { + "build": "tsc" + }, + "devDependencies": { + "typescript": "~5.4.5", + "@types/node": "^20.11.25" + }, + "dependencies": { + "@modelcontextprotocol/sdk": "^1.11.0", + "zod": "^3.22.4" + } +} diff --git a/packages/cli/src/commands/extensions/examples/mcp-server/tsconfig.json b/packages/cli/src/commands/extensions/examples/mcp-server/tsconfig.json new file mode 100644 index 0000000000..b94585edce --- /dev/null +++ b/packages/cli/src/commands/extensions/examples/mcp-server/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "outDir": "./dist" + }, + "include": ["example.ts"] +} diff --git a/packages/cli/src/commands/extensions/new.test.ts b/packages/cli/src/commands/extensions/new.test.ts index c6388244af..0b660d55e0 100644 --- a/packages/cli/src/commands/extensions/new.test.ts +++ b/packages/cli/src/commands/extensions/new.test.ts @@ -53,8 +53,18 @@ describe('extensions new command', () => { recursive: true, }); expect(mockedFs.cp).toHaveBeenCalledWith( - expect.stringContaining('context'), - '/some/path', + expect.stringContaining('context/context'), + '/some/path/context', + { recursive: true }, + ); + expect(mockedFs.cp).toHaveBeenCalledWith( + expect.stringContaining('context/custom-commands'), + '/some/path/custom-commands', + { recursive: true }, + ); + expect(mockedFs.cp).toHaveBeenCalledWith( + expect.stringContaining('context/mcp-server'), + '/some/path/mcp-server', { recursive: true }, ); }); diff --git a/packages/cli/src/commands/extensions/new.ts b/packages/cli/src/commands/extensions/new.ts index c502e8813f..0403c122fb 100644 --- a/packages/cli/src/commands/extensions/new.ts +++ b/packages/cli/src/commands/extensions/new.ts @@ -36,7 +36,12 @@ async function copyDirectory(template: string, path: string) { const examplePath = join(EXAMPLES_PATH, template); await mkdir(path, { recursive: true }); - await cp(examplePath, path, { recursive: true }); + const entries = await readdir(examplePath, { withFileTypes: true }); + for (const entry of entries) { + const srcPath = join(examplePath, entry.name); + const destPath = join(path, entry.name); + await cp(srcPath, destPath, { recursive: true }); + } } async function handleNew(args: NewArgs) { diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index 368b7d57e6..9d4afbdfbf 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -81,7 +81,8 @@ "src/ui/utils/computeStats.test.ts", "src/ui/themes/theme.test.ts", "src/validateNonInterActiveAuth.test.ts", - "src/services/prompt-processors/shellProcessor.test.ts" + "src/services/prompt-processors/shellProcessor.test.ts", + "src/commands/extensions/examples/**" ], "references": [{ "path": "../core" }] }