ollama_chat
Chat with local LLMs using conversation history, system messages, tool calling, and adjustable generation settings.
Instructions
Chat with a model using conversation messages. Supports system messages, multi-turn conversations, tool calling, and generation options.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| model | Yes | Name of the model to use | |
| messages | Yes | Array of chat messages | |
| tools | No | Tools that the model can call (optional). Provide as JSON array of tool objects. | |
| options | No | Generation options (optional). Provide as JSON object with settings like temperature, top_p, etc. | |
| format | No | json |
Implementation Reference
- src/tools/chat.ts:61-120 (handler)Exports toolDefinition for 'ollama_chat' containing the handler function that validates input via ChatInputSchema.parse(args) and calls chatWithModel() which invokes the Ollama API's ollama.chat() to perform conversation-based chat with a model.
export const toolDefinition: ToolDefinition = { name: 'ollama_chat', description: 'Chat with a model using conversation messages. Supports system messages, multi-turn conversations, tool calling, and generation options.', inputSchema: { type: 'object', properties: { model: { type: 'string', description: 'Name of the model to use', }, messages: { type: 'array', description: 'Array of chat messages', items: { type: 'object', properties: { role: { type: 'string', enum: ['system', 'user', 'assistant'], }, content: { type: 'string', }, images: { type: 'array', items: { type: 'string' }, }, }, required: ['role', 'content'], }, }, tools: { type: 'string', description: 'Tools that the model can call (optional). Provide as JSON array of tool objects.', }, 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', 'messages'], }, handler: async (ollama: Ollama, args: Record<string, unknown>, format: ResponseFormat) => { const validated = ChatInputSchema.parse(args); return chatWithModel( ollama, validated.model, validated.messages, validated.options || {}, format, validated.tools.length > 0 ? validated.tools : undefined ); }, }; - src/tools/chat.ts:11-59 (handler)Core chat handler function 'chatWithModel' that calls ollama.chat() with model, messages, tools, options and format, then formats and returns the response content (including tool_calls if present).
export async function chatWithModel( ollama: Ollama, model: string, messages: ChatMessage[], options: GenerationOptions, format: ResponseFormat, tools?: Tool[] ): Promise<string> { // Determine format parameter for Ollama API let ollamaFormat: 'json' | undefined = undefined; if (format === ResponseFormat.JSON) { ollamaFormat = 'json'; } const response = await ollama.chat({ model, messages, tools, options, format: ollamaFormat, stream: false, }); // Extract content with fallback let content = response.message.content; if (!content) { content = ''; } const tool_calls = response.message.tool_calls; // If the response includes tool calls, include them in the output let hasToolCalls = false; if (tool_calls) { if (tool_calls.length > 0) { hasToolCalls = true; } } if (hasToolCalls) { const fullResponse = { content, tool_calls, }; return formatResponse(JSON.stringify(fullResponse), format); } return formatResponse(content, format); } - src/schemas.ts:88-98 (schema)ChatInputSchema Zod schema for ollama_chat tool, validating model, messages, tools (JSON parsed), options, format, and stream fields.
/** * Schema for ollama_chat tool */ export const ChatInputSchema = z.object({ model: z.string().min(1), messages: z.array(ChatMessageSchema).min(1), tools: parseJsonOrDefault([]).pipe(z.array(ToolSchema)), options: parseJsonOrDefault({}).pipe(GenerationOptionsSchema), format: ResponseFormatSchema.default('json'), stream: z.boolean().default(false), }); - src/server.ts:61-120 (registration)MCP server CallToolRequestSchema handler that discovers all tools via discoverTools(), finds the matching tool by name ('ollama_chat'), and invokes its handler.
// Register tool call handler server.setRequestHandler(CallToolRequestSchema, async (request) => { try { const { name, arguments: args } = request.params; // Discover all tools const tools = await discoverTools(); // Find the matching tool const tool = tools.find((t) => t.name === name); if (!tool) { throw new Error(`Unknown tool: ${name}`); } // Determine format from args const formatArg = (args as Record<string, unknown>).format; const format = formatArg === 'markdown' ? ResponseFormat.MARKDOWN : ResponseFormat.JSON; // Call the tool handler const result = await tool.handler( ollama, args as Record<string, unknown>, format ); // Parse the result to extract structured data let structuredData: unknown = undefined; try { // Attempt to parse the result as JSON structuredData = JSON.parse(result); } catch { // If parsing fails, leave structuredData as undefined // This handles cases where the result is markdown or plain text } return { structuredContent: structuredData, content: [ { type: 'text', text: result, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: 'text', text: `Error: ${errorMessage}`, }, ], isError: true, }; } }); - src/autoloader.ts:31-57 (registration)Auto-discovery mechanism that loads all tool definitions from the tools/ directory by importing modules that export a 'toolDefinition' matching the ToolDefinition interface.
export async function discoverTools(): Promise<ToolDefinition[]> { const toolsDir = join(__dirname, 'tools'); const files = await readdir(toolsDir); // Filter for .js files (production) or .ts files (development) // Exclude test files and declaration files const toolFiles = files.filter( (file) => (file.endsWith('.js') || file.endsWith('.ts')) && !file.includes('.test.') && !file.endsWith('.d.ts') ); const tools: ToolDefinition[] = []; for (const file of toolFiles) { const toolPath = join(toolsDir, file); const module = await import(toolPath); // Check if module exports tool metadata if (module.toolDefinition) { tools.push(module.toolDefinition); } } return tools; }