ollama_generate
Generate text completions from prompts using local LLM models for single-turn tasks like content creation or data formatting.
Instructions
Generate completion from a prompt. Simpler than chat, useful for single-turn completions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| model | Yes | Name of the model to use | |
| prompt | Yes | The prompt to generate from | |
| options | No | Generation options (optional). Provide as JSON object with settings like temperature, top_p, etc. | |
| format | No | json |
Implementation Reference
- src/tools/generate.ts:29-66 (registration)Exports the ToolDefinition object that registers the 'ollama_generate' tool with the autoloader, including name, description, JSON input schema for MCP protocol, and the handler function.export const toolDefinition: ToolDefinition = { name: 'ollama_generate', description: 'Generate completion from a prompt. Simpler than chat, useful for single-turn completions.', inputSchema: { type: 'object', properties: { model: { type: 'string', description: 'Name of the model to use', }, prompt: { type: 'string', description: 'The prompt to generate from', }, options: { type: 'string', description: 'Generation options (optional). Provide as JSON object with settings like temperature, top_p, etc.', }, format: { type: 'string', enum: ['json', 'markdown'], default: 'json', }, }, required: ['model', 'prompt'], }, handler: async (ollama: Ollama, args: Record<string, unknown>, format: ResponseFormat) => { const validated = GenerateInputSchema.parse(args); return generateWithModel( ollama, validated.model, validated.prompt, validated.options || {}, format ); }, };
- src/tools/generate.ts:56-65 (handler)Handler function executed by the MCP server for ollama_generate calls. Validates arguments using Zod schema and delegates to generateWithModel helper.handler: async (ollama: Ollama, args: Record<string, unknown>, format: ResponseFormat) => { const validated = GenerateInputSchema.parse(args); return generateWithModel( ollama, validated.model, validated.prompt, validated.options || {}, format ); },
- src/schemas.ts:100-109 (schema)Zod schema for validating and parsing inputs to the ollama_generate tool, used in the handler./** * Schema for ollama_generate tool */ export const GenerateInputSchema = z.object({ model: z.string().min(1), prompt: z.string(), options: parseJsonOrDefault({}).pipe(GenerationOptionsSchema), format: ResponseFormatSchema.default('json'), stream: z.boolean().default(false), });
- src/tools/generate.ts:8-27 (helper)Core helper function that performs the actual Ollama generate API call and formats the response./** * Generate completion from a prompt */ export async function generateWithModel( ollama: Ollama, model: string, prompt: string, options: GenerationOptions, format: ResponseFormat ): Promise<string> { const response = await ollama.generate({ model, prompt, options, format: format === ResponseFormat.JSON ? 'json' : undefined, stream: false, }); return formatResponse(response.response, format); }