[PR #13562] [CLOSED] server: add MCP (Model Context Protocol) support #60969

Closed
opened 2026-04-29 16:04:01 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/13562
Author: @Code4me2
Created: 12/25/2025
Status: Closed

Base: mainHead: mcp-stable-clean


📝 Commits (8)

  • a8027c6 server: add MCP client for JSON-RPC communication
  • 0fd68e4 server: add MCP manager for multi-client orchestration
  • fc05536 server: add MCP security layer for command validation
  • abcb81b cmd: add --tools flag for MCP server integration
  • c747bed server: add declarative auto-enable for MCP servers
  • 1912be6 server: add MCP integration tests
  • aacb25e docs: add MCP integration documentation
  • 738c253 cmd: add --list-tools flag to display available MCP servers

📊 Changes

18 files changed (+4756 additions, -218 deletions)

View changed files

📝 api/types.go (+141 -11)
📝 cmd/cmd.go (+546 -24)
📝 cmd/cmd_test.go (+4 -0)
📝 docs/api.md (+1 -0)
📝 docs/cli.mdx (+8 -0)
docs/mcp.md (+360 -0)
examples/mcp-servers.json (+59 -0)
server/mcp.go (+114 -0)
server/mcp_client.go (+810 -0)
server/mcp_code_api.go (+81 -0)
server/mcp_command_resolver.go (+214 -0)
server/mcp_definitions.go (+280 -0)
server/mcp_manager.go (+484 -0)
server/mcp_security_config.go (+152 -0)
server/mcp_sessions.go (+178 -0)
server/mcp_test.go (+731 -0)
📝 server/routes.go (+502 -183)
server/routes_tools.go (+91 -0)

📄 Description

Summary

Adds MCP (Model Context Protocol) support to Ollama, enabling language models to execute tools and interact with external systems autonomously via JSON-RPC over stdio.

Closes #7865

Changes

Core MCP Implementation

  • server/mcp_client.go - JSON-RPC 2.0 client for MCP server communication
  • server/mcp_manager.go - Multi-client orchestration and tool execution
  • server/mcp_sessions.go - Session management for persistent connections
  • server/mcp_definitions.go - Declarative server definitions with auto-enable

Security

  • server/mcp_security_config.go - Command blocklists and environment filtering
  • server/mcp_command_resolver.go - Safe command resolution with PATH validation

API Integration

  • server/routes.go - Chat handler with multi-round tool execution loop
  • server/routes_tools.go - /api/tools endpoint for tool discovery
  • api/types.go - New mcp_servers field in ChatRequest

CLI

  • cmd/cmd.go - --tools flag for interactive mode, --list-tools for discovery

Documentation

  • docs/mcp.md - Comprehensive MCP documentation
  • docs/api.md - Brief mention of mcp_servers parameter
  • docs/cli.mdx - Brief mention of --tools flag

Usage

CLI

ollama run qwen2.5:7b --tools /path/to/directory

API

curl -X POST http://localhost:11434/api/chat \
  -d '{
    "model": "qwen2.5:7b",
    "messages": [{"role": "user", "content": "List the files"}],
    "mcp_servers": [{
      "name": "filesystem",
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"]
    }]
  }'

Testing

go test ./server/... -run "MCP" -v

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/13562 **Author:** [@Code4me2](https://github.com/Code4me2) **Created:** 12/25/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `mcp-stable-clean` --- ### 📝 Commits (8) - [`a8027c6`](https://github.com/ollama/ollama/commit/a8027c63866b5ea9306d0e1b6e7179ddf1a42947) server: add MCP client for JSON-RPC communication - [`0fd68e4`](https://github.com/ollama/ollama/commit/0fd68e46fae1465e67e57c1e39864fcbfef73123) server: add MCP manager for multi-client orchestration - [`fc05536`](https://github.com/ollama/ollama/commit/fc05536d526808e234b161737c461a2b300c859d) server: add MCP security layer for command validation - [`abcb81b`](https://github.com/ollama/ollama/commit/abcb81bb07978ba868028b228cd0f07714005dd0) cmd: add --tools flag for MCP server integration - [`c747bed`](https://github.com/ollama/ollama/commit/c747bedbfa1799c690736a1b4739ca4211a9ab83) server: add declarative auto-enable for MCP servers - [`1912be6`](https://github.com/ollama/ollama/commit/1912be60fbd38868be35497d5161cc5f037f35ff) server: add MCP integration tests - [`aacb25e`](https://github.com/ollama/ollama/commit/aacb25e1be5293aa7dce5f5d87745471a4149c7b) docs: add MCP integration documentation - [`738c253`](https://github.com/ollama/ollama/commit/738c253cb097a4eb4e88a7031030cff2327e016d) cmd: add --list-tools flag to display available MCP servers ### 📊 Changes **18 files changed** (+4756 additions, -218 deletions) <details> <summary>View changed files</summary> 📝 `api/types.go` (+141 -11) 📝 `cmd/cmd.go` (+546 -24) 📝 `cmd/cmd_test.go` (+4 -0) 📝 `docs/api.md` (+1 -0) 📝 `docs/cli.mdx` (+8 -0) ➕ `docs/mcp.md` (+360 -0) ➕ `examples/mcp-servers.json` (+59 -0) ➕ `server/mcp.go` (+114 -0) ➕ `server/mcp_client.go` (+810 -0) ➕ `server/mcp_code_api.go` (+81 -0) ➕ `server/mcp_command_resolver.go` (+214 -0) ➕ `server/mcp_definitions.go` (+280 -0) ➕ `server/mcp_manager.go` (+484 -0) ➕ `server/mcp_security_config.go` (+152 -0) ➕ `server/mcp_sessions.go` (+178 -0) ➕ `server/mcp_test.go` (+731 -0) 📝 `server/routes.go` (+502 -183) ➕ `server/routes_tools.go` (+91 -0) </details> ### 📄 Description ## Summary Adds MCP (Model Context Protocol) support to Ollama, enabling language models to execute tools and interact with external systems autonomously via JSON-RPC over stdio. Closes #7865 ## Changes ### Core MCP Implementation - `server/mcp_client.go` - JSON-RPC 2.0 client for MCP server communication - `server/mcp_manager.go` - Multi-client orchestration and tool execution - `server/mcp_sessions.go` - Session management for persistent connections - `server/mcp_definitions.go` - Declarative server definitions with auto-enable ### Security - `server/mcp_security_config.go` - Command blocklists and environment filtering - `server/mcp_command_resolver.go` - Safe command resolution with PATH validation ### API Integration - `server/routes.go` - Chat handler with multi-round tool execution loop - `server/routes_tools.go` - `/api/tools` endpoint for tool discovery - `api/types.go` - New `mcp_servers` field in ChatRequest ### CLI - `cmd/cmd.go` - `--tools` flag for interactive mode, `--list-tools` for discovery ### Documentation - `docs/mcp.md` - Comprehensive MCP documentation - `docs/api.md` - Brief mention of `mcp_servers` parameter - `docs/cli.mdx` - Brief mention of `--tools` flag ## Usage ### CLI ```bash ollama run qwen2.5:7b --tools /path/to/directory ``` ### API ```bash curl -X POST http://localhost:11434/api/chat \ -d '{ "model": "qwen2.5:7b", "messages": [{"role": "user", "content": "List the files"}], "mcp_servers": [{ "name": "filesystem", "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"] }] }' ``` ## Testing ```bash go test ./server/... -run "MCP" -v ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-29 16:04:01 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#60969