MCP Xcode服务器与Ollama+矢量数据库
🧠 Xcode的本地AI编码助手
使用LanceDB将本地Ollama LLM连接到Xcode的具有持久内存的AI助手。
______________________________________________________________________
📋 目录
______________________________________________________________________
概述
此项目创建了一个 模型上下文协议(MCP)服务器 这座桥:
| 组件 | 描述 |
|---|---|
| 奥拉玛 | 用于代码生成的本地LLM(Qwen2.5-Coder-7B) |
| Xcode 26.X | 支持AI助手的苹果IDE |
| 兰斯数据库 | 持久内存矢量数据库(RAG) |
| 重新排名 | 交叉编码器用于精确的搜索结果排名 |
主要特点
- ✅ 完全本地化 -无云依赖,完全隐私
- ✅ 代码优化LLM -Qwen2.5-Coder-7B,专门接受过代码培训
- ✅ 持久内存 -你的代码“笔记本”在会话中幸存下来
- ✅ RAG+重新排名 -具有精确重新排名的上下文感知响应
- ✅ 自动标签 -自动代码分类(用户界面、网络、测试等)
- ✅ 调试模式 -用于故障排除的全面日志记录
- ✅ 简易设置 -用于快速安装的自动化脚本
______________________________________________________________________
建筑
┌─────────────────────────────────────────────────────────────────┐
│ Your Mac │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────┐ │
│ │ Xcode 26.x │ │
│ │ ┌───────────────┐ │ │
│ │ │ AI Assistant │ │ │
│ │ └───────┬───────┘ │ │
│ └──────────┼──────────┘ │
│ │ OpenAI-compatible API │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ MCP Xcode Server (Python) │ │
│ │ ┌───────────────────────────────────────┐ │ │
│ │ │ MCP Protocol │ │ │
│ │ ├───────────────────────────────────────┤ │ │
│ │ │ Tools │ │ │
│ │ │ • generate_code • explain_code │ │ │
│ │ │ • fix_code • ask_with_context│ │ │
│ │ │ • add_to_memory • search_memory │ │ │
│ │ ├───────────────────────────────────────┤ │ │
│ │ │ RAG Pipeline │ │ │
│ │ │ • Retrieval • Augmentation │ │ │
│ │ └───────────────────────────────────────┘ │ │
│ └──────────┬──────────────────┬───────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ Ollama │ │ LanceDB │ │
│ │ (Local LLM) │ │ (Vector DB) │ │
│ │ │ │ │ │
│ │ Qwen2.5-Coder │ │ Code Memory │ │
│ │ nomic-embed │ │ + Auto Labels │ │
│ │ :v1.5 │ │ + Re-Ranker │ │
│ └──────────────────┘ └──────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘______________________________________________________________________
先决条件
1.Xcode 26.x或更高版本
# Check Xcode version
xcodebuild -version备注:Xcode 26.3(2025)中引入了MCP支持。确保您拥有最新版本。
2.奥拉马
从以下位置安装 奥拉玛 或通过Homebrew:
brew install ollama验证安装:
ollama --version3.Python 3.10+
# Check Python version
python3 --version
# Install if needed
brew install python@3.12______________________________________________________________________
快速开始
步骤1:克隆和设置
# Navigate to project directory
cd /path/to/xcode-mcp-server
# Run setup script
chmod +x scripts/setup.sh
./scripts/setup.sh安装脚本将:
- 创建Python虚拟环境
- 安装所有依赖项
- 确认Ollama正在运行
- 创建自定义iOS型号或提取所需型号
- 创建必要的目录
第二步:承接你的项目
# Ingest an Xcode project
python scripts/ingest_project.py /path/to/your/XcodeProject步骤3:启动服务器
./scripts/start_server.sh步骤4:连接到Xcode 26+
启动Xcode“本地托管”提供程序的HTTP服务器:
./scripts/start_http_server.sh然后在Xcode中:
- 打开 Xcode→ 设置→ 智能
- 点击 “添加模型提供程序…”
- 选择 “本地托管”
- 设置端口:
1234 - 设置说明:
MCP Xcode Server - 点击 “添加”
就是这样!Xcode现在将使用您的本地Ollama模型和您摄入的代码库中的RAG上下文。
替代方案:MCP检查员测试
对于没有Xcode的测试:
npx @anthropic-ai/mcp-inspector ./venv/bin/python -m server.mcp_server______________________________________________________________________
详细设置指南
安装Ollama
- 下载自 ollama.ai/下载
- 安装应用程序
- 启动Ollama服务:
ollama serve- 拉动所需型号:
# Code generation model (choose one)
ollama pull codellama:13b # Recommended for code
# OR
ollama pull llama3.2:latest # General purpose
# OR
ollama pull deepseek-coder # Alternative code model
# Embedding model (required for RAG)
ollama pull nomic-embed-text # Best for embeddings- 验证模型是否可用:
ollama list项目设置
# 1. Navigate to project
cd xcode-mcp-server
# 2. Create virtual environment
python3 -m venv venv
# 3. Activate it
source venv/bin/activate
# 4. Install dependencies
pip install -r requirements.txt
# 5. Create directories
mkdir -p data/lancedb logsXcode配置
将Xcode 26+连接到服务器
- 启动HTTP服务器
./scripts/start_http_server.sh- 打开Xcode设置
- 菜单: Xcode > Settings... 或按 ⌘,
- 导航到情报
- 点击 智能 侧边栏中的选项卡
- 添加模型提供程序
- 点击 “添加模型提供程序…” - 选择 “本地托管” - 设置端口: 1234 - 设置说明: MCP Xcode Server - 点击 “添加”
- 选择型号
- 点击进入您的新提供商 - ✅ 启用 ios-qwen-coder:latest (推荐--代码优化) - ✅ 可选启用 ios-swift-architect:latest (通用) - ❌ 不启用 nomic-embed-text (仅嵌入,无法聊天)
- 启用Xcode工具
- 同时启用 Xcode工具 智能设置中的(双筒望远镜图标) - 这使AI可以访问您的项目结构并构建诊断
验证连接
# Server health
curl http://localhost:1234/health
# Available models
curl http://localhost:1234/v1/models
# Vector DB stats
curl http://localhost:1234/mcp/stats在Xcode中:按 ⌥⌘/ 打开AI助手并键入提示。查看终端的请求日志。
______________________________________________________________________
用法
吸收你的代码库
在有效使用AI助手之前,请先消化您的项目:
# Ingest a directory
python scripts/ingest_project.py /path/to/project
# Ingest with specific extensions only
python scripts/ingest_project.py /path/to/project --extensions swift m h
# Ingest with custom labels
python scripts/ingest_project.py /path/to/project --labels ios production
# Watch mode (auto-ingest on file changes)
python scripts/ingest_project.py /path/to/project --watch
# Clear existing memory and re-ingest
python scripts/ingest_project.py /path/to/project --clear
# Disable auto-labeling
python scripts/ingest_project.py /path/to/project --no-auto-labels用于快速访问的Shell别名
将这些别名添加到您的 ~/.zshrc 从任何地方轻松获取项目:
# Add to ~/.zshrc
export MCP_SERVER_PATH="/Users/user65419/Documents/Development/AI/xcode-mcp-server"
# Ingest current directory
alias mcp-ingest='$MCP_SERVER_PATH/venv/bin/python $MCP_SERVER_PATH/scripts/ingest_project.py .'
# Ingest with watch mode
alias mcp-watch='$MCP_SERVER_PATH/venv/bin/python $MCP_SERVER_PATH/scripts/ingest_project.py . --watch'
# Search memory
alias mcp-search='$MCP_SERVER_PATH/venv/bin/python $MCP_SERVER_PATH/scripts/memory_search.py'
# Start MCP server
alias mcp-server='$MCP_SERVER_PATH/scripts/start_server.sh'添加后,重新加载shell:
source ~/.zshrc现在,您可以从任何Xcode项目目录运行这些命令:
cd ~/MyXcodeProject
# Ingest current project
mcp-ingest
# Watch for changes during development
mcp-watch
# Search your code memory
mcp-search --query "authentication"搜索内存
使用交互式内存搜索工具:
# Interactive mode
python scripts/memory_search.py
# Direct search
python scripts/memory_search.py --query "authentication"
# Show statistics
python scripts/memory_search.py --stats
# Export memory backup
python scripts/memory_search.py --export backup.json在Xcode中使用
连接后,正常使用Xcode中的AI助手。MCP服务器提供以下增强功能:
- 提出问题 -使用代码库获取上下文感知答案
- 生成代码 -基于现有模式创建代码
- 解释代码 -理解复杂的代码段
- 修复错误 -从项目上下文中获取错误修复
- 添加到内存 -保存重要片段以备将来参考
______________________________________________________________________
配置
编辑 config.yaml 要自定义服务器,请执行以下操作:
Ollama设置
ollama:
base_url: "http://localhost:11434"
chat_model: "ios-qwen-coder" # Qwen2.5-Coder-7B (recommended)
embedding_model: "nomic-embed-text:v1.5"
temperature: 0.1 # Lower = more deterministic
max_tokens: 4096AI堆栈
| 组件 | 型号 | RAM |
|---|---|---|
LLM Qwen2.5-Coder-7B (通过 ios-qwen-coder) | ~5.5 GB | |
| 嵌入件 | nomic-embed-text:v1.5 | ~0.3 GB |
| 重新排名 | ms-marco-MiniLM-L-6-v2 (自动加载) | ~0.1 GB |
| 矢量数据库 | LanceDB(嵌入式) | ~0.5 GB |
LanceDB设置
lancedb:
db_path: "./data/lancedb"
max_results: 10调试设置
debug:
enabled: true
save_requests: true
request_log: "./logs/requests.jsonl"
verbose_mcp: true日志记录
logging:
level: "DEBUG" # DEBUG, INFO, WARNING, ERROR
file: "./logs/mcp_server.log"
console: true______________________________________________________________________
工具参考
MCP服务器为Xcode提供了以下工具:
generate_code
使用RAG上下文从自然语言描述生成代码。
Input:
- description: What to generate
- language: Programming language (default: swift)
Example: "Create a function that fetches user data from an API"explain_code
用简单的英语解释代码的作用。
Input:
- code: The code to explain
- language: Programming language
Example: Pass a complex Swift function for explanationfix_code
根据错误消息修复损坏的代码。
Input:
- code: The broken code
- error: The error message
- language: Programming language
Example: Pass code and compiler error for a fixask_with_context
从你的代码库中询问任何与RAG上下文相关的问题。
Input:
- question: Your question
Example: "How does authentication work in this project?"add_to_memory
将代码或注释添加到持久内存中。
Input:
- content: Content to remember
- type: "code", "docs", or "note"
- file_path: Optional file reference
- description: Optional descriptionsearch_memory
搜索你的代码存储器/笔记本。
Input:
- query: Search query
- type: "code", "docs", or "all"
- limit: Maximum resultsget_memory_stats
获取矢量数据库的统计信息。
clear_memory
清除内存集合(小心使用)。
Input:
- collection: "code", "docs", or "history"______________________________________________________________________
内存/笔记本功能
“笔记本”功能使用LanceDB来维护持久内存:
运作原理
- 代码存储器:存储项目中的代码片段
- 文档存储器:存储评论和文档
- 对话历史:存储过去的问答以获取上下文
内存流
┌─────────────┐ ┌───────────────┐ ┌─────────────┐
│ Your Code │────▶│ Embeddings │────▶│ LanceDB │
│ Files │ │ (Ollama) │ │ Vector DB │
└─────────────┘ └───────────────┘ └─────────────┘
│
│ Similarity
│ Search
▼
┌─────────────┐ ┌───────────────┐ ┌─────────────┐
│ AI Response │◀────│ LLM + Context │◀────│ Relevant │
│ │ │ (Ollama) │ │ Chunks │
└─────────────┘ └───────────────┘ └─────────────┘最佳实践
- 承接您的项目 提问前
- 定期重新摄入 保持记忆新鲜
- 使用手表模式 在开发过程中
- 添加重要片段 手动快速参考
______________________________________________________________________
调试
启用调试模式
./scripts/start_server.sh --debug查看日志
# Real-time log viewing
tail -f logs/mcp_server.log
# View request/response log
cat logs/requests.jsonl | jq .调试请求日志格式
每一行 requests.jsonl 是一个JSON对象:
{
"id": 1,
"timestamp": "2025-02-08T10:30:00",
"type": "request",
"method": "tools/call/generate_code",
"params": {"description": "..."}
}测试Ollama连接
# Test Ollama health
curl http://localhost:11434/api/tags
# Test chat
curl http://localhost:11434/api/chat -d '{
"model": "codellama:13b",
"messages": [{"role": "user", "content": "Hello!"}],
"stream": false
}'测试向量存储
# In Python REPL
from server.vector_store import VectorStore, create_vector_store_with_ollama
import asyncio
async def test():
store = await create_vector_store_with_ollama()
stats = await store.get_stats()
print(stats)
asyncio.run(test())______________________________________________________________________
故障排除
Ollama不跑
错误: Ollama server is not running!
解决方案:
ollama serve未找到型号
错误: Model 'codellama:13b' not found
解决方案:
ollama pull codellama:13bPython导入错误
错误: ModuleNotFoundError: No module named 'mcp'
解决方案:
source venv/bin/activate
pip install -r requirements.txtLanceDB权限错误
错误: Permission denied: ./data/lancedb
解决方案:
chmod -R 755 data/Xcode未连接
- 确保HTTP服务器正在运行:
./scripts/start_http_server.sh - 验证端口是否匹配:默认值为
1234 - 检查Xcode>设置>智能显示您的提供商
- 测试端点:
curl http://localhost:1234/health
反应缓慢
- 使用较小的模型:
ollama pull codellama:7b - 减少
max_results在config.yaml中 - 检查CPU/GPU使用情况
htop
内存使用率高
- 限制config.yaml中摄入的文件大小
- 清除历史记录:
python scripts/memory_search.py然后clear history - 减少RAG配置中的块大小
______________________________________________________________________
文件结构
xcode-mcp-server/
├── config.yaml # Configuration file
├── requirements.txt # Python dependencies
├── README.md # This file
│
├── server/ # Main server package
│ ├── __init__.py
│ ├── mcp_server.py # MCP protocol server
│ ├── ollama_client.py # Ollama LLM client
│ └── vector_store.py # LanceDB wrapper
│
├── scripts/ # Helper scripts
│ ├── setup.sh # Initial setup
│ ├── start_server.sh # Start server
│ ├── create_models.sh # Create custom Ollama models
│ ├── ingest_project.py # Ingest code files
│ └── memory_search.py # Search memory
│
├── models/ # Custom Ollama Modelfiles
│ ├── Modelfile.ios-architect
│ ├── Modelfile.swiftui
│ └── Modelfile.reviewer
│
├── data/ # Data storage
│ └── lancedb/ # Vector database
│
└── logs/ # Log files
├── mcp_server.log
└── requests.jsonl______________________________________________________________________
贡献
欢迎投稿!拜托:
- 克隆该仓库
- 创建要素分支
- 提交拉取请求
______________________________________________________________________
许可证
MIT许可证-可以自由使用和修改。
______________________________________________________________________
