Back to blog
    Claude Desktop + 本地微調模型:完整設置指南
    claude-desktoplocal-modelollamamcpsetupsegment:vibecoder

    Claude Desktop + 本地微調模型:完整設置指南

    在本地運行您的微調模型,通過 MCP 連接到 Claude Desktop,在 Claude 介面內獲得零費用的領域 AI 助理。完整逐步設置說明。

    EErtas Team·

    Claude Desktop 是一款強大的 AI 助理。您的微調模型針對您的領域進行了專門訓練。您可以透過 MCP(模型上下文協議)將兩者連接,獲得 Claude 介面與您模型領域專業知識的結合——且無需支付任何 API 費用即可進行專業推理。

    本指南將帶您完成完整設置:Ollama 部署、MCP 伺服器、Claude Desktop 配置以及測試。

    前置條件

    • 已安裝 Claude Desktop(於 claude.ai/download 下載)
    • 來自 Ertas 的微調 GGUF 模型
    • Node.js 18 以上版本(用於 MCP 伺服器)
    • 機器上 8GB 以上記憶體(用於執行帶有 7B 模型的 Ollama)

    步驟 1:安裝並配置 Ollama

    # macOS / Linux
    curl -fsSL https://ollama.ai/install.sh | sh
    
    # Windows:從 ollama.ai 下載安裝程式

    匯入您的微調 GGUF 模型:

    # 建立 Modelfile——定義 Ollama 如何運行您的模型
    cat > Modelfile << 'EOF'
    FROM /path/to/your-model.gguf
    
    # 內嵌於模型的系統提示
    SYSTEM """
    您是 [您的領域] 的專業 AI 助理。[添加對模型功能和行為方式的簡短說明。]
    """
    
    # 可選:調整生成參數
    PARAMETER temperature 0.3
    PARAMETER num_ctx 4096
    EOF
    
    # 使用您將用於調用的名稱建立模型
    ollama create my-domain-model -f Modelfile

    驗證模型是否正確載入:

    ollama run my-domain-model "測試提示——描述您的功能"

    您應該會看到來自微調模型的回應。如果返回有關載入 GGUF 的錯誤,請驗證 Modelfile 中的檔案路徑。

    步驟 2:安裝 MCP SDK

    為您的 MCP 伺服器建立專案目錄:

    mkdir claude-domain-mcp
    cd claude-domain-mcp
    npm init -y
    npm install @modelcontextprotocol/sdk

    步驟 3:編寫 MCP 伺服器

    建立 server.mjs

    import { Server } from '@modelcontextprotocol/sdk/server/index.js';
    import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
    import {
      CallToolRequestSchema,
      ListToolsRequestSchema,
    } from '@modelcontextprotocol/sdk/types.js';
    
    const OLLAMA_URL = 'http://localhost:11434/api/chat';
    const MODEL = 'my-domain-model'; // 必須與您在 Ollama 中命名的一致
    
    const server = new Server(
      { name: 'domain-assistant', version: '1.0.0' },
      { capabilities: { tools: {} } }
    );
    
    // 定義 Claude 可以使用的工具
    server.setRequestHandler(ListToolsRequestSchema, async () => {
      return {
        tools: [
          {
            name: 'ask_domain_model',
            description:
              '用於 [精確描述您的領域任務——例如「生成房地產列表描述」、「分類支援工單」、「為客戶 X 撰寫品牌文案」]。此模型具有 [領域細節] 的專業知識。',
            inputSchema: {
              type: 'object',
              properties: {
                request: {
                  type: 'string',
                  description: '領域模型的具體任務或問題',
                },
                context: {
                  type: 'string',
                  description: '相關背景資訊,例如物件詳情、產品規格、客戶資訊等。',
                },
              },
              required: ['request'],
            },
          },
        ],
      };
    });
    
    // 處理來自 Claude 的工具調用
    server.setRequestHandler(CallToolRequestSchema, async (request) => {
      if (request.params.name !== 'ask_domain_model') {
        throw new Error(`未知工具:${request.params.name}`);
      }
    
      const { request: userRequest, context } = request.params.arguments;
    
      const userMessage = context
        ? `${context}\n\n${userRequest}`
        : userRequest;
    
      let response;
      try {
        const res = await fetch(OLLAMA_URL, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            model: MODEL,
            messages: [{ role: 'user', content: userMessage }],
            stream: false,
          }),
        });
    
        if (!res.ok) {
          throw new Error(`Ollama 返回 ${res.status}:${await res.text()}`);
        }
    
        const data = await res.json();
        response = data.message?.content ?? '模型無回應';
      } catch (err) {
        return {
          content: [
            {
              type: 'text',
              text: `呼叫領域模型時出錯:${err.message}。Ollama 是否正在運行?`,
            },
          ],
          isError: true,
        };
      }
    
      return {
        content: [{ type: 'text', text: response }],
      };
    });
    
    const transport = new StdioServerTransport();
    await server.connect(transport);

    獨立測試伺服器是否正常運作:

    echo '{"jsonrpc":"2.0","method":"tools/list","id":1}' | node server.mjs

    您應該看到列出工具的 JSON 回應。

    步驟 4:配置 Claude Desktop

    macOS: 編輯 ~/Library/Application Support/Claude/claude_desktop_config.json Windows: 編輯 %APPDATA%\Claude\claude_desktop_config.json

    {
      "mcpServers": {
        "domain-assistant": {
          "command": "node",
          "args": ["/absolute/path/to/claude-domain-mcp/server.mjs"],
          "env": {
            "NODE_ENV": "production"
          }
        }
      }
    }

    重要: 使用 server.mjs 的絕對路徑。相對路徑不能可靠地工作。

    完全退出 Claude Desktop(Command+Q / Windows 上右鍵任務欄 → 退出),然後重新啟動。

    步驟 5:驗證連接

    在 Claude Desktop 中,查看工具圖示(輸入欄中的錘子圖示)。點擊它查看可用工具。您應該在「domain-assistant」下看到 ask_domain_model 列出。

    使用應觸發您工具的提示進行測試:

    「使用領域模型,為 [測試輸入] 生成 [您預期的輸出類型]。」

    Claude 應調用您的工具並返回回應。如果不調用工具,請嘗試更明確:「請使用領域助理工具…」

    故障排除

    工具未出現在 Claude Desktop 中:

    • 驗證配置檔案 JSON 是否有效(沒有多餘逗號,語法正確)
    • 檢查 server.mjs 的路徑是否為絕對路徑且正確
    • 查看 Claude Desktop 日誌:macOS ~/Library/Logs/Claude/,Windows %APPDATA%\Claude\logs\

    工具被調用但返回錯誤:

    • 在終端機中手動運行 ollama serve,確認 ollama run my-domain-model "test" 有效
    • 驗證 server.mjs 中的模型名稱與您在 Ollama 中命名的完全一致
    • 檢查 Ollama 是否監聽埠 11434:curl http://localhost:11434/api/tags

    模型給出錯誤或通用回應:

    • 驗證 GGUF 檔案是否正確載入(它是微調版本,而非基礎模型)
    • 確認 Modelfile 系統提示是否適當
    • 在 Modelfile 中嘗試不同的溫度設置

    Ship AI that runs on your users' devices.

    Ertas early bird pricing starts at $14.50/mo — locked in for life. Plans for builders and agencies.

    延伸閱讀

    Ship AI that runs on your users' devices.

    Early bird pricing starts at $14.50/mo — locked in for life. Plans for builders and agencies.

    Keep reading