list_ollama_models
Retrieve all available Ollama models from your local instance to identify which AI models you can consult for alternative viewpoints.
Instructions
List all available Ollama models on the local instance.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/handlers/callToolHandler.ts:39-83 (handler)Main handler logic for the 'list_ollama_models' tool. Fetches available models using ModelValidator, handles empty list and errors, returns JSON-formatted list of model names.case 'list_ollama_models': { try { const available = await this.modelValidator.getAvailableModels(); if (available.length === 0) { return { content: [ { type: 'text', text: 'No models available. Please install a model locally or use a cloud-based model (e.g., qwen2.5-coder:7b-cloud)', }, ], isError: true, }; } const modelNames = available.map((m) => m.name); return { content: [ { type: 'text', text: JSON.stringify( { models: modelNames, count: modelNames.length, note: 'These are available models (installed locally or cloud-based)', }, null, 2 ), }, ], }; } catch (error) { const message = error instanceof Error ? error.message : 'Failed to list models'; return { content: [ { type: 'text', text: `Error: ${message}`, }, ], isError: true, }; } }
- src/services/ModelValidator.ts:27-62 (helper)Core helper method that queries Ollama /api/tags endpoint, filters safe models (cloud or installed locals), and returns structured list of available models.async getAvailableModels(): Promise<AvailableModel[]> { try { const url = this.config.getApiUrl('/api/tags'); const response = await fetch(url, { method: 'GET', signal: AbortSignal.timeout(this.config.getTimeout()), }); if (!response.ok) { throw new OllamaError( `Failed to fetch models: ${response.statusText}`, 'LIST_MODELS_FAILED' ); } const text = await response.text(); const data = JSON.parse(text) as { models?: any[] }; const models = data.models || []; const available = models .filter((m) => this.modelIsSafe(m)) .map((m) => ({ name: m.name, installed: !this.looksLikeCloudModel(m.name), isCloud: this.looksLikeCloudModel(m.name), })); return available; } catch (error) { if (error instanceof OllamaError) throw error; throw new OllamaError( `Failed to fetch available models: ${error instanceof Error ? error.message : 'Unknown error'}`, 'CONNECTION_FAILED' ); } }
- Tool schema definition: name, description, and empty input schema (no parameters required). Returned by listTools endpoint.{ name: 'list_ollama_models', description: 'List all available Ollama models on the local system (installed or cloud-based)', inputSchema: { type: 'object', properties: {}, required: [], }, },
- src/handlers/listToolsHandler.ts:137-137 (registration)Factory function to create ListToolsHandler instance, which registers the tool in MCP listTools response.export const listToolsHandler = () => new ListToolsHandler();
- Legacy handler implementation for 'list_ollama_models', directly using axios to fetch and filter models.case 'list_ollama_models': { try { const response = await axios.get(`${OLLAMA_BASE_URL}/api/tags`); // Add defensive checks for response data if (!response || !response.data) { throw new Error('Empty response from Ollama API'); } const rawModels = response.data.models || []; if (!Array.isArray(rawModels)) { throw new Error(`Unexpected response format: models is not an array`); } const safe = rawModels.filter((m: any) => modelObjIsSafe(m)).map((m: any) => m.name); let modelsList: string[] = []; if (safe.length > 0) { modelsList = safe; } else { // Fallback: prefer cloud-like names if present, otherwise return raw list const cloudOnly = rawModels .filter((m: any) => m && looksLikeCloudModelName(m.name)) .map((m: any) => m.name); modelsList = cloudOnly.length > 0 ? cloudOnly : rawModels.map((m: any) => m.name); } const note = safe.length > 0 ? ' (cloud models and installed locals)' : ' (no installed locals detected; showing cloud-like or raw models)'; return { content: [ { type: 'text', text: `Available models${note}: ${modelsList.join(', ')}`, }, ], }; } catch (_error) { const error = _error; const message = error instanceof Error ? error.message : 'Unknown error'; const stack = error instanceof Error ? error.stack : ''; return { content: [ { type: 'text', text: `Error listing models: ${message}\n${stack ? `Stack: ${stack}` : ''}`, }, ], isError: true, }; } }