实战教程来了!从零开始打造MCP+Ollama集成

模型上下文协议MCP与Ollama的整合实现指南

在过去一两个个月里,模型上下文协议(Model Context Protocol,MCP)频繁出现在各种技术微信交流群中。我们已经看到了许多很酷的集成案例,大家似乎相信这个标准会长期存在,因为它为大模型与工具或软件的集成设立了规范。

前面一篇文章给大家分享了MCP一些基础概念,但是读完之后还是模棱两可,所以决定尝试将Ollama中的小型语言模型与MCP服务器连接起来,体验一下这个新标准。今天,向大家展示如何实现Ollama与MCP服务器的集成。

实现步骤

整个集成的主要步骤包括:

  1. 创建测试以及使用MCP服务
  2. 创建客户端文件来发送请求并启动服务
  3. 从服务获取工具到客户端
  4. 将工具转换为pydantic模型
  5. 通过response format将工具(作为pydantic模型)传递给Ollama
  6. 通过Ollama发送对话并接收结构化输出
  7. 如果响应中包含工具,则向服务器发送请求

安装依赖

要运行这个项目,需要安装必要的包。fastmcp库在使用uv运行代码时效果最佳。uv很容易下载和使用,类似于Poetry和pip。

使用以下命令将所需库添加到你的项目中:

uv add fastmcp ollama

这会同时安装MCP服务器和Ollama聊天库,以便你在它们的基础上构建客户端和服务器逻辑。

文件结构

设置时,你的文件夹应该是这样的:

your folder
├── server.py
└── client.py

server.py文件包含MCP服务器和想要暴露的工具。client.py文件在后台进程中启动服务器,获取可用工具,并与Ollama连接。

示例MCP服务器

首先,让我们使用fastmcp库创建一个简单的MCP服务器。该服务器暴露了一个名为magicoutput的工具。这个函数接受两个字符串输入并返回一个固定的字符串作为输出。

@mcp.tool()装饰器用于将函数注册为MCP服务器中的可用工具。当服务器启动后,任何客户端都可以获取并调用这个工具。

通过在主块中调用mcp.run()来启动服务器。

# server.py
from fastmcp import FastMCP
# 创建MCP服务器
mcp = FastMCP("TestServer")
# 我的工具:
@mcp.tool()
def magicoutput(obj1: str, obj2: str) -> int:
    """使用此函数获取魔法输出"""
    print(f"输入参数:obj1:{
     obj1},obj2:{
     obj2}")
    return f"输入参数:obj1:{
     obj1},obj2:{
     obj2},魔法输出:Hello MCP,MCP Hello"
if __name__ == "__main__":
    mcp.run()

我们运行下面命令,进行调试服务端的工具:

fastmcp dev server.py

输入日志如下:

Need to install the following packages:
@modelcontextprotocol/inspector@0.10.2
Ok to proceed? (y) y

Starting MCP inspector...
⚙️ Proxy server listening on port 6277
🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀
New SSE connection
Query parameters: [Object: 

本地访问页面http://127.0.0.1:6274/#tools,我们可以看构造的函数,并且可以调试

获取服务器工具

为了连接到MCP服务器并列出可用工具,我们使用来自mcp库的ClientSessionStdioServerParametersstdio_client

我们定义一个名为OllamaMCP的类来处理服务器连接和工具获取。在类内部,_async_run方法启动异步会话,初始化它,并从服务器获取工具列表。

我们使用threading.Event()来跟踪会话何时准备就绪,并将工具列表存储在self.tools中。

在脚本末尾,我们定义服务器参数并在后台线程中运行客户端。这会启动连接并打印服务器返回的工具元数据。

# client.py
import asyncio
import threading 
from pathlib import Path
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from typing import Any

class OllamaMCP:
    """
    Ollama和FastMCP的简单集成
    """
    def __init__(self, server_params: StdioServerParameters):
        self.server_params = server_params
        self.initialized = threading.Event()
        self.tools: list[Any] = []
    def _run_background(self):
        asyncio.run(self._async_run())
    async def _async_run(self):
        try:
            async with stdio_client(self.server_params) as (read, write):
                async with ClientSession(read, write) as session:
                    await session.initialize()
                    self.session = session
                    tools_result = await session.list_tools()
                    self.tools = tools_result.tools
                    print(tools_result)
        except Exception as e:  
            print(f"启动MCP服务器时出错 {
     str(e)}")
if __name__ == "__main__":
    server_parameters = StdioServerParameters(
        command="uv",
        args=["run", "python", "server.py"],
        cwd=str(Path.cwd())
    )
    ollamamcp = OllamaMCP(server_params=server_parameters)
    ollamamcp._run_background()

运行上面的代码后,你会从服务器得到以下响应,其中可以看到服务器上可用的工具列表。

[04/19/25 12:10:47] INFO     Starting server "TestServer"...       server.py:261
meta=None nextCursor=None tools=[Tool(name='magicoutput', description='使用此函数获取魔法输出', inputSchema={'properties': {'obj1': {'title': 'Obj1', 'type': 'string'}, 'obj2': {'title': 'Obj2', 'type': 'string'}}, 'required': ['obj1', 'obj2'], 'title': 'magicoutputArguments', 'type': 'object'})]

将工具转换为pydantic模型

现在我们已经从服务器接收到了工具列表,下一步是将它们转换为Pydantic模型。我们使用Pydantic的create_model来动态定义新的响应模式,基于服务器的工具定义。还有一个辅助函数来将JSON类型映射到有效的Python类型。

Pydantic 是一个用于数据验证和序列化的 Python 模型库。它在 FastAPI 中广泛使用,用于定义请求体、响应体和其他数据模型,提供了强大的类型检查和自动文档生成功能

这帮助我们动态定义模型,使语言模型确切知道在返回工具参数时应使用什么结构。

# client.py
import asyncio
import threading
from pathlib import Path
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from typing import Any, Union, Optional
from pydantic import BaseModel, create_model, Field

class OllamaMCP:
    """Ollama和FastMCP的简单集成"""

    def __init__(self, server_params: StdioServerParameters):
        self.server_params = server_params
        self.initialized = threading.Event()
        self.tools: list[Any] = []

    def _run_background(self):
        asyncio.run(self._async_run())

    async def _async_run(self):
        try:
            async with stdio_client(self.server_params) as (read, write):
                async with ClientSession(read, write) as session:
                    await session.initialize()
                    self.session = session
                    tools_result = await session.list_tools()
                    self.tools = tools_result.tools
        except Exception as e:  
            print(f"启动MCP服务器时出错 {
     str(e)}")


    def create_response_model(self):
        dynamic_classes = {
   }
        for tool in self.tools
<think>好的,用户想了解如何通过MCP调用Ollama模型,并且需要方法示例。首先,我需要明确MCP是什么,可能是指某个特定的中间件、平台或工具。结合用户提供的引用内容,尤其是引用[1]提到SpringAI调用本地Ollama模型,可能MCP是Spring框架中的一个模块,或者用户可能有拼写错误,比如应该是“Spring”而不是“MCP”。需要进一步确认。 查看引用内容,引用[1]提到在Spring中调用Ollama模型,通过API接口。引用[3]提到Ollama支持很多模型,比如deepseek-r1,安装命令是`ollama run deepseek-r1:7b`。可能用户的题是关于如何在Spring项目中配置和使用Ollama模型,可能MCP是笔误,或者指某个特定的中间件服务。 假设用户确实是指Spring框架中的调用方法,那么需要参考Spring AI的文档。根据引用[1],Spring AI提供了AI Models和Chat Models的接口,因此调用Ollama可能需要配置相应的模型参数和API地址。 接下来需要考虑如何配置Spring项目,添加依赖,创建配置类,以及编写调用代码。同时,用户需要示例代码,所以需要提供具体的Java或Kotlin代码片段,展示如何初始化Ollama的客户端,发送请求,处理响应。 另外,用户可能遇到的题包括模型服务的启动、端口配置是否正确,以及如何处理不同的模型版本。需要确保Ollama服务在本地运行,并且端口正确(如默认的11434)。同时,模型名称需要与安装的模型一致,比如`deepseek-r1:7b`。 可能还需要解释配置文件中的参数,如baseUrl和model,以及如何通过Spring的依赖注入来获取OllamaClient实例。此外,可能需要处理异常情况,比如连接失败或模型未找到的情况。 最后,生成的相关题需要围绕调用Ollama模型的其他方面,比如参数调优、流式响应处理、集成到Web应用,以及模型选择建议。这些题可以帮助用户进一步深入使用Ollama模型。</think>### 通过MCP调用Ollama模型的方法示例 根据您提供的引用内容,推测"MCP"可能指向中间件服务或框架(如Spring框架中的模块),以下以**Spring AI调用本地Ollama模型**为例说明具体实现步骤: --- #### 步骤1:启动Ollama模型服务 首先确保已通过Ollama安装并运行所需模型(以deepseek-r1:7b为例): ```bash ollama run deepseek-r1:7b ``` 服务默认运行在`http://localhost:11434`[^3] --- #### 步骤2:Spring项目配置 1. **添加依赖**:在`pom.xml`中引入Spring AI模块 ```xml <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-ollama-spring-boot-starter</artifactId> <version>0.8.1</version> </dependency> ``` 2. **配置文件**:`application.yml` ```yaml spring: ai: ollama: base-url: http://localhost:11434 chat: model: deepseek-r1:7b ``` --- #### 步骤3:代码实现 ```java import org.springframework.ai.ollama.OllamaClient; import org.springframework.ai.ollama.api.OllamaApi; import org.springframework.ai.ollama.api.OllamaOptions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class OllamaController { private final OllamaClient ollamaClient; @Autowired public OllamaController(OllamaApi ollamaApi) { this.ollamaClient = new OllamaClient(ollamaApi); } @GetMapping("/ask") public String askQuestion(@RequestParam String prompt) { OllamaOptions options = OllamaOptions.create() .withTemperature(0.7f); return ollamaClient.generate(prompt, options).getGeneration().getText(); } } ``` --- #### 关键参数说明 1. `temperature`:控制生成文本的随机性(0-1) 2. `top_p`:核采样概率阈值 3. `max_tokens`:最大输出长度[^2] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值