Skip to main content
Glama
NightTrek

Ollama MCP Server

by NightTrek

run

Execute local AI models through the Ollama MCP Server by specifying a model name and prompt to generate responses.

Instructions

Run a model

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYesName of the model
promptYesPrompt to send to the model
timeoutNoTimeout in milliseconds (default: 60000)

Implementation Reference

  • The core handler function that executes the 'run' tool logic. It makes a streaming POST request to Ollama's /api/generate endpoint using axios, processes the SSE stream by parsing JSON chunks and extracting the 'response' field, and returns an MCP stream content block.
    private async handleRun(args: any) {
      try {
        // Use streaming mode with SSE
        const response = await axios.post(
          `${OLLAMA_HOST}/api/generate`,
          {
            model: args.name,
            prompt: args.prompt,
            stream: true,
          },
          {
            timeout: args.timeout || DEFAULT_TIMEOUT,
            responseType: 'stream'
          }
        );
    
        // Create a transform stream to process the SSE events
        const transformStream = new TransformStream({
          transform(chunk, controller) {
            try {
              const data = chunk.toString();
              const json = JSON.parse(data);
              controller.enqueue(json.response);
            } catch (error) {
              controller.error(new McpError(
                ErrorCode.InternalError,
                `Error processing stream: ${formatError(error)}`
              ));
            }
          }
        });
    
        return {
          content: [
            {
              type: 'stream',
              stream: response.data.pipeThrough(transformStream),
            },
          ],
        };
      } catch (error) {
        if (axios.isAxiosError(error)) {
          throw new McpError(
            ErrorCode.InternalError,
            `Ollama API error: ${error.response?.data?.error || error.message}`
          );
        }
        throw new McpError(ErrorCode.InternalError, `Failed to run model: ${formatError(error)}`);
      }
    }
  • The input schema definition for the 'run' tool, specifying required 'name' and 'prompt' parameters, optional 'timeout', used in tool registration.
    {
      name: 'run',
      description: 'Run a model',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'Name of the model',
          },
          prompt: {
            type: 'string',
            description: 'Prompt to send to the model',
          },
          timeout: {
            type: 'number',
            description: 'Timeout in milliseconds (default: 60000)',
            minimum: 1000,
          },
        },
        required: ['name', 'prompt'],
        additionalProperties: false,
      },
  • src/index.ts:253-289 (registration)
    Registers the request handler for CallToolRequestSchema, which dispatches 'run' tool calls (and others) to their respective handle* methods via a switch statement on request.params.name.
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
      try {
        switch (request.params.name) {
          case 'serve':
            return await this.handleServe();
          case 'create':
            return await this.handleCreate(request.params.arguments);
          case 'show':
            return await this.handleShow(request.params.arguments);
          case 'run':
            return await this.handleRun(request.params.arguments);
          case 'pull':
            return await this.handlePull(request.params.arguments);
          case 'push':
            return await this.handlePush(request.params.arguments);
          case 'list':
            return await this.handleList();
          case 'cp':
            return await this.handleCopy(request.params.arguments);
          case 'rm':
            return await this.handleRemove(request.params.arguments);
          case 'chat_completion':
            return await this.handleChatCompletion(request.params.arguments);
          default:
            throw new McpError(
              ErrorCode.MethodNotFound,
              `Unknown tool: ${request.params.name}`
            );
        }
      } catch (error) {
        if (error instanceof McpError) throw error;
        throw new McpError(
          ErrorCode.InternalError,
          `Error executing ${request.params.name}: ${formatError(error)}`
        );
      }
    });

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/NightTrek/Ollama-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server