内容
# 反重力 IDE — 本地 Ollama 模型連動
[](https://opensource.org/licenses/MIT)
[](https://www.python.org/)
[](https://ollama.com/)
[](https://modelcontextprotocol.io/)
[](https://github.com/20eung/antigravity-ollama-setup)
[](https://github.com/20eung/antigravity-ollama-setup/releases)
反重力 IDE(基於 Google Gemini 的 VS Code 分叉版)在輸入 `@gemma`、`@qwen` 時,會自動呼叫本地 Ollama 模型,提供 **全局 MCP + Skill 設定**。
---
## 目錄
- [運作原理](#運作原理)
- [前提條件](#前提條件)
- [快速安裝](#快速安裝)
- [手動安裝](#手動安裝)
- [使用方法](#使用方法)
- [運作確認](#運作確認)
- [模型變更](#模型變更)
- [故障排除](#故障排除)
- [刪除方法](#刪除方法)
- [檔案結構](#檔案結構)
- [授權](#授權)
---
## 運作原理
```
使用者輸入: @gemma 你好!
↓
SKILL.md(模式偵測:@gemma 開頭)
↓
MCP 工具呼叫:ask_gemma("你好!")
↓
ollama_mcp.py(MCP 伺服器,JSON-RPC 2.0 over stdio)
↓
Ollama REST API:http://localhost:11434/v1/chat/completions
↓
gemma4:e4b 模型回應返回
```
**核心組成 3 個**
| 檔案 | 角色 |
|------|------|
| `mcp_config.json` | MCP 伺服器註冊 |
| `ollama_mcp.py` | MCP 伺服器實作(ask_gemma / ask_qwen 工具) |
| `skills/ollama-models/SKILL.md` | @mention 模式偵測及工具呼叫指示 |
所有檔案皆安裝於 `~/.gemini/antigravity/` 下,**所有工作空間皆可使用**。
---
## 前提條件
- [反重力 IDE](https://antigravity.dev) 已安裝
- [Ollama](https://ollama.com/) 已安裝並運行中
- Python 3.8 以上(僅使用標準函式庫,無需外部套件)
- 模型下載完成:
```bash
ollama pull gemma4:e2b
ollama pull qwen2.5:1.5b
```
> **模型選擇基準** — 反重力的 MCP 內部逾時(約 30 秒)內應回應。
>
> | 模型 | 大小 | 回應速度 | 推薦與否 |
> |------|------|-----------|-----------|
> | gemma4:e2b | - | - | 推薦(`@gemma`) |
> | qwen2.5:1.5b | 1GB | ~19秒 | 推薦(`@qwen`) |
> | gemma4:e4b | 10GB | ~117秒 | 逾時 |
> | qwen3.5:9b | 6.6GB | ~55秒 | 逾時 |
---
## 快速安裝
```bash
git clone https://github.com/20eung/antigravity-ollama-setup.git
cd antigravity-ollama-setup
bash setup.sh
```
`setup.sh` 自動處理以下事項:
- [x] 前提條件確認(python3、ollama、伺服器運行狀態、模型存在)
- [x] 目錄建立(`~/.gemini/antigravity/skills/ollama-models/`)
- [x] `ollama_mcp.py` 複製並賦予執行權限
- [x] `mcp_config.json` 建立(若已有檔案,則詢問是否覆蓋)
- [x] `SKILL.md` 建立
---
## 手動安裝
**步驟 1:目錄建立**
```bash
mkdir -p ~/.gemini/antigravity/skills/ollama-models
```
**步驟 2:MCP 伺服器檔案複製**
```bash
cp ollama_mcp.py ~/.gemini/antigravity/ollama_mcp.py
chmod +x ~/.gemini/antigravity/ollama_mcp.py
```
**步驟 3:`mcp_config.json` 建立**
`~/.gemini/antigravity/mcp_config.json`
```json
{
"mcpServers": {
"ollama": {
"command": "python3",
"args": ["/Users/YOUR_USERNAME/.gemini/antigravity/ollama_mcp.py"]
}
}
}
```
> **`~`(波浪號)不可使用** — `mcp_config.json` 的 `args` 不會經過 Shell 處理,直接傳遞給程序,因此 `~` 無法展開為家目錄。請使用絕對路徑。
>
> 絕對路徑確認:
> ```bash
> echo "$HOME/.gemini/antigravity/ollama_mcp.py"
> ```
**步驟 4:`SKILL.md` 建立**
`~/.gemini/antigravity/skills/ollama-models/SKILL.md`
```markdown
---
name: ollama-models
description: Routes @gemma and @qwen mentions to local Ollama models using MCP tools.
---
When the user's message starts with `@gemma`:
1. Extract the text after `@gemma` as the prompt
2. Call the `ask_gemma` MCP tool with that prompt
3. Return the tool response directly without additional commentary
When the user's message starts with `@qwen`:
1. Extract the text after `@qwen` as the prompt
2. Call the `ask_qwen` MCP tool with that prompt
3. Return the tool response directly without additional commentary
```
**步驟 5:反重力 IDE 重啟**
---
## 使用方法
在反重力 IDE 的聊天視窗中,輸入以 `@gemma` 或 `@qwen` 開頭的訊息。
```
@gemma 你好,自我介紹一下
@qwen Python 裡如何排序列表?
@gemma explain recursion in simple terms
@qwen 解釋 Docker 容器和映像的差異
```
---
## 運作確認
1. 在聊天視窗中輸入 `@gemma` 或 `@qwen`
2. 在 Exploring 階段,確認顯示以下訊息:
```
MCP Tool: ollama / ask_gemma
```
3. Ollama 模型的回應在聊天視窗中輸出
確認 Ollama 活躍模型列表:
```bash
ollama ps
```
---
## 模型變更
修改 `ollama_mcp.py` 開頭的常數:
```python
MODEL_GEMMA = "gemma4:e2b" # 變更為所需的 gemma 系列模型
MODEL_QWEN = "qwen2.5:1.5b" # 變更為所需的 qwen 系列模型
```
> 為了在反重力 MCP 的逾時內回應,建議 **回應時間在 30 秒以內** 的模型。
重啟反重力 IDE 後生效。
確認可用模型列表:
```bash
ollama list
```
---
## 故障排除
| 症狀 | 原因 | 解決方法 |
|------|------|------|
| 輸入 `@gemma` 時 Gemini 回應 | SKILL.md 未識別或 IDE 未重啟 | 完全關閉 IDE 後重啟 |
| MCP 工具呼叫顯示不出現 | `mcp_config.json` 路徑錯誤 | 確認絕對路徑(`echo $HOME`) |
| `Ollama 伺服器連線失敗` | Ollama 伺服器未運行 | 執行 `ollama serve` |
| `模型不存在` 錯誤 | 模型未下載 | `ollama pull gemma4:e4b` |
| 回應過慢 | 模型大小 / 硬體問題 | 變更為較小的模型 |
---
## 檔案結構
```
antigravity-ollama-setup/
├── README.md # 本文件
├── setup.sh # 自動安裝腳本
└── ollama_mcp.py # MCP 伺服器實作(僅使用 Python 標準函式庫)
```
安裝後建立的全域路徑:
```
~/.gemini/antigravity/
├── mcp_config.json
├── ollama_mcp.py
└── skills/
└── ollama-models/
└── SKILL.md
```
---
## 刪除方法
刪除安裝的所有檔案。
```bash
# 刪除 MCP 伺服器檔案
rm -f ~/.gemini/antigravity/ollama_mcp.py
# 刪除技能檔案
rm -rf ~/.gemini/antigravity/skills/ollama-models/
# 刪除 mcp_config.json(若只有 ollama 項目)
rm -f ~/.gemini/antigravity/mcp_config.json
```
> 若 `mcp_config.json` 中還有其他 MCP 伺服器註冊,請勿刪除整個檔案,只需刪除 `ollama` 項目即可。
刪除後重啟反重力 IDE,即可完全移除。
---
## 授權
[MIT License](LICENSE)