ollama_embed
Generate numerical vector embeddings from text input to enable semantic search, clustering, or similarity comparisons.
Instructions
Generate embeddings for text input. Returns numerical vector representations.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| model | Yes | Name of the model to use | |
| input | Yes | Text input. For batch processing, provide a JSON-encoded array of strings, e.g., ["text1", "text2"] | |
| format | No | json |
Implementation Reference
- src/tools/embed.ts:10-22 (handler)Main handler function for ollama_embed. Calls ollama.embed() with the model and input, then formats the response.
export async function embedWithModel( ollama: Ollama, model: string, input: string | string[], format: ResponseFormat ): Promise<string> { const response = await ollama.embed({ model, input, }); return formatResponse(JSON.stringify(response), format); } - src/tools/embed.ts:48-52 (handler)Tool handler that validates args via EmbedInputSchema.parse() and delegates to embedWithModel().
handler: async (ollama: Ollama, args: Record<string, unknown>, format: ResponseFormat) => { const validated = EmbedInputSchema.parse(args); return embedWithModel(ollama, validated.model, validated.input, format); }, }; - src/tools/embed.ts:24-52 (registration)Tool definition registration for 'ollama_embed' including name, description, inputSchema, and handler. Exported as toolDefinition for autoloader discovery.
export const toolDefinition: ToolDefinition = { name: 'ollama_embed', description: 'Generate embeddings for text input. Returns numerical vector representations.', inputSchema: { type: 'object', properties: { model: { type: 'string', description: 'Name of the model to use', }, input: { type: 'string', description: 'Text input. For batch processing, provide a JSON-encoded array of strings, e.g., ["text1", "text2"]', }, format: { type: 'string', enum: ['json', 'markdown'], default: 'json', }, }, required: ['model', 'input'], }, handler: async (ollama: Ollama, args: Record<string, unknown>, format: ResponseFormat) => { const validated = EmbedInputSchema.parse(args); return embedWithModel(ollama, validated.model, validated.input, format); }, }; - src/schemas.ts:112-144 (schema)Zod schema for ollama_embed tool input validation (EmbedInputSchema). Validates model (string), input (string or JSON array of strings), and format.
* Schema for ollama_embed tool */ export const EmbedInputSchema = z.object({ model: z.string().min(1), input: z.string().transform((val, ctx) => { const trimmed = val.trim(); // If it looks like a JSON array, try to parse it if (trimmed.startsWith('[') && trimmed.endsWith(']')) { try { const parsed = JSON.parse(trimmed); if (Array.isArray(parsed)) { // Validate all elements are strings const allStrings = parsed.every((item) => typeof item === 'string'); if (allStrings) { return parsed as string[]; } else { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Input is a JSON array but contains non-string elements', }); return z.NEVER; } } } catch (e) { // Failed to parse as JSON, treat as plain string } } // Return as plain string return trimmed; }), format: ResponseFormatSchema.default('json'), }); - src/utils/response-formatter.ts:7-34 (helper)Helper function formatResponse used by the handler to format output as JSON or markdown.
export function formatResponse( content: string, format: ResponseFormat ): string { if (format === ResponseFormat.JSON) { // For JSON format, validate and potentially wrap errors try { // Try to parse to validate it's valid JSON JSON.parse(content); return content; } catch { // If not valid JSON, wrap in error object return JSON.stringify({ error: 'Invalid JSON content', raw_content: content, }); } } // Format as markdown try { const data = JSON.parse(content); return jsonToMarkdown(data); } catch { // If not valid JSON, return as-is return content; } }